Creating Hdf5 Compound Attributes Using H5py
I'm trying to create some simple HDF5 datasets that contain attributes with a compound datatype using h5py. The goal is an attribute that has two integers. Here are two example o
Solution 1:
To make an array with dt_type
, you have to properly nest lists and tuples:
In [162]: arr = np.array([(['23','3'],)], dt_type)
In [163]: arr
Out[163]: array([([23, 3],)], dtype=[('val1', '<i4', (2,))])
This is (1,) array with a compound dtype. The dtype has 1 field, but 2 values within that field.
With the alternative dtype:
In [165]: dt2 = np.dtype({"names": ["val1", "val2"],"formats": ['<i4', '<i4']})
In [166]: arr2 = np.array([('23','3',)], dt2)
In [167]: arr2
Out[167]: array([(23, 3)], dtype=[('val1', '<i4'), ('val2', '<i4')])
or the simplest array:
In [168]: arr3 = np.array([23,2])
In [169]: arr3
Out[169]: array([23, 2])
Writing to a dataset:
In [170]: ds.attrs.create('arr', arr)
In [172]: ds.attrs.create('arr2', arr2)
In [173]: ds.attrs.create('arr3', arr3)
check the fetch:
In[175]: ds.attrs['arr']Out[175]: array([([23, 3],)], dtype=[('val1', '<i4', (2,))])
In[176]: ds.attrs['arr2']Out[176]: array([(23, 3)], dtype=[('val1', '<i4'), ('val2', '<i4')])
In[177]: ds.attrs['arr3']Out[177]: array([23, 2])
dump:
1203:~/mypy$ h5dump compound.h5
HDF5 "compound.h5" {
GROUP "/" {
DATASET "test" {
DATATYPE H5T_STD_I64LE
DATASPACE SIMPLE { ( 10 ) / ( 10 ) }
DATA {
(0): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
}
ATTRIBUTE "arr" {
DATATYPE H5T_COMPOUND {
H5T_ARRAY { [2] H5T_STD_I32LE } "val1";
}
DATASPACE SIMPLE { ( 1 ) / ( 1 ) }
DATA {
(0): {
[ 23, 3 ]
}
}
}
ATTRIBUTE "arr2" {
DATATYPE H5T_COMPOUND {
H5T_STD_I32LE "val1";
H5T_STD_I32LE "val2";
}
DATASPACE SIMPLE { ( 1 ) / ( 1 ) }
DATA {
(0): {
23,
3
}
}
}
ATTRIBUTE "arr3" {
DATATYPE H5T_STD_I64LE
DATASPACE SIMPLE { ( 2 ) / ( 2 ) }
DATA {
(0): 23, 2
}
}
}
}
}
Post a Comment for "Creating Hdf5 Compound Attributes Using H5py"