sequence.input_reader module#

Read input files.

This module contains utilities for reading Sequence input data.

class sequence.input_reader.TimeVaryingConfig(times: Iterable[float], dicts: Iterable[dict])[source]#

Bases: object

A configuration dictionary that is able to change with time.

__call__(time: float) dict[source]#

Return the configuration at a given time.

Parameters:

time (float) – The time of the configuration.

Returns:

The configuration at the time.

Return type:

dict

__init__(times: Iterable[float], dicts: Iterable[dict])[source]#

Create a time-varying configuration.

Parameters:
  • times (iterable of int) – Time keys for each dictionary.

  • dicts (iterable of dict) – Dictionary to use at each time.

Examples

>>> from sequence.input_reader import TimeVaryingConfig
>>> params = TimeVaryingConfig([0, 1], [dict(foo=0, bar=1), dict(foo=1)])
>>> sorted(params.items())
[(('bar',), 1), (('foo',), 0)]
>>> params.update(1)
{'foo': 1}
>>> sorted(params.items())
[(('bar',), 1), (('foo',), 1)]
as_dict() dict[source]#

Represent the current configuration as a dict.

diff(start: float, stop: float) dict[source]#

Return the difference between two different configurations.

Parameters:
  • start (float) – The time of the first configuration.

  • stop (float) – The time of the second configuration.

Returns:

The key/values that have changed between the two configurations.

Return type:

dict

dump(fmt: str = 'toml') str[source]#

Write the current configurations to a string.

Parameters:

fmt (str, optional) – Format to dump the configuration to.

Returns:

The configuration in the requested format.

Return type:

str

classmethod from_file(name: PathLike, fmt: str | None = None) TimeVaryingConfig[source]#

Load a configuration from a file.

Parameters:
  • name (path-like) – The name of the configuration file to read.

  • fmt (str, optional) – The format of the configuration file.

classmethod from_files(names: Iterable[str | PathLike[str]], times: Iterable[float] | None = None) TimeVaryingConfig[source]#

Load a configuration from a set of files.

Parameters:
  • names (iterable of path-like) – Names of files to read.

  • times (iterable of float) – Times associated with each file.

static get_loader(fmt: str) Callable[[TextIO], list[tuple[float, dict]]][source]#

Get a configuration loader for a given format.

Parameters:

fmt (str) – The configuration format (e.g. ‘toml’, ‘yaml’).

Returns:

A loader function for the format.

Return type:

func

Raises:

ValueError – If a loader for the format can’t be found.

static get_supported_formats() list[str][source]#

Return a list of supported configuration formats.

Returns:

Names of the supported formats.

Return type:

list of str

items() Iterable[tuple[float, dict]][source]#

Return the items of the current configuration.

static load_toml(stream: TextIO) list[tuple[float, dict]][source]#

Load a configuration in toml-format.

Parameters:

stream (file-like) – The toml-formatted configuration.

Returns:

The configurations and their associated times.

Return type:

list of (float, dict)

static load_yaml(stream: TextIO) list[tuple[float, dict]][source]#

Load a configuration in yaml-format.

Parameters:

stream (file-like) – The yaml-formatted configuration.

Returns:

The configurations and their associated times.

Return type:

list of (float, dict)

update(inc: int) dict[source]#

Update the configurations by a time step.

Parameters:

inc (float) – The amount of time to increment the configuration by.

Returns:

The difference between the current configuration and the new configuration.

Return type:

dict

sequence.input_reader.load_config(stream: TextIO | str | PathLike[str], fmt: str | None = None) Iterable[tuple[float, dict]] | dict[source]#

Load model configuration from a file-like object.

Parameters:
  • stream (file-like) – A test stream that contains the configuration.

  • fmt (str, optional) – The format of the configuration (e.g. ‘toml’, ‘yaml’).

Returns:

The, possibly time-varying, configuration.

Return type:

TimeVaryingConfig