Questions tagged [cython]

Cython is a superset of the Python language for quickly generating Python C extensions.

cython
Filter by
Sorted by
Tagged with
1 vote
1 answer
1k views

How do I implement vector <vector <Obj>> for Cython (C++ to Python)?

See the code here: https://github.com/rootpy/root_numpy/blob/master/root_numpy/src/tree.pyx#L228 cdef cppclass VectorConverter[T](VectorConverterBase): int elesize int nptypecode ...
kratsg's user avatar
  • 601
0 votes
0 answers
178 views

What kind of 'int' to use in numpy arrays?

When passing an integer array to a C function from numpy, what is the right type of int to use? If the C functions expects an array of plain int, then: A = np.array([1,2,3]) seems incorrect since it ...
user avatar
7 votes
1 answer
3k views

Correct way to return numpy friendly arrays using typed memoryviews in Cython?

I am trying to use Cython memoryviews without creating or taking numpy arrays, so that I never have to compile with numpy, but I'd like to return things that on the Python side can be 'cast' into ...
user avatar
8 votes
2 answers
1k views

way to eliminate Cython numpy compilation warnings?

I ran into the problem described here (What is this import_umath function?) and wanted to know if there is a fix for it? I have the exact same case where compiling Cython code that uses numpy with the ...
user avatar
8 votes
2 answers
6k views

np.ascontiguousarray versus np.asarray with Cython

follow up question to how to pass numpy array to Cython function correctly?: when passing numpy.ndarrays in Cython to a C function that only deals with contiguous arrays, is there a difference ...
user avatar
2 votes
1 answer
3k views

ctypedef in Cython with numpy: what is right convention?

In Cython when using numpy, what is the point of writing: cimport numpy as np import numpy as np ctypedef np.int_t DTYPE_t and then using DTYPE_t everywhere instead of just using np.int_t? Does the ...
user avatar
2 votes
1 answer
2k views

Python Tuple to Cython Struct

Scipy splprep (spline preperation) produces a Tuple tckp tckp : tuple (t,c,k) a tuple containing the vector of knots, the B-spline coefficients, and the degree of the spline....
martburg's user avatar
  • 510
5 votes
1 answer
833 views

How to wrap a C++ functor in Cython

I'm trying to wrap a C++ library in which the logic is implemented as templatized functors in .hpp files, and I'm struggling to find the right way to expose the C++ functors as Cython/Python functions....
Will's user avatar
  • 4,451
14 votes
1 answer
10k views

how to pass numpy array to Cython function correctly?

