9

Is there any way I can recursively get all keys in h5 file using python library h5py? I tried using the code below

import h5py

h5_data = h5py.File(h5_file_location, 'r')
print(h5_data.keys())

but it only print the top level keys of the h5 file.

Vger
  • 115
  • 1
  • 4
  • The `group` class has `visit` and `visititems` methods that can be used to `walk` the group hierarchy. – hpaulj Jan 24 '20 at 17:29
  • This really should be part of the HDF5 API. If I save `f["a/b/c"] = data` I expect to find `"a/b/c"`in `f.keys()`. – TheMechanic Jan 05 '21 at 13:53

3 Answers3

9

Some of the keys returned by keys() on a Group may be Datasets some may be sub Groups. In order to find all keys you need to recurse the Groups. Here is a simple script to do that:

import h5py

def allkeys(obj):
    "Recursively find all keys in an h5py.Group."
    keys = (obj.name,)
    if isinstance(obj, h5py.Group):
        for key, value in obj.items():
            if isinstance(value, h5py.Group):
                keys = keys + allkeys(value)
            else:
                keys = keys + (value.name,)
    return keys

h5 = h5py.File('/dev/null', 'w')
h5.create_group('g1')
h5.create_group('g2')
h5.create_dataset('d1', (10,), 'i')
h5.create_dataset('d2', (10, 10,), 'f')
h5['g1'].create_group('g1')
h5['g1'].create_dataset('d1', (10,), 'i')
h5['g1'].create_dataset('d2', (10,), 'f')
h5['g1/g1'].attrs['a'] = 'b'
print(allkeys(h5))

Gives:

('/', '/d1', '/d2', '/g1', '/g1/d1', '/g1/d2', '/g1/g1', '/g2')
Jim Pivarski
  • 5,568
  • 2
  • 35
  • 47
spinkus
  • 7,694
  • 4
  • 38
  • 62
1

You may want to loop through the key to return their values. Below is a simple function that does that.

import h5py

h5_file_location = '../../..'
h5_data = h5py.File(h5_file_location, 'r')

def keys(f):
    return [key for key in f.keys()]
print(keys(h5_data))
Sami
  • 341
  • 7
  • 22
  • Thank you for your help but this still only return keys in the top level instead of keep going down to the h5 hierarchy that has additional keys – Vger Jan 24 '20 at 13:36
  • You need to use `.visititems()` method to recursively operate on each group or dataset. I reviewed my answers. Here are the 2 most helpful examples (IMHO) -- [my answer to 57067674](https://stackoverflow.com/a/57067674/10462884) and [my answer to 56177453](https://stackoverflow.com/a/56177453/10462884) – kcw78 Jan 24 '20 at 17:25
0

Based on the top answer I wrote this function to return all bottom-level keys only:

def allbottomkeys(obj):
bottomkeys=[]
def allkeys(obj):
    keys = (obj.name,)
    if isinstance(obj, h5py.Group):
        for key, value in obj.items():
            if isinstance(value, h5py.Group):
                keys = keys + allkeys(value)
            else:
                keys = keys + (value.name,)
                bottomkeys.append(value.name)
    return keys
allkeys(obj)
return bottomkeys