Skip to content
Snippets Groups Projects
Commit cace8e80 authored by Jameson Rollins's avatar Jameson Rollins
Browse files

io: serialize 'ifo' objects in HDF5 IO

convert IFO Structs to yaml for HDF5 storage
parent 1b2462cd
No related branches found
No related tags found
No related merge requests found
import h5py
import datetime
from . import Struct
SCHEMA = 'GWINC noise budget'
SCHEMA_VERSION = 1
......@@ -19,7 +21,13 @@ def _write_trace_recursive(grp, traces):
def save_hdf5(path, freq, traces, **kwargs):
"""Save GWINC traces dict to HDF5 file.
"""Save GWINC budget data to an HDF5 file.
The `freq` argument should be the frequency array, and `traces`
should be the traces (recursive) dictionary. Keyword arguments
are stored in the HDF5 top level 'attrs' key-value store. If an
'ifo' keyword arg is supplied, it is assumed to be a Struct and
will be serialized to YAML for storage.
See HDF5_SCHEMA.
......@@ -30,7 +38,10 @@ def save_hdf5(path, freq, traces, **kwargs):
# FIXME: add budget code hash or something
f.attrs['date'] = datetime.datetime.now().isoformat()
for key, val in kwargs.items():
f.attrs[key] = val
if key == 'ifo':
f.attrs['ifo'] = val.to_yaml()
else:
f.attrs[key] = val
f.create_dataset('Freq', data=freq)
tgrp = f.create_group('traces')
_write_trace_recursive(tgrp, traces)
......@@ -47,14 +58,19 @@ def _read_trace_recursive(element):
def load_hdf5(path):
"""Load traces from HDF5 file.
"""Load GWINC budget data from an HDF5 file.
Returns a recursive traces dictionary. See HDF5_SCHEMA.
Returns (freq, traces, attrs). An 'ifo' attr will be
de-serialized from YAML into a Struct object.
See HDF5_SCHEMA.
"""
with h5py.File(path, 'r') as f:
# FIXME: check SCHEMA name/version
freq = f['Freq'][:]
traces = _read_trace_recursive(f['/traces'])
attrs = dict(f.attrs.items())
attrs = dict(f.attrs)
if 'ifo' in attrs:
attrs['ifo'] = Struct.from_yaml(attrs['ifo'])
return freq, traces, attrs
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment