def save_h5(f, group, key, namedtuple):
""" Save a namedtuple to an h5 file under a group and subgroup """
if VERSION_KEY in f.root:
version = int(getattr(f.root, VERSION_KEY))
if version != VERSION:
raise ValueError("Attempted to write analysis HDF5 version %d data to a version %d file" % (VERSION, version))
else:
ds = f.create_array(f.root, VERSION_KEY, np.int64(VERSION))
subgroup = f.create_group(group, '_'+key)
for field in namedtuple._fields:
arr = getattr(namedtuple, field)
if not hasattr(arr, 'dtype'):
raise ValueError('%s/%s must be a numpy array or scalar' % (group,key))
atom = tables.Atom.from_dtype(arr.dtype)
if len(arr.shape) > 0:
if arr.size > 0:
ds = f.create_carray(subgroup, field, atom, arr.shape)
else:
ds = f.create_earray(subgroup, field, atom, arr.shape)
ds[:] = arr
else:
ds = f.create_array(subgroup, field, arr)
评论列表
文章目录