Christopher Jordan-Squire | 8 Apr 07:06 2014
Picon

recarray and typed memory views

I'm writing cython code to glue together some C code and python. The C code takes in many different struct types, such as

typdef struct{
char[16] name
int id
double value
} Container;

In total there are around 10 different types like that. So in the cython code I have many declarations like

ctypedef struct Container:
char[16] name
int id
double value

But I'd also like to create arrays of Container in python and pass those into the C code via Cython. Ideally without extension types that glom together Python objects to put into an array of strut at the cython level. The obvious thing to try was numpy record arrays. So in addition to the cython boilerplate I have numpy boiler plate for these types, e.g. 

container_t = np.dtype( '|S16,i4,f8', align=True). 

This works. But I'm repeating myself a lot. Not only do I create the cython type, but I also create a numpy dtype containing essentially the same information. Is there any way to get around this while keeping the same functionality? 

Thanks,
Chris

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Dan Farmer | 7 Apr 20:03 2014
Picon

Serialization advice

Hello,

Here's the background: I have a C++ library (which could less charitably be called C with classes) that I've wrapped with Cython. This all works fine. But now I would like to be able to save things to disk. Under the hood the problematic structure is a singly linked list that is allocated by the C++ library when the constructor is called (TList). In my python code I have dictionaries that contain meta-data and the wrapped versions of the TLists e.g., {'experiment': 'foo', 'result': <TList>}. I'd like to be able to pickle this structure.

The Cython FAQ has an entry on this, but it just links to the python docs on the pickle protocol, which vaguely makes sense but it's still not clear to me how that interacts concretely with cython and cdef'd objects. So where I have cdef class ListWrapper I would def __reduce__(self) and this returns the constructor that would create an empty ListWrapper, and then... somehow I need to squirrel the data away and then reinsert the data. This is the part that has me confused.

I'd appreciate any tips or pointers to similar code that's out there.

Thanks,
Dan

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
chetan raga | 7 Apr 07:31 2014
Picon

error while creating cython wrapper class between c++ and python in linux

This is pyx:
def extern from "Rect.h":
    cdef cppclass Rectangle:
        Rectangle()
        int getLength()
 
cdef class PyRectangle:
    cdef Rectangle *thisptr
    def __cinit__(self):
        self.thisptr = new Rectangle()
    def __dealloc__(self):
        del self.thisptr
    def getLength(self):
        return self.thisptr.getLength()


this is .cpp file
#include "dijsdk.h"
#include <iostream>
#include <QString>
#include "arraysize.h"
#include "Rect.h"



Rectangle::Rectangle()
{
}
 
Rectangle::~Rectangle()
{
}
 
int Rectangle::getLength()
{
QString m_guids[16];
    DijSDK_CamGuid guids[ARRAYSIZE(m_guids)] = {0};
    unsigned int numGuids = ARRAYSIZE(guids);
    int result;
    int index;
    int res = DijSDK_Init();
    result = DijSDK_FindCameras(guids, &numGuids);
   
    std::cout<<"\nNumGuids = "<<numGuids;
   
    std::cout<<"\nresult = "<<result;
       
    if(result == 0)
        result1 = DijSDK_FindCameras(guids, &numGuids); //API call
       
    std::cout<<"\nNumguids = " <<numGuids;
    std::cout<<"\nResult = "<<result1;
    std::cout<<"\nGuids = "<<guids[0];
   
    return 0;
}

this is .h file
class Rectangle {
public:
  Rectangle();
  ~Rectangle();
  int getLength();

};
this is setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
 
setup(ext_modules=[Extension("rectangle", ["rect.pyx", "Rect.cpp"], language="c++",)],
include_dirs=["/usr/include/qt4/QtCore -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtGui -I/usr/include/qt4"],
cmdclass = {'build_ext': build_ext})

after that iam using command in linux
user <at> emb-ubu-085:~/Desktop/reports$ sudo python setup.py build_ext --inplace  -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I.
running build_ext
cythoning rect.pyx to rect.cpp
building 'rectangle' extension
i686-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I. -I/usr/include/python2.7 -c rect.cpp -o build/temp.linux-i686-2.7/rect.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
i686-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I. -I/usr/include/python2.7 -c Rect.cpp -o build/temp.linux-i686-2.7/Rect.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
Rect.cpp:3:19: fatal error: QString: No such file or directory
compilation terminated.
error: command 'i686-linux-gnu-gcc' failed with exit status 1

