HDF5 Wrapper

The hdf5-wrapper python package adds a wrapper to h5py that greatly simplifies reading/writing to/from hdf5-format files.

Usage

Once loaded, the contents of a file can be navigated in the same way as a native python dictionary.

from geos.hdf5_wrapper import hdf5_wrapper

data = hdf5_wrapper('data.hdf5')

test = data['test']
for k, v in data.items():
  print('key: %s, value: %s' % (k, str(v)))

If the user indicates that a file should be opened in write-mode (w) or read/write-mode (a), then the file can be created or modified. Note: for these changes to be written to the disk, the wrapper may need to be closed or deleted.

from geos.hdf5_wrapper import hdf5_wrapper
import numpy as np

data = hdf5_wrapper('data.hdf5', mode='w')
data['string'] = 'string'
data['integer'] = 123
data['array'] = np.random.randn(3, 4, 5)
data['child'] = {'float': 1.234}

Existing dictionaries can be placed on the current level:

existing_dict = {'some': 'value'}
data.insert(existing_dict)

And external hdf5 format files can be linked together:

for k in ['child_a', 'child_b']:
  data.link(k, '%s.hdf5' % (k))

API

class geos.hdf5_wrapper.wrapper.hdf5_wrapper(fname='', target=None, mode='r')[source]

A class for reading/writing hdf5 files, which behaves similar to a native dict

Initialize the hdf5_wrapper class

If the fname is supplied (either by a positional or keyword argument), the wrapper will open a hdf5 database from the filesystem. The recommended options for the mode flag include ‘r’ for read-only and ‘a’ for read/write access. If write mode is enabled, and the fname does not point to an existing file, a new database will be created.

If the target is supplied, then a new instance of the wrapper will be created using an existing database handle.

Parameters:
  • fname (str) – the filename of a new or existing hdf5 database

  • target (hdf5_wrapper) – the handle of an existing hdf5 dataset

  • mode (str) – the read/write behavior of the database (default=’r’)

close()[source]

Closes the database

copy()[source]

Copy the entire database into memory

Returns:

a dictionary holding the database contents

Return type:

dict

get_copy()[source]

Copy the entire database into memory

Returns:

a dictionary holding the database contents

Return type:

dict

insert(x)[source]

Insert the contents of the target object to the current location

Parameters:

x (dict, hdf5_wrapper) – the dictionary to insert

keys()[source]

Get a list of groups and arrays located at the current level

Returns:

a list of key names pointing to objects at the current level

Return type:

list

Link an external hdf5 file to this location in the database

Parameters:
  • k (str) – the name of the new link in the database

  • target (str) – the path to the external database

values()[source]

Get a list of values located on the current level