Skip to content

Instantly share code, notes, and snippets.

@sjdv1982
Created February 5, 2023 19:54
Show Gist options
  • Save sjdv1982/1ab245d30a39481deee067e85166ba8f to your computer and use it in GitHub Desktop.
Save sjdv1982/1ab245d30a39481deee067e85166ba8f to your computer and use it in GitHub Desktop.
dump ActivePaper data directory as .npy
# Author: Sjoerd de Vries, CNRS
# license: public domain
import sys
import os
import h5py
import numpy as np
active_paper_file = sys.argv[1]
target_directory = sys.argv[2]
ap = h5py.File(active_paper_file)
apdata = ap["data"]
assert isinstance(apdata, h5py.Group)
datadir = os.path.join(target_directory, "data")
os.makedirs(datadir, exist_ok=True)
def dump(datagroup, targetdir):
for childname, child in datagroup.items():
child_target = os.path.join(targetdir, childname)
if isinstance(child, h5py.Group):
os.makedirs(child_target, exist_ok=True)
dump(child, child_target)
else:
print("dump", child_target)
if child.ndim == 0 or not child.dtype.shape:
child2 = child
else: #structured data, bugged in h5py
child2 = np.empty(child.shape, child.dtype)
for n in range(len(child)):
child2[n] = child[n]
np.save(child_target, child2)
dump(apdata, datadir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment