Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read HDF5 files that have only datasets (no groups) using h5py?

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()
[]
like image 277
Joshua Zollweg Avatar asked Feb 16 '15 18:02

Joshua Zollweg


People also ask

How do I read an HDF5 file?

Reading HDF5 filesTo open and read data we use the same File method in read mode, r. To see what data is in this file, we can call the keys() method on the file object. We can then grab each dataset we created above using the get method, specifying the name. This returns a HDF5 dataset object.

Is HDF5 human readable?

HDF in python For my work, I had to study the data stored in HDF5 files. These files are not human readable, and so I had to write some codes in Python to access the data.


2 Answers

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 :-)

like image 111
Joshua Zollweg Avatar answered Sep 20 '22 14:09

Joshua Zollweg


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
like image 31
Jasmine Avatar answered Sep 17 '22 14:09

Jasmine