smif.data_layer package¶
Data access modules for loading system-of-systems model configuration
- class smif.data_layer.DataArray(spec: ~smif.metadata.spec.Spec, data: <MagicMock id='135493239504048'>)[source]¶
Bases:
objectDataArray provides access to input/parameter/results data, with conversions to common python data libraries (for example: numpy, pandas, xarray).
- spec¶
- Type:
- data¶
- Type:
- as_df() <MagicMock id='135493245860944'>[source]¶
Access DataArray as a
pandas.DataFrame
- as_ndarray() <MagicMock id='135493240406144'>[source]¶
Access as a
numpy.ndarray
- as_xarray()[source]¶
Access DataArray as a
xarray.DataArray
- property coords¶
Coordinate labels for each dimension.
- property description¶
A human-friendly description
- property dims¶
Names for each dimension
- classmethod from_df(spec, dataframe)[source]¶
Create a DataArray from a
pandas.DataFrame
- classmethod from_xarray(spec, xr_data_array)[source]¶
Create a DataArray from a
xarray.DataArray
- property name¶
The name of the data that this spec describes.
- property shape¶
The shape of the data array
- property unit¶
The unit for all data points.
- class smif.data_layer.DataHandle(store: Store, modelrun_name, current_timestep, timesteps, model, decision_iteration=None)[source]¶
Bases:
objectGet/set model parameters and data
- property base_timestep¶
Base timestep
- property current_timestep¶
Current timestep
- property decision_iteration¶
- derive_for(model)[source]¶
Derive a new DataHandle configured for the given Model
- Parameters:
model (Model) – Model which will use this DataHandle
- get_base_timestep_data(input_name)[source]¶
Get data from the base timestep as required for model inputs
- Parameters:
input_name (str)
- Return type:
- 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) DataArray[source]¶
Get data required for model inputs
- Parameters:
input_name (str)
timestep (RelativeTimestep or int, optional) – defaults to RelativeTimestep.CURRENT
- Returns:
Contains data annotated with the metadata and provides utility methods to access the data in different ways
- Return type:
- 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_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:
- get_parameters()[source]¶
Get all parameter values
- Returns:
parameters – Read-only view of parameters (like a read-only dict)
- Return type:
MappingProxyType
- get_previous_timestep_data(input_name)[source]¶
Get data from the previous timestep as required for model inputs
- Parameters:
input_name (str)
- Return type:
- 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:
Notes
Access to model results is only granted to models contained within self._model if self._model is a smif.model.model.CompositeModel
- 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:
- Raises:
ValueError – If self._current_timestep is None an error is raised.
- property previous_timestep¶
Previous timestep
- read_coefficients(source_dim: str, destination_dim: str) <MagicMock id='135493267455824'>[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:
- Return type:
- set_results(output_name, data)[source]¶
Set results values for model outputs
- Parameters:
output_name (str)
data (numpy.ndarray)
- property timesteps¶
All timesteps (as tuple)
- write_coefficients(source_dim: str, destination_dim: str, data: <MagicMock id='135493236816448'>)[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.Results(store: Store | dict)[source]¶
Bases:
objectCommon interface to access results from model runs.
- Parameters:
store (Store or dict) – pre-created Store object or dictionary of the form {‘interface’: <interface>, ‘dir’: <dir>} where <interface> is either ‘local_csv’ or ‘local_parquet’, and <dir> is the model base directory
- available_results(model_run_name)[source]¶
Return the results available for a given model run.
- Parameters:
model_run_name (str the requested model run)
- Return type:
A nested dictionary data structure of the results available for the given model run
- list_outputs(sector_model_name: str)[source]¶
Return a list of model run names.
- Parameters:
sector_model_name (str the requested sector model)
- Return type:
List of outputs for the given sector model
- list_scenario_outputs(scenario_name: str)[source]¶
Return a list of outputs of a given scenario.
- Parameters:
scenario_name (str the requested scenario)
- Return type:
List of outputs for the requested scenario
- list_scenarios(model_run_name: str)[source]¶
Return a dictionary of scenarios for given model run.
- Parameters:
model_run_name (str the requested model run)
- Return type:
Dictionary of (scenario name, variant) for the given model run.
- list_sector_models(model_run_name: str)[source]¶
Return a list of sector models for given model run.
- Parameters:
model_run_name (str the requested model run)
- Return type:
List of sector models for the given model run
- read_results(model_run_names: list, model_names: list, output_names: list, timesteps: list = None, decisions: list = None, time_decision_tuples: list = None)[source]¶
Return results from the store as a formatted pandas data frame. There are a number of ways of requesting specific timesteps/decisions. You can specify either:
- a list of (timestep, decision) tuples
in which case data for all of those tuples matching the available results will be returned
- or:
- a list of timesteps
in which case data for all of those timesteps (and any decision iterations) matching the available results will be returned
- or:
- a list of decision iterations
in which case data for all of those decision iterations (and any timesteps) matching the available results will be returned
- or:
- a list of timesteps and a list of decision iterations
in which case data for the Cartesian product of those timesteps and those decision iterations matching the available results will be returned
- or:
- nothing
in which case all available results will be returned
- Parameters:
model_run_names (list) – the requested model run names
model_names (list) – the requested sector model names (exactly one required)
output_names (list) – the requested output names (output specs must all match)
timesteps (list) – the requested timesteps
decisions (list) – the requested decision iterations
time_decision_tuples (list) – a list of requested (timestep, decision) tuples
- Raises:
SmifDataNotFoundError – If data cannot be found in the store when try to read from the store
SmifDataMismatchError – Data presented to read, write and update methods is in the incorrect format or of wrong dimensions to that expected
SmifDataReadError – When unable to read data e.g. unable to handle file type or connect to database
- Return type:
- read_scenario_data(scenario_name: str, variant_name: str, variable_name: str, timesteps: list) <MagicMock id='135493236657712'>[source]¶
Return scenario variant data from the store as a formatted pandas data frame.
- Parameters:
- Raises:
SmifDataNotFoundError – If data cannot be found in the store when try to read from the store
SmifDataMismatchError – Data presented to read, write and update methods is in the incorrect format or of wrong dimensions to that expected
SmifDataReadError – When unable to read data e.g. unable to handle file type or connect to database
- Return type:
- class smif.data_layer.Store(config_store, metadata_store: MetadataStore, data_store: DataStore, model_base_folder='.')[source]¶
Bases:
objectCommon interface to data store, composed of config, metadata and data store implementations.
- Parameters:
config_store (ConfigStore)
metadata_store (MetadataStore)
data_store (DataStore)
- canonical_available_results(model_run_name)[source]¶
List the results that are available from a model run, collapsing all decision iterations.
This is the unique items from calling available_results, with all decision iterations set to 0.
This method is used to determine whether a model run is complete, given that it is impossible to know how many decision iterations to expect: we simply check that each expected timestep has been completed.
- Parameters:
model_run_name (str)
- Return type:
set Set of tuples representing available results
- canonical_expected_results(model_run_name)[source]¶
List the results that are expected from a model run, collapsing all decision iterations.
For a complete model run, this would coincide with the unique list returned from available_results, where all decision iterations are set to 0.
This method is used to determine whether a model run is complete, given that it is impossible to know how many decision iterations to expect: we simply check that each expected timestep has been completed.
- Parameters:
model_run_name (str)
- Return type:
set Set of tuples representing expected results
- canonical_missing_results(model_run_name)[source]¶
List the results that are missing from a model run, collapsing all decision iterations.
For a complete model run, this is what is left after removing canonical_available_results from canonical_expected_results.
- Parameters:
model_run_name (str)
- Return type:
set Set of tuples representing missing results
- clear_results(model_run_name)[source]¶
Clear all results from a single model run
- Parameters:
model_run_name (str)
- delete_model_run(model_run_name)[source]¶
Delete a system-of-system model run
- Parameters:
model_run_name (str)
- delete_results(model_run_name, model_name, output_name, timestep=None, decision_iteration=None)[source]¶
Delete results for a single timestep/iteration of a model output in a model run
- delete_scenario(scenario_name)[source]¶
Delete scenario from project configuration
- Parameters:
scenario_name (str)
- delete_scenario_variant(scenario_name, variant_name)[source]¶
Delete scenario from project configuration
- delete_sos_model(sos_model_name)[source]¶
Delete a system-of-system model
- Parameters:
sos_model_name (str)
- static filter_complete_available_results(available_results, expected_model_outputs)[source]¶
Filter available results from a model run to include only complete timestep/decision iteration combinations
- get_result_darray(model_run_name, model_name, output_name, timesteps=None, decision_iterations=None, time_decision_tuples=None)[source]¶
Return data for multiple timesteps and decision iterations for a given output from a given sector model in a specific model run.
- You can specify either:
- a list of (timestep, decision) tuples
in which case data for all of those tuples matching the available results will be returned
- or:
- a list of timesteps
in which case data for all of those timesteps (and any decision iterations) matching the available results will be returned
- or:
- a list of decision iterations
in which case data for all of those decision iterations (and any timesteps) matching the available results will be returned
- or:
- a list of timesteps and a list of decision iterations
in which case data for the Cartesian product of those timesteps and those decision iterations matching the available results will be returned
- or:
- nothing
in which case all available results will be returned
Then, for each tuple, the data array from the corresponding read_results call is stacked, and together with the new spec this information is returned as a new DataArray.
- Parameters:
- Return type:
DataArray with expanded spec and the data requested
- get_results(model_run_names: list, model_name: str, output_names: list, timesteps: list = None, decisions: list = None, time_decision_tuples: list = None)[source]¶
Return data for multiple timesteps and decision iterations for a given output from a given sector model for multiple model runs.
- Parameters:
- Returns:
Nested dictionary of DataArray objects, keyed on model run name and output name. Returned DataArrays include one extra (timestep, decision_iteration) dimension.
- Return type:
- prepare_model_runs(model_run_name, scenario_name, first_var, last_var)[source]¶
Write multiple model run config files corresponding to multiple scenario variants of {scenario_name}, based on template {model_run_name}
Write batchfile containing each of the generated model runs
- prepare_scenario(scenario_name, list_of_variants)[source]¶
Modify {scenario_name} config file to include multiple scenario variants.
- prepare_warm_start(model_run_name)[source]¶
Copy the results from the previous model_run if available
The method allows a previous unsuccessful model_run to ‘warm start’ a new model run from a later timestep. Model results are recovered from the timestep that the previous model_run was run until, and the new model run runs from the returned timestep
- Parameters:
model_run_name (str)
- Return type:
int The timestep to which the data store was recovered
Notes
Called from smif.controller.execute
- read_all_initial_conditions(model_run_name) List[Dict][source]¶
A list of all historical interventions
- read_coefficients(source_dim: str, destination_dim: str) <MagicMock id='135493238206208'>[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:
- Return type:
Notes
To be called from
Adaptorimplementations.
- read_dimension(dimension_name, skip_coords=False)[source]¶
Return dimension
- Parameters:
dimension_name (str)
- Returns:
A dimension definition (including elements)
- Return type:
Coords
- read_initial_conditions(model_name) List[Dict][source]¶
Read historical interventions for model_name
- read_model_parameter_default(model_name, parameter_name, assert_exists=False)[source]¶
Read default data for a sector model parameter
- read_narrative_variant_data(sos_model_name, narrative_name, variant_name, parameter_name, timestep=None, assert_exists=False)[source]¶
Read narrative data file
- read_results(model_run_name: str, model_name: str, output_spec: Spec, timestep: int | None = None, decision_iteration: int | None = None) DataArray[source]¶
Return results of a model_name in model_run_name for a given output_name
- read_scenario(scenario_name, skip_coords=False)[source]¶
Read a scenario
- Parameters:
scenario_name (str)
- Return type:
- read_scenario_variant_data(scenario_name: str, variant_name: str, variable: str, timestep: int | None = None, timesteps: List[int] | None = None, assert_exists: bool = False) DataArray | bool[source]¶
Read scenario data file
- read_state(model_run_name, timestep, decision_iteration=None) List[Dict][source]¶
Read list of (name, build_year) for a given model_run, timestep, decision
- read_strategy_interventions(strategy, assert_exists=False)[source]¶
Read interventions as defined in a model run strategy
- update_dimension(dimension_name, dimension)[source]¶
Update dimension
- Parameters:
dimension_name (str)
dimension (Coords)
- update_scenario(scenario_name, scenario)[source]¶
Update scenario
- Parameters:
scenario_name (str)
scenario (ScenarioModel)
- update_scenario_variant(scenario_name, variant_name, variant)[source]¶
Update scenario to project configuration
- write_coefficients(source_dim: str, destination_dim: str, data: <MagicMock id='135493237137168'>)[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)
Notes
To be called from
Adaptorimplementations.
- write_dimension(dimension)[source]¶
Write dimension to project configuration
- Parameters:
dimension (Coords)
- write_initial_conditions(model_name, initial_conditions)[source]¶
Write historical interventions for a model
- Parameters:
list[dict] – A list of historical interventions, with keys ‘name’ and ‘build_year’
- write_interventions(model_name, interventions)[source]¶
Write interventions data for a model
- Parameters:
dict[str – A dict of intervention dictionaries containing intervention attributes keyed by intervention name
dict] – A dict of intervention dictionaries containing intervention attributes keyed by intervention name
- write_model_parameter_default(model_name, parameter_name, data)[source]¶
Write default data for a sector model parameter
- write_model_run(model_run)[source]¶
Write system-of-system model run
- Parameters:
model_run (ModelRun)
- write_narrative_variant_data(sos_model_name, narrative_name, variant_name, data)[source]¶
Read narrative data file
- write_results(data_array, model_run_name, model_name, timestep=None, decision_iteration=None)[source]¶
Write results of a model_name in model_run_name for a given output_name
- write_scenario(scenario)[source]¶
Write scenario
- Parameters:
scenario (ScenarioModel)
- write_state(state, model_run_name, timestep, decision_iteration=None)[source]¶
State is a list of decisions with name and build_year.
State is output from the DecisionManager
Subpackages¶
- smif.data_layer.file package
CSVDataStoreFileMetadataStoreParquetDataStoreYamlConfigStoreYamlConfigStore.delete_model()YamlConfigStore.delete_model_run()YamlConfigStore.delete_scenario()YamlConfigStore.delete_scenario_variant()YamlConfigStore.delete_sos_model()YamlConfigStore.read_model()YamlConfigStore.read_model_run()YamlConfigStore.read_model_runs()YamlConfigStore.read_models()YamlConfigStore.read_narrative()YamlConfigStore.read_project_config()YamlConfigStore.read_scenario()YamlConfigStore.read_scenario_variant()YamlConfigStore.read_scenario_variants()YamlConfigStore.read_scenarios()YamlConfigStore.read_sos_model()YamlConfigStore.read_sos_models()YamlConfigStore.read_strategies()YamlConfigStore.update_model()YamlConfigStore.update_model_run()YamlConfigStore.update_scenario()YamlConfigStore.update_scenario_variant()YamlConfigStore.update_sos_model()YamlConfigStore.write_model()YamlConfigStore.write_model_run()YamlConfigStore.write_scenario()YamlConfigStore.write_scenario_variant()YamlConfigStore.write_sos_model()YamlConfigStore.write_strategies()
- Submodules
- smif.data_layer.file.file_config_store module
YamlConfigStoreYamlConfigStore.delete_model()YamlConfigStore.delete_model_run()YamlConfigStore.delete_scenario()YamlConfigStore.delete_scenario_variant()YamlConfigStore.delete_sos_model()YamlConfigStore.read_model()YamlConfigStore.read_model_run()YamlConfigStore.read_model_runs()YamlConfigStore.read_models()YamlConfigStore.read_narrative()YamlConfigStore.read_project_config()YamlConfigStore.read_scenario()YamlConfigStore.read_scenario_variant()YamlConfigStore.read_scenario_variants()YamlConfigStore.read_scenarios()YamlConfigStore.read_sos_model()YamlConfigStore.read_sos_models()YamlConfigStore.read_strategies()YamlConfigStore.update_model()YamlConfigStore.update_model_run()YamlConfigStore.update_scenario()YamlConfigStore.update_scenario_variant()YamlConfigStore.update_sos_model()YamlConfigStore.write_model()YamlConfigStore.write_model_run()YamlConfigStore.write_scenario()YamlConfigStore.write_scenario_variant()YamlConfigStore.write_sos_model()YamlConfigStore.write_strategies()
- smif.data_layer.file.file_data_store module
CSVDataStoreFileDataStoreFileDataStore.available_results()FileDataStore.delete_results()FileDataStore.get_timesteps_from_data()FileDataStore.initial_conditions_data_exists()FileDataStore.interventions_data_exists()FileDataStore.model_parameter_default_data_exists()FileDataStore.narrative_variant_data_exists()FileDataStore.read_coefficients()FileDataStore.read_initial_conditions()FileDataStore.read_interventions()FileDataStore.read_model_parameter_default()FileDataStore.read_narrative_variant_data()FileDataStore.read_results()FileDataStore.read_scenario_variant_data()FileDataStore.read_state()FileDataStore.read_strategy_interventions()FileDataStore.scenario_variant_data_exists()FileDataStore.strategy_data_exists()FileDataStore.write_coefficients()FileDataStore.write_initial_conditions()FileDataStore.write_interventions()FileDataStore.write_model_parameter_default()FileDataStore.write_narrative_variant_data()FileDataStore.write_results()FileDataStore.write_scenario_variant_data()FileDataStore.write_state()FileDataStore.write_strategy_interventions()
ParquetDataStore
- smif.data_layer.file.file_metadata_store module
Submodules¶
smif.data_layer.abstract_config_store module¶
A config store holds the configuration data for running system-of-systems models with smif: - model runs - system-of-systems models - model definitions - strategies - scenarios and scenario variants - narratives
- class smif.data_layer.abstract_config_store.ConfigStore[source]¶
Bases:
objectA ConfigStore must implement each of the abstract methods defined in this interface
- abstractmethod delete_model_run(model_run_name)[source]¶
Delete a system-of-system model run
- Parameters:
model_run_name (str)
- abstractmethod delete_scenario(scenario_name)[source]¶
Delete scenario from project configuration
- Parameters:
scenario_name (str)
- abstractmethod delete_scenario_variant(scenario_name, variant_name)[source]¶
Delete scenario from project configuration
- abstractmethod delete_sos_model(sos_model_name)[source]¶
Delete a system-of-system model
- Parameters:
sos_model_name (str)
- abstractmethod read_narrative(sos_model_name, narrative_name)[source]¶
Read narrative from sos_model
- abstractmethod read_scenario(scenario_name)[source]¶
Read a scenario
- Parameters:
scenario_name (str)
- Return type:
- abstractmethod update_model_run(model_run_name, model_run)[source]¶
Update system-of-system model run
- abstractmethod update_scenario(scenario_name, scenario)[source]¶
Update scenario
- Parameters:
scenario_name (str)
scenario (ScenarioModel)
- abstractmethod update_scenario_variant(scenario_name, variant_name, variant)[source]¶
Update scenario to project configuration
- abstractmethod write_model_run(model_run)[source]¶
Write system-of-system model run
- Parameters:
model_run (ModelRun)
- abstractmethod write_scenario(scenario)[source]¶
Write scenario
- Parameters:
scenario (ScenarioModel)
- abstractmethod write_scenario_variant(scenario_name, variant)[source]¶
Write scenario to project configuration
smif.data_layer.abstract_data_store module¶
A data store holds the bulk of model setup, intermediate and output data: - scenario variant and narrative data (including parameter defaults) - model interventions, initial conditions and state - conversion coefficients - results
- class smif.data_layer.abstract_data_store.DataStore[source]¶
Bases:
objectA DataStore must implement each of the abstract methods defined in this interface
- abstractmethod delete_results(model_run_name, model_name, output_name, timestep=None, decision_iteration=None)[source]¶
Delete results for a single timestep/iteration of a model output in a model run
- classmethod filter_on_timesteps(dataframe, spec, path, timestep=None, timesteps=None)[source]¶
Filter dataframe by timestep
The ‘timestep’ dimension is treated as follows:
If a single timestep is specified, the spec MAY include ‘timestep’ as a dimension. If so, the returned DataArray’s spec will match the timestep requested. Otherwise, the DataArray will not include timestep as a dimension.
If multiple timesteps are specified, the returned DataArray’s spec will include a ‘timestep’ dimension to match the timesteps requested.
If timestep and timesteps are None, and the stored data has a timestep column, read all available timesteps. The returned DataArray’s spec ‘timestep’ dimension will match the timesteps requested. If the stored data does not have a timestep column, ignore and pass through unchanged.
- abstractmethod read_coefficients(source_dim, destination_dim)[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 a single pair of dimensions.
- Parameters:
- Return type:
Notes
To be called from
Adaptorimplementations.
- abstractmethod read_initial_conditions(key) List[Dict][source]¶
Read historical interventions for key
- abstractmethod read_results(modelrun_name, model_name, output_spec, timestep=None, decision_iteration=None) DataArray[source]¶
Return results of a model from a model_run for a given output at a timestep and decision iteration
- abstractmethod read_scenario_variant_data(key, spec, timestep=None, timesteps=None) DataArray[source]¶
Read scenario variant data array.
If a single timestep is specified, the spec MAY include ‘timestep’ as a dimension, which should match the timestep specified.
If multiple timesteps are specified, the spec MUST include ‘timestep’ as a dimension, which should match the timesteps specified.
If timestep and timesteps are None, read all available timesteps. Whether or not the spec includes ‘timestep’ as a dimension, the returned DataArray will include a ‘timestep’ dimension with all available timesteps included.
- abstractmethod read_state(modelrun_name, timestep, decision_iteration=None) List[Dict][source]¶
Read list of (name, build_year) for a given model_run, timestep, decision
- abstractmethod write_coefficients(source_dim, destination_dim, data)[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 a single pair of dimensions.
- Parameters:
source_dim (str) – dimension name
destination_dim (str) – dimension name
data (numpy.ndarray)
Notes
To be called from
Adaptorimplementations.
- abstractmethod write_initial_conditions(key, initial_conditions)[source]¶
Write historical interventions for key
- abstractmethod write_results(data, modelrun_name, model_name, timestep=None, decision_iteration=None)[source]¶
Write results of a model_name in model_run_name for a given output_name
smif.data_layer.abstract_metadata_store module¶
A metadata store holds shared metadata for smif model scenarios, inputs, parameters and outputs: - units - dimension definitions
- class smif.data_layer.abstract_metadata_store.MetadataStore[source]¶
Bases:
objectA MetaDataStore must implement each of the abstract methods defined in this interface
- abstractmethod delete_dimension(dimension_name)[source]¶
Delete dimension
- Parameters:
dimension_name (str)
- abstractmethod update_dimension(dimension_name, dimension)[source]¶
Update dimension
- Parameters:
dimension_name (str)
dimension (Coords)
smif.data_layer.data_array module¶
DataArray provides a thin wrapper around multidimensional arrays and metadata
- class smif.data_layer.data_array.DataArray(spec: ~smif.metadata.spec.Spec, data: <MagicMock id='135493239504048'>)[source]¶
Bases:
objectDataArray provides access to input/parameter/results data, with conversions to common python data libraries (for example: numpy, pandas, xarray).
- spec¶
- Type:
- data¶
- Type:
- as_df() <MagicMock id='135493245860944'>[source]¶
Access DataArray as a
pandas.DataFrame
- as_ndarray() <MagicMock id='135493240406144'>[source]¶
Access as a
numpy.ndarray
- as_xarray()[source]¶
Access DataArray as a
xarray.DataArray
- property coords¶
Coordinate labels for each dimension.
- property description¶
A human-friendly description
- property dims¶
Names for each dimension
- classmethod from_df(spec, dataframe)[source]¶
Create a DataArray from a
pandas.DataFrame
- classmethod from_xarray(spec, xr_data_array)[source]¶
Create a DataArray from a
xarray.DataArray
- property name¶
The name of the data that this spec describes.
- property shape¶
The shape of the data array
- property unit¶
The unit for all data points.
- smif.data_layer.data_array.find_duplicate_indices(dataframe)[source]¶
Find duplicate indices in a DataFrame
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).
- class smif.data_layer.data_handle.DataHandle(store: Store, modelrun_name, current_timestep, timesteps, model, decision_iteration=None)[source]¶
Bases:
objectGet/set model parameters and data
- property base_timestep¶
Base timestep
- property current_timestep¶
Current timestep
- property decision_iteration¶
- derive_for(model)[source]¶
Derive a new DataHandle configured for the given Model
- Parameters:
model (Model) – Model which will use this DataHandle
- get_base_timestep_data(input_name)[source]¶
Get data from the base timestep as required for model inputs
- Parameters:
input_name (str)
- Return type:
- 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) DataArray[source]¶
Get data required for model inputs
- Parameters:
input_name (str)
timestep (RelativeTimestep or int, optional) – defaults to RelativeTimestep.CURRENT
- Returns:
Contains data annotated with the metadata and provides utility methods to access the data in different ways
- Return type:
- 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_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:
- get_parameters()[source]¶
Get all parameter values
- Returns:
parameters – Read-only view of parameters (like a read-only dict)
- Return type:
MappingProxyType
- get_previous_timestep_data(input_name)[source]¶
Get data from the previous timestep as required for model inputs
- Parameters:
input_name (str)
- Return type:
- 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:
Notes
Access to model results is only granted to models contained within self._model if self._model is a smif.model.model.CompositeModel
- 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:
- Raises:
ValueError – If self._current_timestep is None an error is raised.
- property previous_timestep¶
Previous timestep
- read_coefficients(source_dim: str, destination_dim: str) <MagicMock id='135493267455824'>[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:
- Return type:
- set_results(output_name, data)[source]¶
Set results values for model outputs
- Parameters:
output_name (str)
data (numpy.ndarray)
- property timesteps¶
All timesteps (as tuple)
- write_coefficients(source_dim: str, destination_dim: str, data: <MagicMock id='135493236816448'>)[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: Store, modelrun_name: str, sos_model, current_timestep: int, timesteps: List[int] | None = None, decision_iteration: int | None = None)[source]¶
Bases:
objectResults access for decision modules
- get_results(model_name: str, output_name: str, timestep: int | RelativeTimestep, decision_iteration: int) DataArray[source]¶
Access model results
- Parameters:
model_name (str)
output_name (str)
timestep ([int, RelativeTimestep])
decision_iteration (int)
- Returns:
Contains data annotated with the metadata and provides utility methods to access the data in different ways
- Return type:
- get_state(timestep: int, decision_iteration: int) List[Dict][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]
smif.data_layer.database_interface module¶
smif.data_layer.memory_interface module¶
Memory-backed store implementations
- class smif.data_layer.memory_interface.MemoryConfigStore[source]¶
Bases:
ConfigStoreConfig store in memory
- delete_model_run(model_run_name)[source]¶
Delete a system-of-system model run
- Parameters:
model_run_name (str)
- delete_scenario(scenario_name)[source]¶
Delete scenario from project configuration
- Parameters:
scenario_name (str)
- delete_scenario_variant(scenario_name, variant_name)[source]¶
Delete scenario from project configuration
- delete_sos_model(sos_model_name)[source]¶
Delete a system-of-system model
- Parameters:
sos_model_name (str)
- update_scenario(scenario_name, scenario)[source]¶
Update scenario
- Parameters:
scenario_name (str)
scenario (ScenarioModel)
- update_scenario_variant(scenario_name, variant_name, variant)[source]¶
Update scenario to project configuration
- write_model_run(model_run)[source]¶
Write system-of-system model run
- Parameters:
model_run (ModelRun)
- write_scenario(scenario)[source]¶
Write scenario
- Parameters:
scenario (ScenarioModel)
- class smif.data_layer.memory_interface.MemoryDataStore[source]¶
Bases:
DataStoreStore data in-memory
- delete_results(model_run_name, model_name, output_name, timestep=None, decision_iteration=None)[source]¶
Delete results for a single timestep/iteration of a model output in a model run
- read_coefficients(source_dim, destination_dim)[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 a single pair of dimensions.
- Parameters:
- Return type:
Notes
To be called from
Adaptorimplementations.
- read_results(modelrun_name, model_name, output_spec, timestep=None, decision_iteration=None)[source]¶
Return results of a model from a model_run for a given output at a timestep and decision iteration
- read_scenario_variant_data(key, spec, timestep=None, timesteps=None)[source]¶
Read scenario variant data array.
If a single timestep is specified, the spec MAY include ‘timestep’ as a dimension, which should match the timestep specified.
If multiple timesteps are specified, the spec MUST include ‘timestep’ as a dimension, which should match the timesteps specified.
If timestep and timesteps are None, read all available timesteps. Whether or not the spec includes ‘timestep’ as a dimension, the returned DataArray will include a ‘timestep’ dimension with all available timesteps included.
- read_state(modelrun_name, timestep=None, decision_iteration=None)[source]¶
Read list of (name, build_year) for a given model_run, timestep, decision
- write_coefficients(source_dim, destination_dim, data)[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 a single pair of dimensions.
- Parameters:
source_dim (str) – dimension name
destination_dim (str) – dimension name
data (numpy.ndarray)
Notes
To be called from
Adaptorimplementations.
- write_results(data_array, modelrun_name, model_name, timestep=None, decision_iteration=None)[source]¶
Write results of a model_name in model_run_name for a given output_name
- class smif.data_layer.memory_interface.MemoryMetadataStore[source]¶
Bases:
MetadataStoreStore metadata in-memory
- update_dimension(dimension_name, dimension)[source]¶
Update dimension
- Parameters:
dimension_name (str)
dimension (Coords)
smif.data_layer.model_loader module¶
ModelLoader reads python modules as specified at runtime, loading and instantiating objects.
- class smif.data_layer.model_loader.ModelLoader[source]¶
Bases:
objectLoad Model from config
Examples
Call
ModelLoader.load()to create, load and return aModelobject.>>> loader = ModelLoader() >>> sector_model = loader.load(sector_model_config) >>> conversion_model = loader.load(conversion_model_config)
- load(config)[source]¶
Loads the model class specified by the config, returns an instance of that class using the Model.from_dict method.
smif.data_layer.results module¶
Results provides a common interface to access results from model runs.
- class smif.data_layer.results.Results(store: Store | dict)[source]¶
Bases:
objectCommon interface to access results from model runs.
- Parameters:
store (Store or dict) – pre-created Store object or dictionary of the form {‘interface’: <interface>, ‘dir’: <dir>} where <interface> is either ‘local_csv’ or ‘local_parquet’, and <dir> is the model base directory
- available_results(model_run_name)[source]¶
Return the results available for a given model run.
- Parameters:
model_run_name (str the requested model run)
- Return type:
A nested dictionary data structure of the results available for the given model run
- list_outputs(sector_model_name: str)[source]¶
Return a list of model run names.
- Parameters:
sector_model_name (str the requested sector model)
- Return type:
List of outputs for the given sector model
- list_scenario_outputs(scenario_name: str)[source]¶
Return a list of outputs of a given scenario.
- Parameters:
scenario_name (str the requested scenario)
- Return type:
List of outputs for the requested scenario
- list_scenarios(model_run_name: str)[source]¶
Return a dictionary of scenarios for given model run.
- Parameters:
model_run_name (str the requested model run)
- Return type:
Dictionary of (scenario name, variant) for the given model run.
- list_sector_models(model_run_name: str)[source]¶
Return a list of sector models for given model run.
- Parameters:
model_run_name (str the requested model run)
- Return type:
List of sector models for the given model run
- read_results(model_run_names: list, model_names: list, output_names: list, timesteps: list = None, decisions: list = None, time_decision_tuples: list = None)[source]¶
Return results from the store as a formatted pandas data frame. There are a number of ways of requesting specific timesteps/decisions. You can specify either:
- a list of (timestep, decision) tuples
in which case data for all of those tuples matching the available results will be returned
- or:
- a list of timesteps
in which case data for all of those timesteps (and any decision iterations) matching the available results will be returned
- or:
- a list of decision iterations
in which case data for all of those decision iterations (and any timesteps) matching the available results will be returned
- or:
- a list of timesteps and a list of decision iterations
in which case data for the Cartesian product of those timesteps and those decision iterations matching the available results will be returned
- or:
- nothing
in which case all available results will be returned
- Parameters:
model_run_names (list) – the requested model run names
model_names (list) – the requested sector model names (exactly one required)
output_names (list) – the requested output names (output specs must all match)
timesteps (list) – the requested timesteps
decisions (list) – the requested decision iterations
time_decision_tuples (list) – a list of requested (timestep, decision) tuples
- Raises:
SmifDataNotFoundError – If data cannot be found in the store when try to read from the store
SmifDataMismatchError – Data presented to read, write and update methods is in the incorrect format or of wrong dimensions to that expected
SmifDataReadError – When unable to read data e.g. unable to handle file type or connect to database
- Return type:
- read_scenario_data(scenario_name: str, variant_name: str, variable_name: str, timesteps: list) <MagicMock id='135493236657712'>[source]¶
Return scenario variant data from the store as a formatted pandas data frame.
- Parameters:
- Raises:
SmifDataNotFoundError – If data cannot be found in the store when try to read from the store
SmifDataMismatchError – Data presented to read, write and update methods is in the incorrect format or of wrong dimensions to that expected
SmifDataReadError – When unable to read data e.g. unable to handle file type or connect to database
- Return type:
smif.data_layer.store module¶
The store provides a common data interface to smif configuration, data and metadata.
- raises SmifDataNotFoundError:
If data cannot be found in the store when try to read from the store
- raises SmifDataExistsError:
If data already exists in the store when trying to write to the store (use an update method instead)
- raises SmifDataMismatchError:
Data presented to read, write and update methods is in the incorrect format or of wrong dimensions to that expected
- raises SmifDataReadError:
When unable to read data e.g. unable to handle file type or connect to database
- class smif.data_layer.store.Store(config_store, metadata_store: MetadataStore, data_store: DataStore, model_base_folder='.')[source]¶
Bases:
objectCommon interface to data store, composed of config, metadata and data store implementations.
- Parameters:
config_store (ConfigStore)
metadata_store (MetadataStore)
data_store (DataStore)
- canonical_available_results(model_run_name)[source]¶
List the results that are available from a model run, collapsing all decision iterations.
This is the unique items from calling available_results, with all decision iterations set to 0.
This method is used to determine whether a model run is complete, given that it is impossible to know how many decision iterations to expect: we simply check that each expected timestep has been completed.
- Parameters:
model_run_name (str)
- Return type:
set Set of tuples representing available results
- canonical_expected_results(model_run_name)[source]¶
List the results that are expected from a model run, collapsing all decision iterations.
For a complete model run, this would coincide with the unique list returned from available_results, where all decision iterations are set to 0.
This method is used to determine whether a model run is complete, given that it is impossible to know how many decision iterations to expect: we simply check that each expected timestep has been completed.
- Parameters:
model_run_name (str)
- Return type:
set Set of tuples representing expected results
- canonical_missing_results(model_run_name)[source]¶
List the results that are missing from a model run, collapsing all decision iterations.
For a complete model run, this is what is left after removing canonical_available_results from canonical_expected_results.
- Parameters:
model_run_name (str)
- Return type:
set Set of tuples representing missing results
- clear_results(model_run_name)[source]¶
Clear all results from a single model run
- Parameters:
model_run_name (str)
- delete_model_run(model_run_name)[source]¶
Delete a system-of-system model run
- Parameters:
model_run_name (str)
- delete_results(model_run_name, model_name, output_name, timestep=None, decision_iteration=None)[source]¶
Delete results for a single timestep/iteration of a model output in a model run
- delete_scenario(scenario_name)[source]¶
Delete scenario from project configuration
- Parameters:
scenario_name (str)
- delete_scenario_variant(scenario_name, variant_name)[source]¶
Delete scenario from project configuration
- delete_sos_model(sos_model_name)[source]¶
Delete a system-of-system model
- Parameters:
sos_model_name (str)
- static filter_complete_available_results(available_results, expected_model_outputs)[source]¶
Filter available results from a model run to include only complete timestep/decision iteration combinations
- get_result_darray(model_run_name, model_name, output_name, timesteps=None, decision_iterations=None, time_decision_tuples=None)[source]¶
Return data for multiple timesteps and decision iterations for a given output from a given sector model in a specific model run.
- You can specify either:
- a list of (timestep, decision) tuples
in which case data for all of those tuples matching the available results will be returned
- or:
- a list of timesteps
in which case data for all of those timesteps (and any decision iterations) matching the available results will be returned
- or:
- a list of decision iterations
in which case data for all of those decision iterations (and any timesteps) matching the available results will be returned
- or:
- a list of timesteps and a list of decision iterations
in which case data for the Cartesian product of those timesteps and those decision iterations matching the available results will be returned
- or:
- nothing
in which case all available results will be returned
Then, for each tuple, the data array from the corresponding read_results call is stacked, and together with the new spec this information is returned as a new DataArray.
- Parameters:
- Return type:
DataArray with expanded spec and the data requested
- get_results(model_run_names: list, model_name: str, output_names: list, timesteps: list = None, decisions: list = None, time_decision_tuples: list = None)[source]¶
Return data for multiple timesteps and decision iterations for a given output from a given sector model for multiple model runs.
- Parameters:
- Returns:
Nested dictionary of DataArray objects, keyed on model run name and output name. Returned DataArrays include one extra (timestep, decision_iteration) dimension.
- Return type:
- prepare_model_runs(model_run_name, scenario_name, first_var, last_var)[source]¶
Write multiple model run config files corresponding to multiple scenario variants of {scenario_name}, based on template {model_run_name}
Write batchfile containing each of the generated model runs
- prepare_scenario(scenario_name, list_of_variants)[source]¶
Modify {scenario_name} config file to include multiple scenario variants.
- prepare_warm_start(model_run_name)[source]¶
Copy the results from the previous model_run if available
The method allows a previous unsuccessful model_run to ‘warm start’ a new model run from a later timestep. Model results are recovered from the timestep that the previous model_run was run until, and the new model run runs from the returned timestep
- Parameters:
model_run_name (str)
- Return type:
int The timestep to which the data store was recovered
Notes
Called from smif.controller.execute
- read_all_initial_conditions(model_run_name) List[Dict][source]¶
A list of all historical interventions
- read_coefficients(source_dim: str, destination_dim: str) <MagicMock id='135493238206208'>[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:
- Return type:
Notes
To be called from
Adaptorimplementations.
- read_dimension(dimension_name, skip_coords=False)[source]¶
Return dimension
- Parameters:
dimension_name (str)
- Returns:
A dimension definition (including elements)
- Return type:
Coords
- read_initial_conditions(model_name) List[Dict][source]¶
Read historical interventions for model_name
- read_model_parameter_default(model_name, parameter_name, assert_exists=False)[source]¶
Read default data for a sector model parameter
- read_narrative_variant_data(sos_model_name, narrative_name, variant_name, parameter_name, timestep=None, assert_exists=False)[source]¶
Read narrative data file
- read_results(model_run_name: str, model_name: str, output_spec: Spec, timestep: int | None = None, decision_iteration: int | None = None) DataArray[source]¶
Return results of a model_name in model_run_name for a given output_name
- read_scenario(scenario_name, skip_coords=False)[source]¶
Read a scenario
- Parameters:
scenario_name (str)
- Return type:
- read_scenario_variant_data(scenario_name: str, variant_name: str, variable: str, timestep: int | None = None, timesteps: List[int] | None = None, assert_exists: bool = False) DataArray | bool[source]¶
Read scenario data file
- read_state(model_run_name, timestep, decision_iteration=None) List[Dict][source]¶
Read list of (name, build_year) for a given model_run, timestep, decision
- read_strategy_interventions(strategy, assert_exists=False)[source]¶
Read interventions as defined in a model run strategy
- update_dimension(dimension_name, dimension)[source]¶
Update dimension
- Parameters:
dimension_name (str)
dimension (Coords)
- update_scenario(scenario_name, scenario)[source]¶
Update scenario
- Parameters:
scenario_name (str)
scenario (ScenarioModel)
- update_scenario_variant(scenario_name, variant_name, variant)[source]¶
Update scenario to project configuration
- write_coefficients(source_dim: str, destination_dim: str, data: <MagicMock id='135493237137168'>)[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)
Notes
To be called from
Adaptorimplementations.
- write_dimension(dimension)[source]¶
Write dimension to project configuration
- Parameters:
dimension (Coords)
- write_initial_conditions(model_name, initial_conditions)[source]¶
Write historical interventions for a model
- Parameters:
list[dict] – A list of historical interventions, with keys ‘name’ and ‘build_year’
- write_interventions(model_name, interventions)[source]¶
Write interventions data for a model
- Parameters:
dict[str – A dict of intervention dictionaries containing intervention attributes keyed by intervention name
dict] – A dict of intervention dictionaries containing intervention attributes keyed by intervention name
- write_model_parameter_default(model_name, parameter_name, data)[source]¶
Write default data for a sector model parameter
- write_model_run(model_run)[source]¶
Write system-of-system model run
- Parameters:
model_run (ModelRun)
- write_narrative_variant_data(sos_model_name, narrative_name, variant_name, data)[source]¶
Read narrative data file
- write_results(data_array, model_run_name, model_name, timestep=None, decision_iteration=None)[source]¶
Write results of a model_name in model_run_name for a given output_name
- write_scenario(scenario)[source]¶
Write scenario
- Parameters:
scenario (ScenarioModel)
- write_state(state, model_run_name, timestep, decision_iteration=None)[source]¶
State is a list of decisions with name and build_year.
State is output from the DecisionManager
smif.data_layer.validate module¶
Validate the correct format and presence of the config data for the system-of-systems model
- smif.data_layer.validate.validate_dependency_spec(input_spec, model_name)[source]¶
Check the input specification for a single sector model
- smif.data_layer.validate.validate_initial_condition(datum, file_path)[source]¶
Check a single initial condition datum
- smif.data_layer.validate.validate_initial_conditions(data, file_path)[source]¶
Check a list of initial condition observations
- smif.data_layer.validate.validate_interventions(data, path)[source]¶
Validate the loaded data as required for model interventions
- smif.data_layer.validate.validate_path_to_timesteps(timesteps)[source]¶
Check timesteps is a path to timesteps file
- smif.data_layer.validate.validate_scenario_data(data, file_path)[source]¶
Check a list of scenario observations
- smif.data_layer.validate.validate_scenario_datum(datum, file_path)[source]¶
Check a single scenario datum
- smif.data_layer.validate.validate_sector_model_initial_config(sector_model_config)[source]¶
Check a single sector model initial configuration
- smif.data_layer.validate.validate_sector_models_initial_config(sector_models)[source]¶
Check list of sector models initial configuration
- smif.data_layer.validate.validate_sos_model_config(sos_model, sector_models, scenarios)[source]¶
Check expected values for data loaded from master config file