Miscellaneous utilities

allel.util.hdf5_cache(filepath=None, parent=None, group=None, names=None, typed=False, hashed_key=True, **h5dcreate_kwargs)[source]

HDF5 cache decorator.

Parameters:

filepath : string, optional

Path to HDF5 file. If None a temporary file name will be used.

parent : string, optional

Path to group within HDF5 file to use as parent. If None the root group will be used.

group : string, optional

Path to group within HDF5 file, relative to parent, to use as container for cached data. If None the name of the wrapped function will be used.

names : sequence of strings, optional

Name(s) of dataset(s). If None, default names will be ‘f00’, ‘f01’, etc.

typed : bool, optional

If True, arguments of different types will be cached separately. For example, f(3.0) and f(3) will be treated as distinct calls with distinct results.

hashed_key : bool, optional

If False, the key will not be hashed, which makes for readable cache group names, but may cause problems if key contains ‘/’ characters.

Returns:

decorator : function

Examples

Without any arguments, will cache using a temporary HDF5 file:

>>> import allel
>>> @allel.util.hdf5_cache()
... def foo(n):
...     print('executing foo')
...     return np.arange(n)
...
>>> foo(3)
executing foo
array([0, 1, 2])
>>> foo(3)
array([0, 1, 2])
>>> foo.cache_filepath 
'/tmp/tmp_jwtwgjz'

Supports multiple return values, including scalars, e.g.:

>>> @allel.util.hdf5_cache()
... def bar(n):
...     print('executing bar')
...     a = np.arange(n)
...     return a, a**2, n**2
...
>>> bar(3)
executing bar
(array([0, 1, 2]), array([0, 1, 4]), 9)
>>> bar(3)
(array([0, 1, 2]), array([0, 1, 4]), 9)