Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discovering keys using h5py in python3

Tags:

In python2.7, I can analyze an hdf5 files keys use

$ python >>> import h5py >>> f = h5py.File('example.h5', 'r') >>> f.keys() [u'some_key'] 

However, in python3.4, I get something different:

$ python3 -q >>> import h5py >>> f = h5py.File('example.h5', 'r') >>> f.keys() KeysViewWithLock(<HDF5 file "example.h5" (mode r)>) 

What is KeysViewWithLock, and how can I examine my HDF5 keys in Python3?

like image 209
user14717 Avatar asked Jun 24 '15 21:06

user14717


People also ask

How do I view HDF5 files in Python?

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.

What is key in HDF5?

Groups are the container mechanism by which HDF5 files are organized. From a Python perspective, they operate somewhat like dictionaries. In this case the “keys” are the names of group members, and the “values” are the members themselves ( Group and Dataset ) objects.

What is the use of H5 File in Python?

HDF5 file stands for Hierarchical Data Format 5. It is an open-source file which comes in handy to store large amount of data. As the name suggests, it stores data in a hierarchical structure within a single file.


1 Answers

From h5py's website (http://docs.h5py.org/en/latest/high/group.html#dict-interface-and-links):

When using h5py from Python 3, the keys(), values() and items() methods will return view-like objects instead of lists. These objects support containership testing and iteration, but can’t be sliced like lists.

This explains why we can't view them. The simplest answer is to convert them to a list:

>>> list(for.keys()) 

Unfortunately, I run things in iPython, and it uses the command 'l'. That means that approach won't work.

In order to actually view them, we need to take advantage of containership testing and iteration. Containership testing means we'd have to already know the keys, so that's out. Fortunately, it's simple to use iteration:

>>> [key for key in f.keys()] ['mins', 'rects_x', 'rects_y'] 

I've created a simple function that does this automatically:

def keys(f):     return [key for key in f.keys()] 

Then you get:

>>> keys(f) ['mins', 'rects_x', 'rects_y'] 
like image 177
Joel Avatar answered Sep 23 '22 04:09

Joel