Exemplo n.º 1
0
def test_calc_data_missing_datapoint(westeros_solved):
    s = westeros_solved
    data = pd.read_excel(W_DATA_PATH, sheet_name=None, engine="openpyxl")
    # skip first data point
    data["gdp_calibrate"] = data["gdp_calibrate"][1:]
    c = macro.Calculate(s, data)
    pytest.raises(ValueError, c.read_data)
Exemplo n.º 2
0
def test_calc_price_zero(westeros_solved):
    s = westeros_solved
    clone = s.clone(scenario="low_demand", keep_solution=False)
    clone.check_out()
    # Lowering demand in the first year
    clone.add_par("demand", ["Westeros", "light", "useful", 700, "year"], 10,
                  "GWa")
    # Making investment and var cost zero for delivering light
    # TODO: these units are based on testing.make_westeros: needs improvement
    clone.add_par("inv_cost", ["Westeros", "bulb", 700], 0, "USD/GWa")
    for y in [690, 700]:
        clone.add_par("var_cost",
                      ["Westeros", "grid", y, 700, "standard", "year"], 0,
                      "USD/GWa")

    clone.commit("demand reduced and zero cost for bulb")
    clone.solve()
    price = clone.var("PRICE_COMMODITY")
    # Assert if there is no zero price (to make sure MACRO receives 0 price)
    assert np.isclose(0, price["lvl"]).any()
    c = macro.Calculate(clone, W_DATA_PATH)
    c.read_data()
    try:
        c._price()
    except RuntimeError as err:
        # To make sure the right error message is raised in macro.py
        assert "0-price found in MESSAGE variable PRICE_" in str(err)
    else:
        raise Exception("No error in macro.read_data() for zero price(s)")
Exemplo n.º 3
0
def test_calc_data_missing_column(westeros_solved):
    s = westeros_solved
    data = pd.read_excel(W_DATA_PATH, sheet_name=None)
    # skip first data point
    data['gdp_calibrate'] = data['gdp_calibrate'].drop('year', axis=1)
    c = macro.Calculate(s, data)
    pytest.raises(ValueError, c.read_data)
Exemplo n.º 4
0
def test_multiregion_derive_data():
    s = MockScenario()
    c = macro.Calculate(s, MR_DATA_PATH)
    c.read_data()
    c.derive_data()

    nodes = ["R11_AFR", "R11_CPA"]
    sectors = ["i_therm", "rc_spec"]

    # make sure no extraneous data is there
    check = c.data["demand"].reset_index()
    assert (check["node"].unique() == nodes).all()
    assert (check["sector"].unique() == sectors).all()

    obs = c.data["aconst"]
    exp = pd.Series(
        [3.74767687, 0.00285472], name="value", index=pd.Index(nodes, name="node")
    )
    pd.testing.assert_series_equal(obs, exp)

    obs = c.data["bconst"]
    idx = pd.MultiIndex.from_product([nodes, sectors], names=["node", "sector"])
    exp = pd.Series(
        [1.071971e-08, 1.487598e-11, 9.637483e-09, 6.955715e-13],
        name="value",
        index=idx,
    )
    pd.testing.assert_series_equal(obs, exp)
Exemplo n.º 5
0
def test_multiregion_derive_data():
    s = MockScenario()
    c = macro.Calculate(s, MR_DATA_PATH)
    c.read_data()
    c.derive_data()

    nodes = ['R11_AFR', 'R11_CPA']
    sectors = ['i_therm', 'rc_spec']

    # make sure no extraneous data is there
    check = c.data['demand'].reset_index()
    assert (check['node'].unique() == nodes).all()
    assert (check['sector'].unique() == sectors).all()

    obs = c.data['aconst']
    exp = pd.Series([3.74767687, 0.00285472],
                    name='value',
                    index=pd.Index(nodes, name='node'))
    pd.testing.assert_series_equal(obs, exp)

    obs = c.data['bconst']
    idx = pd.MultiIndex.from_product([nodes, sectors],
                                     names=['node', 'sector'])
    exp = pd.Series([1.071971e-08, 1.487598e-11, 9.637483e-09, 6.955715e-13],
                    name='value',
                    index=idx)
    pd.testing.assert_series_equal(obs, exp)
Exemplo n.º 6
0
def test_calc(westeros_solved, method, test, expected):
    calc = macro.Calculate(westeros_solved, W_DATA_PATH)
    calc.read_data()

    function = getattr(calc, method)
    assertion = getattr(npt, f"assert_{test}")

    assertion(function().values, expected)
Exemplo n.º 7
0
def test_calc_bconst(westeros_solved):
    s = westeros_solved
    c = macro.Calculate(s, W_DATA_PATH)
    c.read_data()
    obs = c._bconst()
    assert len(obs) == 1
    obs = obs[0]
    exp = 3.6846576e-05
    assert np.isclose(obs, exp)
Exemplo n.º 8
0
def test_calc_k0(westeros_solved):
    s = westeros_solved
    c = macro.Calculate(s, W_DATA_PATH)
    c.read_data()
    obs = c._k0()
    assert len(obs) == 1
    obs = obs[0]
    exp = 1500
    assert obs == exp
Exemplo n.º 9
0
def test_calc_growth(westeros_solved):
    s = westeros_solved
    c = macro.Calculate(s, W_DATA_PATH)
    c.read_data()
    obs = c._growth()
    assert len(obs) == 4
    obs = obs.values
    exp = np.array([0.0265836, 0.041380, 0.041380, 0.029186])
    assert np.isclose(obs, exp).all()
Exemplo n.º 10
0
def test_calc_aconst(westeros_solved):
    s = westeros_solved
    c = macro.Calculate(s, W_DATA_PATH)
    c.read_data()
    obs = c._aconst()

    assert len(obs) == 1
    obs = obs[0]
    exp = 26.027323
    assert np.isclose(obs, exp)
Exemplo n.º 11
0
def test_calc_demand(westeros_solved):
    s = westeros_solved
    c = macro.Calculate(s, W_DATA_PATH)
    c.read_data()
    obs = c._demand()
    # 4 values, 3 in model period, one in history
    assert len(obs) == 4
    obs = obs.values
    exp = np.array([90, 100, 150, 190])
    assert np.isclose(obs, exp).all()
Exemplo n.º 12
0
def test_calc_price(westeros_solved):
    s = westeros_solved
    c = macro.Calculate(s, W_DATA_PATH)
    c.read_data()
    obs = c._price()
    # 4 values, 3 in model period, one in history
    assert len(obs) == 4
    obs = obs.values
    exp = np.array([195, 182.852229, 162.039539, 161.002627])
    assert np.isclose(obs, exp).all()
Exemplo n.º 13
0
def test_calc_total_cost(westeros_solved):
    s = westeros_solved
    c = macro.Calculate(s, W_DATA_PATH)
    c.read_data()
    obs = c._total_cost()
    # 4 values, 3 in model period, one in history
    assert len(obs) == 4
    obs = obs.values
    exp = np.array([15, 17.477751, 22.143634, 28.114812]) / 1e3
    assert np.isclose(obs, exp).all()
Exemplo n.º 14
0
def test_config(westeros_solved):
    s = westeros_solved
    c = macro.Calculate(s, W_DATA_PATH)
    c.nodes = set(list(c.nodes) + ["foo"])
    c.sectors = set(list(c.sectors) + ["bar"])

    assert c.nodes == set(["Westeros", "foo"])
    assert c.sectors == set(["light", "bar"])
    c.read_data()
    assert c.nodes == set(["Westeros"])
    assert c.sectors == set(["light"])
Exemplo n.º 15
0
def test_config(westeros_solved):
    s = westeros_solved
    c = macro.Calculate(s, W_DATA_PATH)
    c.nodes = set(list(c.nodes) + ['foo'])
    c.sectors = set(list(c.sectors) + ['bar'])

    assert c.nodes == set(['Westeros', 'foo'])
    assert c.sectors == set(['light', 'bar'])
    c.read_data()
    assert c.nodes == set(['Westeros'])
    assert c.sectors == set(['light'])
Exemplo n.º 16
0
def test_calc_valid_years(westeros_solved):
    s = westeros_solved
    data = pd.read_excel(W_DATA_PATH, sheet_name=None, engine="openpyxl")
    # Adding an arbitrary year
    arbitrary_yr = 2021
    gdp_extra_yr = data["gdp_calibrate"].iloc[0, :].copy()
    gdp_extra_yr["year"] = arbitrary_yr
    data["gdp_calibrate"] = data["gdp_calibrate"].append(gdp_extra_yr)
    # Check the arbitrary year is not in config
    assert arbitrary_yr not in data["config"]["year"]
    # But it is in gdp_calibrate
    assert arbitrary_yr in set(data["gdp_calibrate"]["year"])
    # And macro does calibration without error
    c = macro.Calculate(s, data)
    c.read_data()
Exemplo n.º 17
0
def test_config(westeros_solved):
    s = westeros_solved
    c = macro.Calculate(s, W_DATA_PATH)
    assert "config" in c.data
    assert "sector" in c.data["config"]

    # Removing a column from config and testing
    data = c.data.copy()
    data["config"] = c.data["config"][["node", "sector"]]
    try:
        macro.Calculate(s, data)
    except KeyError as error:
        assert 'Missing config data for "level"' in str(error)

    # Removing config completely and testing
    data.pop("config")
    try:
        macro.Calculate(s, data)
    except KeyError as error:
        assert "Missing config in input data" in str(error)

    c.read_data()
    assert c.nodes == set(["Westeros"])
    assert c.sectors == set(["light"])
Exemplo n.º 18
0
def test_calc_data_missing_par(westeros_solved):
    s = westeros_solved
    data = pd.read_excel(W_DATA_PATH, sheet_name=None, engine="openpyxl")
    data.pop("gdp_calibrate")
    c = macro.Calculate(s, data)
    pytest.raises(ValueError, c.read_data)
Exemplo n.º 19
0
def test_multiregion_valid_data():
    s = MockScenario()
    c = macro.Calculate(s, MR_DATA_PATH)
    c.read_data()
Exemplo n.º 20
0
def test_calc_invalid_data(westeros_solved):
    with pytest.raises(TypeError, match="neither a dict nor a valid path"):
        macro.Calculate(westeros_solved, list())

    with pytest.raises(ValueError, match="not an Excel data file"):
        macro.Calculate(westeros_solved, Path(__file__).joinpath("other.zip"))
Exemplo n.º 21
0
def test_calc_valid_data_dict(westeros_solved):
    s = westeros_solved
    data = pd.read_excel(W_DATA_PATH, sheet_name=None, engine="openpyxl")
    c = macro.Calculate(s, data)
    c.read_data()
Exemplo n.º 22
0
def test_calc_valid_data_file(westeros_solved):
    s = westeros_solved
    c = macro.Calculate(s, W_DATA_PATH)
    c.read_data()