예제 #1
0
def test_new_subannual_timeseries_as_iamc(mp):
    mp.add_timeslice('Summer', 'Season', 1.0 / 4)
    scen = TimeSeries(mp, *test_args, version='new', annotation='fo')
    timeseries = DATA['timeseries'].pivot_table(values='value', index=IDX_COLS)
    scen.add_timeseries(timeseries)
    scen.commit('adding yearly data')

    # add subannual timeseries data
    ts_summer = timeseries.copy()
    ts_summer['subannual'] = 'Summer'
    scen.check_out()
    scen.add_timeseries(ts_summer)
    scen.commit('adding subannual data')

    # generate expected dataframe+
    ts_year = timeseries.copy()
    ts_year['subannual'] = 'Year'
    exp = pd.concat([ts_year, ts_summer]).reset_index()
    exp['model'] = 'Douglas Adams'
    exp['scenario'] = 'Hitchhiker'

    # compare returned dataframe - default behaviour set to 'auto'
    assert_timeseries(scen, exp=exp[COLS_WITH_SUBANNUAL],
                      cols=COLS_WITH_SUBANNUAL)
    # test behaviour of 'auto' explicitly
    assert_timeseries(scen, exp=exp[COLS_WITH_SUBANNUAL],
                      cols=COLS_WITH_SUBANNUAL, subannual='auto')
    # test behaviour of 'True' explicitly
    assert_timeseries(scen, exp=exp[COLS_WITH_SUBANNUAL],
                      cols=COLS_WITH_SUBANNUAL, subannual=True)
    # setting False raises an error because subannual data exists
    pytest.raises(ValueError, scen.timeseries, subannual=False)
예제 #2
0
def test_remove_multiple_geodata(mp):
    scen = TimeSeries(mp, *test_args, version='new', annotation='fo')
    scen.add_geodata(DATA['geo'])
    row = DATA['geo'].loc[[False, True, True]]
    scen.remove_geodata(row)
    scen.commit('adding geodata (references to map layers)')
    assert_geodata(scen.get_geodata(), DATA['geo'].loc[[True, False, False]])
예제 #3
0
def test_remove_multiple_geodata(mp):
    scen = TimeSeries(mp, *test_args, version="new", annotation="fo")
    scen.add_geodata(DATA["geo"])
    row = DATA["geo"].loc[[False, True, True]]
    scen.remove_geodata(row)
    scen.commit("adding geodata (references to map layers)")
    assert_geodata(scen.get_geodata(), DATA["geo"].loc[[True, False, False]])
예제 #4
0
def test_timeseries_edit(mp):
    scen = TimeSeries(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[IDX_COLS], obs[IDX_COLS])
    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)')
    mp.close_db()

    mp.open_db()
    scen = TimeSeries(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[IDX_COLS], obs[IDX_COLS])
    npt.assert_array_almost_equal(df['value'], obs['value'])
예제 #5
0
파일: data.py 프로젝트: iiasa/ixmp
def populate_test_platform(platform):
    """Populate `platform` with data for testing.

    Many of the tests in :mod:`ixmp.tests.core` depend on this set of data.

    The data consist of:

    - 3 versions of the Dantzig cannery/transport Scenario.

      - Version 2 is the default.
      - All have :obj:`HIST_DF` and :obj:`TS_DF` as time-series data.

    - 1 version of a TimeSeries with model name 'Douglas Adams' and scenario
      name 'Hitchhiker', containing 2 values.
    """
    s1 = make_dantzig(platform, solve=True, quiet=True)

    s2 = s1.clone()
    s2.set_as_default()

    s2.clone()

    s4 = TimeSeries(platform, **models["h2g2"], version="new")
    s4.add_timeseries(
        pd.DataFrame.from_dict(
            dict(
                region="World",
                variable="Testing",
                unit="???",
                year=[2010, 2020],
                value=[23.7, 23.8],
            )))
    s4.commit("")
    s4.set_as_default()
예제 #6
0
    def test_new_timeseries_as_iamc(self, test_mp):
        # TODO rescue use of subannual= here

        scen = TimeSeries(test_mp,
                          *models["h2g2"],
                          version="new",
                          annotation="fo")
        scen.add_timeseries(DATA["timeseries"].pivot_table(values="value",
                                                           index=IDX_COLS))
        scen.commit("importing a testing timeseries")

        # compare returned dataframe - default behaviour set to 'auto'
        assert_timeseries(scen)
        # test behaviour of 'auto' explicitly
        assert_timeseries(scen, subannual="auto")
        # test behaviour of 'False' explicitly
        assert_timeseries(scen, subannual=False)

        # test behaviour of 'True' explicitly
        exp = (DATA["timeseries"].pivot_table(values="value",
                                              index=IDX_COLS).reset_index())
        exp["model"] = "Douglas Adams"
        exp["scenario"] = "Hitchhiker"
        exp["subannual"] = "Year"
        assert_timeseries(scen,
                          exp=exp,
                          cols=COLS_WITH_SUBANNUAL,
                          subannual=True)
