I have some data that I need to write into an HDF5 file which will be read by a data processing application. This I do with h5py. However, the data processing application seems to only accept attributes with an array size of one, but when I create my attributes with h5py I get an array size named scalar. Below picture is from HDFView.
I understand that when I call dataset.attrs.create(key, value)
that I can set a dtype and shape h5py documentation for attributes. But I do not understand what I need to provide to always have the array size as 1 and not scalar. In particular when as shown above, some of them seem to just be set to one out of the box.
I create the attributes in the following way:
import h5py
if __name__ == '__main__':
out_file = h5py.File('tmp.h5')
data = [0, 1, 2, 3, 4]
attributes = {'sample': 1, 'option': 'tmp'}
out_file.create_dataset('tmp_dataset', data=data)
for attr_key, attr_value in attributes.items():
out_file['tmp_dataset'].attrs.create(attr_key, attr_value)
out_file.close()
I have tried to specify the dtype to float
and so on. But it did not seem to have an effect.
Okay I found a solution to this issue.
I had not caught that it was the shape parameter to create I would use for this. So by chaing the MRE from above to:
import h5py
if __name__ == '__main__':
out_file = h5py.File('tmp.h5')
data = [0, 1, 2, 3, 4]
attributes = {'sample': 1, 'option': 'tmp'}
out_file.create_dataset('tmp_dataset', data=data)
for attr_key, attr_value in attributes.items():
out_file['tmp_dataset'].attrs.create(name=attr_key, data=attr_value, shape=(1,))
out_file.close()
I have solved the issue