I am trying to read a subset of the columns in a dataset (compound type). However, when I try to do so using a list, I get an error. See below
What I really want is to be able to select using a list of field names. Any way I can accomplish this? Looping through the list and requesting a field at a time is undesirable since it will reread the file every time (I assume, since it takes almost the same amount of time to read 1 field as it does 2 fields).
when I do this, I get an error. I also tried ds[*names], but that doesn't work either. See below:
In [2]: import numpy
In [3]: f = h5py.File('myfile.hdf5')
In [4]: my_dtype = numpy.dtype([('field1', 'i'), ('field2', 'f')])
In [5]: ds = f.create_dataset('ds', (3,3), dtype=my_dtype)
In [6]: ds['field1','field2']
Out[6]:
array([[(0, 0.0), (0, 0.0), (0, 0.0)],
[(0, 0.0), (0, 0.0), (0, 0.0)],
[(0, 0.0), (0, 0.0), (0, 0.0)]],
dtype=[('field1', '<i4'), ('field2', '<f4')])
In [7]: names = ['field1', 'field2']
In [8]: ds[names]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-8-e6921c938272> in <module>()
----> 1 ds[names]
/usr/lib/python2.7/dist-packages/h5py/_hl/dataset.pyc in __getitem__(self, args)
312
313 # Perform the dataspace selection.
--> 314 selection = sel.select(self.shape, args, dsid=self.id) 315
316 if selection.nselect == 0:
/usr/lib/python2.7/dist-packages/h5py/_hl/selections.pyc in select(shape, args, dsid)
88 except Exception:
89 sel = FancySelection(shape)
---> 90 sel[args]
91 return sel
92
/usr/lib/python2.7/dist-packages/h5py/_hl/selections.pyc in __getitem__(self, args)
449 self._id.select_none()
450 for idx, vector in enumerate(argvector):
--> 451 start, count, step, scalar = _handle_simple(self.shape, vector)
452 self._id.select_hyperslab(start, count, step, op=h5s.SELECT_OR)
453
/usr/lib/python2.7/dist-packages/h5py/_hl/selections.pyc in _handle_simple(shape, args)
514 else:
515 try:
--> 516 x,y,z = _translate_int(int(arg), length)
517 s = True
518 except TypeError:
ValueError: invalid literal for int() with base 10: 'field1'