Using structs and 2D arrays in "pure python" mode

123 views
Skip to first unread message

Ross Rochford

unread,
Aug 29, 2017, 1:02:49 AM8/29/17
to cython-users
Hi all,

I'm trying to define a struct that stores or points to a 50x50 2-d array of integers, in "pure python" mode.

How do I adjust the following to work? I don't mind using pointers if necessary.


import cython
import numpy

RectStruct = cython.struct(
image_summary=numpy.ndarray,
)

my_array = numpy.zeros((50,50), dtype=numpy.int)

rect = cython.declare(RectStruct)
rect.image_summary = my_array # throws: TypeError: only integer scalar arrays can be converted to a scalar index


Stefan Behnel

unread,
Sep 1, 2017, 2:03:15 AM9/1/17
to cython...@googlegroups.com
You cannot define a struct with Python object members, since the reference
counting cannot safely be done for it.

However, you didn't say why you want to do this. Maybe there is a better
way than using a struct.

Stefan

Ross Rochford

unread,
Sep 23, 2017, 2:23:35 PM9/23/17
to cython-users
Hi Stefan,

I'm trying to write a python script that uses C types that I can both run as standard python and run as C (using https://github.com/markolopa/pyorcy, similar to pure python mode). 

My code's logic needs to store a bundle of data containing ints, floats and a 2d array. The original code used a python dictionary with 2d numpy arrays. Then I attempted to use a cython.struct instead of a dictionary, this works with the ints and floats but I can't figure out how to store the 2d array in the struct. It doesn't have to be a numpy array, maybe a pointer? I just need a way of storing it in the struct. The 2d array will contain ints.


Ross
Reply all
Reply to author
Forward
0 new messages