This is described in many places but i simply cannot get it to work. I am calling a C++ function from Cython: cimport numpy as np cdef extern from "test.h" namespace "mytest": void test(double *A, ...
user avatar
0 votes
1 answer
113 views

How to implement a faster condition checking function? python

I have a small script that wants to extract some unique words based on several conditions and checking through the conditions is taking super long. Possibly because it checks through a large ...
alvas's user avatar
  • 119k
2 votes
1 answer
1k views

unable to release GIL with Cython when using callback-based C libraries

I'm creating a GTK/GStreamer application (C gstreamer for audio and pygobject gtk for gui) which waits for MIDI input and responds to it. The MIDI capturing is handled with the RTMidi library. In the ...
sugarmuff's user avatar
  • 435
0 votes
0 answers
393 views

TypeError: integer required while defining a function

I have writen a cython file to do some time consuming operations for me. However, if I try to call this function I get a TypeError: an integer is required. I am working with python 2.7on a Win7 ...
swot's user avatar
  • 278
2 votes
2 answers
3k views

Fastest way to get subset of numpy array in Cython

I have a Cython function that takes a 2d nd.array (numpy array) of integers and returns a 1d numpy array whose length is the same as the input 2d array. import numpy as np cimport numpy as np np....
user avatar
0 votes
1 answer
1k views

How to extern from <algorithm> in Cython

I'm trying to use partial_sort from the <algorithm> library within Cython, but I just cannot find the correct way to properly extern it. reference Here's my failed attempt: %%cython -f cdef ...
richizy's user avatar
  • 2,052
0 votes
1 answer
549 views

Recursion limit exceeded with cython cdef classes and super

I'm having some trouble with this piece of code running under Cython 2.0: cdef class Foo(object): cpdef twerk(self): #using def instead does not help print "Bustin' some awkward moves." cdef ...
badp's user avatar
  • 11.6k
3 votes
1 answer
1k views

Using timeit on a cython function

I am trying to time a cython compiled function with timeit using the following syntax: from offset_back import offset_back print timeit.timeit(stmt='offset_back(10000,1000,1000)',setup='from main ...
Padraic Cunningham's user avatar
4 votes
1 answer
2k views

Cython compilation error - local variable referenced before assignment

I'm new to Cython and trying to speed up my kivy mobile game by using Cython code in critical AI module. My code goes as follows: import numpy as np cimport numpy as np #not relevant parts cdef np....
Jacek Nowak's user avatar
3 votes
0 answers
1k views

How to configure pyximport to always make a cpp file? [duplicate]

I want to use c++ functionality most of the time in cython and I find using pyximport very convenient, but making pyxbld configuration file for each pyx module (as described in How do you tell ...
Apogentus's user avatar
  • 6,533
2 votes
0 answers
411 views

Cython runtests.py: no attribute 'may_share_memory' [closed]

I installed cython on Windows (running python(x,y)) by downloading it from http://cython.org/ and I ran python setup.py install in the installation directory. There is a testmodule "runtests.py" ...
Anna Christine's user avatar
18 votes
2 answers
12k views

C array vs NumPy array

In terms of performance (algebraic operations, lookup, caching, etc.), is there a difference between C arrays (which can be exposed as a C array, or a cython.view.array [Cython array], or a memoryview ...
richizy's user avatar
  • 2,052
0 votes
2 answers
228 views

Understanding Cython "typedness" report

I'm using Cython to make my Python code more efficient. I have read about the Cython's function cython -a filename.pyx to see the "typedness" of my cython code. Here is the short reference from ...
jjepsuomi's user avatar
  • 4,313
2 votes
1 answer
3k views

Getting Cython to work in Windows 7 with Eclipse PyDev

I'm a newbie trying to learn Cython. My operating system is Windows 7 and I'm using Eclipse PyDev as my IDE for Python development. I have tried to install and test Cython according to this guide: ...
jjepsuomi's user avatar
  • 4,313
2 votes
1 answer
5k views

Cython: How to expose void* and function pointer in struct?

I have a C header with: typedef struct { <normal members> void (*cb_func)(glp_tree *T, void *info); void *cb_info; <normal members> } glp_iocp; Currently, in ...
equaeghe's user avatar
  • 1,714
5 votes
1 answer
782 views

Saving memory of dict by using bitarray instead of int?

I am trying to reduce the memory-consumption of a python dict, which in my case serves as a word-->document_id "inverted index". Each word is hashed as an integer, which takes up 24 bytes. I was ...
richizy's user avatar
  • 2,052
4 votes
1 answer
2k views

Writing to file with cython.parallel.parallel and nogil

I'm getting my feet wet with Cython. Consider me a total noob with it. Writing to a very large file is a major bottleneck in my code, so I thought I'd look into parallelism, but I haven't been able ...
swingsoneto's user avatar
8 votes
1 answer
3k views

Cython not much faster than Python for Basic Sum Calculation

I'm trying to follow an example given on the Continuum Analytics blog benchmarking Python, Cython, Numba for a sum calculated using a for loop. Unfortunately, I'm seeing that Cython is slower than ...
hobscrk777's user avatar
  • 2,507
59 votes
3 answers
61k views

Difference between np.int, np.int_, int, and np.int_t in cython?

I am a bit struggled with so many int data types in cython. np.int, np.int_, np.int_t, int I guess int in pure python is equivalent to np.int_, then where does np.int come from? I cannot find the ...
colinfang's user avatar
  • 21.4k
4 votes
1 answer
499 views

No speed gains from Cython again?

The following is my cython code, the purpose is to do a bootstrap. def boots(int trial, np.ndarray[double, ndim=2] empirical, np.ndarray[double, ndim=2] expected): cdef int length = len(empirical)...
colinfang's user avatar
  • 21.4k
1 vote
1 answer
765 views

Cythonize a .pyc no .pyx available

In my hands i have a .pyc and not the corresponding .py or .pyx. Is it possible to cythonize a .pyc ? should it be decompressed to .py first and how ?
kiriloff's user avatar
  • 26k
28 votes
2 answers
24k views

Cython setup.py for several .pyx

I would like to cythonize faster. Code for one .pyx is from distutils.core import setup from Cython.Build import cythonize setup( ext_modules = cythonize("MyFile.pyx") ) What if i want to ...
kiriloff's user avatar
  • 26k
2 votes
2 answers
1k views

How do I use Cython for external module compilation, from within my setup.py file?

First, if i have a project that is all python (no C++ or C) files, what is the advantage of using Cython, vs, say, using the python library directly (for integration in a large C++ software program) ? ...
kiriloff's user avatar
  • 26k
0 votes
0 answers
101 views

Enthought Canopy lapacke

It seems that the simplest way of calling lapack functions from cython is to use lapacke. I would like to use the lapack functions that come with Enthought Canopy in the libmkl_* dynamic libraries. ...
user1003993's user avatar
2 votes
1 answer
2k views

Cython: Fastest way to convert char value to string

I need to convert multiple char values to strings of corresponding ASCII characters as fast as possible. Here is a toy example. I want H() function, called from python environment, to return str 'aaa'....
Apogentus's user avatar
  • 6,533
0 votes
1 answer
564 views

python setup.py build with a given python version

All, I have several versions of Python installed. In the environment variables, i have two paths to site-packages folders. In the cmd, I would like to do python setup.py build with one given ...
kiriloff's user avatar
  • 26k
0 votes
1 answer
283 views

What is going in this malloc'ed array in Cython?

%%cython -f -c=-O3 -c=-fopenmp --link-args=-fopenmp from cython.parallel import parallel, prange from libc.stdlib cimport abort, malloc, free cdef int idx, i, n = 100 cdef int k cdef int * local_buf ...
richizy's user avatar
  • 2,052
2 votes
1 answer
9k views

Getting started with cython on mac os

I wrote a simple program in python: // main.py import re links = re.findall('(https?://\S+)', 'http://google.pl http://youtube.com') print(links) Then I execute this: cython main.py It was ...
mitch's user avatar
  • 2,245
25 votes
1 answer
13k views

Cython & C++: passing by reference

I am a noob with Cython and C++, so I have a question on argument passing. I want to avoid passing a copy of an argument in the following scenario: # somefile.pyx #distutils: language = c++ from ...
richizy's user avatar
  • 2,052
2 votes
1 answer
3k views

writing to file from cython

By profiling, I've determined that about 60% of my program's time is spent in writing to file, in the one line: fout.write('%d\t%d\n' % (i, j)) Here i and j are integers. Two questions: would ...
AatG's user avatar
  • 715
6 votes
2 answers
6k views

How to use unordered_map in cython?

I want a step by step guide, how to use unordered_map in cython. I've included file unordered_map.pxd into Cython/Includes/libcpp from https://gist.github.com/ikuyamada/3265267 and use 3 other files: ...
Apogentus's user avatar
  • 6,533
2 votes
1 answer
630 views

Why does Python 2.7 sign extend HDC returned from GetDC() on x64?

On Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] why is the return value of GetDC() sign extended? from ctypes import * hDC1 = windll.user32.GetDC(None) print hex(hDC1), ...
Jay Borseth's user avatar
  • 1,933
1 vote
1 answer
370 views

cython doesn't support sorted with key?

def test(): return sorted([(a,b) for a in xrange(10) for b in xrange(10)], key=lambda (x,y): x + y) The above is valid python code, yet triggers an error in cython. The error message ...
colinfang's user avatar
  • 21.4k
10 votes
1 answer
2k views

Cython Numpy code not faster than pure python

First I know that there are many similarly themed question on SO, but I can't find a solution after a day of searching, reading, and testing. I have a python function which calculates the pairwise ...
Davy Kavanagh's user avatar
0 votes
1 answer
299 views

"error: not 32-bit: 2" when using cython complied code on kivy based android app

I used cython to try improve a bottleneck in my code on a kivy application I have designed for android, everything works fine when I try to run it on my computer but I get: "ImportError: dlopen ...
Padraic Cunningham's user avatar
3 votes
0 answers
958 views

Can't install any Python modules, possible C compiler issue [closed]

I'm having a lot of issues installing/upgrading python modules. I'm new to this so please keep that in mind. Now I've tried installing/uninstalling Xcode and its command line tools. I've tried gcc-...
Mike's user avatar
  • 31
6 votes
1 answer
171 views

Cython optimize the critical part of a numpy array summation

Let L be a list L = [A_1, A_2, ..., A_n], and each of the A_i are numpy.int32 arrays of length 1024. (Most of the time 1000 < n < 4000). After some profiling, I have seen that one the most ...
Basj's user avatar
  • 44.1k
2 votes
1 answer
767 views

Why Cython forces declaration of locals at the beginning of a function

This was asked as a comment in Cython - copy constructors. The following code doesn't compile in Cython: def bar(int i): if i == 0: return i else: cdef int j j = i+1 ...
hivert's user avatar
  • 10.6k
1 vote
2 answers
194 views

How to modify a python code to a cython code? [closed]

i have been working on a project on python for physics, using enthought canopy, i wished to enhance it by modifying it to a cython code. Could someone pleases tell me how to rewrite a python to cython ...
dilyar's user avatar
  • 251
3 votes
3 answers
1k views

How to interrupt native extension code without killing the interpreter?

I am working on a project which combines high-performance algorithms written in C++ with a Python interface. C++ classes and functions are wrapped and exposed to Python via the Cython compiler. ...
clstaudt's user avatar
  • 22k
4 votes
1 answer
968 views

Cython - copy constructors

I've got a C library that I'm trying to wrap in Cython. One of the classes I'm creating contains a pointer to a C structure. I'd like to write a copy constructor that would create a second Python ...
alaroldai's user avatar
  • 336
0 votes
2 answers
691 views

using cython to speed up a python application

I am using the following python code in a Kivy application. The code works fine and displays the correct information but is quite slow when calculating the return values and displaying to the screen. ...
Padraic Cunningham's user avatar

1
89 90
91
92 93
107