New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trouble setting value for vlen type on compound data #1921
Comments
It seems that we have these the Anyway, adjusting the code to match the type. import h5py
import numpy as np
f = h5py.File('test.h5','w')
table = f.create_dataset(
'packets',
shape=(1,),
chunks=True,
maxshape=(None,),
dtype=[
('timestamp', np.float64),
('data', h5py.vlen_dtype(np.uint8)),
],
)
table[0, 'timestamp'] = 1.5
table[0, 'data'] = np.frombuffer(b'test', dtype=[('data', np.uint8)])
f.close() I am now getting. $ python example.py
Traceback (most recent call last):
File "/home/anubis/git/usbviewer/example.py", line 19, in <module>
table[0, 'data'] = np.frombuffer(b'test', dtype=[('data', np.uint8)])
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "/usr/lib/python3.9/site-packages/h5py/_hl/dataset.py", line 946, in __setitem__
mspace = h5s.create_simple(selection.expand_shape(mshape))
File "/usr/lib/python3.9/site-packages/h5py/_hl/selections.py", line 269, in expand_shape
raise TypeError("Can't broadcast %s -> %s" % (source_shape, self.array_shape)) # array shape
TypeError: Can't broadcast (4,) -> () |
I tried patching |
What about if you put the array into an object array, something like this: a = np.array(None, dtype=object)
a[()] = np.frombuffer(b'test', dtype=np.uint8) vlen data in h5py is somewhere between awkward and a kludge. Compound datatypes are somewhat better, but still not the main thing the API is designed around. So it's not a great surprise that using a vlen inside a compound datatype causes problems. h5py, HDF5 and NumPy are all at their best with homogeneous arrays of numbers. |
That fails with
I also tried doing
I understand, this stack is mostly designed to hold datasets, not for variable data storage like what I am doing. I think I am gonna go with import struct
import h5py
import numpy as np
f = h5py.File('test.h5','w')
table = f.create_dataset('packets',
shape=(1,),
chunks=True,
maxshape=(None,),
dtype=h5py.vlen_dtype(np.uint8),
)
table[0] = np.frombuffer(struct.pack('f', 1.5) + b'test', dtype=np.uint8)
...
((timestamp,), data) = struct.unpack('f', table[0][:4]), table[0][4:].tobytes() And if I ever need more complex data, import pickle
import h5py
import numpy as np
f = h5py.File('test.h5','w')
table = f.create_dataset('packets',
shape=(1,),
chunks=True,
maxshape=(None,),
dtype=h5py.vlen_dtype(np.uint8),
)
table[0] = np.frombuffer(pickle.dumps((1.5, b'test')), dtype=np.uint8)
...
timestamp, data = pickle.loads(table[0]) It still would be very nice to see this issue fixed 😕 |
I'd be happy to see it get fixed - but I probably won't prioritise working on it myself. 😉 |
I would be happy to give it a try, but I would probably need some pointers. If/when you, or any of the other maintainers, have time, please let me know. |
Well, glancing at Lines 783 to 803 in a6c6590
Lines 808 to 813 in a6c6590
Lines 851 to 877 in a6c6590
|
I've bumped into a similar issue when trying to insert a compound object made up of vlen strings. I posted it on stackoverflow.com and received an answer that points in a different direction (which worked for me):
In other words, perhaps try changing this:
into this:
|
Reproducible
I do not understand what I am doing wrong, the array is the correct type. An using the API incorrectly and not seting the value? Admittedly, I am a bit confused by this API and just trying to figure out how to actually write the data 🙃. (also, is there any way to write the whole row as a tuple, eg.
table[0] = (1.5, array)
?)The text was updated successfully, but these errors were encountered: