As @hpaulj
commented, you need to determine which objects are Groups and which are Datasets. And with Matlab datasets, you need to determine which are arrays and which are objects (objects point to other HDF5 objects in your file). Until you're comfortable with HDF5 and h5py, the easiest way to do this is with the HDFView utility from the HDF Group.
When you're ready to code, you can do it pragmatically with isinstance()
referencing h5py
objects.
To test if variable node
is a Group use:
if isinstance(node, h5py.Group):
To test if variable node
is a Dataset use:
if isinstance(node, h5py.Dataset):
To test if variable node
is an Object Dataset use:
if (node.dtype == 'object') :
You can use visititems(-function-)
to loop recursively down an object tree, calling -function-
with each object.
Here's a very simple example to demonstrate. Put your filename in the place of foo.hdf5
and run. Warning: This creates a lot of output if you have a lot of groups and datasets. Once you understand your file schema, you should be able to access the datasets. If you find object datasets, read my linked answer to dereference them.
import numpy as np
import h5py
def visitor_func(name, node):
if isinstance(node, h5py.Group):
print(node.name, 'is a Group')
elif isinstance(node, h5py.Dataset):
if (node.dtype == 'object') :
print (node.name, 'is an object Dataset')
else:
print(node.name, 'is a Dataset')
else:
print(node.name, 'is an unknown type')
#########
print ('testing hdf5 matlab file')
h5f = h5py.File('foo.hdf5')
h5f.visititems(visitor_func)
h5f.close()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…