cython

Boolean numpy arrays with Cython

夙愿已清 提交于 2021-02-05 03:55:50
问题 I have a numpy boolean array: myarr = np.array([[False, True], [True, False]]) If I try to initialise a Cython MemoryView with it, like this: cdef bint[:,:] mymem = myarr I get this error: ValueError: Does not understand character buffer dtype format string ('?') If I do this instead, it works fine: cdef np.int_t[:,:] mymem = np.int_(myarr) How can I store a boolean numpy array using Cython MemoryViews? 回答1: I ran into the same problem some time ago. Unfortunately I did not find a direct

Boolean numpy arrays with Cython

亡梦爱人 提交于 2021-02-05 03:55:28
问题 I have a numpy boolean array: myarr = np.array([[False, True], [True, False]]) If I try to initialise a Cython MemoryView with it, like this: cdef bint[:,:] mymem = myarr I get this error: ValueError: Does not understand character buffer dtype format string ('?') If I do this instead, it works fine: cdef np.int_t[:,:] mymem = np.int_(myarr) How can I store a boolean numpy array using Cython MemoryViews? 回答1: I ran into the same problem some time ago. Unfortunately I did not find a direct

Python C wrapper for reading variable argument lengths

和自甴很熟 提交于 2021-02-04 08:33:19
问题 I am trying to replace MATLAB/MEX and switch to Python. I came across SWIG, ctypes & Cython as possible solutions and started trying out SWIG (which seems very simple). My C functions have variable argument lengths of the form main(int argc, char *argv[]) . I found solutions online, but getting this working with SWIG lead to a lot of issues. Are the other methods (ctypes / Cython) any simpler? Any example to do this task with SWIG will be helpful. 回答1: There's actually an example in the SWIG

Cython cimport from another directory

此生再无相见时 提交于 2021-02-04 08:18:18
问题 For background, I have read the following questions: https://github.com/cython/cython/wiki/PackageHierarchy https://cython.readthedocs.io/en/latest/src/userguide/sharing_declarations.html#search-paths-for-definition-files Cython cimport cannot find .pxd module How do you get cimport to work in Cython? Error compiling Cython file: pxd not found in package The issue is this. I used to have a ton of C, C++, and Cython all sitting in one directory and compiling fine. Now, I am splitting code into

Safe writing to variable in cython c wrapper within two python processes or distinct memory for python processes

ⅰ亾dé卋堺 提交于 2021-01-29 09:53:40
问题 I am creating a wrapper over c library that recieves some financial data and I want to collect it into python data type (dict with list of field names and list of lists with financial data fields). On the c level there is function that starts "listening" to some port and when any event appears some user-defined function is called. This function is written in cython. Simplified example of such function is here: cdef void default_listener(const event_data_t* data, int data_count, void* user

Compiling cython with openMP support on OSX

只谈情不闲聊 提交于 2021-01-29 04:24:15
问题 I'm using OSX v10.11.6 with a recent version of xcode installed. So my default compiler is gcc , which is really clang . I have used homebrew to install gcc5 so that I can use openMP, and by setting CC := g++-5 in my Makefiles for my source code in C, I can successfully compile C source code with non-trivial usage of -fopenmp. What I want to do is get Cython to compile with gcc5 so that I can use Cython's native prange feature, as demonstrated in a minimal example here. I have written a

Passing numpy arrays in Cython to a C function that requires dynamically allocated arrays

冷暖自知 提交于 2021-01-29 02:16:34
问题 I have some C code that has the following declaration: int myfunc(int m, int n, const double **a, double **b, double *c); So a is a constant 2D array, b is a 2D array, and c is a 1D array, all dynamically allocated. b and c do not need to be anything specifically before they are passed to myfunc , and should be understood as output information. For the purposes of this question, I'm not allowed to change the declaration of myfunc . Question 1: How do I convert a given numpy array a_np into an

Passing numpy arrays in Cython to a C function that requires dynamically allocated arrays

吃可爱长大的小学妹 提交于 2021-01-29 02:13:49
问题 I have some C code that has the following declaration: int myfunc(int m, int n, const double **a, double **b, double *c); So a is a constant 2D array, b is a 2D array, and c is a 1D array, all dynamically allocated. b and c do not need to be anything specifically before they are passed to myfunc , and should be understood as output information. For the purposes of this question, I'm not allowed to change the declaration of myfunc . Question 1: How do I convert a given numpy array a_np into an

pyobjus nameerror: name 'build_ext' is not defined during installation

我只是一个虾纸丫 提交于 2021-01-29 00:00:35
问题 I'm trying to install pyobjus but I still geting error: ERROR: Command errored out with exit status 1: command: /root/anaconda3/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-lm35sz6r/pyobjus/setup.py'"'"'; _file__='"'"'/tmp/pip-install-lm35sz6r/pyobjus/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file_);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, _file_, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip

pyobjus nameerror: name 'build_ext' is not defined during installation

╄→гoц情女王★ 提交于 2021-01-28 23:18:10
问题 I'm trying to install pyobjus but I still geting error: ERROR: Command errored out with exit status 1: command: /root/anaconda3/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-lm35sz6r/pyobjus/setup.py'"'"'; _file__='"'"'/tmp/pip-install-lm35sz6r/pyobjus/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file_);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, _file_, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip