FAQ Database Discussion Community


Cythonise a pandas loop

python,pandas,cython
Can anyone help show me how convert this loop into cython to improve performance. I get you need to create static types using cdef for performance but what else is required: If I have a dataframe df with column 'a'. for i in range(0, len(df.a)-1): if (i < len(df.a)-1): y=...

Compile a Cython project and clean

python,cython
I'm using: from distutils.core import setup from Cython.Build import cythonize setup(ext_modules = cythonize("myfile.pyx")) to compile myfile.pyx. Is there a way to: Have the .pyd in the current folder (instead of build/ subfolder) Clean after compile (i.e. delete build/ subfolder, delete the .c file) without having to do it myself with...

Cython: simple function with 2 lists, what is the fastest way?

python,performance,optimization,cython
What I want to do - transform my pure Python code into Cython. Pure Python code: def conflicts(list1,list2): numIt = 10 for i in list1: for j in list2: if i == j and i < numIt: return True return False conflicts([1,2,3], [6,9,8]) My Cython code so far: cdef char...

Tornado coroutines don’t work in Cython

python,generator,tornado,cython,coroutine
This code works in Python 3.4.3 using Tornado 4.1 — it sleeps for 1 second and then prints "Hello World! 123". But when compiled using Cython (I tried versions 0.20.1post0 and 0.23dev), it does nothing. import tornado.ioloop import datetime from tornado import gen @gen.coroutine def test(): yield gen.Task(ioloop.add_timeout, datetime.timedelta(seconds=1)) return...

Cython for loop conversion

python,cython
Using cython -a, I found that a for i in range(0, a, b) statement was run as a python loop (very yellow line in cython -a html output). i, a and b were cdef-ed as int64_t. Then I tried the 'old' syntax for i from 0 <= i < b...

OverflowError:python int too large to convert to C long on win8

python,cython,pyinstaller
I am using pyPDF2 to do pdf merging stuff using python, the python code is then deployed with pyinstaller, The resulting exe runs fine on win7 but it shows error overflow error on win8. The exception happens in getNumPages() which returns the number of pages a pdf includes, I am...

Cython - error when trying to access contents of pointer to struct

python,c,pointers,struct,cython
I have a cdefed class in Cython which looks very similar to this: cdef class AprilTagDetector: cdef capriltag.apriltag_detector_t* _apriltag_detector def __cinit__(self): self._apriltag_detector = capriltag.apriltag_detector_create(); # standard null checks # standard __dealloc__(self) here property quad_decimate: def __get__(self): return self._apriltag_detector.quad_decimate The corresponding .pxd file looks like this: cdef extern from "apriltag.h": #...

Reading and writing an array in a file using C functions in cython

python,c,arrays,numpy,cython
I am instantiating a class in cython. I want to declare one of the instance which is an array of float values once by computing it with a given function and save it in a binary file using c functions. For the further call of my class if the input...

cython compiles but no pyd files are generated

python-3.x,cython
I tried to run a python code, say myfile.py (also tried to rename it as myfile.pyx) as follows: import pyximport pyximport.install(setup_args={"script_args":["--compiler=mingw32"]}, reload_support=True) import myfile myfile.mycode() I am using PyCharm. The code seems to have run fine without any error and even gave me correct results on the Python Console within...

create ndarray out of c++ pointer

python,c++,pointers,numpy,cython
I created a module in c++ and need to use the results in python. Already wrote a wrapper and it is working with this code a = np.empty([r, hn]) for i in xrange(r): for j in xrange(hn): a[i,j]=self.thisptr.H[i*hn+j] return a The code is working, but I think there should be...

Use Cython for exposing statically linked c-library symbols

python,cython
I have a question on best practice on how to use code from a local C library with Cython. In order to avoid setting the LD_LIBRARY_PATH (or installing the C library in /usr/lib or similar directories), I think static linking would be a suitable solution for my use case. However,...

Cython/Python/C++ - Inheritance: Passing Derived Class as Argument to Function expecting base class

