I am attempting to create a function in cython that accepts a numpy structured array or record array by defining a cython struct type. Suppose I have the data:
a = np.recarray(3, dtype=[('a', np.float32), ('b', np.int32), ('c', '|S5'), ('d', '|S3')])
a[0] = (1.1, 1, 'this\0', 'to\0')
a[1] = (2.1, 2, 'that\0', 'ta\0')
a[2] = (3.1, 3, 'dogs\0', 'ot\0')
and then the cython code:
import numpy as np
cimport numpy as np
cdef packed struct tstruct:
np.float32_t a
np.int32_t b
char[5] c
char[3] d
def test_struct(tstruct[:] x):
cdef:
int k
tstruct y
for k in xrange(3):
y = x[k]
print y.a, y.b, y.c, y.d
When I try to run test_struct(a), I get the error:
ValueError: Expected a dimension of size 5, got 8
If in the array and corresponding struct I reorder the fields such that the string fields are not adjacent to each other, then the function works as expected. Null-terminating the strings as I show above does not seem to make a difference.
I was wondering if there was some simple string handling issue that I’m missing? In the actual code, I don’t typically even need to manipulate the string fields. I just need them read in properly.
Any suggestions would be appreciated.
Thanks,
Josh