def test_apply_spec1(caplog, scenario, spec): """Add data using the data= argument.""" def add_data_func(scenario, dry_run): return dict(demand=make_df( "demand", commodity="cases", level="consumption", node="chicago", time="year", unit="case", value=301.0, year=1963, )) apply_spec(scenario, spec, data=add_data_func, dry_run=True) # Messages are logged about additions assert "1 rows in 'demand'" in caplog.messages, caplog.messages # …but no change because `dry_run` assert not any(301.0 == scenario.par("demand")["value"]) caplog.clear() apply_spec(scenario, spec, data=add_data_func) # Messages are logged about additions assert "1 rows in 'demand'" in caplog.messages, caplog.messages # Value was actually changed assert 1 == sum(301.0 == scenario.par("demand")["value"])
def create_res(context, quiet=True): """Create a 'bare' MESSAGEix-GLOBIOM reference energy system (RES). Parameters ---------- context : .Context :attr:`.Context.scenario_info` determines the model name and scenario name of the created Scenario. If not provided, the defaults are: - Model name generated by :func:`name`. - Scenario name "baseline". quiet : bool, optional Passed to `quiet` argument of :func:`.build.apply_spec`. Returns ------- message_ix.Scenario A scenario as described by :func:`.bare.get_spec`, prepared using :func:`.apply_spec`. """ mp = context.get_platform() # Retrieve the spec; this also sets defaults expected by name() spec = get_spec(context) # Model and scenario name for the RES args = dict( mp=mp, model=context.scenario_info.get("model", name(context)), scenario=context.scenario_info.get("scenario", "baseline"), version="new", ) # TODO move this to ixmp as a method similar to ixmp.util.parse_url() url = urlunsplit(("ixmp", mp.name, args["model"] + "/" + args["scenario"], "", args["version"])) log.info(f"Create {repr(url)}") # Create the Scenario scenario = message_ix.Scenario(**args) # TODO move to message_ix scenario.init_par("MERtoPPP", ["node", "year"]) # Apply the spec apply_spec( scenario, spec, data=partial(get_data, context=context, spec=spec), quiet=quiet, message= f"Create using message-ix-models {message_ix_models.__version__}", ) return scenario
def test_apply_spec2(caplog, scenario, spec): """Remove an element, with fast=True.""" spec["remove"].set["node"] = ["new-york"] apply_spec(scenario, spec, fast=True) # Messages are logged about removals assert all(msg in caplog.messages for msg in ( " Skip removing 'new-york' and associated parameter elements (fast=True)", "0 parameter elements removed", )), caplog.messages
def test_apply_spec0(caplog, scenario, spec): """Require missing element raises ValueError.""" spec["require"].set["node"].append("vienna") with pytest.raises(ValueError): apply_spec(scenario, spec) assert ( "message_ix_models.model.build", logging.ERROR, " 1 elements not found: ['vienna']", ) in caplog.record_tuples
def test_apply_spec3(caplog, scenario, spec): """Actually remove data.""" spec["remove"].set["node"] = ["new-york"] apply_spec(scenario, spec) # Messages are logged about removals assert all(msg in caplog.messages for msg in ( " Remove 'new-york' and associated parameter elements", "Remove 1 rows in 'demand'", "Remove 2 rows in 'output'", "3 parameter elements removed", )), caplog.messages
def add( scenario: message_ix.Scenario, groups: Sequence[Code], technologies: Sequence[Code], template: Code, **options, ) -> None: """Add disutility formulation to `scenario`.""" # Generate the spec given the configuration options spec = get_spec(groups, technologies, template) # Apply spec and add data apply_spec(scenario, spec, partial(get_data, spec=spec), **options)