def test_get_sparse(): db = dt.Gdx("test.gdx") dense = dt.Gdx("test.gdx", sparse=False) assert len(dense["qY"]) > len(db["qY"]) assert len(dense["qY"]) == len( pd.MultiIndex.from_product(db.get("s_", "t"))) assert len(dense["pBolig"]) == len(db["t"])
def test_export_scalar_with_changes(): db = dt.Gdx("test.gdx") db["eHh"] = db["eHh"] * 2 db.export("test_export.gdx", relative_path=True) old, new = dt.Gdx("test.gdx"), dt.Gdx("test_export.gdx") assert approximately_equal(old["eHh"] * 2, new["eHh"])
def test_export_added_variable(): db = dt.Gdx("test.gdx") db.create_variable("foo", [db.a, db.t], explanatory_text="Variable added from Python.") db["foo"] = 42 db.export("test_export.gdx", relative_path=True) assert all(approximately_equal(dt.Gdx("test_export.gdx")["foo"], 42))
def test_copy_set(): db = dt.Gdx("test.gdx") db["alias"] = db["s"] db["alias"].name = "alias" index = db["alias"] domains = [ "*" if i in (None, index.name) else i for i in db.get_domains_from_index(index, index.name) ] db.database.add_set_dc(index.name, domains, "") index = index.copy() index.domains = domains db.series[index.name] = index db.export("test_export.gdx", relative_path=True) assert all(dt.Gdx("test_export.gdx")["alias"] == db["s"])
def get_reference_database(): if dt.REFERENCE_DATABASE is None: import easygui dt.REFERENCE_DATABASE = dt.Gdx( easygui.fileopenbox("Select reference gdx file", filetypes=["*.gdx"])) return dt.REFERENCE_DATABASE
def test_export_NAs(): db = dt.GamsPandasDatabase() t = db.create_set("t", range(5)) p = db.create_parameter("p", t) assert len(db["p"]) == 5 assert len(db.symbols["p"]) == 0 db.export("test_export.gdx") db = dt.Gdx("test_export.gdx") assert all(pd.isna(db["p"])) assert len(db["p"]) == 0 assert len(db.symbols["p"]) == 0
def test_gdx_read(): db = dt.Gdx("test.gdx") assert approximately_equal(db["qY"]["byg", 2025], 257.55) assert approximately_equal(db["qI_s"]["IB", "fre", 2025], 7.41) assert approximately_equal(db["eHh"], 1.25) assert db["fp"] == 1.0178 assert all( approximately_equal(db["inf_factor"], db["fp"]**(2010 - db["inf_factor"].index))) assert db["s"].name == "s_" assert db.vHh.loc["NetFin", "tot", 1970] == 0 assert set(db["vHh"].index.get_level_values("a_")).issubset(set(db["a_"]))
def test_import_export_empty(): # Create empty GamsPandasDatabase and alias creation methods db = dt.GamsPandasDatabase() Par, Var, Set = db.create_parameter, db.create_variable, db.create_set # Create sets from scratch t = Set("t", range(2000, 2021), "Årstal") s = Set("s", ["tjenester", "fremstilling"], "Brancher", ["Tjenester", "Fremstilling"]) p = Par("p", [s, t]) v = Var("v", [s, t]) db.p = p.loc[[], []] db.v = p.loc[[], []] db.export("test_export.gdx") db = dt.Gdx("test_export.gdx") db.p.loc[t, s] = 1 db.v.loc[t, s] = 1 db.export("test_export.gdx") db = dt.Gdx("test_export.gdx") assert all(db.p == 1) assert all(db.v == 1)
def test_create_parameter(): db = dt.GamsPandasDatabase() db.create_parameter("scalar", data=3.2) assert db.scalar == 3.2 db.create_parameter("vector", data=[1, 2], index=pd.Index(["a", "b"], name="ab"), add_missing_domains=True) assert all(db.vector == [1, 2]) db.create_parameter("dataframe", data=pd.DataFrame([ [2025, "ser", 3], [2025, "goo", 2], [2035, "ser", 6], [2035, "goo", 4], ], columns=["t", "s", "value"]), add_missing_domains=True) db.export("test_export.gdx") assert dt.Gdx("test_export.gdx")["scalar"] == 3.2 assert all(dt.Gdx("test_export.gdx")["vector"] == [1, 2]) assert all(db.s == ["ser", "goo"]) assert all(db.t == [2025, 2035])
def test_export_set_with_changes(): db = dt.Gdx("test.gdx") db["s"].texts["tje"] = "New text" db.export("test_export.gdx", relative_path=True) assert dt.Gdx("test_export.gdx")["s"].texts["tje"] == "New text"
def test_export_variable_with_changes(): db = dt.Gdx("test.gdx") db["qY"] = db["qY"] * 2 db.export("test_export.gdx", relative_path=True) old, new = dt.Gdx("test.gdx"), dt.Gdx("test_export.gdx") assert all(old["qY"] * 2 == new["qY"])
def test_export_with_no_changes(): dt.Gdx("test.gdx").export("test_export.gdx", relative_path=True) assert round(os.stat("test.gdx").st_size, -5) == round(os.stat("test_export.gdx").st_size, -5)
def test_multiply_with_different_sets(): db = dt.Gdx("test.gdx") i, s = db["i"], db["s"] result = (db["pI"] * db["qI_s"].loc[i, s]).groupby("t").sum() / db["vI"][ "iTot"] # Using pI_s would give exact 1 assert all(approximately_equal(result.loc[2030:], 1))
import dreamtools as dt # Åben gdx fil gdx = dt.Gdx("test.gdx") gdx.export("export.gdx") # Hent sets fra gdx s, i, t = gdx.get("s", "i", "t") # Hent variable fra gdx qY, qBNP, qI_s = gdx.get("qY", "qBNP", "qI_s") # Plot variabel qBNP.plot() # Plot variabel i begrænset periode qBNP.plot(2010, 2020) # Plot variabel begrænset til et enkelt element eller set qY["tot"].plot() qY[s].plot() # Plot flere variable sammen dt.plot(qY[s], qBNP, start=2010, end=2020) # Hvis en serie har flere dimensioner, skal vi skrive .loc for at begrænse med på mere end første dimension. Fx: qI_s.loc[i, s] # Vi kan stadig begænse på første dimension uden .loc qI_s[i]