Exemple #1
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'])
Exemple #2
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)
Exemple #3
0
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()
Exemple #4
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)
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"])
Exemple #6
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)
Exemple #7
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)
Exemple #8
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)
Exemple #9
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
Exemple #10
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)