let me know guys where iam doing mistake in the above case

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Saullo Castro | 3 Apr 19:47 2014
Picon

Wrong signature error using function types in multiple modules

I have the following Cython modules:

compmech integrate integratev.pxd integratev.pyx conecyl main.pyx

In integratev.pxd I've declared:

ctypedef void (*f_type)(int npts, double *xs, double *ts, double *out, double *alphas, double *betas, void *args) nogil cdef int trapz2d(f_type f, int fdim, np.ndarray[cDOUBLE, ndim=1] final_out, double xmin, double xmax, int m, double ymin, double ymax, int n, void *args, int num_cores)

I call trapz2d from main.pyx, and the function passed to trapz2d is declared in main.pyx, e.g:

from compmech.integrate.integratev cimport trapz2d cdef void cfk0L(int npts, double *xs, double *ts, double *out, double *alphas, double *betas, void *args) nogil: ... trapz2d(cfk0L, fdim, k0Lv, xa, xb, nx, ta, tb, nt, &args, num_cores)

It compiles just fine, but when I run I get the error:

TypeError: C function compmech.integrate.integratev.trapz2d has wrong signature (expected int (__pyx_t_8compmech_9integrate_10integratev_f_type, int, PyArrayObject *, double, double, int, double, double, int, void *, int), got int (__pyx_t_10integratev_f_type, int, PyArrayObject *, double, double, int, double, double, int, void *, int))ddoes anybody known how to avoid this error?

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Skipper Seabold | 2 Apr 16:26 2014
Picon

Re: Supporting Capsule and CObject?

On Wed, Apr 2, 2014 at 2:19 AM, Stefan Behnel <stefan_ml <at> behnel.de> wrote:
> Skipper Seabold, 01.04.2014 22:57:
>> I am trying to use the header file provided by numpy [1] so that our
>> code builds on both Python 2 and Python 3. I must be missing something
>> though. So far I can only get it to compile on Python 2. Python 3
>> results in the attached error log. Two questions.
>>
>> 1) why do I need to use object in the declaration rather than a
>> pointer to cython.object.PyObject to get it to compile on Python 2.
>> 2) Any idea why this isn't compiling on Python 3?
>
> What's the error you get in Py3?

For this minimum example, I get the following

https://gist.github.com/jseabold/9935135

Longer project build. Similar errors, but in numpy/npy_3kcompat.h

https://travis-ci.org/statsmodels/statsmodels/jobs/22067954

>
> Regarding the subject line, note that PyCObject is no longer available in
> Py3, so you have to use PyCapsule there (which is also available in Py2.7
> but not earlier).

Right. We need to support Python 2.6 - 3.4. It looks like that's what
the NpyCapsule_AsVoidPtr is for in numpy linked upthread.

>
>
>> cdef extern from "numpy/npy_3kcompat.h":
>>     void *NpyCapsule_AsVoidPtr(object ptr)
>
> Assuming this is what your question 1) is referring to, you need to use
> "object" here because you are passing a Python object.

I guess this is what's confusing me

[~/]
[3]: scipy.linalg.get_blas_funcs('gemm')._cpointer
[3]: <PyCObject at 0x5657648>

>
> Cython distinguishes between a (non-refcounted) pointer (which includes
> PyObject*) and a ref-counted Python object reference. The normal thing to
> use is "object", unless you know what you are doing.
>
> The only case where you have to be careful is when a function return value
> is a so-called "borrowed reference". In that case, you can't use "object",
> because that would make Cython think it owns the reference. In that case,
> declare it as "PyObject*" and use an explicit cast to <object>.
>

Thanks. That's helpful. I did read the faq on this, I guess I just
haven't fully absorbed it.

Skipper

> Stefan
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--

-- 

--- 
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stefan Behnel | 2 Apr 09:42 2014
Picon

Re: CYTHON_INLINE causing errors on MacOS with clang

