smif.data_layer.data_handle module

Provide an interface to data, parameters and results

A DataHandle is passed in to a Model at runtime, to provide transparent access to the relevant data and parameters for the current ModelRun and iteration. It gives read access to parameters and input data (at any computed or pre-computed timestep) and write access to output data (at the current timestep).

Summary

Data:

DataHandle Get/set model parameters and data
ResultsHandle Results access for decision modules

Reference

class smif.data_layer.data_handle.DataHandle(store: smif.data_layer.store.Store, modelrun_name, current_timestep, timesteps, model, decision_iteration=None)[source]

Bases: object

Get/set model parameters and data

derive_for(model)[source]

Derive a new DataHandle configured for the given Model

Parameters:model (Model) – Model which will use this DataHandle
current_timestep

Current timestep

previous_timestep

Previous timestep

base_timestep

Base timestep

timesteps

All timesteps (as tuple)

decision_iteration
get_state()[source]

The current state of the model

If the DataHandle instance has a timestep, then state is established from the state file.

Returns:A list of (intervention name, build_year) installed in the current timestep
Return type:list of tuple
Raises:ValueError – If self._current_timestep is None an error is raised.
get_current_interventions()[source]

Get the interventions that exist in the current state

Returns:A dict of intervention dicts with build_year attribute keyed by name
Return type:dict of dicts
get_data(input_name: str, timestep=None) → smif.data_layer.data_array.DataArray[source]

Get data required for model inputs

Parameters:
Returns:

Contains data annotated with the metadata and provides utility methods to access the data in different ways

Return type:

smif.data_layer.data_array.DataArray

Raises:

SmifDataError – If any data reading error occurs below this method, the error is handled and reraised within the context of the current call

get_base_timestep_data(input_name)[source]

Get data from the base timestep as required for model inputs

Parameters:input_name (str) –
Returns:
Return type:smif.data_layer.data_array.DataArray
get_previous_timestep_data(input_name)[source]

Get data from the previous timestep as required for model inputs

Parameters:input_name (str) –
Returns:
Return type:smif.data_layer.data_array.DataArray
get_parameter(parameter_name)[source]

Get the value for a parameter

Parameters:parameter_name (str) –
Returns:Contains data annotated with the metadata and provides utility methods to access the data in different ways
Return type:smif.data_layer.data_array.DataArray
get_parameters()[source]

Get all parameter values

Returns:parameters – Read-only view of parameters (like a read-only dict)
Return type:MappingProxyType
set_results(output_name, data)[source]

Set results values for model outputs

Parameters:
get_results(output_name, decision_iteration=None, timestep=None)[source]

Get results values for model outputs

Parameters:
  • output_name (str) – The name of an output for model_name
  • decision_iteration (int, default=None) –
  • timestep (int or RelativeTimestep, default=None) –
Returns:

Contains data annotated with the metadata and provides utility methods to access the data in different ways

Return type:

smif.data_layer.data_array.DataArray

Notes

Access to model results is only granted to models contained within self._model if self._model is a smif.model.model.CompositeModel

read_unit_definitions() → List[str][source]

Read unit definitions

Returns:
Return type:list[str]
read_coefficients(source_dim: str, destination_dim: str) → <MagicMock id='139764769571984'>[source]

Reads coefficients from the store

Coefficients are uniquely identified by their source/destination dimensions. This method and write_coefficients implement caching of conversion coefficients between dimensions.

Parameters:
  • source_dim (str) – Dimension name
  • destination_dim (str) – Dimension name
Returns:

Return type:

numpy.ndarray

write_coefficients(source_dim: str, destination_dim: str, data: <MagicMock id='139764769594672'>)[source]

Writes coefficients to the store

Coefficients are uniquely identified by their source/destination dimensions. This method and read_coefficients implement caching of conversion coefficients between dimensions.

Parameters:
  • source_dim (str) – Dimension name
  • destination_dim (str) – Dimension name
  • data (numpy.ndarray) –
class smif.data_layer.data_handle.ResultsHandle(store: smif.data_layer.store.Store, modelrun_name: str, sos_model, current_timestep: int, timesteps: Optional[List[int]] = None, decision_iteration: Optional[int] = None)[source]

Bases: object

Results access for decision modules

base_timestep
current_timestep
previous_timestep
decision_iteration
get_results(model_name: str, output_name: str, timestep: Union[int, smif.metadata.timestep.RelativeTimestep], decision_iteration: int) → smif.data_layer.data_array.DataArray[source]

Access model results

Parameters:
Returns:

Contains data annotated with the metadata and provides utility methods to access the data in different ways

Return type:

smif.data_layer.data_array.DataArray

get_state(timestep: int, decision_iteration: int) → List[Dict[KT, VT]][source]

Retrieve the pre-decision state of the model

If the DataHandle instance has a timestep, then state is established from the state file.

Returns:A list of {‘name’, ‘build_year’} dictionaries showing the history of decisions made up to this point
Return type:List[Dict]