def test_timeseries_edit(test_mp): scen = ixmp.TimeSeries(test_mp, *test_args) df = {'region': ['World'] * 2, 'variable': ['Testing'] * 2, 'unit': ['???', '???'], 'year': [2010, 2020], 'value': [23.7, 23.8]} exp = pd.DataFrame.from_dict(df) obs = scen.timeseries() npt.assert_array_equal(exp[cols_str], obs[cols_str]) npt.assert_array_almost_equal(exp['value'], obs['value']) scen.check_out(timeseries_only=True) df = {'region': ['World'] * 2, 'variable': ['Testing'] * 2, 'unit': ['???', '???'], 'year': [2010, 2020], 'value': [23.7, 23.8]} df = pd.DataFrame.from_dict(df) scen.add_timeseries(df) scen.commit('testing of editing timeseries (same years)') scen.check_out(timeseries_only=True) df = {'region': ['World'] * 3, 'variable': ['Testing', 'Testing', 'Testing2'], 'unit': ['???', '???', '???'], 'year': [2020, 2030, 2030], 'value': [24.8, 24.9, 25.1]} df = pd.DataFrame.from_dict(df) scen.add_timeseries(df) scen.commit('testing of editing timeseries (other years)') test_mp.close_db() test_mp.open_db() scen = ixmp.TimeSeries(test_mp, *test_args) obs = scen.timeseries().sort_values(by=['year']) df = df.append(exp.loc[0]).sort_values(by=['year']) npt.assert_array_equal(df[cols_str], obs[cols_str]) npt.assert_array_almost_equal(df['value'], obs['value'])
def test_timeseries_edit_with_region_synonyms(test_mp): args_all = ('Douglas Adams 1', 'test_remove_all') test_mp.set_log_level('DEBUG') test_mp.add_region_synonym('Hell', 'World') scen = ixmp.TimeSeries(test_mp, *args_all, version='new', annotation='nk') df = pd.DataFrame.from_dict({ 'region': ['World'], 'variable': ['Testing'], 'unit': ['???'], '2010': [23.7], '2020': [23.8] }) scen.add_timeseries(df) scen.commit('updating timeseries in IAMC format') scen = ixmp.TimeSeries(test_mp, *args_all) obs = scen.timeseries() exp = pd.DataFrame.from_dict({ 'region': ['World'] * 2, 'variable': ['Testing'] * 2, 'unit': ['???', '???'], 'year': [2010, 2020], 'value': [23.7, 23.8] }) npt.assert_array_equal(exp[cols_str], obs[cols_str]) npt.assert_array_almost_equal(exp['value'], obs['value']) scen.check_out(timeseries_only=True) df = pd.DataFrame.from_dict({ 'region': ['Hell'], 'variable': ['Testing'], 'unit': ['???'], '2000': [21.7], '2010': [22.7], '2020': [23.7], '2030': [24.7], '2040': [25.7], '2050': [25.8] }) scen.preload_timeseries() scen.add_timeseries(df) scen.commit('updating timeseries in IAMC format') exp = pd.DataFrame.from_dict({ 'region': ['World'] * 6, 'variable': ['Testing'] * 6, 'unit': ['???'] * 6, 'year': [2000, 2010, 2020, 2030, 2040, 2050], 'value': [21.7, 22.7, 23.7, 24.7, 25.7, 25.8] }) obs = scen.timeseries() npt.assert_array_equal(exp[cols_str], obs[cols_str]) npt.assert_array_almost_equal(exp['value'], obs['value']) test_mp.close_db()
def test_new_timeseries_error(test_mp): scen = ixmp.TimeSeries(test_mp, *test_args, version='new', annotation='fo') df = {'year': [2010, 2020], 'value': [23.5, 23.6]} df = pd.DataFrame.from_dict(df) df['region'] = 'World' df['variable'] = 'Testing' pytest.raises(ValueError, scen.add_timeseries, df)
def test_export_ts_of_all_runs(mp, tmp_path): """Export timeseries of all runs.""" path = tmp_path / "export.csv" # Add a new version of a run ts = ixmp.TimeSeries(mp, **models["h2g2"], version="new", annotation="fo") ts.add_timeseries(DATA[0]) ts.commit("create a new version") ts.set_as_default() # Export all default model+scenario runs mp.export_timeseries_data(path, unit="???", region="World", default=True, export_all_runs=True) obs = pd.read_csv(path, index_col=False, header=0) exp = (DATA[0].assign(**models["h2g2"], version=2, subannual="Year", meta=0).rename(columns=lambda c: c.upper()).reindex( columns=FIELDS["write_file"])) assert_frame_equal(exp, obs) # Export all model+scenario run versions (including non-default) mp.export_timeseries_data(path, unit="???", region="World", default=False, export_all_runs=True) obs = pd.read_csv(path, index_col=False, header=0) assert 4 == len(obs)
def test_remove_multiple_geodata(test_mp): scen = ixmp.TimeSeries(test_mp, *test_args, version='new', annotation='fo') scen.add_geodata(GEODATA) row = GEODATA.loc[[False, True, True]] scen.remove_geodata(row) scen.commit('adding geodata (references to map layers)') assert_geodata(scen.get_geodata(), GEODATA.loc[[True, False, False]])
def test_get_timeseries(test_mp): scen = ixmp.TimeSeries(test_mp, *test_args, version=2) obs = scen.timeseries(regions='World', variables='Testing', units='???', years=2020) df = {'region': ['World'], 'variable': ['Testing'], 'unit': ['???'], 'year': [2020], 'value': [23.6]} exp = pd.DataFrame.from_dict(df) npt.assert_array_equal(exp[cols_str], obs[cols_str]) npt.assert_array_almost_equal(exp['value'], obs['value'])
def test_new_timeseries(test_mp): scen = ixmp.TimeSeries(test_mp, *test_args, version='new', annotation='fo') df = {'year': [2010, 2020], 'value': [23.5, 23.6]} df = pd.DataFrame.from_dict(df) df['region'] = 'World' df['variable'] = 'Testing' df['unit'] = '???' scen.add_timeseries(df) scen.commit('importing a testing timeseries')
def test_get_timeseries_iamc(test_mp): scen = ixmp.TimeSeries(test_mp, *test_args) obs = scen.timeseries(iamc=True, regions='World', variables='Testing') exp = TS_DF.pivot_table(index=['region', 'variable', 'unit'], columns='year')['value'].reset_index() exp['model'] = 'Douglas Adams' exp['scenario'] = 'Hitchhiker' npt.assert_array_equal(exp[iamc_idx_cols], obs[iamc_idx_cols]) npt.assert_array_almost_equal(exp[2010], obs[2010])
def test_resource_limit(resource_limit, test_mp): """Exercise :func:`memory_usage` and :func:`resource_limit`.""" # TODO expand to cover other missed lines in those functions memory_usage("setup") s = ixmp.TimeSeries(test_mp, **models["h2g2"], version="new") memory_usage("1 TimeSeries") del s
def mp(test_mp_f): """A test Platform. The platform contains one time series with the "dantizg" model & scenario name from :data:`testing.models`. Unlike the other submodules of :mod:`ixmp.tests.core`, the tests in this file generally require a clean platform each time, so this is a function-scoped fixture. """ # Don't call populate_test_platform(), since this is all that's needed ts = ixmp.TimeSeries(test_mp_f, **models["dantzig"], version="new") ts.commit("") yield test_mp_f
def test_get_timeseries_iamc(test_mp): scen = ixmp.TimeSeries(test_mp, *test_args, version=2) obs = scen.timeseries(iamc=True, regions='World', variables='Testing') df = {'year': [2010, 2020], 'value': [23.5, 23.6]} df = pd.DataFrame.from_dict(df) df['model'] = 'Douglas Adams' df['scenario'] = 'Hitchhiker' df['region'] = 'World' df['variable'] = 'Testing' df['unit'] = '???' df = df.pivot_table(index=iamc_idx_cols, columns='year')['value'] df.reset_index(inplace=True) exp = pd.DataFrame.from_dict(df) npt.assert_array_equal(exp[iamc_idx_cols], obs[iamc_idx_cols]) npt.assert_array_almost_equal(exp[2010], obs[2010])
def test_export_ts_of_all_runs(mp, tmp_path): """Export timeseries of all runs.""" path = tmp_path / "export.csv" # Add a new version of a run scen = ixmp.TimeSeries(mp, "Douglas Adams", "Hitchhiker", version="new", annotation="fo") timeseries = pd.DataFrame.from_dict( dict( region="World", variable="Testing", unit="???", year=[2010, 2020], value=[24.7, 24.8], )) scen.add_timeseries(timeseries) scen.commit("create a new version") scen.set_as_default() columns = [ "model", "scenario", "version", "variable", "unit", "region", "meta", "subannual", "year", "value", ] csv_dtype = dict( model=str, scenario=str, version=int, variable=str, unit=str, region=str, year=int, subannual=str, meta=int, value=float, ) # Export all default model+scenario runs mp.export_timeseries_data(path, unit="???", region="World", default=True, export_all_runs=True) obs = pd.read_csv(path, index_col=False, header=0, names=columns, dtype=csv_dtype) assert len(obs.model) == 2 assert pd.Series.all(obs.value == timeseries.value) # Export all model+scenario run versions (including non-default) mp.export_timeseries_data(path, unit="???", region="World", default=False, export_all_runs=True) obs = pd.read_csv(path, index_col=False, header=0, names=columns, dtype=csv_dtype) assert len(obs.model) == 4
def test_add_geodata(test_mp): scen = ixmp.TimeSeries(test_mp, *test_args, version='new', annotation='fo') scen.add_geodata(GEODATA) scen.commit('adding geodata (references to map layers)') assert_geodata(scen.get_geodata(), GEODATA)
def test_fetch_empty_geodata(test_mp): scen = ixmp.TimeSeries(test_mp, *test_args, version='new', annotation='fo') empty = scen.get_geodata() assert_geodata(empty, GEODATA.loc[[False, False, False]])
def test_new_timeseries_as_iamc(test_mp): scen = ixmp.TimeSeries(test_mp, *test_args, version='new', annotation='fo') scen.add_timeseries(TS_DF.pivot_table(values='value', index=cols_str)) scen.commit('importing a testing timeseries') assert_timeseries(scen)
def test_new_timeseries_as_year_value(test_mp): scen = ixmp.TimeSeries(test_mp, *test_args, version='new', annotation='fo') scen.add_timeseries(TS_DF) scen.commit('importing a testing timeseries') assert_timeseries(scen)
os.path.join(db_dir, 'ixmptest.lobs'), os.path.join(db_dir, 'ixmptest.properties'), os.path.join(db_dir, 'ixmptest.script') ]: if os.path.isfile(fname): os.remove(fname) # %% launch the modeling platform instance, creating a new test database file mp = ixmp.Platform(dbprops=test_db, dbtype='HSQLDB') # %% initialize a new timeseries instance ts = ixmp.TimeSeries(mp, 'Douglas Adams', 'Hitchhiker', version='new', annotation='testing') df = { 'region': ['World', 'World'], 'variable': ['Testing', 'Testing'], 'unit': ['???', '???'], 'year': [2010, 2020], 'value': [23.7, 23.8] } df = pd.DataFrame.from_dict(df) ts.add_timeseries(df) ts.commit('importing a testing timeseries') ts.set_as_default() # %% initialize the standard canning problem
def test_get_timeseries(test_mp): scen = ixmp.TimeSeries(test_mp, *test_args) assert_timeseries(scen)