def test_subset_imask_non_iterable_subset(self, model_data, imask_subset_config): """ Subset using a string, when a non-string iterable is required """ foreach = ["nodes", "techs"] imask = _imask_foreach(model_data, foreach) with pytest.raises(TypeError) as excinfo: _subset_imask( "foo", AttrDict({"foreach": foreach, "subset.nodes": "bar"}), imask ) assert check_error_or_warning( excinfo, "set `foo` must subset over an iterable, instead got non-iterable `bar` for subset `nodes`", )
def test_generate_scenarios(self): runner = CliRunner() with runner.isolated_filesystem() as tempdir: out_file = os.path.join(tempdir, 'scenarios.yaml') result = runner.invoke(cli.generate_scenarios, [ _MODEL_NATIONAL, out_file, 'cold_fusion', 'run1;run2', 'group_share_cold_fusion_cap;group_share_cold_fusion_prod', ]) assert result.exit_code == 0 assert os.path.isfile(out_file) scenarios = AttrDict.from_yaml(out_file) assert 'scenario_0' not in scenarios['scenarios'] assert scenarios['scenarios'][ 'scenario_1'] == 'cold_fusion,run1,group_share_cold_fusion_cap'
def model_data(self): model_data = xr.Dataset( coords={ dim: ["foo", "bar"] if dim != "techs" else ["foo", "bar", "foobar", "foobaz"] for dim in BASE_DIMS }, data_vars={ "node_tech": ( ["nodes", "techs"], np.random.choice(a=[np.nan, True], size=(2, 4)), ), "carrier": ( ["carrier_tiers", "carriers", "techs"], np.random.choice(a=[np.nan, True], size=(2, 2, 4)), ), "with_inf": ( ["nodes", "techs"], [[1.0, np.nan, 1.0, 3], [np.inf, 2.0, True, np.nan]], ), "all_inf": (["nodes", "techs"], np.ones((2, 4)) * np.inf), "all_nan": (["nodes", "techs"], np.ones((2, 4)) * np.nan), "inheritance": ( ["nodes", "techs"], [ ["foo.bar", "boo", "baz", "boo"], ["bar", "ar", "baz.boo", "foo.boo"], ], ), }, ) UpdateObserverDict( initial_dict=AttrDict({"foo": True, "baz": {"bar": "foobar"}}), name="run_config", observer=model_data, ) UpdateObserverDict( initial_dict={"foz": 0}, name="model_config", observer=model_data ) return model_data
def generate_scenarios( model_file, out_file, overrides, scenario_name_prefix, debug, quiet, pdb): set_quietness_level(quiet) with format_exceptions(debug, pdb): combinations = list(itertools.product( *[i.split(';') for i in overrides] )) if not scenario_name_prefix: scenario_name_prefix = 'scenario_' # len(str(x)) gives us the number of digits in x, for padding scenario_string = '{}{:0>' + str(len(str(len(combinations)))) + 'd}' scenarios = {'scenarios': { scenario_string.format(scenario_name_prefix, i + 1): c for i, c in enumerate(combinations)}} AttrDict(scenarios).to_yaml(out_file)
def generate_scenarios(model_file, out_file, overrides, scenario_name_prefix, debug, quiet, pdb): _cli_start(debug, quiet) with format_exceptions(debug, pdb): combinations = list( itertools.product(*[i.split(";") for i in overrides])) if not scenario_name_prefix: scenario_name_prefix = "scenario_" # len(str(x)) gives us the number of digits in x, for padding scenario_string = "{}{:0>" + str(len(str(len(combinations)))) + "d}" scenarios = { "scenarios": { scenario_string.format(scenario_name_prefix, i + 1): c for i, c in enumerate(combinations) } } AttrDict(scenarios).to_yaml(out_file)
def test_generate_scenarios(self): runner = CliRunner() with runner.isolated_filesystem() as tempdir: out_file = os.path.join(tempdir, "scenarios.yaml") result = runner.invoke( cli.generate_scenarios, [ _MODEL_NATIONAL, out_file, "cold_fusion", "run1;run2", "cold_fusion_cap_share;cold_fusion_prod_share", ], ) assert result.exit_code == 0 assert os.path.isfile(out_file) scenarios = AttrDict.from_yaml(out_file) assert "scenario_0" not in scenarios["scenarios"] assert scenarios["scenarios"]["scenario_1"] == [ "cold_fusion", "run1", "cold_fusion_cap_share", ]
import os import calliope from calliope import AttrDict constraint_sets = AttrDict.from_yaml(os.path.join(os.path.dirname(__file__), 'constraint_sets.yaml')) _defaults_files = { k: os.path.join(os.path.dirname(calliope.__file__), 'config', k + '.yaml') for k in ['model', 'defaults'] } defaults = AttrDict.from_yaml(_defaults_files['defaults']) defaults_model = AttrDict.from_yaml(_defaults_files['model']) def build_test_model(override_dict=None, override_groups=None): this_path = os.path.dirname(__file__) model_location = os.path.join(this_path, 'test_model', 'model.yaml') override_location = os.path.join(this_path, 'test_model', 'overrides.yaml') if override_groups: override_file = override_location + ':' + override_groups else: override_file = None return calliope.Model( model_location, override_dict=override_dict, override_file=override_file )
import os import sys import pytest from pyomo.core.expr.current import identify_variables import calliope from calliope import AttrDict constraint_sets = AttrDict.from_yaml( os.path.join(os.path.dirname(__file__), "constraint_sets.yaml")) defaults = AttrDict.from_yaml( os.path.join(os.path.dirname(calliope.__file__), "config", "defaults.yaml")) python36_or_higher = pytest.mark.skipif( sys.version_info < (3, 6), reason="Requires ordered dicts from Python >= 3.6") def build_test_model(override_dict=None, scenario=None, model_file="model.yaml"): return calliope.Model( os.path.join(os.path.dirname(__file__), "test_model", model_file), override_dict=override_dict, scenario=scenario, )
import sys import ast import pytest from pyomo.core.expr.current import identify_variables import pyomo.core as po import calliope from calliope import AttrDict constraint_sets = { k: [ast.literal_eval(i) for i in v] for k, v in AttrDict.from_yaml( os.path.join(os.path.dirname(__file__), "constraint_sets.yaml") ) .as_dict_flat() .items() } defaults = AttrDict.from_yaml( os.path.join(os.path.dirname(calliope.__file__), "config", "defaults.yaml") ) subsets_config = AttrDict.from_yaml( os.path.join(os.path.dirname(calliope.__file__), "config", "subsets.yaml") ) python36_or_higher = pytest.mark.skipif( sys.version_info < (3, 6), reason="Requires ordered dicts from Python >= 3.6" )
import os import sys import pytest import calliope from calliope import AttrDict constraint_sets = AttrDict.from_yaml(os.path.join(os.path.dirname(__file__), 'constraint_sets.yaml')) _defaults_files = { k: os.path.join(os.path.dirname(calliope.__file__), 'config', k + '.yaml') for k in ['model', 'defaults'] } defaults = AttrDict.from_yaml(_defaults_files['defaults']) defaults_model = AttrDict.from_yaml(_defaults_files['model']) python36_or_higher = pytest.mark.skipif( sys.version_info < (3, 6), reason="Requires ordered dicts from Python >= 3.6" ) def build_test_model(override_dict=None, scenario=None, minimal=False): this_path = os.path.dirname(__file__) model = 'model_minimal.yaml' if minimal is True else 'model.yaml' model_location = os.path.join(this_path, 'test_model', model)
def parse_yaml(self, yaml_string): return AttrDict.from_yaml_string(yaml_string)