Daniel Farrell, 02.04.2014 04:43:
> A follow up to this. In the end I removed the inline from the functions in,
> https://github.com/bmcage/odes/blob/master/scikits/odes/sundials/common_defs.pxd
>  
> 
> This allowed the compilation to complete without further errors, however 
> the end product is not valid. When running the tests that come with the 
> scikits.odes package produce an namespace error when loading the complied 
> code,
> 
> Expected in: flat namespace

What is expected there? The function symbol? Or something else? When
reporting errors, please always take care to provide the complete error
message, including any context that might help in understanding it (and/or
the actual problem).

The error message above generally hints at some kind of linking problem,
maybe a missing library.

Stefan

--

-- 

--- 
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Skipper Seabold | 1 Apr 22:57 2014
Picon

Supporting Capsule and CObject?

Hi,

I am trying to use the header file provided by numpy [1] so that our
code builds on both Python 2 and Python 3. I must be missing something
though. So far I can only get it to compile on Python 2. Python 3
results in the attached error log. Two questions.

1) why do I need to use object in the declaration rather than a
pointer to cython.object.PyObject to get it to compile on Python 2.
2) Any idea why this isn't compiling on Python 3?

[1] https://github.com/numpy/numpy/blob/master/numpy/core/include/numpy/npy_3kcompat.h

Skipper

------

#mwe.pyx
cimport cython

import scipy.linalg
from numpy import float64
from numpy cimport float64_t, PyArray_EMPTY, ndarray
cimport numpy as cnp
cnp.import_array()

ctypedef float64_t DOUBLE

from blas cimport dgemm_t

cdef extern from "numpy/npy_3kcompat.h":
    void *NpyCapsule_AsVoidPtr(object ptr)

cdef dgemm_t *dgemm =
<dgemm_t*>NpyCapsule_AsVoidPtr(scipy.linalg.blas.get_blas_funcs('gemm',
dtype=float64)._cpointer)

 <at> cython.boundscheck(False)
 <at> cython.wraparound(False)
 <at> cython.cdivision(True)
def mydot(ndarray[DOUBLE, ndim=2] A, ndarray[DOUBLE, ndim=2] B):
    cdef:
        cnp.npy_intp outshape[2]
        int m = A.strides[1]/sizeof(DOUBLE)
        int FORTRAN = 1
    outshape[0] = <cnp.npy_intp> m
    outshape[1] = <cnp.npy_intp> m
    cdef:
        DOUBLE alpha = 1.0
        DOUBLE beta = 0.0
        ndarray[DOUBLE, ndim=2] out = PyArray_EMPTY(2, outshape,
                                                    cnp.NPY_DOUBLE,
                                                    FORTRAN)

    dgemm("N", "N", &m, &m, &m, &alpha, <DOUBLE*>A.data, &m,
          <DOUBLE*>B.data, &m, &beta, <DOUBLE*>out.data, &m)
    return out

-------------------

#blas.pxd
ctypedef int dgemm_t(
    # Compute C := alpha*A*B + beta*C
    char *transa,  # {'T','C'}: o(A)=A'; {'N'}: o(A)=A
    char *transb,  # {'T','C'}: o(B)=B'; {'N'}: o(B)=B
    int *m,        # Rows of o(A)    (and of C)
    int *n,        # Columns of o(B) (and of C)
    int *k,        # Columns of o(A) / Rows of o(B)
    double *alpha, # Scalar multiple
    double *a,     # Matrix A: mxk
    int *lda,      # The size of the first dimension of A (in memory)
    double *b,     # Matrix B: kxn
    int *ldb,      # The size of the first dimension of B (in memory)
    double *beta,  # Scalar multiple
    double *c,     # Matrix C: mxn
    int *ldc       # The size of the first dimension of C (in memory)
)

--

-- 

--- 
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Attachment (mwe.pyx): application/octet-stream, 1531 bytes
Attachment (blas.pxd): application/octet-stream, 960 bytes
Attachment (build): application/octet-stream, 10 KiB
Jamie Diprose | 31 Mar 00:24 2014
Picon

Python classes as parameter types

Hi

I know this is possible using cdef classes, but I was wondering if its possible to use Python classes as parameter types. For example:

