7
votes

I have HDF5 files that I would like to open using the Python module h5py (in Python 2.7).

This is easy when I have a file with groups and datasets:

import h5py as hdf

with hdf.File(relative_path_to_file, 'r') as f:
    my_data = f['a_group']['a_dataset'].value

However, in my current situation I do not have groups. There are only datasets. Unfortunately, I cannot access my data no matter what I try. None of the following work (all break with KeyErrors or ValueErrors):

my_data = f['a_dataset'].value #KeyError

my_data = f['/a_dataset'].value #KeyError

my_data = f['/']['a_dataset'].value #KeyError

my_data = f['']['a_dataset'].value #ValueError

my_data = f['.']['a_dataset'].value #KeyError

I can remake my files to have a group if there is no solution. It really seems like there should be a solution, though...

It seems like h5py is not seeing any keys:

f.keys()
[]
3
What does f.keys() contain? Print it out and see! - gspr
Sorry, I should have included that. I get an empty list: [ ] - Joshua Zollweg
Well then I doubt your HDF5 file actually contains anything. Have you tried checking with h5ls or similar? Maybe you can share the output of h5ls or show us the code that produced the HDF5 file? - gspr
I can see the data when I open the file with HDHView, so I know it's there. In HDFView I can select each dataset with syntax like [/a_dataset], but that doesn't work in python h5py. - Joshua Zollweg
And you're absolutely positive that you inspected f.keys within the with hdf… block? - gspr

3 Answers

4
votes

I found the issue, which I think is an issue h5py should address.

The issue (which I originally forgot to detail in the question, now edited) is that I open the hdf5 file with a relative file path. When I use and absolute file path, everything works perfectly.

Sadly, this is going to cause me problems down the road as my code is intended to run portably on different machines...

Thanks to gspr and jimmyb for their help :-)

2
votes

It worked fine when I was using a relative path.

To write:

fileName = "data/hdf5/topo.hdf5"

with h5py.File(fileName, 'w') as f:
    dset = f.create_dataset('topography', data = z, dtype = 'float32')

To read data:

with h5py.File(fileName, 'r') as f:
    my_data = f['.']['topography'].value
1
votes

I think that this should work:

f['.']['a_dataset']

And you might try to do:

dir(f['/']) 
dir(f['.'])