smif.data_layer package

Submodules

smif.data_layer.load module

Parse yaml config files, to construct sector models

smif.data_layer.load.dump(data, file_path)[source]

Write plain data to a file as yaml

Parameters:
  • file_path (str) – The path of the configuration file to write
  • data – Data to write (should be lists, dicts and simple values)
smif.data_layer.load.load(file_path)[source]

Parse yaml config file into plain data (lists, dicts and simple values)

Parameters:file_path (str) – The path of the configuration file to parse

smif.data_layer.sector_model_config module

Read and parse the config for sector models

class smif.data_layer.sector_model_config.SectorModelReader(initial_config=None)[source]

Bases: object

Parses the configuration and input data for a sector model

Parameters:initial_config (dict) –

Sector model details, sufficient to read the full config from a set of files. Must contain the following fields:

”model_name”
The name of the sector model, for reference within the system-of-systems model
”model_path”
The path to the python module file that contains an implementation of SectorModel
”model_classname”
The name of the class that implements SectorModel
”model_config_dir”
The root path of model config/data to use, which must contain inputs.yaml, outputs.yaml, time_intervals.yaml and regions.shp/regions.geojson
”initial_conditions”
List of files containing initial conditions
”interventions”
List of files containing interventions
”parameters”
List of files containing parameter configuration
data

Expose all loaded config data

Returns:data
Model configuration data, with the following fields:
”name”: The name of the sector model, for reference within the system-of-systems model

”path”: The path to the python module file that contains an implementation of SectorModel

”classname”: The name of the class that implements SectorModel

”inputs”: A list of the inputs that this model requires

”outputs”: A list of the outputs that this model provides

”time_intervals”: A list of time intervals within a year that are represented by the model, each with reference to the model’s internal identifier for timesteps

”regions”: A list of geographical regions used within the model, as objects with both geography and attributes

”initial_conditions”: A list of initial conditions required to set up the modelled system in the base year

”interventions”: A list of possible interventions that could be made in the modelled system

”parameters”: A list of model parameters

Return type:dict
load()[source]

Load and check all config

load_initial_conditions()[source]

Inital conditions are located in yaml files specified in sector model blocks in the sos model config

load_inputs()[source]

Input spec is located in the data/<sectormodel>/inputs.yaml file

load_interventions()[source]

Interventions are located in yaml files specified in sector model blocks in the sos model config

load_io_metadata(inputs_or_outputs)[source]

Load inputs or outputs, allowing missing or empty file

load_outputs()[source]

Output spec is located in data/<sectormodel>/output.yaml file

load_parameters()[source]

Parameter configurations are located in yaml files specified in sector model blocks in the sos model config

smif.data_layer.sos_model_config module

Read and parse the config files for the system-of-systems model

class smif.data_layer.sos_model_config.SosModelReader(config_file_path)[source]

Bases: object

Encapsulates the parsing of the system-of-systems configuration

Parameters:config_file_path (str) – A path to the master config file
data

Expose all model configuration data

Returns:Returns a dictionary with the following keys:
timesteps
the sequence of years
max_iterations
limit iterations for solving interdependencies
sector_model_config: list
The list of sector model configuration data
scenario_data: dict
A dictionary of scenario data, with the parameter name as the key and the data as the value
planning: list
A list of dicts of planning instructions
region_sets: dict
A dictionary of region set data, with the name as the key and the data as the value
interval_sets: dict
A dictionary of interval set data, with the name as the key and the data as the value
scenario_metadata: list of dicts
The spatial and temporal resolutions and units of scenario data
Return type:dict
load()[source]

Load and check all config

load_convergence_absolute_tolerance()[source]

Parse convergence_absolute_tolerance setting

load_convergence_max_iterations()[source]

Parse convergence_max_iterations setting

load_convergence_relative_tolerance()[source]

Parse convergence_relative_tolerance setting

load_dependencies()[source]

Parse dependencies

load_planning()[source]

Loads the set of build instructions for planning

Returns:A list of planning instructions loaded from the planning file
Return type:list
load_regions()[source]

Model regions are specified in data/<sectormodel>/regions.*

