Skip to content

Instantly share code, notes, and snippets.

@delijati
Created December 7, 2021 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delijati/06abacc60b501a3b2a5b87b03ee23930 to your computer and use it in GitHub Desktop.
Save delijati/06abacc60b501a3b2a5b87b03ee23930 to your computer and use it in GitHub Desktop.
Safe metadata and multiple datasets
import numpy as np
import h5py
SIZE = 500000
with h5py.File("test.hdf", "w") as f:
f["first"] = np.random.random(SIZE)
f["first"].attrs["git id"] = "yay this is a string"
f["second"] = np.random.random(SIZE)
f["second"].attrs["git id"] = "yay this is a string"
f["third"] = np.random.random(SIZE)
f["third"].attrs["wo"] = [1, 2, 3, 4, 5]
f["third"].attrs["bla"] = "terrow"
with h5py.File("testc.hdf", "w") as f:
f.create_dataset('first', data=np.random.random(SIZE), compression='gzip', compression_opts=9)
f["first"].attrs["git id"] = "yay this is a string"
f.create_dataset('second', data=np.random.random(SIZE), compression='gzip', compression_opts=9)
f["second"].attrs["git id"] = "yay this is a string"
f.create_dataset('third', data=np.random.random(SIZE), compression='gzip', compression_opts=9)
f["third"].attrs["wo"] = [1, 2, 3, 4, 5]
f["third"].attrs["bla"] = "terrow"
for name in ("test.hdf", "testc.hdf"):
with h5py.File("test.hdf", "r") as f:
print("Keys: %s" % f.keys())
for k in f.keys():
print("Data: %s" % f[k])
print("Attr: %s" % f[k].attrs.keys())
for ak in f[k].attrs.keys():
print("Attr val: %s" % f[k].attrs[ak])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment