chore: add h5py utils to agebd package
Deploy / deploy (push) Successful in 3s

This commit is contained in:
Benjamin Labrecque
2026-07-16 14:27:29 +02:00
parent 0ef919ad71
commit 970fd99297
3 changed files with 169 additions and 933 deletions
+1
View File
@@ -5,6 +5,7 @@ description = "SLS HLA Framework"
requires-python = "==3.10.*"
dependencies = [
"h5py>=3.16.0",
"typer>=0.23.2",
]
+113
View File
@@ -0,0 +1,113 @@
import os
from time import strftime
import numpy as np
from h5py import File as h5pyFile
from h5py._hl.dataset import Dataset
from h5py._hl.group import Group
def h5save(filename, datadict, timestamp=True):
"""save dataset to hdf5 format (for load see print(h5load.__doc__))
input:
- desired (path/)filename as string
- dictionary of data
return:
- saves data to "(path/)timestamp_filename.h5"
- complete (path/)filename is returned
usage-example:
datadict = {'dataset1' : {'x': array(...), 'y': array(...)},
'dataset2' : {'x': array(...), 'y': array(...), 'yerr': array(...)},
'parameter1' : 1.337,
'list1' : [1, 2, 'c']}
h5save(filename, True. datadict)
"""
def dict2h5(datadict, h5id):
for key, val in datadict.items():
if isinstance(key, bytes):
key = key.decode().replace("/", "|")
else:
key = key.replace("/", "|")
if isinstance(val, (list, tuple, str, bytes, int, float, np.ndarray)):
try:
h5id.create_dataset(key, data=val)
except:
print(
"Data of type {:} ({:}) is not yet supported, sorry for that!".format(
type(val), key
)
)
elif isinstance(val, (dict)):
hdf5_subid = h5id.create_group(key)
dict2h5(val, hdf5_subid)
else:
print(
"Data of type {:} ({:}) is not yet supported, sorry for that!".format(
type(val), key
)
)
raise Exception("Datatype is not yet supported, sorry for that!")
return
if timestamp:
path = "/".join(filename.split("/")[:-1] + [""])
filename = strftime("%Y%m%d%H%M%S") + "_" + filename.split("/")[-1]
filename = path + filename
if filename[-3:] != ".h5":
filename += ".h5"
hdf5_fid = h5pyFile(filename, "w")
metadata = {
"Date": strftime("%d.%m.%Y %H:%M:%S"),
"Uname": str(os.uname()),
"User": os.getlogin(),
}
hdf5_subid = hdf5_fid.create_group("metadata")
dict2h5(metadata, hdf5_subid)
dict2h5(datadict, hdf5_fid)
hdf5_fid.close()
return filename
def h5load(filename):
"""h5load(filename, verbose)
input:
- filename (as string) of h5save savedfile
- desired verbosity
return:
- dictionary of saved data
ALTERNATIVE:
if the dataset is too large for memory it is also possible to work with it on disk:
>>> import h5py
>>> data = h5py.File(filename, 'r')
"""
def h52dict(h5id, datadict):
for key, val in h5id.items():
if isinstance(val, (Dataset)):
datadict[key] = h5id[key][()]
elif isinstance(val, (Group)):
datadict[key] = {}
h52dict(h5id[key], datadict[key])
else:
print(
"Data of type {:} ({:}) is not yet supported, sorry for that!".format(
type(val), key
)
)
raise Exception("Datatype is not yet supported, sorry for that!")
return
if filename[-3:] != ".h5":
filename += ".h5"
data = {}
hdf5_fid = h5pyFile(filename, "r")
h52dict(hdf5_fid, data)
hdf5_fid.close()
return data
+55 -933
View File
File diff suppressed because it is too large Load Diff