Пример #1
0
def test_add_variable_from_series():
    db = dt.GamsPandasDatabase()
    t = pd.Index(range(2010, 2026), name="t")
    var = pd.Series(1.4, index=t, name="var")
    db.add_variable_from_series(var, add_missing_domains=True)
    assert all(db["var"] == 1.4)
    assert len(db["var"]) == 16
Пример #2
0
def test_add_parameter_from_dataframe():
    db = dt.GamsPandasDatabase()
    df = pd.DataFrame()
    df["t"] = range(2025, 2036)
    df["value"] = 1.3
    db.add_parameter_from_dataframe("par", df, add_missing_domains=True)
    assert all(db["par"] == 1.3)
    assert len(db["par"]) == 11
Пример #3
0
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
Пример #4
0
def test_add_variable_from_dataframe():
    db = dt.GamsPandasDatabase()
    df = pd.DataFrame([
        [2010, "ser", 3],
        [2010, "goo", 2],
        [2020, "ser", 6],
        [2020, "goo", 4],
    ],
                      columns=["t", "s", "value"])
    db.add_variable_from_dataframe("q", df, add_missing_domains=True)
    assert all(db.t == [2010, 2020])
    assert all(db.s == ["ser", "goo"])
Пример #5
0
def test_add_parameter_from_series():
    db = dt.GamsPandasDatabase()

    t = pd.Index(range(2025, 2036), name="t")
    par = pd.Series(1.4, index=t, name="par")
    db.add_parameter_from_series(par, add_missing_domains=True)
    assert all(db["par"] == 1.4)
    assert len(db["par"]) == 11

    ss = pd.Index(["foo"], name="ss")
    singleton = pd.Series(1.4, index=ss, name="singleton")
    db.add_parameter_from_series(singleton, add_missing_domains=True)
    assert db["singleton"]["foo"] == 1.4
    assert len(db["singleton"]) == 1

    scalar = pd.Series(1.4, name="scalar")
    db.add_parameter_from_series(scalar)
    assert all(db["scalar"] == 1.4)
    assert len(db["scalar"]) == 1
Пример #6
0
def test_multiply_added():
    db = dt.GamsPandasDatabase()
    df = pd.DataFrame([
        [2025, "ser", 3],
        [2025, "goo", 2],
        [2035, "ser", 6],
        [2035, "goo", 4],
    ],
                      columns=["t", "s", "value"])
    db.add_parameter_from_dataframe("q", df, add_missing_domains=True)

    df = pd.DataFrame([
        [2025, 1],
        [2035, 1.2],
    ], columns=["t", "value"])
    db.add_parameter_from_dataframe("p", df, add_missing_domains=True)

    v = db["p"] * db["q"]
    v.name = "v"
    db.add_parameter_from_series(v)

    assert db["v"][2035, "goo"] == 4.8
Пример #7
0
def test_create_set_from_index():
    db = dt.GamsPandasDatabase()
    t = pd.Index(range(2025, 2036), name="t")
    db.create_set("t", t)
    assert db["t"].name == "t"
    assert all(db["t"] == t)
    assert db.symbols["t"].domains_as_strings == ["*"]
    assert db.t.domains == ["*"]

    db.create_set("tsub", t[5:], domains=["t"])
    assert db["tsub"].name == "tsub"
    assert all(db["tsub"] == t[5:])
    assert db.symbols["tsub"].domains_as_strings == ["t"]
    assert db.tsub.domains == ["t"]

    s = pd.Index(["services", "goods"], name="s")
    st = pd.MultiIndex.from_product([s, t], names=["s", "t"])
    db.create_set("st", st)
    assert db["st"].name == "st"
    assert all(db["st"] == st[:])
    assert db.symbols["st"].domains_as_strings == ["s", "t"]
    assert db.st.domains == ["s", "t"]
Пример #8
0
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)
Пример #9
0
def test_create_methods():
    # 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"])
    st = Set("st", [s, t], "Branche x år dummy")
    sub = Set("sub", ["tjenester"], "Subset af brancher", domains=["s"])

    one2one = Set("one2one", [(2010, 2015), (2011, 2016)],
                  "1 til 1 mapping",
                  domains=["t", "t"])

    one2many = Set(
        "one2many",
        [("tot", "tjenester"), ("tot", "fremstilling")],
        "1 til mange mapping",
        domains=["*", "s"],
    )
    assert one2many.name == "one2many"
    assert one2many.names == ["*", "s"]
    assert one2many.domains == ["*", "s"]

    # Create parameters and variables based on zero ore more sets
    gq = Par("gq", None, "Produktivitets-vækst", 0.01)
    fq = Par("fp", t, "Vækstkorrektionsfaktor", (1 + 0.01)**(t - 2010))
    d = Par("d", st, "Dummy")
    y = Var("y", [s, t], "Produktion")

    assert gq == 0.01
    assert all(fq.loc[2010:2011] == [1, 1.01])
    assert pd.isna(d["tjenester", 2010])
    assert pd.isna(y["tjenester", 2010])
Пример #10
0
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])
Пример #11
0
# Check at der eksisterer en 'images' mappe og opret den hvis den ikke gør
import os
if not os.path.exists("images"):
    os.mkdir("images")

# Plot som statisk billed, fx som svg, pdf eller png. Bemærk at mange billed-formater kan åbnes direkte i PyCharm
qBNP.plot(file="images/plot.svg")
# Dette kræver at man har Orca installeret - spørg evt. Lucas eller undertegnede for hjælp

# Skift om der renderes til browser, pdf, png etc. som standard
import plotly.io as pio

pio.renderers.default = "browser"

# Lav ny GamsPandasDatabase og alias af metoder til at lave nye symboler
db = dt.GamsPandasDatabase()
Par, Var, Set = db.create_parameter, db.create_variable, db.create_set

# Definer sets
t = Set("t", range(2000, 2020), "Årstal")
s = Set("s", ["tjenester", "fremstilling"], "Brancher")
st = Set("st", [s, t], "Branche x år dummy")
sub = Set("sub", ["tjenester"], "Subset af brancher", domains=["s"])

one2one = Set("one2one", [(2010, 2015), (2011, 2016)],
              "1 til 1 mapping",
              domains=["t", "t"])

one2many = Set(
    "one2many",
    [("tot", "tjenester"), ("tot", "fremstilling")],