Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numpy structured arrays cannot be mapped to a struct if the hierarchy is deeper than two levels #1840

Open
sjdv1982 opened this issue Aug 24, 2017 · 0 comments

Comments

@sjdv1982
Copy link

Level 0: dtype => float, works
Level 1: structured dtype => [(name: float), (name: float), ... ], works
Level 2: structured dtype => [(name: [level 1 dtype] ), ... ], works
Level 3: structured dtype => [(name: [level 2 dtype] ), ... ], does not work

IPython notebook attached
cython-bug-notebook.zip

%load_ext Cython
%%cython -a
import numpy as np
cimport numpy as np

cdef struct Coordinate:
    float x
    float y
    float z

cdef struct AxisSystem:
    Coordinate origin
    Coordinate x
    Coordinate y
    Coordinate z
    
cdef struct TwoAxisSystem:
    AxisSystem a
    AxisSystem b

dCoordinate0 = [('x', '<f4'), ('y', '<f4'), ('z', '<f4')]
dCoordinate = np.dtype(dCoordinate0, align=True)

dAxisSystem0 =[('origin', dCoordinate0), ('x', dCoordinate0), ('y', dCoordinate0), ('z', dCoordinate0)]
dAxisSystem = np.dtype(dAxisSystem0, align=True)

dTwoAxisSystem0 = [('a', dAxisSystem0),('b', dAxisSystem0)]
dTwoAxisSystem = np.dtype(dTwoAxisSystem0, align=True)

def print_test():
    cdef Coordinate[:] coor2
    coor = np.zeros((1), dtype=dCoordinate)
    coor["x"] = 999
    coor2 = coor
    print(coor2[0].x)
    print("success test 1")
    
    cdef AxisSystem[:] ax2
    ax = np.zeros((1), dtype=dAxisSystem)
    ax["z"] = coor
    ax2 = ax
    print(ax2[0].z.x)
    print("success test 2")
    
    cdef TwoAxisSystem[:] tax2
    tax = np.zeros((1), dtype=dTwoAxisSystem)
    tax["b"] = ax
    tax2 = tax
    print(tax2[0].b.z.x)
    print("success test 3")
<!-- Generated by Cython 0.25.2 -->
...
print_test()
999.0
success test 1
999.0
success test 2



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

ValueError                                Traceback (most recent call last)

<ipython-input-12-a9a24816d227> in <module>()
----> 1 print_test()


_cython_magic_ac12ada873519b268feb55f8cf90729a.pyx in _cython_magic_ac12ada873519b268feb55f8cf90729a.print_test (/home/sjoerd/.cache/ipython/cython/_cython_magic_ac12ada873519b268feb55f8cf90729a.c:2493)()


ValueError: Buffer dtype mismatch, expected 'Coordinate' but got 'float' in 'AxisSystem.origin'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant