Skip to content

Instantly share code, notes, and snippets.

@loganbvh
Last active November 9, 2019 23:34
Show Gist options
  • Save loganbvh/6bae62c0808f5f9f176c570de8f15267 to your computer and use it in GitHub Desktop.
Save loganbvh/6bae62c0808f5f9f176c570de8f15267 to your computer and use it in GitHub Desktop.
import h5py
import numpy as np
def set_h5_attrs(grp, data):
"""Sets attributes of h5py group or File `grp` according to dict `data`.
Args:
grp (h5py group or File): Group or file you would like to update.
data (dict): Dict of data with which to update `grp`.
"""
for name, value in data.items():
name = str(name)
if isinstance(value, dict):
subgrp = grp.require_group(name)
set_h5_attrs(subgrp, value)
else:
if isinstance(value, (list, tuple, np.ndarray)) and len(value):
if isinstance(value[0], str):
grp.attrs[name] = [str(val) for val in value]
else:
# create or overwrite dataset
# this only works if value has the same shape as original dataset
array = np.array(value)
ds = grp.require_dataset(name, shape=array.shape, dtype=array.dtype, exact=True)
ds[...] = array
# we could instead do the following to overwrite with data of a different shape:
# ds = grp.require_dataset(name, shape=array.shape, dtype=array.dtype, exact=True)
# del ds
# grp.create_dataset(name, data=array)
else:
grp.attrs[name] = value
def group_to_dict(group):
"""Recursively load the contents of an h5py group into a dict.
Args:
group (h5py group): Group from which you want to load all data.
Returns:
target (dict): Dict with contents of group loaded into it.
"""
target = {}
for key, value in group.items():
target[key] = {}
if hasattr(value, 'attrs') and len(value.attrs):
target[key].update(group_to_dict(value.attrs))
if hasattr(value, 'keys'):
target[key].update(group_to_dict(value))
elif isinstance(value, h5py.Dataset):
target[key] = np.array(value)
else:
target[key] = value
return target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment