Skip to content

Dictionary methods of HDF5File use absolute keys

In the new dictionary methods included by @amohammadi some month ago via !18 (merged), the HDF5File.keys function is used with its default parameters: https://gitlab.idiap.ch/bob/bob.io.base/blob/d82af062412ea637d203556628c34fa65a389e28/bob/io/base/__init__.py#L42 which define that the keys are returned absolute: https://www.idiap.ch/software/bob/docs/bob/bob.io.base/stable/py_api.html#bob.io.base.HDF5File.keys

Related, the sub_groups function by default returns absolute paths and iterates recursively through the sub_groups: https://www.idiap.ch/software/bob/docs/bob/bob.io.base/stable/py_api.html#bob.io.base.HDF5File.sub_groups

I never liked these default values. When building hierarchical HDF5 files, (i.e., writing all sub-classes of a class into HDF5) I always use relative paths, e.g.: https://gitlab.idiap.ch/bob/bob.bio.video/blob/master/bob/bio/video/utils/FrameContainer.py#L39 (this is an example of sub_groups, but the keys function has a similar issue).

As another example, I often write dictionaries to HDF5. I hope the following code snippet will show the issue better:

import bob.io.base
hdf5 = bob.io.base.HDF5File("x.hdf5", 'w')
d = dict(key1=1, key2=2)
for k,v in d.items():
  hdf5[k]=v

e = {k:v for k,v in hdf5.items()}
print (d.keys(), e.keys())

which will print ['key2', 'key1'] ['/key1', '/key2']. Hence, writing and reading work different in this case.

So, my questions are:

  1. Shall we use relative keys when we iterate over the file? If not, this function is usually useless.
  2. Shall we change the default values in sub_groups and keys to match the expected behavior, i.e., relative=True and recursive=False?

ping @bob