result.netcdf¶
The netCDF files of the results: compressed netCDF4, the attributes as JSON.
A result is written as netCDF4 (HDF5) through h5netcdf, the one file format
which xarray, netCDF-C, R, MATLAB and Julia read with the dimensions,
coordinates and attributes of the result. The attributes of a result are
nested, they are written as one JSON attribute, NETCDF_ATTRS.
The numeric variables are compressed (#412): the results of simulations
compress well, the HCTZ model with its 191 selections over 5000 points from
1551 MB to 136 MB. zstd (the default) is the HDF5 filter 32015 of
hdf5plugin with the shuffle filter of HDF5, which writes about twice as long
as no compression and reads about as fast; netCDF-C 4.9 reads it, a python
reader outside of sbmlsim imports hdf5plugin first. gzip is the filter
every HDF5 reads, at about five times the time of writing. A variable is
stored in chunks of whole timecourses (its last dimension) of about
CHUNK_BYTES, which compress better than the chunks h5netcdf would choose.
The variables of one shape are written as one array and not as one variable each (#607): HDF5 and h5netcdf cost about 2.5 ms to write and 3.4 ms to read per variable of a file, whatever its size, so the 190 selections of the HCTZ model over 200 points took 474 ms to write and 559 ms to read as 190 variables against 62 ms and 30 ms as one array of them, and a result of the large model (3301 variables) would take about 8 s and 11 s. The cost is the variable and not the compression, which the same measurement without it shows (366 ms and 422 ms against 15 ms and 10 ms), and the file is the same size either way, since a chunk of the array holds what a chunk of a variable held.
So write_netcdf gathers the numeric variables of equal dimensions and dtype,
from STACK_MIN of them on, into one array over a dimension of their names
(NETCDF_STACKS records what it gathered, read_netcdf hands the variables
back in the order they were written, with their attributes) and leaves a
result of a few variables as it is, which keeps a small file readable as
itself. An array over a dimension of names is ordinary netCDF, so every
reader still reads a gathered file; what it does not read is one variable per
selection by name.
Arrow, Zarr and npz were measured as well (#412). Arrow IPC writes and reads fastest, but it is a table: the dimensions, coordinates and attributes would be an encoding of our own which no other tool reads as such. Zarr is N-dimensional and native to xarray, but a directory writes a file per chunk, a zip file duplicate entries, and it adds its dependencies. npz writes slowest.
write_netcdf
¶
Write a dataset as netCDF4, its attributes as JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
the dataset, whose attributes must be JSON serializable; numpy scalars and arrays are written as numbers and lists. |
required |
path
|
str | Path
|
the file. |
required |
compression
|
Compression
|
the compression of the numeric variables, see the module. |
'zstd'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
for an unknown compression. |
read_netcdf
¶
Read a dataset of write_netcdf into memory, with its attributes.
Raises:
| Type | Description |
|---|---|
ValueError
|
if the file has no attributes of a result of sbmlsim. |
chunk_shape
¶
Get the chunks of a variable: whole rows of its last dimension.
A chunk holds about CHUNK_BYTES: the whole last dimension (a timecourse)
and as many of the dimensions before it as fit, from the last of them on.
A last dimension larger than a chunk is cut.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, ...]
|
the shape of the variable, every size at least one. |
required |
itemsize
|
int
|
the bytes of a value. |
required |
json_attributes
¶
Get attributes as the JSON types which read_netcdf reads them as.
Attributes in memory and attributes read from a file compare equal in this form, e.g. a tuple and a numpy array are lists.