The file format must be possible to parse with GDAL, and must contain an attribute “name” to use as an identifier for the region.

load_scenario_data()[source]

Load scenario data from list in sos model config

Working assumptions:

  • scenario data is list of dicts, each like:

    {
        'parameter': 'parameter_name',
        'file': 'relative file path',
        'spatial_resolution': 'national',
        'temporal_resolution': 'annual',
        'units': 'kg'
    }
    
  • data in file is list of dicts, each like:

    {
        'value': 100,
        # optional, depending on parameter type:
        'region': 'UK',
        'year': 2015
    }
    
Returns:A dictionary where keys are parameters names and values are the file contents, so a list of dicts
Return type:dict
load_sector_model_data()[source]

Parse list of sector models to run

Model details include: - model name - model config directory - SectorModel class name to call

load_sos_config()[source]

Parse model master config

  • configures run mode
  • sets max iterations for solving interdependencies
  • points to timesteps file
  • points to shared data files
  • points to sector models and sector model data files
load_time_intervals()[source]

Within-year time intervals are specified in data/<sectormodel>/time_intervals.yaml

These specify the mapping of model timesteps to durations within a year (assume modelling 365 days: no extra day in leap years, no leap seconds)

Each time interval must have - start (period since beginning of year) - end (period since beginning of year) - id (label to use when passing between integration layer and sector model)

use ISO 8601[1]_ duration format to specify periods:

P[n]Y[n]M[n]DT[n]H[n]M[n]S

For example:

P1Y == 1 year
P3M == 3 months
PT168H == 168 hours

So to specify a period from the beginning of March to the end of May:

start: P2M
end: P5M
id: spring

References

[1]https://en.wikipedia.org/wiki/ISO_8601#Durations
load_timesteps()[source]

Parse model timesteps

smif.data_layer.validate module

Validate the correct format and presence of the config data for the system-of-systems model

exception smif.data_layer.validate.ValidationError[source]

Bases: Exception

Custom exception to use for parsing validation.

smif.data_layer.validate.validate_dependency(dep)[source]

Check a dependency specification

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_interval_sets_config(interval_sets)[source]

Check interval sets

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_planning_config(planning)[source]

Check planning options

smif.data_layer.validate.validate_region_sets_config(region_sets)[source]

Check regions sets

smif.data_layer.validate.validate_scenario(scenario)[source]

Check a single scenario specification

smif.data_layer.validate.validate_scenario_data(data, file_path)[source]

Check a list of scenario observations

smif.data_layer.validate.validate_scenario_data_config(scenario_data)[source]

Check scenario data

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(data)[source]

Check expected values for data loaded from master config file

smif.data_layer.validate.validate_time_interval(interval)[source]

Check a single time interval

smif.data_layer.validate.validate_time_intervals(intervals, file_path)[source]

Check time intervals

smif.data_layer.validate.validate_timesteps(timesteps, file_path)[source]

Check timesteps is a list of integers

Module contents

Data access modules for loading system-of-systems model configuration

class smif.data_layer.DataInterface[source]

Bases: object

read_interval_definition_data(interval_definition_name)[source]
read_interval_definitions()[source]
read_narrative_data(narrative_name)[source]
read_narrative_set(narrative_set_name)[source]
read_narrative_sets()[source]
read_region_definition_data(region_definition_name)[source]
read_region_definitions()[source]
read_scenario_data(scenario_name)[source]
read_scenario_set(scenario_set_name)[source]
read_scenario_sets()[source]
read_sector_model(sector_model_name)[source]
read_sector_models()[source]
read_sos_model_runs()[source]
read_sos_models()[source]
update_interval_definition(interval_definition)[source]
update_narrative(narrative)[source]
update_narrative_set(narrative_set)[source]
update_region_definition(region_definition)[source]
update_scenario(scenario)[source]
update_scenario_set(scenario_set)[source]
update_sector_model(sector_model_name, sector_model)[source]
update_sos_model(sos_model_name, sos_model)[source]
update_sos_model_run(sos_model_run_name, sos_model_run)[source]
write_interval_definition(interval_definition)[source]
write_narrative(narrative)[source]
write_narrative_set(narrative_set)[source]
write_region_definition(region_definition)[source]
write_scenario(scenario)[source]
write_scenario_set(scenario_set)[source]
write_sector_model(sector_model)[source]
write_sos_model(sos_model)[source]
write_sos_model_run(sos_model_run)[source]
class smif.data_layer.DatabaseInterface(config_path)[source]

