H5Object

class specatalog.data_management.hdf5_reader.H5Object(h5node, writable=False, auto_flush=True)[source]

Bases: object

Represents a hdf5-file with its internal structure and all attributes/ datasets as a python object.

Overview of the methods

Using the set_attr()- / set_dataset()-method new data can be added to the object or existing data are updated. Using the delete_attr()- / delete_dataset()- method data can be deleted. After all changes are done they are wirtten to the hdf5-file using the sync()-method.

param h5node:

The object is build from the contents of the hdf5-file.

type h5node:

h5py.File

param writable:

The original hdf5-file is only changable if writable=True otherwise changes can be only done to the object but not to the file itself. The default is False.

type writable:

bool, optional

param auto_flush:

If set to True changes are directly written to the filesystem using the sync()-method. If set to False the “synced”-changes are only written to the file when it is closed manually using f.close(). The default is True.

type auto_flush:

bool, optional

The attributes of the object are set recursive as the groups of the hdf5-
file and the attributes and datasets as stored.

Example

>>> # data are stored in the hdf5-file (f) in /data/raw_data/spec
>>> dat = H5Object(f, writable=True)
>>> x = dat.raw_data.xaxis
>>> intensity = dat.raw_data.spec
>>> fit = -2*(x-12000)**2 + 25000
>>> dat.evaluations.set_dataset("fit1", fit)
>>> dat.sync()
delete_attr(key)[source]

Delete an attribute from the object and mark it to be deleted from the hdf5-file at the next call of the sync()-method.

Parameters:

key (str) – Name of the attribute.

Return type:

None.

delete_dataset(key)[source]

Delete a dataset from the object and mark it to be deleted from the hdf5-file at the next call of the sync()-method.

Parameters:

key (str) – Name of the dataset.

Return type:

None.

set_attr(key, value)[source]

Set a new attribute or update an exising attribute of the H5Object.

Parameters:
  • key (str) – Name of the attribute.

  • value (Any) – The value of the attribute. Should be a number or a string, for arrays use set_dataset.

Example

>>> dat = H5Object(f, writable=True)
>>> dat.set_attr("name", "dataset name") # set to main file
>>> dat.raw_data.set_attr("measurement_temperature", 80) # set to group
>>> dat.sync()
Return type:

None.

Parameters:
  • key (str)

  • value (Any)

set_dataset(key, value)[source]

Set a new dataset or update an exising dataset of the H5Object.

Parameters:
  • key (str) – Name of the dataset.

  • value (np.ndarray) – Array that contains the data. Should be an array of numbers.

Example

>>> dat = H5Object(f, writable=True)
>>> corrected_data = dat.raw_data.spec - 10
>>> dat.corrected_data.set_dataset("minus_10", corrected_data)
>>> dat.sync()
Return type:

None.

Parameters:
  • key (str)

  • value (ndarray)

sync()[source]

Write the changes that were done to the H5Object to the corresponding h5-file.

Raises:

RuntimeError – An error is raised if the object was generated with the option writable=False.

Return type:

None.

Parameters:
  • h5node (File)

  • writable (bool)

  • auto_flush (bool)