问题
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 of attributes I'd like to create.
My attempts end up with an array of two values such as
How can I code this using h5py and get a single value that contains two integers? Current code looks something like
dt_type = np.dtype({"names": ["val1"],"formats": [('<i4', 2)]})
# also tried np.dtype({"names": ["val1", "val2"],"formats": [('<i4', 1), ('<i4', 1)]})
dataset.attrs.create('time.start', [('23', '3')], dtype=dt_type)
How can I specify the type or the attribute create to get the first example?
回答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
}
}
}
}
}
来源:https://stackoverflow.com/questions/60174899/creating-hdf5-compound-attributes-using-h5py