Bases: smif.data_layer.DataInterface

Read and write interface to Database

read_interval_data(interval_name)[source]
read_intervals()[source]
read_narrative_data(narrative_name)[source]
read_narrative_set(narrative_set_name)[source]
read_narrative_sets()[source]
read_region_data(region_name)[source]
read_regions()[source]
read_scenario_data(scenario_name)[source]
read_scenario_set(scenario_set_name)[source]
read_scenario_sets()[source]
read_sector_model(sector_model_name)[source]
read_sector_models()[source]
read_sos_model_runs()[source]
read_sos_models()[source]
read_units()[source]
write_interval(interval)[source]
write_narrative(narrative)[source]
write_narrative_set(narrative_set)[source]
write_region(region)[source]
write_scenario(scenario)[source]
write_scenario_set(scenario_set)[source]
write_sector_model(sector_model)[source]
write_sos_model(sos_model)[source]
write_sos_model_run(sos_model_run)[source]
write_unit(unit)[source]
class smif.data_layer.DatafileInterface(base_folder)[source]

Bases: smif.data_layer.DataInterface

Read and write interface to YAML / CSV configuration files

Project.yml

Parameters:base_folder (str) – The path to the configuration and data files
read_initial_conditions(filename)[source]

Read the initial conditions from filename

Parameters:filename (str) – The name of the initial conditions yml file to read in
read_interval_definition_data(interval_definition_name)[source]
Parameters:interval_definition_name (str) –
Returns:Interval definition data
Return type:dict

Notes

Expects csv file to contain headings of year, start, end

read_interval_definitions()[source]

Read interval_definition sets from project configuration

Returns:A list of interval_definition set dicts
Return type:list
read_interventions(filename)[source]

Read the interventions from filename

Parameters:filename (str) – The name of the intervention yml file to read in
read_narrative_data(narrative_name)[source]

Read narrative data file

Parameters:narrative_name (str) – Name of the narrative
Returns:A list with dictionaries containing the contents of ‘narrative_name’ data file
Return type:list
read_narrative_definition(narrative_name)[source]

Read the narrative definition

Parameters:narrative_name (str) – Name of the narrative
Returns:
Return type:dict
read_narrative_set(narrative_set_name)[source]

Read all narratives from a certain narrative_set

Parameters:narrative_set_name (str) – Name of the narrative_set
Returns:A list of narratives within the specified ‘narrative_set_name’
Return type:list
read_narrative_sets()[source]

Read narrative sets from project configuration

Returns:A list of narrative set dicts
Return type:list
read_region_definition_data(region_definition_name)[source]

Read region_definition data file into a Fiona feature collection

The file format must be possible to parse with GDAL, and must contain an attribute “name” to use as an identifier for the region_definition.

Parameters:region_definition_name (str) – Name of the region_definition
Returns:A list of data from the specified file in a fiona formatted dict
Return type:list
read_region_definitions()[source]

Read region_definitions from project configuration

Returns:A list of region_definition dicts
Return type:list
read_scenario_data(scenario_name)[source]

Read scenario data file

Parameters:scenario_name (str) – Name of the scenario
Returns:A dict of lists of dicts containing the contents of scenario_name data file(s) associated with the scenario parameters. The keys of the dict are the parameter names
Return type:dict
read_scenario_definition(scenario_name)[source]

Read scenario definition data

Parameters:scenario_name (str) – Name of the scenario
Returns:The scenario definition
Return type:dict
read_scenario_set(scenario_set_name)[source]

Read all scenarios from a certain scenario_set

Parameters:scenario_set_name (str) – Name of the scenario_set
Returns:A list of scenarios within the specified ‘scenario_set_name’
Return type:list
read_scenario_sets()[source]

