Cube h5 File Handling

class pycube.fileop.h5.CubeH5(h5file_url, **kwargs)

Class for Cube h5 handling

__init__(h5file_url, **kwargs)
Initialize object
  • If the h5 file exists: open and check if it is a Cube file of the correct version

  • If not: create it and set source and version

Parameters:

h5file_url (Path | str) – url of the h5 file

export_dataset(label, target_path, filetype='csv', explicit='', x_interval=[])

Export a dataset to a file of type ‘filetype’.

The name of the output file is composed of the input file name without suffix, the dataset’s group and label seperated by ‘_’, with suffix according to the file type.

Parameters:
  • label (str) – dataset label

  • target_path (Path | str) – path to the output folder

  • filetype (str) – type of export file (currently ‘csv’, ‘pkl’, ‘h5’)

  • explicit (str) – force explicit h5 output by setting to ‘compound’

  • x_interval (List) – export data only in the given x interval (e.g. time interval), defaults to empty list for no x interval limit. A third optional value defines the start value for x in the returned interval.

get_attr(label='', attr='')

Get the value of attribute “attr” from dataset ‘label’ of current group, or from the current group itself if label=''

Parameters:
  • label (str) – name of the dataset

  • attr (str) – name of the attribute

Return type:

Optional[Any]

Returns:

searched dataset or None if not found

get_dataset(label=None, name=None, log_info=False)

Get dataset object with given label or name, where label or name may include the path to the dataset starting from the current group. Return None if not existing.

if label:

Search the first dataset with given label in current group and return it. DSHplus display names for signals as labels must be passed without suffix ‘.Value’ or ‘.Wert’ and must not contain any dot(s)

Parameters:
  • label (Optional[str]) – dataset label (display name), e.g. ‘p1’ (signal p1) or ‘Node1.Q’ (Flow at Node1)

  • log_info (bool)

  • name (Optional[str]) – dataset name

Return type:

Dataset | None

Returns:

searched dataset or None if not found

get_dataset_keys(group='/')

Get a recursive list of datasets of group

Parameters:

group (str) – group name from which the list starts, defaults to ‘/’ (If no group name is specified, the list contains all datasets of the file together with the name of their respective subgroup)

Return type:

list

Returns:

list of datasets

get_dataset_labels(group='/')

Get a recursive list of dataset labels of group

Parameters:

group (str) – group name from which the list starts, defaults to ‘/’ (If no group name is specified, the list contains all datasets of the file together with the name of their respective subgroup)

Return type:

list

Returns:

list of labels

get_dict(dictionary=None, subgroupname=None)

Gets the dict values of the keys in dictionary “dictionary” from current group or subgroup “subgroupname”.

If dictionary is not given, all items in the group are returned as resulting dict.

Parameters:
  • dictionary (Optional[dict]) – dict to get

  • subgroupname (Optional[str]) – name of the subgroup

Return type:

dict

Returns:

updated dictionary if called with input dictionary, new dictionary with all attributes found if input dictionary = None

get_group_keys(group='/')

Get a recursive list groups in group

Parameters:

group (str) – group name from which the list starts, defaults to ‘/’ (If no group name is specified, the list contains all datasets of the file together with the name of their respective subgroup)

Return type:

list

Returns:

list of groups

get_x(label)

Get the x values from dataset “name” from current group

Parameters:

label (str) – label of the dataset

Return type:

ndarray | None

Returns:

array of x values of the dataset, None if not found

get_xy(label, x_interval=[])

Get the x and y values from dataset “label” from current group.

Parameters:
  • label (str) – label of the dataset

  • x_interval (List) – return xy in the desired x interval only. A third value defines the start value for x in the returned interval (optional)

Return type:

(ndarray, ndarray)

Returns:

arrays of x and y values of the dataset or values between the x limits given in x_interval if given

get_y(label)

Get the y values from dataset “label” from current group

Parameters:

label (str) – label of the dataset

Return type:

ndarray | None

Returns:

array of y values of the dataset, None if not found

is_dataset(label=None, name=None)

Verify if dataset exists.

if label:

Search the first dataset with given label in current group and return it. DSHplus display names for signals as labels must be passed without suffix ‘.Value’ or ‘.Wert’ and must not contain any dot(s)

Parameters:
  • label (Optional[str]) – dataset label (display name), e.g. ‘p1’ (signal p1) or ‘Node1.Q’ (Flow at Node1)

  • name (Optional[str]) – dataset name

Return type:

bool

Returns:

True if found

set_group(groupname)

Set group or creates it if not existing

Parameters:

groupname (str) – name of the group

Return type:

Group | None

Returns:

new current group, None on error

write_dict(dictionary)

Write the dictionary “dictionary” to the current group

Parameters:

dictionary (dict) – dict to be written

write_xy(label, mode='x', y_values=array([], dtype=float64), x_values=array([], dtype=float64), **kwargs)

Write a dataset or dataset attributes (overwrites if existing)

Parameters:
  • label (str) – label of the dataset (display name for DSHplus results)

  • mode (str) – ‘w’ overwrite existing datasets, ‘w-’ or ‘x’ fail if the dataset exists

  • y_values (ndarray) – y values array

  • x_values (ndarray) – x values array (implicit x values can also be defined by kwargs)

Keyword Arguments:
  • name (str): dataset name if name is not passed, label will be used as name

  • comment (str): any comment text

  • unit (str): unit of y values

  • x_0, x_end (float): first and last value of an equally spaced x-axis definition

  • x_unit (str): unit of x values

  • explicit (bool): force explicit x data even for equally spaced x values

pycube.fileop.h5.h5_scan(with_attr=False)

Scan all h5 files in the results folder of the current task and write their structure into txt files.

Parameters:

with_attr (bool) – include attributes if True

pycube.fileop.h5.compare_h5(url1, url2, rtol=1e-08)

Compare h5 files and return a dict with differences, empty if equal.

Following checks are done:
  • equal names of objects (groups and datasets)

  • object types correspond

  • object attribute names are equal

  • attribute values are equal

  • dataset data values are equal

Parameters:
  • url1 – url of 1st h5 file

  • url2 (Path | str) – url of 2nd h5 file

  • rtol (float) – relative tolerance for float comparison

Return type:

dict

Returns:

dict with differences, empty if equal