pyloggrid.Libs.IOLib

A library to handle reading/writing files.

Module Contents

Functions

store_hdf5(→ Callable)

Decorator to handle fail-safe saving to hdf5.

load_hdf5(→ Callable)

Decorator to handle fail-safe loading from hdf5.

read_json_settings(→ dict)

reads the settings.json simulation file (fail-safe), returns the data

update_json_settings(→ None)

Save simulation parameters to settings.json file.

load_step(→ dict)

Load the data corresponding to a simulation step saved in output.h5, or older legacy formats.

save_step(→ None)

Save current step's fields to a .h5 file save.h5 (appending if existing), as a new dataset step_<step>

add_drawables(→ None)

Adds a drawables data dict to a DataExplorer result

store_hdf5(fun: Callable) Callable

Decorator to handle fail-safe saving to hdf5.

Attempts to save to <path>/output.h5, then to <path>/output_bk.h5.

Note

The decorated function’s path argument becomes a directory instead of a file.

Parameters:

fun – a non failsafe saving function that takes a path (hdf5 file) first argument

Example

@store_hdf5
def add_group(path: str, group_name: str) -> None:
    path: h5py.File  # from the decorator
    path.create_group(group_name)
load_hdf5(fun: Callable) Callable

Decorator to handle fail-safe loading from hdf5.

Attempts to load from <path>/output.h5, or to <path>/output_bk.h5 if the first one fails.

Note

The decorated function’s path argument becomes a directory instead of a file.

Warning

If the decorated path argument is a file instead of a directory, this will only load this file (non fail-safe).

Parameters:

fun – a non failsafe loading function that takes a path (hdf5 file) first argument

Example

@load_hdf5
def load_group(f: h5py.File, group_name: str) -> dict:
    group = f[group_name]
    return {name: group[name][:] for name in group}
read_json_settings(path: str) dict

reads the settings.json simulation file (fail-safe), returns the data

Parameters:

path – directory where to find the file

Returns:

the json data as a dict

Raises:

AssertionError – if the path doesn’t exist

update_json_settings(path: str, params: dict, update: bool = False) None

Save simulation parameters to settings.json file.

This is fail-safe though the use of settings_bk.json.

Parameters:
  • path – directory to save the settings to

  • params – parameters to save to file

  • update – if True, then existing parameters will be updated according to existing keys in params. Otherwise, settings are overwritten.

load_step(fields_names: List[str], path: str = None, step: int = None, filepath: str = None) dict

Load the data corresponding to a simulation step saved in output.h5, or older legacy formats.

Only the path and filepath arguments are necessary in the latest version.

Parameters:
  • filepath – path to save file. ([legacy] If omitted, the combination of path and step will be used instead.)

  • fields_names – [legacy] grid’s fields’ names.

  • path – data’s parent directory

  • step – [legacy] step number to load

Warning

The legacy support (ordered and unordered .npz) may be deprecated in the future, and only exists for loading old results.

Todo

Once we deprecate the legacy support (3.x ?), remove the additional obsolete arguments, and reflect in usages

Returns:

a dict containing the saved data, {fields: the grid's fields, t: step time, N_points: grid size, k_min: grid k min, elapsed_time: real elapsed time, k0: grid.k0}

save_step(path: str, step: int, grid: Grid, t: float, dt: float, time_tracker: TimeTracker) None

Save current step’s fields to a .h5 file save.h5 (appending if existing), as a new dataset step_<step>

Parameters:
  • path – folder to save to

  • grid – grid containing the fields to save

  • t – time of the step

  • dt – timestep used

  • step – # of the step

  • time_tracker – object that tracks elapsed time

add_drawables(path: str, drawables: dict) None

Adds a drawables data dict to a DataExplorer result

Parameters:
  • path – the path where the DataExplorer result is

  • drawables – the dict to save