python,c++,inheritance,cython
I am using Cython to wrap a set of C++ classes, allowing a Python interface to them. Example Code is provided below: BaseClass.h: #ifndef __BaseClass__ #define __BaseClass__ #include <stdio.h> #include <stdlib.h> #include <string> using namespace std; class BaseClass { public: BaseClass(){}; virtual ~BaseClass(){}; virtual void SetName(string name){printf("in base set name\n");}...

Performance comparison Fortran, Numpy,Cython and Numexpr

python,numpy,fortran,cython
I have following function: def get_denom(n_comp,qs,x,cp,cs): ''' len(n_comp) = 1 # number of proteins len(cp) = n_comp # protein concentration len(qp) = n_comp # protein capacity len(x) = 3*n_comp + 1 # fit parameters len(cs) = 1 ''' k = x[0:n_comp] sigma = x[n_comp:2*n_comp] z = x[2*n_comp:3*n_comp] a = (sigma...

importing functions from .cpp file into python

python,c++,cython,boost-python
Let me start with a disclaimer that i'm putting this question after lot of research and not finding any direct and step by step example. Have gone through Cython , SWIG, Boostpython documentation but couldn't get a step by step process and so posting here - I have a .cpp...

Link error when installing grako in Python

cython,grako
I'm trying to install the package grako from PyPI which apparently compiles stuff with Cython. I don't know what this process means and it breaks with an unresolved symbol. Maybe someone can give me a hint what to check for to make this work? Here is the output on Windows,...

How do I optimize this python code using cython?

python,optimization,cython
I have some python code, and I'm wondering what I can do to optimize the speed for creating the array using Cython. Note that I have tried other methods: Counting Algorithm Performance Optimization in Pypy vs Python (Numpy vs List) It seems like Cython is significantly faster than anything I've...

pyqt gui locked when calling cython function from QThread.run

python,user-interface,pyqt,cython,qthread
In PyQt4 I want to execute code with no lock gui. I use QThread and emit signal for gui update. But if i call cython function in QThread.run gui will be locked. class Runner(QtCore.QThread): iter = QtCore.pyqtSignal(int) def __init__(self): QtCore.QThread.__init__(self) def do_something(self): beg = time.time() s = 0.0 while time.time()...

Using Cython to wrap a c++ template to accept any numpy array

python,c++,arrays,numpy,cython
I'm trying to wrap a parallel sort written in c++ as a template, to use it with numpy arrays of any numeric type. I'm trying to use Cython to do this. My problem is that I don't know how to pass a pointer to the numpy array data (of a...

TypeError in cython

python,cython
I have a cython program that is parsing billions of records. However, in my output i am getting. TypeError: 'an integer is required' in cython_test.parse_func ignored Is there any setting for compilation in which it will raise so I can find the line instead of cython handling the error and...

Vectorizing a function of a class with two arrays as inputs in cython

python,arrays,numpy,vectorization,cython
I am struggling with the problem of optimizing my cython code in order to improve its speed as much as possible. One of the challenges that I could not still figure out how it should be done in cython is mapping an array on a function like what is done...

gzip in cython file

gzip,cython
I am using cython to parsing large gzipped files. Is there any recommended way to use the gzip library as a file buffer to getline? The readline from gzip in Python takes well too long. I would like to do this: gzip_file = gzip.open(file_name, ¨rb¨) getline(line,n, gzip_file) ...

String in Cython functions

python,string,cython
I'd like to do this to pass a string to a Cython code: # test.py s = "Bonjour" myfunc(s) # test.pyx def myfunc(char *mystr): cdef int i for i in range(len(mystr)): # error! len(mystr) is not the length of string print mystr[i] # but the length of the *pointer*, ie...

Multiple Inheritance/Virtual Functions

python,c++,inheritance,virtual,cython
Using Cython (acting as an interface so I can call C++ based functions via Python), I am trying to wrap a complicated hierarchal structure of C++ classes, all of which eventually inherit from one base class. This singular base class has many virtual functions, whose implementation is different based on...

Operation 10**(-9) correct in python, but wrong in Cython

python,cython
Very basic question: in my python 2.7 code I have situation roughly as follows: b=5.0*10**(-9) a=9 print(a) c=a/(1.0*b) the code runs in python / spyder (64bit), but fails in Cython, because of a float division by 0. The printed number is 0. When I define b=0.000000005 the division is fine...

How to create empty char arrays in Cython without loops

python,c,cython
Well, this seems easy, but I can't find a single reference on the web. In C we can create a char array of n null-characters as follows: char arr[n] = ""; But when I try to do the same in Cython with cdef char arr[n] = "" I get this...

Cython: memory views on `numpy` arrays lose `numpy` array features?

python,numpy,cython
Consider the following example: cdef test_function(): cdef: double[:] p1 = np.array([3.2, 2.1]) double[:] p2 = np.array([0.9, 6.]) return p1-p2 If used, it returns the following error: Error compiling Cython file: ------------------------------------------------------------ ... cdef test_function(): cdef: double[:] p1 = np.array([3.2, 2.1]) double[:] p2 = np.array([0.9, 6.]) return p1-p2 ^ ------------------------------------------------------------ cython_cell_v3.pyx:354:13:...

vectorization of looping on an array from cython

gcc,cython,auto-vectorization
Consider the following example of doing an inplace-add on a Cython memoryview: #cython: boundscheck=False, wraparound=False, initializedcheck=False, nonecheck=False, cdivision=True from libc.stdlib cimport malloc, free from libc.stdio cimport printf cimport numpy as np import numpy as np cdef extern from "time.h": int clock() cdef void inplace_add(double[::1] a, double[::1] b): cdef int i...

cython.parallel: variable assignment without thread-locality

python,multithreading,parallel-processing,openmp,cython
Using cython.parallel I am looking to assign a shared-memory variable value from the prange-threads without the implicit thread-locality. Or formulated more differently: how can I define a variable as openmp shared rather than private with cython.parallel? how can different threads or a prange block communicate? Some very simple (and useless)...

Optimizing a reed-solomon encoder (polynomial division)

python,numpy,optimization,cython,pypy
I am trying to optimize a Reed-Solomon encoder, which is in fact simply a polynomial division operation over Galois Fields 2^8 (which simply means that values wrap-around over 255). The code is in fact very very similar to what can be found here for Go: http://research.swtch.com/field The algorithm for polynomial...

Numpy in Cython, no improvement

python-3.x,numpy,cython
I am writing a simple function in cython using numpy but it seems that cython is producing a ton of API while converting to C++. Could anyone help me with the error? I did not find anything more in the cython docs. operations.pyx: import numpy as np cimport numpy as...

Converting string date to epoch time not working with Cython and POSIX C libraries

python,date,pandas,cython,epoch
I have a very large pandas dataframe and I would like to create a column that contains the time in seconds since the epoch for a ISO-8601 format date string. I originally used the standard Python libraries for this but the result is quite slow. I have tried to replace...

How can I speed up Cython code to compute conditional log likelihood of dirichlet?

python,performance,numpy,optimization,cython
I have a function that computes the conditional (on kth alpha) log likelihood of a dirichlet distribution. I have it written in Cython and compiled, but my code calls it about 12M times and it seems to be the bottleneck, so I'm hoping to speed it up. cimport numpy as...

Cython: a class needs to have as its attribute a python list of Cython extension types — thus, this class cannot be an extension type itself?

python,design-patterns,cython
Let us say I have a Cython extension type named Point. Then, I need to create a class called Points, which has as one of its attributes, a Python list of Point objects. Now, Python lists, as far as I understand, cannot be attributes of an extension type, as only...

How to add a static member to a Cython class (from python, not C)

python,c,class,static,cython
How would I add a static, typed member to a cython class? The syntax for adding typed instance-members uses the syntax as follows (e.g.): import cython cdef class NoStaticMembers: cdef public int instanceValue # but, how do I create a static member?? def __init__(self, int value): self.instanceValue = value ...

Cython basic build still failing

python,c++,c,dll,cython
I am using python 2.7.6 and am on Windows 7 x64 Here is the entry point display for my python: >python Python 2.7.6 (default, Jan 28 2014, 17:59:34) [MSC v.1800 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. So I am using all 32bit...

Defining a custom pandas aggregation function using Cython

python,performance,numpy,pandas,cython
I have a big DataFrame in pandas with three columns: 'col1' is string, 'col2' and 'col3' are numpy.int64. I need to do a groupby, then apply a custom aggregation function using apply, as follows: pd = pandas.read_csv(...) groups = pd.groupby('col1').apply(my_custom_function) Each group can be seen as a numpy array with...

using cython to generate a unix timestamp

python,cython,unix-timestamp,python-datetime
If you have every integer in memory to construct a datetime object, is there a better way than the following. atoi(datetime(year,month,day,hour,minute,second).stftime("%s")) ...

Kivy 1.8 and Cython 0.21.2

cython,kivy,buildozer
Using Buildozer I get 12 times: #error Do not use this file, it is the result of a failed Cython compilation. And eventually: Command failed: ./distribute.sh -m "kivy" -d "myapp" According to the Kivy docs, I use Cython 0.21.2. See the full log. Any help would be very much appreciated!...

Cython: How to wrap a C++ function that returns a C++ object?

python,c++,wrapper,cython
I'm working on a Python project where I'd like to interface with a C++ package that has already been written. Since I'll be using Cython in other portions of this project, I'd prefer to wrap using Cython. In brief, I need to wrap a function, FooBar, that returns an object...

Where does cython pyximport compile?

python,cython
My cython / pyximport code works very well on a read/write filesystem. But (for testing purposes), I need to try it on a read only filesystem. How to change the cython / pyximport temporary directory ? (where does it do the job? i.e. the on-the-fly compilation?) How to set this...

Embedding Cython in C++

python,c++,cython
I am trying to embed a piece of Cython code in a C++ project, such that I can compile a binary that has no dependencies on Python 2.7 (so users can run the executable without having Python installed). The Cython source is not pure Cython: There is also Python code...

cython: create ndarray object without allocating memory for data

numpy,cython
In cython, how do I create an ndarray object with defined properties without allocating memory for its contents? My problem is that I want to call a function that requires a ndarray but my data is in a pure c array. Due to some restrictions I cannot switch to using...

Using cython when arguments and returns are matrices, not scalars

python,pandas,cython
I am new to cython and I would like to make calculations on pandas DataFrames faster. Here is a typical, simplified example of such calculations. In this example, the arguments are vectors and the return is a scalar but in other cases both arguments and returns can be DataFrame/arrays. I...

How to pass python function as an argument to c++ function using Cython

python,c++,callback,wrapper,cython
Here are my settings: I have next c++ class which I want to wrap: // Foo.h class Foo { public: typedef int MyType; typedef int ArgType1; typedef int ArgType2; ... typedef MyType (*FooFunction) (ArgType1 a, ArgType2 b); ... void setFooFunction(FooFunction f); Example of using this class in c++: #include "Foo.h"...

Extracting SWIG wrapped C++ instance/pointer for use in Cython

python,c++,swig,cython,cpython
I have an instance of a class from SWIG-wrapped C++ library from which I would like to extract its reference, in order to be able to use it inside a Cython file, in which I am directly linking to the same C++ library by using a more lightweight self-made Cython...

Rebuilding Cython code

python,compilation,cython
I downloaded a module that uses Cython. I installed it after building the content as folows, python setup.py build python setup.py install But I would like to make changes to the Cython parts of that module and recompile them. It turns out that I can't recompile the Cython files. In...

_main undefined during link from cython

python,python-2.7,cython
I am attempting to produce a binary from a minimal Python file: print 'hello' Using this makefile: #!/usr/bin/make -f export flags= cflags=$(flags) $(shell python-config --cflags) ldflags=$(flags) $(shell python-config --ldflags) all: multi .PRECIOUS: %.o %.c multi: multi.o makefile gcc -o [email protected] $< $$ldflags %.o: %.c makefile gcc -o [email protected] $< $$cflags...

Set coefficient/element of Eigen::Matrix3d in Cython

cython,eigen,eigen3
I am trying to create a wrapper in Cython for a library which uses Eigen::Matrix3d matrices. How can I set an individual element/coefficient of the Matrix3d object? I know, I can get the value with the coeff(row, col) method but could not find any function set_coeff(row, col, value) - or...

Cython: use globals().update(…)?

python,cython,opcodes
I am writing a Python bytecode optimizer for a module that previously had a Cython tool-chain. While that may be deprecated by now, I encountered something strange while fiddling with it. Consider this simple piece of code: from opcode import opmap globals().update(opmap) print(STORE_GLOBAL) This should print 97, as STORE_GLOBAL is...

return numpy array in cython defined c function

numpy,cython
I am not sure if the following question makes sense, since I am newbie. I am trying to create in Cython a C function which returns a numpy array, like the following. cdef np.ndarray[np.int32_t, ndim=1] SumPlusOne(np.ndarray[np.int32_t, ndim=1] ArgArray): cdef np.ndarray[int32_t, ndim=1] ReturnArray = np.zeros((len(ArgArray), dtype = np.int32) ReturnArray = ArgArray...

How to conditionally declare code according to Python version in Cython?

python,python-2.7,python-3.x,cython
I have the following pxd header which augments a regular Python module: #!/usr/bin/env python # coding: utf-8 cimport cython @cython.locals(media_type=unicode, format=unicode, charset=unicode, render_style=unicode) cdef class BaseRenderer(object): """ All renderers should extend this class, setting the `media_type` and `format` attributes, and override the `.render()` method. """ @cython.locals(indent=int, separators=tuple) cpdef object render(self,...

Definition of def, cdef and cpdef in cython

python,declaration,cython
I'd like to know the difference between def, cdef and cpdef when I declare a function. The difference between def and the others it's more or less clear. And I've also seen that sometimes it's added the return type in the declaration (cdef void/double/int... name) and sometimes not. I'd also...

How to expose a numpy array from c array in cython?

python,numpy,cython
cpdef myf(): # pd has to be a c array. # Because it will then be consumed by some c function. cdef double pd[8000] # Do something with pd ... # Get a memoryview. cdef double[:] pd_view = pd # Coercion the memoryview to numpy array. Not working. ret =...

Passing other Cython-bound objects to methods on a Cython-bound object

python,c,cython
I've got a Cython class which binds to a C library, called TagDetector, and I've got another class TagFamily, which I want to be able to instantiate and pass with a message as such: td = TagDetector() td.add_family(TagFamily('tag16h5')) TagFamily, internally, uses struct creation functions from the C library to set...

How to wrap a C pointer and length in a new-style buffer object in Cython?

python,python-2.7,cython,python-c-extension,memoryview
I'm writing a Python 2.7 extension module in Cython. How do I create a Python object implementing the new-style buffer interface that wraps a chunk of memory given to me by a C library? The chunk of memory is just a string of bytes, not a structure or multidimensional array....

inherit a Cython class in a Python class

python,class,inheritance,cython
I have two classes which are written in cython and I want to use them in a class in python. position.pyx import numpy as np cimport numpy as np cimport cython cpdef double std_G,v=4.3e-9, 299792.458 cdef class cosmo(object): cdef public double o_m, o_l, h, w, o_r, G, v def __init__(self,double...

Cython: does the __getattr__ method have to be manually implemented when creating an extension type?

python,cython
Let's say I am creating a new extension type: cdef class foo: int x def __cinit__(self, x): self.x = x Do I manually have to create a __getattr__ special method? It seems that way, because foo.__getattr__('x') gives me an error like so: ImportError: Building module simple_exttype failed: ["AttributeError: type object...

Correct way to import file from different directory in Cython

python,include,cython
I have a .pxd file which contains declaration of a variable in another c header file as can be understood from the following code: cdef extern from "parameters.h": ctypedef struct mm_np: int nlay double n1 double n2[3] double d[3] double n3 int lut when the file parameters.h is in the...

Mixin Cython class and SqlAlchemy

python,sqlalchemy,cython
Abstract: I have one cython class which represents a business unit. This class is declared in pure cython style. In one project, I need to map the business unit to a database. For doing this I would like to import the .pxd file and "map" it with SQLAlchemy. Cython definition...

Copy Numpy array to a memoryview

numpy,cython,memoryview
I have a memoryview on a numpy array and want to copy the content of another numpy array into it by using this memoryview: import numpy as np cimport numpy as np cdef double[:,::1] test = np.array([[0,1],[2,3]], dtype=np.double) test[...] = np.array([[4,5],[6,7]], dtype=np.double) But why is this not possible? It keeps...

cython: memory view of ndarray of strings (or direct ndarray indexing)

python,numpy,cython
How does one specify a memory view of a ndarray that contains strings? char[:], char*[:], ... do not work. To illustrate, my problem is the definition of function abc(...): cdef void abc(char[:] in_buffer): cdef char * element element = address(in_buffer[1]) ... def main(): cdef Py_ssize_t i, n = 100 a...

Casting Cython fused types to C++ pointers

python,c++,templates,casting,cython
This is a general question about casting from Cython fused types to C++ types, which I'll describe with a minimal example. Consider the superficial C++ function template: template <typename T> void scale_impl(const T * x, T * y, const T a, const size_t N) { for (size_t n = 0;...

Importing cython function: AttributeError: 'module' object has no attribute 'fun'

python,cython
I have written a small cython code that is #t3.pyx from libc.stdlib cimport atoi cdef int fun(char *s): return atoi(s) the setup.py file is from distutils.core import setup from Cython.Build import cythonize setup(ext_modules=cythonize("t3.pyx")) I run setup.py using this command python setup.py build_ext --inplace This gives me Compiling t3.pyx because it...

I can't compile c++ code with cython

python,c++,cython
I try to cythonize the following code: from libcpp.vector import vector from libcpp cimport bool def test(): cdef vector[bool] = pos(top, 0) return 1 with: cython test.pyx --cplus -o test.cpp which however results in: Error compiling Cython file: ------------------------------------------------------------ ... from libcpp.vector import vector from libcpp cimport bool def test():...

Kivy is not compitable with Cython?

python,install,cython,kivy
I decided to choose kivy to to help me design my gui applications, but I am facing this problem; I installed cython 0.22 and everytime I try to install kivy I get this (with a lot of other lines): ImportError: Incompatible Cython Version Can anyone suggest a way to solve...

Optimizing point - circle distance method

python,opencv,numpy,optimization,cython
I'm implementing a RANSAC algorithm for circle detection in images. I profiled the execution and I get: 13699392 function calls in 799.981 seconds Random listing order was used ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {time.time} 579810 0.564 0.000 0.564 0.000 {getattr} 289905 2.343 0.000 8.661...

Casting fftw_complex pointer (aka double[2]) to cython complex memoryview

cython,fftw
(Cross-posted from cython-users) I am trying to use FFTW for some Cython code. I am running into issues when trying to cast a pointer to a fftw_complex (a typedef to double[2]) to a complex memoryview, namely (minimal example) cdef extern from "fftw3.h": ctypedef double fftw_complex[2] fftw_complex* fftw_alloc_complex(int N) cdef foo(complex[::1]...

Speed up cython code

python,c,performance,cython
I wrote a python code that manages a lot of data and thus it takes a lot of time. So, I found out Cython and I began to change my code. Basically, all I did is to change functions' declarations (cdef type name(arguments with variable type) ), to declare cdef...

Cython equivalent of c define #define myfunc(node x,…) SetNode(x.getattributeNode(),__VA_ARGS__)

c,cython,wrapping,variable-length-arguments
Cython equivalent of c define #define myfunc(Node x,...) SetNode(x.getattributeNode(),__VA_ARGS__) I have a c api SetNode which takes first argument a node of struct type node and N variables (N is variable number from 0-N) here is a c example to solve such problum exampleAPI.c #include<stdarg.h> float sumN(int len,...){ va_list argp;...

Cython cannot convert to int?

python,cython
using Anaconda, Python 3.4 and Win7 64bit, i can't get this running: C Code: int addInts(int a, int b) { return a+b; } PYX file: cdef extern from "square.cpp": int addInts(int, int) def pAddInts(int a, int b): return addInts(a, b) and testfile: res = callCpp.pAddInts(3, 4) error message: Traceback (most...

How to use Cython typed memoryviews to accept strings from Python?

python,python-2.7,cython,python-c-extension,memoryview
How can I write a Cython function that takes a byte string object (a normal string, a bytearray, or another object that follows the buffer protocol) as a typed memoryview? According to the Unicode and Passing Strings Cython tutorial page, the following should work: cpdef object printbuf(unsigned char[:] buf): chars...

Complex numbers in Cython

python,c,numpy,cython,complex-numbers
What is the correct way to work with complex numbers in Cython? I would like to write a pure C loop using a numpy.ndarray of dtype np.complex128. In Cython, the associated C type is defined in Cython/Includes/numpy/__init__.pxd as ctypedef double complex complex128_t so it seems this is just a simple...

How can I manually compile Cython code that uses C++?

python,c++,numpy,cython
I have exactly copied the example code given in the Cython documentation for wrapping C++ classes. I can successfully build and import the rect.so extension using distutils and the cythonize() method, i.e. by: Putting the following directives at the top of rect.pyx: # distutils: language = c++ # distutils: sources...

Loop over a Numpy array with Cython

python,arrays,numpy,cython
Let a and b be two numpy.float arrays of length 1024, defined with cdef numpy.ndarray a cdef numpy.ndarray b I notice that: cdef int i for i in range(1024): b[i] += a[i] is considerably slower than: b += a Why? I really need to be able to loop manually over...

Calling PyArray_SearchSorted from Cython — 3 or 4 arguments?

numpy,cython
I'm trying to use PyArray_SearchSorted using NumPy's C API from Cython. When call it like PyArray_SearchSorted(values, point, NPY_SEARCHLEFT) I get the GCC error: error: too few arguments to function call, expected 4, have 3. On the other hand, when I call it like PyArray_SearchSorted(values, point, NPY_SEARCHLEFT, NULL), Cython supplies an...

I've mangled Cython badly, it's performing worse than pure Python. Why?

python,arrays,list,cython
I'm rather new to Python and absolutely ignorant of C (unfortunately) so I am struggling to properly understand some aspects of working with Cython. After profiling a Python program and discovering that it was just a couple of loops that were hogging most of the time, I decided to look...

Cython Class, create NumPy with Zeros

python,performance,numpy,optimization,cython
i am trying to create a cython class which creates a NumPy with zeros. Later i want to write float values in that Numpy... My python class looks like this: class test: def __init__(self, b): self.b = b self.eTest = np.zeros((100, 100)) My cython class looks like this so far:...

combine multiple decorators in cython

python,cython
I found this interesting question on SO to combine multiple decorators in python to a single decorator. I wanted to do the same thing in Cython. Typically, I have Cython codes that look like: @cython.boundscheck(False) @cython.wraparound(False) @cython.cdivision(True) cdef ar[dtype_t, ndim=2] sma_vec(ar[dtype_t, ndim=2] x, int m): cdef int n cdef Py_ssize_t...

Convert big pandas DataFrame efficiently

python,pandas,cython,opentsdb
I have several metrics (2000 rows x 100 columns dataframes per second, could be bigger) and I want to store them in OpenTSDB. To do so, I need to format the values in a way understood by the database, either telnet style or json style. The problem is that with...