HDF5
HDF5 files as a tree of groups and datasets, readable and writable without h5py.
:func:write and :func:read work on :class:Group and :class:Dataset; :func:update_attrs changes the
attributes of an existing file. h5py is used when it is installed
(the hdf5 extra); without it, files are written by a small built-in writer and read with pyfive, a pure-Python
HDF5 reader. Either way the files are ordinary HDF5: h5py, GWpy and the HDF5 tools read what the built-in writer
writes, and the built-in path reads what they write.
The built-in writer covers what the gwmock packages store: datasets of booleans, integers, 32- and 64-bit floats, fixed-length strings and compound rows of those; attributes of numbers, arrays and strings, a string of any length (stored, as h5py stores it, as a variable-length string), a number or array up to 64 KiB; nested groups. It writes data uncompressed; files h5py wrote with gzip compression read either way.
Dataset
dataclass
¶
An HDF5 dataset: an array and its attributes. A str is stored as a fixed-length UTF-8 string.
data is None for a dataset read with read(..., data=False).
Source code in src/gwmock_data/hdf5/__init__.py
34 35 36 37 38 39 40 41 42 | |
Group
dataclass
¶
An HDF5 group: named members (groups or datasets) and attributes. The file itself is the root group.
Source code in src/gwmock_data/hdf5/__init__.py
45 46 47 48 49 50 | |
read(path, *, backend='auto', members=None, data=True)
¶
Read the HDF5 file at path into a :class:Group.
Datasets are read into NumPy arrays (a scalar as a 0-d array). A scalar string, as a dataset or an
attribute, is returned as str; an array of fixed-length strings stays a NumPy array of bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Input file. |
required |
backend
|
Backend
|
|
'auto'
|
members
|
Iterable[str] | None
|
Names of the top-level members to read; the others are not read at all, which matters for a
large file of which only a few columns are wanted. A name the file lacks is absent from the
result. |
None
|
data
|
bool
|
|
True
|
Raises:
| Type | Description |
|---|---|
ModuleNotFoundError
|
If |
TypeError
|
If |
ValueError
|
If a name in |
Source code in src/gwmock_data/hdf5/__init__.py
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 | |
update_attrs(path, attrs, *, backend='auto')
¶
Set attributes on members of an existing HDF5 file; the samples and every other attribute are kept.
With h5py the file is changed in place. Without it the whole file is read, the attributes merged, and the result written beside the file and moved over it once complete, so a failed update leaves the file as it was; the rewritten file keeps the original's permissions, and a symlink is written through to its target. The built-in writer does not compress and stores a string dataset fixed-length, so a file h5py wrote comes back uncompressed when it is updated without h5py, holding the same values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The file to change. |
required |
attrs
|
Mapping[str, Mapping[str, Any]]
|
Attributes to set, keyed by the path of the member that carries them: |
required |
backend
|
Backend
|
|
'auto'
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If there is no file at |
KeyError
|
If a member path names nothing in the file; the file is not changed. |
ModuleNotFoundError
|
If |
TypeError
|
If the built-in writer cannot write a value the file holds, such as a half-precision float; the file is not changed. |
ValueError
|
If a member path has an empty, |
Source code in src/gwmock_data/hdf5/__init__.py
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 | |
write(root, path, *, backend='auto', compression=None)
¶
Write root to path as an HDF5 file, replacing it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
Group
|
The file's contents. |
required |
path
|
str | Path
|
Output file. |
required |
backend
|
Backend
|
|
'auto'
|
compression
|
str | None
|
A compression filter for h5py to apply to non-scalar datasets, such as |
None
|
Raises:
| Type | Description |
|---|---|
ModuleNotFoundError
|
If |
TypeError
|
If a dtype cannot be written. |
ValueError
|
If a member name cannot name an HDF5 member, or a string attribute contains a NUL character. |
Source code in src/gwmock_data/hdf5/__init__.py
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 | |