C attributes cannot be added in implementation part of extension type defined in a pxd

659 views
Skip to first unread message

Zahra Sheikh

unread,
Dec 13, 2017, 3:16:56 PM12/13/17
to cython-users
Hello,

I am trying to use a class namely "Matrix" which is written in a cython script "matrix.pyx", in another cython script "Sampling_debugger.pyx".  Here you can find part of my codes
matrix.pyx
import ctypes
cimport numpy as np
cimport cython                                               
from libc.math cimport log, exp, pow                                              
from libc.stdint cimport *
from cython.parallel import parallel, prange
cdef class Matrix:
     
     cdef vector[double] matrix   
     cdef int _rows
     cdef int _columns
     def __cinit__(self, int rows=0, int columns=0, bint Identity=False):
         self._rows=rows
         self._columns=columns
         self.matrix.resize(rows*columns)
         if Identity:
            self._IdentityMatrix()
         
     property rows:
        def __get__(self):
            return self._rows
        def __set__(self, int x):
            self._rows = x    
     property columns:
        def __get__(self):
            return self._columns
        def __set__(self, int y):
            self._columns = y    


     cpdef double getVal(self, int r, int c):
           return self.matrix[r*self._columns+c]

 
     cpdef void setVal(self, int r, int c, double v): 
           self.matrix[r*self._columns+c] = v

matrix.pxd
from libcpp.vector cimport vector
cdef class Matrix:

     cdef public:
          int _rows
          int _columns
          bint Identity

     cpdef double getVal(self, int r, int c)
     cpdef void setVal(self, int r, int c, double v)

Sampling_debugger.pyx
import numpy as np
cimport numpy as np
cimport cython
from cython_gsl cimport *
 
from matrix cimport *
from matrix import *

cdef extern from "stdlib.h":         
    long rand()                    
    void srand(int seed)                    

cdef extern from "time.h":
    ctypedef long time_t
    time_t time(time_t*)

cdef gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937)
srand(time(NULL))
     
cdef Matrix initialWeight(double alpha, int K ):
     cdef:
          Matrix mu = Matrix(K,1)
          int i
          int seed = rand()
     #generate random seed
     gsl_rng_set(r, seed)
     cdef double prev_mu
     for i from 0 <= i < K:
         if i >= 1:
            prev_mu *= gsl_ran_beta(r, alpha, 1)            
         else:
            prev_mu = gsl_ran_beta(r, alpha, 1)
                
         mu.setVal( i, 0, prev_mu)
     return mu
 

cpdef void run( int k, double alpha):
      cdef:
          int i
          Matrix mu = initialWeight(alpha, k )           
    
      for i from 0 <= i < k:
          print mu.getVal(i,0)
      return 

and eventually, my "setup.py" script is:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
from numpy import get_include
import numpy
import cython_gsl

modules = [
    Extension("matrix", sources=["matrix.pyx"],language="c++",libraries=cython_gsl.get_libraries(),include_dirs=[numpy.get_include(), cython_gsl.get_include()]),
    Extension("Sampling_debugger", sources=["Sampling_debugger.pyx"],language="c++",libraries=cython_gsl.get_libraries(),include_dirs=[numpy.get_include(), cython_gsl.get_include()])
]

setup(
    cmdclass={'build_ext': build_ext},
    ext_modules=cythonize(modules)
)

when I compile my scripts, I get these error messages  

cdef class Matrix:
     
     cdef vector[double] matrix   
                        ^
------------------------------------------------------------

matrix.pyx:55:25: C attributes cannot be added in implementation part of extension type defined in a pxd

Error compiling Cython file:
------------------------------------------------------------
...
     
cdef class Matrix:
     
     cdef vector[double] matrix   
     cdef int _rows
             ^
------------------------------------------------------------

matrix.pyx:56:14: C attributes cannot be added in implementation part of extension type defined in a pxd

Error compiling Cython file:
------------------------------------------------------------
...
         
cdef class Matrix:
     
     cdef vector[double] matrix   
     cdef int _rows
             ^
------------------------------------------------------------

matrix.pyx:56:14: '_rows' redeclared 

If I individually compile "matrix.pyx" without the header file "matrix.pxd", it gets compiled without any error. I will appreciate if one of the experts here explains what I am doing wrong?!!

Thanks in advance.





Nils Bruin

unread,
Dec 13, 2017, 4:20:32 PM12/13/17
to cython-users
I suspect the problem is that C attributes cannot be added in implementation part of extension type defined in a pxd. The attributes "matrix" and "_rows" affect the memory layout of Matrix object. When you "cimport matrix", cython needs the memory layout of the object in order to address entries appropriately. cdef methods do not affect the memory layout of the individual objects, so it's fine to declare those in a "pyx" but not in the accompanying pxd.

Zahra Sheikh

unread,
Dec 14, 2017, 5:58:04 AM12/14/17
to cython-users
So to be clear about what you suggest: I should not redeclare  "vector[double] matrix" or "int _rows" in my pxd script. Did I understand it correctly? If I remove these lines I still get this error:
 C attributes cannot be added in implementation part of extension type defined in a pxd
but no longer the following error:
'_rows' redeclared

Thanks

Chris Barker - NOAA Federal

unread,
Dec 14, 2017, 11:59:30 AM12/14/17
to cython...@googlegroups.com


On Dec 14, 2017, at 2:58 AM, Zahra Sheikh <sheikh...@gmail.com> wrote:

So to be clear about what you suggest: I should not redeclare  "vector[double] matrix" or "int _rows" in my pxd script. Did I understand it correctly?

I think you may need to do it the other way around:

Declare it in the pxd, use it in the pyx.

Pxd is for declarations, after all.

(Untested)

-CHB

--

---
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Bradshaw

unread,
Dec 14, 2017, 12:12:30 PM12/14/17
to cython...@googlegroups.com
Exactly. The declaration information must be in the pxd so cimporting
modules can access it (for the aforementioned reasons of memory layout
as well as access).
Reply all
Reply to author
Forward
0 new messages