def test_testing_make_scenario(test_mp): """Check the model creation functions in message_ix.testing.""" # MESSAGE-scheme Dantzig problem can be created scen = make_dantzig(test_mp, True) assert isinstance(scen, Scenario) # Multi-year variant can be created scen = make_dantzig(test_mp, solve=True, multi_year=True) assert isinstance(scen, Scenario) # Westeros model can be created scen = make_westeros(test_mp, solve=True) assert isinstance(scen, Scenario)
def test_cli_export_test_data(monkeypatch, session_context, mix_models_cli, tmp_path): """The :command:`export-test-data` command can be invoked.""" # Create an empty scenario in the temporary local file database platform = "local" mp = ixmp.Platform(platform) scen = make_dantzig(mp) # URL url = f"ixmp://{platform}/{scen.model}/{scen.scenario}#{scen.version}" # Monkeypatch MESSAGE_DATA_PATH in case tests are being performed on a system # without message_data installed monkeypatch.setattr(util.common, "MESSAGE_DATA_PATH", tmp_path) tmp_path.joinpath("data", "tests").mkdir(exist_ok=True, parents=True) # File that will be created technology = ["coal_ppl"] dest_file = util.private_data_path( "tests", f"{scen.model}_{scen.scenario}_{'_'.join(technology)}.xlsx") # Release the database lock mp.close_db() try: # Export works result = mix_models_cli.assert_exit_0( [f"--url={url}", "export-test-data"]) # The file is created in the expected location assert str(dest_file) in result.output assert dest_file.exists() finally: # Remove this temporary file dest_file.unlink()
def test_run_clone(tmpdir): # this test is designed to cover the full functionality of the GAMS API # - initialize a new ixmp platform instance # - create a new scenario based on Dantzigs tutorial transport model # - solve the model and read back the solution from the output # - perform tests on the objective value and the timeseries data mp = Platform(driver='hsqldb', path=tmpdir / 'db') scen = make_dantzig(mp, solve=True) assert np.isclose(scen.var('OBJ')['lvl'], 153.675) assert scen.firstmodelyear == 1963 pdt.assert_frame_equal(scen.timeseries(iamc=True), TS_DF) # cloning with `keep_solution=True` keeps all timeseries and the solution # (same behaviour as `ixmp.Scenario`) scen2 = scen.clone(keep_solution=True) assert np.isclose(scen2.var('OBJ')['lvl'], 153.675) assert scen2.firstmodelyear == 1963 pdt.assert_frame_equal(scen2.timeseries(iamc=True), TS_DF) # cloning with `keep_solution=False` drops the solution and only keeps # timeseries set as `meta=True` or prior to the first model year # (DIFFERENT behaviour from `ixmp.Scenario`) scen3 = scen.clone(keep_solution=False) assert np.isnan(scen3.var('OBJ')['lvl']) assert scen3.firstmodelyear == 1963 pdt.assert_frame_equal(scen3.timeseries(iamc=True), TS_DF_CLEARED)
def test_reporter_from_dantzig(test_mp): scen = make_dantzig(test_mp, solve=True) # Reporter.from_scenario can handle Dantzig example model rep = Reporter.from_scenario(scen) # Default target can be calculated rep.get("all")
def test_year_int(test_mp): scen = make_dantzig(test_mp, solve=True, multi_year=True) # Dimensions indexed by 'year' are returned as integers for all item types assert scen.set("cat_year").dtypes["year"] == "int" assert scen.par("demand").dtypes["year"] == "int" assert scen.par("bound_activity_up").dtypes["year_act"] == "int" assert scen.var("ACT").dtypes["year_vtg"] == "int" assert scen.equ("COMMODITY_BALANCE_GT").dtypes["year"] == "int"
def test_year_int(test_mp): scen = make_dantzig(test_mp, solve=True, multi_year=True) # Dimensions indexed by 'year' are returned as integers for all item types assert scen.set('cat_year').dtypes['year'] == 'int' assert scen.par('demand').dtypes['year'] == 'int' assert scen.par('bound_activity_up').dtypes['year_act'] == 'int' assert scen.var('ACT').dtypes['year_vtg'] == 'int' assert scen.equ('COMMODITY_BALANCE_GT').dtypes['year'] == 'int'
def test_shift_first_model_year(test_mp): scen = make_dantzig(test_mp, solve=True, multi_year=True) # assert that `historical_activity` is empty in the source scenario assert scen.par('historical_activity').empty # clone and shift first model year clone = scen.clone(shift_first_model_year=1964) # check that solution and timeseries in new model horizon have been removed assert np.isnan(clone.var('OBJ')['lvl']) pdt.assert_frame_equal(clone.timeseries(iamc=True), TS_DF_SHIFT) assert clone.firstmodelyear == 1964 # check that the variable `ACT` is now the parameter `historical_activity` assert not clone.par('historical_activity').empty
def test_run_remove_solution(test_mp): # create a new instance of the transport problem and solve it scen = make_dantzig(test_mp, solve=True) assert np.isclose(scen.var('OBJ')['lvl'], 153.675) # check that re-solving the model will raise an error if a solution exists pytest.raises(ValueError, scen.solve) # check that removing solution with a first-model-year arg raises an error # (DIFFERENT behaviour from `ixmp.Scenario`) pytest.raises(TypeError, scen.remove_solution, first_model_year=1964) # check that removing solution does not delete timeseries data # before first model year (DIFFERENT behaviour from `ixmp.Scenario`) scen.remove_solution() assert scen.firstmodelyear == 1963 pdt.assert_frame_equal(scen.timeseries(iamc=True), TS_DF_CLEARED)
def test_shift_first_model_year(test_mp): scen = make_dantzig(test_mp, solve=True, multi_year=True) # assert that `historical_activity` is empty in the source scenario assert scen.par("historical_activity").empty # clone and shift first model year clone = scen.clone(shift_first_model_year=1964) exp = TS_DF.copy() exp.loc[0, 1964] = np.nan exp["scenario"] = "multi-year" # check that solution and timeseries in new model horizon have been removed assert np.isnan(clone.var("OBJ")["lvl"]) assert_frame_equal(exp, clone.timeseries(iamc=True)) assert clone.firstmodelyear == 1964 # check that the variable `ACT` is now the parameter `historical_activity` assert not clone.par("historical_activity").empty
def test_multi_db_run(tmpdir): # create a new instance of the transport problem and solve it mp1 = Platform(driver="hsqldb", path=tmpdir / "mp1") scen1 = make_dantzig(mp1, solve=True) mp2 = Platform(driver="hsqldb", path=tmpdir / "mp2") # add other unit to make sure that the mapping is correct during clone mp2.add_unit("wrong_unit") mp2.add_region("wrong_region", "country") # check that cloning across platforms must copy the full solution dest = dict(platform=mp2) pytest.raises(NotImplementedError, scen1.clone, keep_solution=False, **dest) pytest.raises(NotImplementedError, scen1.clone, shift_first_model_year=1964, **dest) # clone solved model across platforms (with default settings) scen1.clone(platform=mp2, keep_solution=True) # close the db to ensure that data and solution of the clone are saved mp2.close_db() del mp2 # reopen the connection to the second platform and reload scenario _mp2 = Platform(driver="hsqldb", path=tmpdir / "mp2") scen2 = Scenario(_mp2, **SCENARIO["dantzig"]) assert_multi_db(mp1, _mp2) # check that sets, variables and parameter were copied correctly npt.assert_array_equal(scen1.set("node"), scen2.set("node")) scen2.firstmodelyear == 1963 assert_frame_equal(scen1.par("var_cost"), scen2.par("var_cost")) assert np.isclose(scen2.var("OBJ")["lvl"], 153.675) assert_frame_equal(scen1.var("ACT"), scen2.var("ACT")) # check that custom unit, region and timeseries are migrated correctly assert_frame_equal(scen2.timeseries(iamc=True), TS_DF)
def test_from_scenario(self, test_context): """ScenarioInfo initialized from an existing Scenario.""" mp = test_context.get_platform() scenario = make_dantzig(mp, multi_year=True) # ScenarioInfo can be initialized from the scenario info = ScenarioInfo(scenario) # Shorthand properties assert_frame_equal( pd.DataFrame( [ [1962, 1963], [1962, 1964], [1962, 1965], [1963, 1963], [1963, 1964], [1963, 1965], [1964, 1964], [1964, 1965], [1965, 1965], ], columns=["year_vtg", "year_act"], ), info.yv_ya, ) assert [ "World", "seattle", "san-diego", "new-york", "chicago", "topeka", ] == info.N assert 1963 == info.y0 assert [1963, 1964, 1965] == info.Y
def test_multi_db_run(tmpdir): # create a new instance of the transport problem and solve it mp1 = Platform(tmpdir / 'mp1', dbtype='HSQLDB') scen1 = make_dantzig(mp1, solve=True) mp2 = Platform(tmpdir / 'mp2', dbtype='HSQLDB') # add other unit to make sure that the mapping is correct during clone mp2.add_unit('wrong_unit') mp2.add_region('wrong_region', 'country') # check that cloning across platforms must copy the full solution dest = dict(platform=mp2) pytest.raises(ValueError, scen1.clone, keep_solution=False, **dest) pytest.raises(ValueError, scen1.clone, shift_first_model_year=1964, **dest) # clone solved model across platforms (with default settings) scen1.clone(platform=mp2, keep_solution=True) # close the db to ensure that data and solution of the clone are saved mp2.close_db() del mp2 # reopen the connection to the second platform and reload scenario _mp2 = Platform(tmpdir / 'mp2', dbtype='HSQLDB') scen2 = Scenario(_mp2, **models['dantzig']) assert_multi_db(mp1, _mp2) # check that sets, variables and parameter were copied correctly npt.assert_array_equal(scen1.set('node'), scen2.set('node')) scen2.firstmodelyear == 1963 pdt.assert_frame_equal(scen1.par('var_cost'), scen2.par('var_cost')) assert np.isclose(scen2.var('OBJ')['lvl'], 153.675) pdt.assert_frame_equal(scen1.var('ACT'), scen2.var('ACT')) # check that custom unit, region and timeseries are migrated correctly pdt.assert_frame_equal(scen2.timeseries(iamc=True), TS_DF)
def message_test_mp(test_mp): make_dantzig(test_mp) make_dantzig(test_mp, multi_year=True) yield test_mp
def scenario(test_context): mp = test_context.get_platform() yield make_dantzig(mp)