IO#

Note

The IO module associates the files generated/required by beorn with their corresponding parameters. As such it is particularly useful when dealing with many simulations with different parameters.

IO module that handles reading and writing of computed data.

class beorn.io.Handler(file_root: Path, input_tag: str = None, clear: bool = False, write_kwargs: dict = None)[source]#

Manage a persistence directory and delegate read/write calls.

The handler wraps the read/write methods implemented by classes deriving from beorn.structs.base_struct.BaseStruct, storing a common file_root directory and optional default write_kwargs that are passed to write operations.

clear()[source]#

Remove and recreate the handler’s persistence directory.

This deletes all files under file_root. Use with caution.

Returns:

None

load_file(parameters: Parameters, cls: type[BaseStructDerived], **kwargs) BaseStructDerived[source]#

Load an instance of cls from the persistence directory.

This convenience wrapper calls BaseStruct.read() on the provided class with the handler’s file_root and returns the instantiated object.

Parameters:
  • parameters (Parameters) – Parameters instance used to identify the file to load.

  • cls (type[BaseStructDerived]) – Class implementing read that returns an instance of BaseStruct.

  • **kwargs – Additional keyword arguments forwarded to``cls.read``.

Returns:

Loaded instance of cls.

Return type:

BaseStructDerived

save_logs(parameters: Parameters) None[source]#

Configure a file handler to save application logs.

The log file is named to match the output igm_cubes_*.h5 file, using the same input_tag and beorn_hash so logs and output sit side-by-side and are trivially associated.

Parameters:

parameters (Parameters) – Parameters object used to derive the log file name.

Returns:

None

write_file(parameters: Parameters, obj: BaseStructDerived, **kwargs) None[source]#

Write obj to the handler’s persistence directory.

This convenience wrapper calls the BaseStruct.write() implementation of obj with the configured file_root and merges any provided kwargs with the handler’s write_kwargs.

Parameters:
  • parameters (Parameters) – Parameters instance used to create or uniquely identify the object.

  • obj (BaseStructDerived) – Instance providing a write method.

  • **kwargs – Additional keyword arguments forwarded to obj.write. These are often used to distinguish file names or control writing behaviour.

Returns:

None