Created
November 2, 2017 19:21
-
-
Save nicain/abad8a4421690790e9dd6b57a53b3805 to your computer and use it in GitHub Desktop.
Squeeze out all of the data from an nwb file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import h5py | |
| import os | |
| def pass_function(*args): return | |
| def visit(group, group_callback=pass_function, | |
| dataset_callback=pass_function, | |
| softlink_callback=pass_function, | |
| broken_link_callback=pass_function, path='/'): | |
| for key in group: | |
| try: | |
| val = group[key] | |
| link_type = group.get(key, getlink=True) | |
| if isinstance(link_type, h5py.SoftLink): | |
| softlink_callback(val.name, link_type.path) | |
| elif isinstance(val, h5py._hl.group.Group): | |
| group_callback(val) | |
| visit(val, group_callback=group_callback, | |
| dataset_callback=dataset_callback, | |
| softlink_callback=softlink_callback, | |
| broken_link_callback=broken_link_callback, path=os.path.join(path, key)) | |
| else: | |
| dataset_callback(val) | |
| except KeyError: | |
| broken_link_callback(os.path.join(path, key)) | |
| f_src = h5py.File('496908818.nwb', 'r') | |
| f_tgt = h5py.File('new.h5', 'w') | |
| def group_callback(group): | |
| assert isinstance(group, h5py._hl.group.Group) | |
| attr_dict = dict(group.attrs) | |
| new_group = f_tgt.create_group(group.name) | |
| for dkey, dval in attr_dict.items(): | |
| new_group.attrs[dkey] = dval | |
| visit(f_src, group_callback=group_callback) | |
| f_src.close() | |
| f_tgt.close() | |
| f_src = h5py.File('496908818.nwb', 'r') | |
| f_tgt = h5py.File('new.h5', 'r+') | |
| def dataset_callback(dataset): | |
| assert isinstance(dataset, h5py._hl.dataset.Dataset) | |
| curr_shape = dataset.shape | |
| dtype = dataset.dtype | |
| attr_dict = dict(dataset.attrs) | |
| if len(curr_shape) == 0: | |
| data = dataset | |
| elif len(curr_shape) == 1: | |
| data = np.array([1.], dtype=dtype) | |
| elif len(curr_shape) == 2: | |
| data = np.array([[1.]], dtype=dtype) | |
| elif len(curr_shape) == 3: | |
| data = np.array([[[1.]]], dtype=dtype) | |
| else: | |
| print len(curr_shape) | |
| raise NotImplementedError | |
| dset = f_tgt.create_dataset(dataset.name, data=data) | |
| for key, val in attr_dict.items(): | |
| dset.attrs[key] = val | |
| def softlink_callback(source, target): | |
| f_tgt[source] = h5py.SoftLink(target) | |
| def broken_link_callback(source_path): | |
| data = np.array([1.]) | |
| f_tgt.create_dataset(source_path, data=data) | |
| visit(f_src, dataset_callback=dataset_callback, | |
| softlink_callback=softlink_callback, | |
| broken_link_callback=broken_link_callback) | |
| f_src.close() | |
| f_tgt.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment