示例#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_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'])
示例#3
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"])
示例#4
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)