A technique that can be used when you are in agony and want to use a C-like structure in Python. For example, if you want to create a structure like ↓.
c_struct.h
typedef struct{
float pos[4];
unsigned char class;
float score;
} HOGE;
HOGE fuga[10];
Use Structured Data types
c_like_struct.py
import numpy as np
HOGE = np.dtype([("pos", np.float32, (4,)),
("class", np.uint8, (1,)),
("score", np.float32, (1,)),
],
align=True
)
fuga = np.zeros(10, dtype=HOGE)
With this, a C language-like structure can be expressed. Padding is done by setting align = True above.
processing
print(HOGE)
print(fuga)
result
{'names':['pos','class','score'], 'formats':[('<f4', (4,)),('u1', (1,)),('<f4', (1,))], 'offsets':[0,16,20], 'itemsize':24, 'aligned':True}
[([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])]
processing
print("fuga.dtype: ",fuga.dtype)
print("fuga.shape: ",fuga.shape)
print("fuga.['pos'].shape: ",fuga['pos'].shape)
print("fuga.['pos'][0].shape: ",fuga['pos'][0].shape)
print("fuga['pos'][1]: ", fuga['pos'][1])
result
fuga.dtype: {'names':['pos','class','score'], 'formats':[('<f4', (4,)),('u1', (1,)),('<f4', (1,))], 'offsets':[0,16,20], 'itemsize':24, 'aligned':True}
fuga.shape: (10,)
fuga['pos'].shape: (10, 4)
fuga['pos'][0].shape: (4,)
fuga['pos'][1]: [0. 0. 0. 0.]
processing
piyo = fuga.view(np.recarray)
print("piyo: ", piyo)
print("piyo.pos.shape: ", piyo.pos.shape)
print("piyo.pos[0].shape: ", piyo.pos[0].shape)
print("piyo.pos[1]: ",piyo.pos[1])
result
piyo: [([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])]
piyo.pos.shape: (10, 4)
piyo.pos[0].shape: (4,)
piyo.pos[1]: [0. 0. 0. 0.]
processing
fuga['pos'][1] = [-1.0 , 2.3, 0.42, 0.0]
print("fuga['pos'][:3]: ",fuga['pos'][:3])
result
fuga['pos'][:3]: [[ 0. 0. 0. 0. ]
[-1. 2.3 0.42 0. ]
[ 0. 0. 0. 0. ]]
Recommended Posts