예제 #7
0
def test_timeseries_edit(mp):
    scen = TimeSeries(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[IDX_COLS], obs[IDX_COLS])
    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)")
    mp.close_db()

    mp.open_db()
    scen = TimeSeries(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[IDX_COLS], obs[IDX_COLS])
    npt.assert_array_almost_equal(df["value"], obs["value"])
예제 #8
0
def test_new_timeseries_as_iamc(test_mp):
    scen = TimeSeries(test_mp, *test_args, version='new', annotation='fo')
    scen.add_timeseries(DATA['timeseries'].pivot_table(values='value',
                                                       index=IDX_COLS))
    scen.commit('importing a testing timeseries')

    # compare returned dataframe - default behaviour set to 'auto'
    assert_timeseries(scen)
    # test behaviour of 'auto' explicitly
    assert_timeseries(scen, subannual='auto')
    # test behaviour of 'False' explicitly
    assert_timeseries(scen, subannual=False)

    # test behaviour of 'True' explicitly
    exp = (DATA['timeseries'].pivot_table(values='value',
                                          index=IDX_COLS).reset_index())
    exp['model'] = 'Douglas Adams'
    exp['scenario'] = 'Hitchhiker'
    exp['subannual'] = 'Year'
    assert_timeseries(scen, exp=exp, cols=COLS_WITH_SUBANNUAL, subannual=True)
예제 #9
0
def test_default(mp, ts):
    # NB this is required before the is_default method can be used
    # FIXME should return False regardless
    ts.commit('')

    # Temporary TimeSeries is has not been set_as_default
    assert not ts.is_default()

    ts.set_as_default()
    assert ts.is_default()

    # NB TimeSeries cannot be cloned, so create a new one with the same
    #    name
    ts2 = TimeSeries(mp, ts.model, ts.scenario, version='new')
    ts2.commit('')
    ts2.set_as_default()

    assert ts2.is_default()
    # Original TimeSeries is no longer default
    assert not ts.is_default()
예제 #10
0
    def test_new_subannual_timeseries_as_iamc(self, mp):
        mp.add_timeslice("Summer", "Season", 1.0 / 4)
        scen = TimeSeries(mp, *models["h2g2"], version="new", annotation="fo")
        timeseries = DATA["timeseries"].pivot_table(values="value",
                                                    index=IDX_COLS)
        scen.add_timeseries(timeseries)
        scen.commit("adding yearly data")

        # add subannual timeseries data
        ts_summer = timeseries.copy()
        ts_summer["subannual"] = "Summer"
        scen.check_out()
        scen.add_timeseries(ts_summer)
        scen.commit("adding subannual data")

        # generate expected dataframe+
        ts_year = timeseries.copy()
        ts_year["subannual"] = "Year"
        exp = pd.concat([ts_year, ts_summer]).reset_index()
        exp["model"] = "Douglas Adams"
        exp["scenario"] = "Hitchhiker"

        # compare returned dataframe - default behaviour set to 'auto'
        assert_timeseries(scen,
                          exp=exp[COLS_WITH_SUBANNUAL],
                          cols=COLS_WITH_SUBANNUAL)
        # test behaviour of 'auto' explicitly
        assert_timeseries(
            scen,
            exp=exp[COLS_WITH_SUBANNUAL],
            cols=COLS_WITH_SUBANNUAL,
            subannual="auto",
        )
        # test behaviour of 'True' explicitly
        assert_timeseries(scen,
                          exp=exp[COLS_WITH_SUBANNUAL],
                          cols=COLS_WITH_SUBANNUAL,
                          subannual=True)
        # setting False raises an error because subannual data exists
        pytest.raises(ValueError, scen.timeseries, subannual=False)
예제 #11
0
def test_new_timeseries_as_year_value(test_mp):
    scen = TimeSeries(test_mp, *test_args, version='new', annotation='fo')
    scen.add_timeseries(DATA['timeseries'])
    scen.commit('importing a testing timeseries')
    assert_timeseries(scen)
예제 #12
0
def prepare_scenario(mp, args_all):
    scen = TimeSeries(mp, *args_all, version='new', annotation='nk')
    scen.add_timeseries(DATA[2010])
    scen.commit('updating timeseries in IAMC format')
    scen = TimeSeries(mp, *args_all)
    return scen
예제 #13
0
def test_new_timeseries_as_year_value(test_mp):
    scen = TimeSeries(test_mp, *test_args, version="new", annotation="fo")
    scen.add_timeseries(DATA["timeseries"])
    scen.commit("importing a testing timeseries")
    assert_timeseries(scen)