Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all datasets in h5py file?

Tags:

h5py

I have a h5py file storing numpy arrays, but I got Object doesn't exist error when trying to open it with the dataset name I remember, so is there a way I can list what datasets the file has?

   with h5py.File('result.h5','r') as hf:         #How can I list all dataset I have saved in hf? 
like image 295
matchifang Avatar asked Jul 03 '17 10:07

matchifang


People also ask

How do I explore HDF5 files?

Open a HDF5/H5 file in HDFView hdf5 file on your computer. Open this file in HDFView. If you click on the name of the HDF5 file in the left hand window of HDFView, you can view metadata for the file. This will be located in the bottom window of the application.

What is the difference between HDF5 group and HDF5 dataset?

Within one HDF5 file, you can store a similar set of data organized in the same way that you might organize files and folders on your computer. However in a HDF5 file, what we call "directories" or "folders" on our computers, are called groups and what we call files on our computer are called datasets .

How do I open an hd5 file in Python?

To use HDF5, numpy needs to be imported. One important feature is that it can attach metaset to every data in the file thus provides powerful searching and accessing. Let's get started with installing HDF5 to the computer. As HDF5 works on numpy, we would need numpy installed in our machine too.


2 Answers

You have to use the keys method. This will give you a List of unicode strings of your dataset and group names. For example:

Datasetnames=hf.keys() 

Another gui based method would be to use HDFView. https://support.hdfgroup.org/products/java/release/download.html

like image 126
max9111 Avatar answered Sep 21 '22 10:09

max9111


The other answers just tell you how to make a list of the keys under the root group, which may refer to other groups or datasets.

If you want something closer to h5dump but in python, you can do something like that:

import h5py  def descend_obj(obj,sep='\t'):     """     Iterate through groups in a HDF5 file and prints the groups and datasets names and datasets attributes     """     if type(obj) in [h5py._hl.group.Group,h5py._hl.files.File]:         for key in obj.keys():             print sep,'-',key,':',obj[key]             descend_obj(obj[key],sep=sep+'\t')     elif type(obj)==h5py._hl.dataset.Dataset:         for key in obj.attrs.keys():             print sep+'\t','-',key,':',obj.attrs[key]  def h5dump(path,group='/'):     """     print HDF5 file metadata      group: you can give a specific group, defaults to the root group     """     with h5py.File(path,'r') as f:          descend_obj(f[group]) 
like image 45
Seb Avatar answered Sep 21 '22 10:09

Seb