Method expects argument of type 'double *'? - python
I'm new to python and I'm trying to write some code using python wrappers for epanet which I believe are SWIG-wrapped. There is a method I need to use, as outlined below from the source. However I can't seem to get the method to accept any type of value for the 3rd argument "values:".. Anything I try I get the error: TypeError: in method 'ptrn_set', argument 3 of type 'double *'
How do I convert my value/values to 'double *' within Python? what is the solution?
Thankful for any help...
"""
ptrn_set(ph, index, values, len) -> int
Parameters
----------
ph: Handle
index: int
values: double *
len: int
"""
return _toolkit.ptrn_set(ph, index, values, len)
I found this here:
https://docs.python.org/2.4/lib/typesnumeric.html:
Floating point numbers are implemented using double in C
Did you try float?
Python itself does not support values of type double *. In C, that is a pointer-to-a-double-precision-floating-point-number. In Python, effectively every name is a pointer, but you can't have a (native) value that is a pointer.
From context, it looks like they actually want an array of doubles. They really ought to write that as double [], but double * is compatible with it. You could try
values = [1.0, 2.0]
_toolkit.ptrn_set(ph, index, values, len(values))
It's not very Pythonic to do that, but if it works, it works. If that doesn't work, see if your expanet python wrappers provide a way of constructing such values.
If your expanet Python wrappers don't provide a way to construct such values, you can try the cffi library, but it's quite tricky. I think x = ffi.new("double[10]") will give you an array of 10 doubles.
I know some time passed an you probably already solved it, but it might be useful for someone else.
you can use the following function to create an array that will be accepted by epanet:
def make_array(values):
dbl_arr = en.doubleArray(len(values))
for i in range(len(values)):
dbl_arr[i] = values[i]
return dbl_arr
the input is an array of float, like [1.2 , 0.8].
This assuming you imported the epanet python library as
from epanet import toolkit as en,
source: https://github.com/OpenWaterAnalytics/epanet-python/issues/57
Related
Converting string array into float array
I have multiple points that look like this: points = '[1078.17,436.18],[1089.48,413.57],[1092.71,389.35],[1091.09,365.12],[1089.48,337.67],[1073.32,316.67],[1057.17,295.68],[1036.18,282.75],[1011.95,279.52],[987.73,273.06],[961.89,273.06],[937.66,276.29],[913.43,281.14],[894.05,297.29],[880.60,316.70],[874.20,343.10],[871.44,371.58],[868.21,395.81],[868.21,421.65],[887.59,437.80],[911.82,444.26],[936.04,449.11],[960.27,452.34],[984.50,453.95],[1010.34,457.18],[1034.56,455.57],[1058.79,447.49]' the points are string but I'm trying to convert it to float so it'll look like this: points = [1078.17,436.18],[1089.48,413.57],[1092.71,389.35],[1091.09,365.12],[1089.48,337.67],[1073.32,316.67],[1057.17,295.68],[1036.18,282.75],[1011.95,279.52],[987.73,273.06],[961.89,273.06],[937.66,276.29],[913.43,281.14],[894.05,297.29],[880.60,316.70],[874.20,343.10],[871.44,371.58],[868.21,395.81],[868.21,421.65],[887.59,437.80],[911.82,444.26],[936.04,449.11],[960.27,452.34],[984.50,453.95],[1010.34,457.18],[1034.56,455.57],[1058.79,447.49] or: points = [[1078.17,436.18],[1089.48,413.57],[1092.71,389.35],[1091.09,365.12],[1089.48,337.67],[1073.32,316.67],[1057.17,295.68],[1036.18,282.75],[1011.95,279.52],[987.73,273.06],[961.89,273.06],[937.66,276.29],[913.43,281.14],[894.05,297.29],[880.60,316.70],[874.20,343.10],[871.44,371.58],[868.21,395.81],[868.21,421.65],[887.59,437.80],[911.82,444.26],[936.04,449.11],[960.27,452.34],[984.50,453.95],[1010.34,457.18],[1034.56,455.57],[1058.79,447.49]] where the shape should be in this case 27x2 I tried np.float and np.astype but non seems to work. The error I got with np.float is: <ipython-input-146-cdfdb0cec2ea>:1: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here. Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations np.float(test) I also tried float by itself: float(points) got the following error ValueError: could not convert string to float: '[1078.17,436.18],[1089.48,413.57],[1092.71,389.35],[1091.09,365.12],[1089.48,337.67],[1073.32,316.67],[1057.17,295.68],[1036.18,282.75],[1011.95,279.52],[987.73,273.06],[961.89,273.06],[937.66,276.29],[913.43,281.14],[894.05,297.29],[880.60,316.70],[874.20,343.10],[871.44,371.58],[868.21,395.81],[868.21,421.65],[887.59,437.80],[911.82,444.26],[936.04,449.11],[960.27,452.34],[984.50,453.95],[1010.34,457.18],[1034.56,455.57],[1058.79,447.49]' Can someone help me converting the string into float array?
Try with ast module import ast points = ast.literal_eval(points)
IF the the input for your points string doesn't come from a user you can just use: result = eval(points) This will return a tuple of lists of floats. Please be aware to never use eval with user input.
How can I identify numpy's integer type?
What is the simplest way to identify whether a data point in numpy's array is an integer? Currently I use numpy.dtype(x[i]).type to return the type of the element i of array x and then `if numpy.dtype(x[i]).type is numpy.int*` to achieve this, where * can be 8, 32 or 64. But it may also return uint, thus this if way can return False. I wonder whether there exists a simple way to identify whether it is an integer, regardless of the exact int type is. And how about float?
this might help (this effectively tests for a signed integer, in a similar fashion 'u' would be unsigned integer, etc.): x[i].dtype.kind == 'i'
You can use: issubdtype(var, type) Usage: numpy.issubdtype(var_to_check, np.integer) More information here How to determine if a number is any type of int (core or numpy, signed or not)?
Use either: issubclass(x[i].dtype.type, np.integer) or np.issubdtype(x[i].dtype, np.integer) Your code of np.dtype(x) is unlikely to do what you want - that doesn't get the dtype of x, but tries to interpret x as a description of a new dtype. If you have a possibly non-numpy object you want a dtype of, you can use np.asanyarray(x).dtype
Parse C Array Size with Python & LibClang
I am currently using Python to parse a C file using LibClang. I've encountered a problem while reading a C-array which size is defined by a define-directive-variable. With node.get_children i can perfectly read the following array: int myarray[20][30][10]; As soon as the array size is replaced with a variable, the array won't be read correctly. The following array code can't be read. #define MAX 60; int myarray[MAX][30][10]; Actually the parser stops at MAX and in the dump there is the error: invalid sloc. How can I solve this? Thanks
Run the code through a C preprocessor before trying to parse it. That will cause all preprocessor-symbols to be replaced by their values, i.e. your [MAX] will become [60]. Note that C code can also do this: const int three[] = { 1, 2, 3 }; i.e. let the compiler deduce the length of the array from the number of initializer values given. Or, from C99, even this: const int hundred[] = { [99] = 4711 }; So a naive approach might still break, but I don't know anything about the capabilities of the parser you're using, of course.
Semicolon ; in the define directive way causing the error.
In python, detect type, then cast to that type
In support of some legacy code, I have to read a text file and parse it for statements like x=102 and file=foo.dat that might be used to overwrite default values. Note that the second one there is not file='foo.dat'; these aren't python statements, but they're close. Now, I can get the type of the default object, so I know that x should be an int and file should be a str. So, I need a way to cast the right-hand side to that type. I'd like to do this programmatically, so that I can call a single, simple default-setting function. In particular, I'd prefer to not have to loop over all the built-in types. Is it possible?
# Get the type object from the default value value_type = type(defaults[fieldname]) # Instantiate an object of that type, using the string from the input new_value = value_type(override_value)
It's actually pretty easy: textfromfile = '102' defaultobject = 101 value = (type(defaultobject))(textfromfile) Now, value is an int equal to 102.
convert PyInt to C Int
i need to convert PyInt to C int. In my code count=PyInt_FromSsize_t(PyList_Size(pValue)) pValue is a PyObject, PyList. the problem i was having is the PyList_Size is not returning me the correct list size (count is supposed to be 5, but it gave me 6 million), or there is a problem with data types since im in C code interfacing to python scripts. Ideally, i want count to be in a C int type. i've found python/c APIs that return me long C data types... which is not what i want... anybody can point me to the correct method or APIs??
The PyInt_FromSsize_t() returns a full-fledged Python int object sitting in memory and returns its memory address — that is where the 6-million number is coming from. You just want to get the scalar returned by PyList_Size() and cast it to a C integer, I think: count = (int) PyList_Size(pValue) If the list could be very long you might want to think about making count a long instead, in which case you could cast to that specific type instead. Note: a count of -1 means that Python encountered an exception while trying to measure the list length. Here are the docs you should read to know how to handle exceptions: http://docs.python.org/c-api/intro.html#exceptions