class Cat:
    def __init__(self, str name):
        self.name = name

class Dog:
    def __init__(self, str name):
        self.name = name

    def eat(self, Cat cat):
        print "I am eating " + cat.name + " the Cat"


I get the following error:

Error compiling Cython file:
------------------------------------------------------------
...

class Dog:
    def __init__(self, str name):
        self.name = name

    def eat(self, Cat cat):
                 ^
------------------------------------------------------------

objects.pyx:12:18: 'Cat' is not a type identifier

Thanks for your help

Jamie

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Bruce Cropley | 29 Mar 00:55 2014
Picon

AttributeError: 'CClassDefNode' object has no attribute 'scope'

While attempting to build kivy, (with Python 2.7.5 on OS X 10.9.2) I get the following cython crash:

File "Visitor.py", line 170, in Cython.Compiler.Visitor.TreeVisitor._visit (/private/var/folders/xd/52m0srtj17d2f54vrv_wy2940000gn/T/pip_build_root/cython/Cython/Compiler/Visitor.c:4284)
File "/Library/Python/2.7/site-packages/Cython/Compiler/ParseTreeTransforms.py", line 1469, in visit_CClassDefNode
node = self.visit_ClassDefNode(node)
File "Visitor.py", line 356, in Cython.Compiler.Visitor.EnvTransform.visit_ClassDefNode (/private/var/folders/xd/52m0srtj17d2f54vrv_wy2940000gn/T/pip_build_root/cython/Cython/Compiler/Visitor.c:8003)
AttributeError: 'CClassDefNode' object has no attribute 'scope'

More details at:
http://pastebin.com/FQpf4ZzT

Any suggestions?

Thanks,
Bruce

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jay L. | 27 Mar 17:39 2014
Picon

Cython Cdef Func. via Python multiprocessing pool

Is it possible to utilize the standard library apply_async method (or map) to make
calls to a cpdef function?

For example using dummy functions that only illustrate the general idea;
the sub matrix slicing is fictional within the cython_func.:

caller.py

import multiprocessing as mp

pool = mp.Pool(2)
n = 10
a = np.arange(n*n).reshape(n,n)
for offset in range(2):
    res = pool.apply_async(cython_func, args=(a, n, offset))
res.get()

callee.pyx
cpdef cython_func(double [:,:] a, int n):
    cdef int i, j
    for i in range(offset, n):
        for j in range(offset, n):
            a[i,j] += 1

I am seeing issues with res.get() and I suspect that the Python check to
see if the python_func has finished is the issue.

Error is: TypeError: 'int' does not have the buffer interface

Since I am working on a memory buffer, I tried including an explicit return
in cython_func, but that resulted in the same issue.

P.S. I realize that using OpenMP and prange is available - I am curious
about compatibility with the multiprocessing module.

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
chetan raga | 27 Mar 06:34 2014
Picon

iam new to cython can any body help me in this

root <at> emb-ubu-085:~/Desktop/reports# python setup.py build
running build
running build_ext
skipping 'rectangle.cpp' Cython extension (up-to-date)
building 'rectangle' extension
i686-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c rectangle.cpp -o build/temp.linux-i686-2.7/rectangle.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
i686-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c Rectangle.cpp -o build/temp.linux-i686-2.7/Rectangle.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
Rectangle.cpp: In member function ‘int Rectangle::getLength()’:
Rectangle.cpp:21:1: error: ‘QString’ was not declared in this scope
Rectangle.cpp:21:9: error: expected ‘;’ before ‘m_guids’
Rectangle.cpp:22:23: error: ‘m_guids’ was not declared in this scope
Rectangle.cpp:23:26: error: ‘guids’ was not declared in this scope
Rectangle.cpp:34:3: error: ‘result1’ was not declared in this scope
Rectangle.cpp:37:28: error: ‘result1’ was not declared in this scope
Rectangle.cpp:25:6: warning: unused variable ‘index’ [-Wunused-variable]
Rectangle.cpp:26:6: warning: unused variable ‘res’ [-Wunused-variable]
error: command 'i686-linux-gnu-gcc' failed with exit status 1

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gmane