Reputation: 426
I am trying to update hdf5 file data which needs calibration. The code which I have added is:
with h5py.File(fileName,'r+') as hf:
keys=hf.keys()
data = hf[key][...]
hf[key][...] = data*calibration_factor
Just for the simple calibration. However, its resulting to all zero in hf[key][...]
. When I am assigning the product on the right side to a variable, I am getting the result properly. The dtype
of data
is int16
. Any possible solution for this? I am struggling a lot on this,thanks for understanding.
This question I have posted as an add-on to another question, but as its separate hence created a new question here. Please don't mark it as duplicate.
Upvotes: 0
Views: 92
Reputation: 7293
In HDF5 files, datasets have a datatype that is fixed when the dataset is created.
As hpaulj mentions, at the line
hf[key][...] = data*calibration_factor
the values on the right-hand side (which are correctly promoted to floating point values) are rounded to integers when storing them in the dataset.
If you want to store the normalized values, you must create a new dataset (with the same name, after removing the old one) that will have the appropriate datatype.
I propose the following:
with h5py.File(fileName,'r+') as hf:
keys=hf.keys()
data = hf[key][...]
del hf[key]
hf[key] = data*calibration_factor
which should store the result as floating point values. The code does not define key
, but I guess the example you gave is simplified with respect to the actual code.
Upvotes: 0
Reputation: 481
Try to np.asarray(data, dtype=float)
.
Based on yours and @hpaulj comment, your result is being truncated to 0 because data is int16
.
Upvotes: 1