Read scenario sets from project configuration

Returns:A list of scenario set dicts
Return type:list
read_sector_model(sector_model_name)[source]

Read a sector model from a Yaml file

Raises an exception when the file does not exists

Parameters:sector_model_name (str) – Name of the sector_model (sector_model[‘name’])
read_sector_models()[source]

Read all sector models from Yaml files

Returns:A list of sector_model dicts
Return type:list
read_sos_model(sos_model_name)[source]

Read a specific system-of-system model

Returns:A sos model configuration dictionary
Return type:dict
read_sos_model_runs()[source]

Read all system-of-system model runs from Yaml files

sos_model_runs.yml

Returns:A list of sos_model_run dicts
Return type:list
read_sos_models()[source]

Read all system-of-system models from Yaml files

Returns:A list of sos_models dicts
Return type:list
update_interval_definition(interval_definition_name, interval_definition)[source]

Update interval_definition to project configuration

Parameters:
  • interval_definition_name (str) – Name of the (original) entry
  • interval_definition (dict) – The updated interval_definition dict
update_narrative(narrative_name, narrative)[source]

Update narrative to project configuration

Parameters:
  • narrative_name (str) – Name of the (original) entry
  • narrative (dict) – The updated narrative dict
update_narrative_set(narrative_set_name, narrative_set)[source]

Update narrative_set to project configuration

Parameters:
  • narrative_set_name (str) – Name of the (original) entry
  • narrative_set (dict) – The updated narrative_set dict
update_region_definition(region_definition_name, region_definition)[source]

Update region_definition to project configuration

Parameters:
  • region_definition_name (str) – Name of the (original) entry
  • region_definition (dict) – The updated region_definition dict
update_scenario(scenario_name, scenario)[source]

Update scenario to project configuration

Parameters:
  • scenario_name (str) – Name of the (original) entry
  • scenario (dict) – The updated scenario dict
update_scenario_set(scenario_set_name, scenario_set)[source]

Update scenario_set to project configuration

Parameters:
  • scenario_set_name (str) – Name of the (original) entry
  • scenario_set (dict) – The updated scenario_set dict
update_sector_model(sector_model_name, sector_model)[source]

Update sector model in Yaml file

Parameters:
  • sector_model_name (str) – A sector_model name
  • sector_model (dict) – A sector_model dictionary
update_sos_model(sos_model_name, sos_model)[source]

Update system-of-system model in Yaml file

Parameters:
  • sos_model_name (str) – A sos_model name
  • sos_model (dict) – A sos_model dictionary
update_sos_model_run(sos_model_run_name, sos_model_run)[source]

Update system-of-system model run in Yaml file

Parameters:
  • sos_model_run_name (str) – A sos_model_run name
  • sos_model_run (dict) – A sos_model_run dictionary
write_interval_definition(interval_definition)[source]

Write interval_definition to project configuration

Parameters:interval_definition (dict) – A interval_definition dict
write_narrative(narrative)[source]

Write narrative to project configuration

Parameters:narrative (dict) – A narrative dict
write_narrative_set(narrative_set)[source]

Write narrative_set to project configuration

Parameters:narrative_set (dict) – A narrative_set dict
write_region_definition(region_definition)[source]

Write region_definition to project configuration

Parameters:region_definition (dict) – A region_definition dict
write_scenario(scenario)[source]

Write scenario to project configuration

Parameters:scenario (dict) – A scenario dict
write_scenario_set(scenario_set)[source]

Write scenario_set to project configuration

Parameters:scenario_set (dict) – A scenario_set dict
write_sector_model(sector_model)[source]

Write sector model to a Yaml file

Existing configuration will be overwritten without warning

Parameters:sector_model (dict) – A sector_model dictionary
write_sos_model(sos_model)[source]

Write system-of-system model to Yaml file

Existing configuration will be overwritten without warning

Parameters:sos_model (dict) – A sos_model dictionary
write_sos_model_run(sos_model_run)[source]

Write system-of-system model run to Yaml file

Parameters:sos_model_run (dict) – A sos_model_run dictionary