111

I am trying to read data from hdf5 file in Python. I can read the hdf5 file using h5py, but I cannot figure out how to access data within the file.

My code

import h5py    
import numpy as np    
f1 = h5py.File(file_name,'r+')    

This works and the file is read. But how can I access data inside the file object f1?

Martin Thoma
  • 108,021
  • 142
  • 552
  • 849
Sameer Damir
  • 1,234
  • 2
  • 10
  • 8
  • 3
    If the file holds a Keras model, you will probably want to [load it with Keras](https://stackoverflow.com/questions/35074549/how-to-load-a-model-from-an-hdf5-file-in-keras) instead. – Josiah Yoder Jun 20 '18 at 19:12
  • 1
    Is an `hdf5` file different from an `hdf` file? I have `hdf`s (they are several bands of images), but I cannot figure out how to open them. – mikey Aug 11 '20 at 14:42
  • df = numpy.read_hdf(fileName.hdf5) -> this stores the data into a numpy dataframe that you can use. – Tanmoy Oct 08 '21 at 13:00

10 Answers10

172

Read HDF5

import h5py
filename = "file.hdf5"

with h5py.File(filename, "r") as f:
    # List all groups
    print("Keys: %s" % f.keys())
    a_group_key = list(f.keys())[0]

    # Get the data
    data = list(f[a_group_key])

Write HDF5

import h5py

# Create random data
import numpy as np
data_matrix = np.random.uniform(-1, 1, size=(10, 3))

# Write data to HDF5
with h5py.File("file.hdf5", "w") as data_file:
    data_file.create_dataset("group_name", data=data_matrix)

See h5py docs for more information.

Alternatives

For your application, the following might be important:

  • Support by other programming languages
  • Reading / writing performance
  • Compactness (file size)

See also: Comparison of data serialization formats

In case you are rather looking for a way to make configuration files, you might want to read my short article Configuration files in Python

Martin Thoma
  • 108,021
  • 142
  • 552
  • 849
  • 2
    To get the data in the HDF5 datasets as a numpy array, you can do `f[key].value` – erickrf May 04 '17 at 20:49
  • 2
    As of `h5py` version 2.1: "The property `Dataset.value`, which dates back to h5py 1.0, is deprecated and will be removed in a later release. This property dumps the entire dataset into a NumPy array. Code using `.value` should be updated to use NumPy indexing, using `mydataset[...]` or `mydataset[()]` as appropriate." – honey_badger Feb 19 '20 at 14:31
  • I am using Julia's hdf5 library and the read operation is much faster (would include it as answer, but OP asked for python). The same hdf5 file read takes forever in h5py, however it is very manageable in Julia, worth learning to program in Julia just for this one problem. The only issue I had with Julia was that it didn't handle null terminated strings correctly, which for me was a bit of a roadblock. – demongolem Mar 20 '20 at 10:53
  • Commenting on the answer itself, the list operation in the read version causes python to freeze. If I just do f[a_group_key] it works at the proper speed. – demongolem Mar 20 '20 at 10:59
  • @demongolem: you should not use the listing of all keys of you already know which one you want to use. I have done it here to have a self-contained example that requires least amount of work to get something running. – Martin Thoma Mar 20 '20 at 11:08
31

Reading the file

import h5py

f = h5py.File(file_name, mode)

Studying the structure of the file by printing what HDF5 groups are present

for key in f.keys():
    print(key) #Names of the groups in HDF5 file.

Extracting the data

#Get the HDF5 group
group = f[key]

#Checkout what keys are inside that group.
for key in group.keys():
    print(key)

data = group[some_key_inside_the_group][()]
#Do whatever you want with data

#After you are done
f.close()
Daksh
  • 976
  • 10
  • 22
22

you can use Pandas.

import pandas as pd
pd.read_hdf(filename,key)
Danny
  • 391
  • 1
  • 6
  • 8
    You should not rely on the Pandas implementation unless you are storing dataframes. read_hdf relies on the HDF file to be in a certain structure; also there is no pd.write_hdf, so you could only use it one-way. See [this post](https://stackoverflow.com/questions/33641246/pandas-cant-read-hdf5-file-created-with-h5py/33644128#33644128). – Max Jan 26 '19 at 21:20
  • 4
    Pandas does have a writing function. See [pd.DataFrame.to_hdf](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_hdf.html) – Eric Taw Mar 17 '19 at 18:58
9

Here's a simple function I just wrote which reads a .hdf5 file generated by the save_weights function in keras and returns a dict with layer names and weights:

def read_hdf5(path):

    weights = {}

    keys = []
    with h5py.File(path, 'r') as f: # open file
        f.visit(keys.append) # append all keys to list
        for key in keys:
            if ':' in key: # contains data if ':' in key
                print(f[key].name)
                weights[f[key].name] = f[key].value
    return weights

https://gist.github.com/Attila94/fb917e03b04035f3737cc8860d9e9f9b.

Haven't tested it thoroughly but does the job for me.

Attila
  • 191
  • 2
  • 7
5

Use below code to data read and convert into numpy array

import h5py
f1 = h5py.File('data_1.h5', 'r')
list(f1.keys())
X1 = f1['x']
y1=f1['y']
df1= np.array(X1.value)
dfy1= np.array(y1.value)
print (df1.shape)
print (dfy1.shape)
Martin Thoma
  • 108,021
  • 142
  • 552
  • 849
ashish bansal
  • 291
  • 4
  • 3
5

To read the content of .hdf5 file as an array, you can do something as follow

> import numpy as np 
> myarray = np.fromfile('file.hdf5', dtype=float)
> print(myarray)
Raza
  • 149
  • 1
  • 7
2
from keras.models import load_model 

h= load_model('FILE_NAME.h5')
taras
  • 5,922
  • 10
  • 36
  • 48
Judice
  • 21
  • 2
1

If you have named datasets in the hdf file then you can use the following code to read and convert these datasets in numpy arrays:

import h5py
file = h5py.File('filename.h5', 'r')

xdata = file.get('xdata')
xdata= np.array(xdata)

If your file is in a different directory you can add the path in front of'filename.h5'.

Machzx
  • 29
  • 6
0

What you need to do is create a dataset. If you take a look at the quickstart guide, it shows you that you need to use the file object in order to create a dataset. So, f.create_dataset and then you can read the data. This is explained in the docs.

Games Brainiac
  • 75,856
  • 32
  • 131
  • 189
0

Using bits of answers from this question and the latest doc, I was able to extract my numerical arrays using

import h5py
with h5py.File(filename, 'r') as h5f:
    h5x = h5f[list(h5f.keys())[0]]['x'][()]

Where 'x' is simply the X coordinate in my case.

Patol75
  • 3,824
  • 1
  • 13
  • 27