append_fields recarray, dtype empty, () .
dtype :
In [102]: dt=np.dtype([('afield','f'),('pos','f',(3,))])
In [103]: dt
Out[103]: dtype([('afield', '<f4'), ('pos', '<f4', (3,))])
In [104]: arr = np.ones((3,),dtype=dt)
In [105]: arr
Out[105]:
array([(1.0, [1.0, 1.0, 1.0]), (1.0, [1.0, 1.0, 1.0]),
(1.0, [1.0, 1.0, 1.0])],
dtype=[('afield', '<f4'), ('pos', '<f4', (3,))])
dtype:
In [106]: dt1=np.dtype([('afield','f'),('pos','f',(3,)),('vel','f',(2,))])
In [107]: arr1 = np.empty((3,),dtype=dt1)
In [108]: arr1
Out[108]:
array([(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])],
dtype=[('afield', '<f4'), ('pos', '<f4', (3,)), ('vel', '<f4', (2,))])
In [109]: for name in dt.names:
.....: arr1[name] = arr[name]
In [110]: arr1
Out[110]:
array([(1.0, [1.0, 1.0, 1.0], [0.0, 0.0]),
(1.0, [1.0, 1.0, 1.0], [0.0, 0.0]),
(1.0, [1.0, 1.0, 1.0], [0.0, 0.0])],
dtype=[('afield', '<f4'), ('pos', '<f4', (3,)), ('vel', '<f4', (2,))])
recarray - , (arr.pos).
:
In [118]: rf.append_fields(arr, 'vel', np.arange(3),usemask=False)
Out[118]:
array([(1.0, [1.0, 1.0, 1.0], 0), (1.0, [1.0, 1.0, 1.0], 1),
(1.0, [1.0, 1.0, 1.0], 2)],
dtype=[('afield', '<f4'), ('pos', '<f4', (3,)), ('vel', '<i4')])
(2) recursive_fill. , dt1:
In [206]: arr = np.ones((3,),dtype=dt)
In [207]: arr1 = np.zeros((3,),dtype=dt1)
In [208]: rf.recursive_fill_fields(arr,arr1)
Out[208]:
array([(1.0, [1.0, 1.0, 1.0], [0.0, 0.0]),
(1.0, [1.0, 1.0, 1.0], [0.0, 0.0]),
(1.0, [1.0, 1.0, 1.0], [0.0, 0.0])],
dtype=[('afield', '<f4'), ('pos', '<f4', (3,)), ('vel', '<f4', (2,))])
In [210]: x = np.ones((3,),dtype=[('vel','f',(2,))])
In [211]: x['vel'] *= 2
In [212]: rf.recursive_fill_fields(x,arr1)
Out[212]:
array([(1.0, [1.0, 1.0, 1.0], [2.0, 2.0]),
(1.0, [1.0, 1.0, 1.0], [2.0, 2.0]),
(1.0, [1.0, 1.0, 1.0], [2.0, 2.0])],
dtype=[('afield', '<f4'), ('pos', '<f4', (3,)), ('vel', '<f4', (2,))])
x append_fields:
In [213]: rf.append_fields(arr, 'vel', x, usemask=False)
Out[213]:
array([(1.0, [1.0, 1.0, 1.0], ([2.0, 2.0],)),
(1.0, [1.0, 1.0, 1.0], ([2.0, 2.0],)),
(1.0, [1.0, 1.0, 1.0], ([2.0, 2.0],))],
dtype=[('afield', '<f4'), ('pos', '<f4', (3,)), ('vel', [('vel', '<f4', (2,))])])
- . - .
merge_arrays -
In [247]: rf.merge_arrays((arr,x),flatten=True)
Out[247]:
array([(1.0, [1.0, 1.0, 1.0], [2.0, 2.0]),
(1.0, [1.0, 1.0, 1.0], [2.0, 2.0]),
(1.0, [1.0, 1.0, 1.0], [2.0, 2.0])],
dtype=[('afield', '<f4'), ('pos', '<f4', (3,)), ('vel', '<f4', (2,))])
In [248]: dx = [('f0','f',(2,))]
In [250]: y=np.zeros((3,), dtype=dx)
In [251]: y['f0'] = np.arange(6.).reshape(3,2)
, .