/
hdf5.py
353 lines (281 loc) · 10.8 KB
/
hdf5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# -*- coding: utf-8 -*-
# Copyright (C) Duncan Macleod (2014-2019)
#
# This file is part of GWpy.
#
# GWpy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GWpy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GWpy. If not, see <http://www.gnu.org/licenses/>.
"""This module attaches the HDF5 input output methods to the DataQualityFlag.
While these methods are avialable as methods of the class itself,
this module attaches them to the unified I/O registry, making it a bit
cleaner.
"""
import os.path
import warnings
import numpy
from astropy.units import (UnitBase, Quantity)
from ...io import hdf5 as io_hdf5
from ...io.registry import (register_reader, register_writer,
register_identifier)
from ...time import LIGOTimeGPS
from .. import (DataQualityFlag, DataQualityDict, Segment, SegmentList)
__author__ = 'Duncan Macleod <duncan.macleod@ligo.org>'
# -- utilities ----------------------------------------------------------------
def find_flag_groups(h5group, strict=True):
"""Returns all HDF5 Groups under the given group that contain a flag
The check is just that the sub-group has a ``'name'`` attribute, so its
not fool-proof by any means.
Parameters
----------
h5group : `h5py.Group`
the parent group in which to search
strict : `bool`, optional, default: `True`
if `True` raise an exception for any sub-group that doesn't have a
name, otherwise just return all of those that do
Raises
------
KeyError
if a sub-group doesn't have a ``'name'`` attribtue and ``strict=True``
"""
names = []
for group in h5group:
try:
names.append(h5group[group].attrs['name'])
except KeyError:
if strict:
raise
continue
return names
# -- read ---------------------------------------------------------------------
def _is_flag_group(obj):
"""Returns `True` if `obj` is an `h5py.Group` that looks like
if contains a flag
"""
import h5py
return (
isinstance(obj, h5py.Group) and
isinstance(obj.get("active"), h5py.Dataset) and
isinstance(obj.get("known"), h5py.Dataset)
)
def _find_flag_groups(h5f):
"""Return all groups in `h5f` that look like flags
"""
flag_groups = []
def _find(name, obj):
if _is_flag_group(obj):
flag_groups.append(name)
h5f.visititems(_find)
return flag_groups
def _get_flag_group(h5f, path):
"""Determine the group to use in order to read a flag
"""
# if user chose the path, just use it
if path:
return h5f[path]
# if the user gave us the group directly, use it
if _is_flag_group(h5f):
return h5f
# otherwise try and find a single group that matches
try:
path, = _find_flag_groups(h5f)
except ValueError:
pass
else:
return h5f[path]
# if not exactly 1 valid group in the file, complain
raise ValueError(
"please pass a valid HDF5 Group, or specify the HDF5 Group "
"path via the ``path=`` keyword argument",
)
@io_hdf5.with_read_hdf5
def read_hdf5_flag(h5f, path=None, gpstype=LIGOTimeGPS):
"""Read a `DataQualityFlag` object from an HDF5 file or group.
"""
# extract correct group
dataset = _get_flag_group(h5f, path)
# read dataset
active = SegmentList.read(dataset['active'], format='hdf5',
gpstype=gpstype)
try:
known = SegmentList.read(dataset['known'], format='hdf5',
gpstype=gpstype)
except KeyError as first_keyerror:
try:
known = SegmentList.read(dataset['valid'], format='hdf5',
gpstype=gpstype)
except KeyError:
raise first_keyerror
return DataQualityFlag(active=active, known=known, **dict(dataset.attrs))
@io_hdf5.with_read_hdf5
def read_hdf5_segmentlist(h5f, path=None, gpstype=LIGOTimeGPS, **kwargs):
"""Read a `SegmentList` object from an HDF5 file or group.
"""
from astropy.table import Table
# find dataset
dataset = io_hdf5.find_dataset(h5f, path=path)
segtable = Table.read(dataset, format='hdf5', **kwargs)
out = SegmentList()
for row in segtable:
start = LIGOTimeGPS(int(row['start_time']), int(row['start_time_ns']))
end = LIGOTimeGPS(int(row['end_time']), int(row['end_time_ns']))
if gpstype is LIGOTimeGPS:
out.append(Segment(start, end))
else:
out.append(Segment(gpstype(start), gpstype(end)))
return out
@io_hdf5.with_read_hdf5
def read_hdf5_dict(h5f, names=None, path=None, on_missing='error', **kwargs):
"""Read a `DataQualityDict` from an HDF5 file
"""
if path:
h5f = h5f[path]
# allow alternative keyword argument name (FIXME)
if names is None:
names = kwargs.pop('flags', None)
# try and get list of names automatically
if names is None:
try:
names = find_flag_groups(h5f, strict=True)
except KeyError:
names = None
if not names:
raise ValueError("Failed to automatically parse available flag "
"names from HDF5, please give a list of names "
"to read via the ``names=`` keyword")
# read data
out = DataQualityDict()
for name in names:
try:
out[name] = read_hdf5_flag(h5f, name, **kwargs)
except KeyError as exc:
if on_missing == 'ignore':
pass
elif on_missing == 'warn':
warnings.warn(str(exc))
else:
raise ValueError('no H5Group found for flag '
'{0!r}'.format(name))
return out
# -- write --------------------------------------------------------------------
def write_hdf5_flag_group(flag, h5group, **kwargs):
"""Write a `DataQualityFlag` into the given HDF5 group
"""
# write segmentlists
flag.active.write(h5group, 'active', **kwargs)
kwargs['append'] = True
flag.known.write(h5group, 'known', **kwargs)
# store metadata
for attr in ['name', 'label', 'category', 'description', 'isgood',
'padding']:
value = getattr(flag, attr)
if value is None:
continue
elif isinstance(value, Quantity):
h5group.attrs[attr] = value.value
elif isinstance(value, UnitBase):
h5group.attrs[attr] = str(value)
else:
h5group.attrs[attr] = value
return h5group
@io_hdf5.with_write_hdf5
def write_hdf5_dict(flags, output, path=None, append=False, overwrite=False,
**kwargs):
"""Write this `DataQualityFlag` to a `h5py.Group`.
This allows writing to an HDF5-format file.
Parameters
----------
output : `str`, :class:`h5py.Group`
path to new output file, or open h5py `Group` to write to.
path : `str`
the HDF5 group path in which to write a new group for this flag
**kwargs
other keyword arguments passed to :meth:`h5py.Group.create_dataset`
Returns
-------
dqfgroup : :class:`h5py.Group`
HDF group containing these data. This group contains 'active'
and 'known' datasets, and metadata attrs.
See also
--------
astropy.io
for details on acceptable keyword arguments when writing a
:class:`~astropy.table.Table` to HDF5
"""
if path:
try:
parent = output[path]
except KeyError:
parent = output.create_group(path)
else:
parent = output
for name in flags:
# handle existing group
if name in parent:
if not (overwrite and append):
raise IOError("Group '%s' already exists, give ``append=True, "
"overwrite=True`` to overwrite it"
% os.path.join(parent.name, name))
del parent[name]
# create group
group = parent.create_group(name)
# write flag
write_hdf5_flag_group(flags[name], group, **kwargs)
def write_hdf5_flag(flag, output, path=None, **kwargs):
"""Write a `DataQualityFlag` to an HDF5 file/group
"""
# verify path (default to flag name)
if path is None:
path = flag.name
if path is None:
raise ValueError("Cannot determine target group name for flag in HDF5 "
"structure, please set `name` for each flag, or "
"specify the ``path`` keyword when writing")
return write_hdf5_dict({path: flag}, output, **kwargs)
def write_hdf5_segmentlist(seglist, output, path=None, **kwargs):
"""Write a `SegmentList` to an HDF5 file/group
Parameters
----------
seglist : :class:`~ligo.segments.segmentlist`
data to write
output : `str`, `h5py.File`, `h5py.Group`
filename or HDF5 object to write to
path : `str`
path to which to write inside the HDF5 file, relative to ``output``
**kwargs
other keyword arguments are passed to
:meth:`~astropy.table.Table.write`
"""
if path is None:
raise ValueError("Please specify the HDF5 path via the "
"``path=`` keyword argument")
from astropy.table import Table
# convert segmentlist to Table
data = numpy.zeros((len(seglist), 4), dtype=int)
for i, seg in enumerate(seglist):
start, end = map(LIGOTimeGPS, seg)
data[i, :] = (start.gpsSeconds, start.gpsNanoSeconds,
end.gpsSeconds, end.gpsNanoSeconds)
segtable = Table(data, names=['start_time', 'start_time_ns',
'end_time', 'end_time_ns'])
# write table to HDF5
return segtable.write(output, path=path, format='hdf5', **kwargs)
# -- register -----------------------------------------------------------------
register_reader('hdf5', SegmentList, read_hdf5_segmentlist)
register_writer('hdf5', SegmentList, write_hdf5_segmentlist)
register_identifier('hdf5', SegmentList, io_hdf5.identify_hdf5)
register_reader('hdf5', DataQualityFlag, read_hdf5_flag)
register_writer('hdf5', DataQualityFlag, write_hdf5_flag)
register_identifier('hdf5', DataQualityFlag, io_hdf5.identify_hdf5)
register_reader('hdf5', DataQualityDict, read_hdf5_dict)
register_writer('hdf5', DataQualityDict, write_hdf5_dict)
register_identifier('hdf5', DataQualityDict, io_hdf5.identify_hdf5)