Skip to content

Commit

Permalink
Fix and test table list and tabulate for specific runs
Browse files Browse the repository at this point in the history
  • Loading branch information
glatterf42 committed Aug 5, 2024
1 parent c306efc commit 35ec82d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ixmp4/core/optimization/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@ def list(self, name: str | None = None) -> Iterable[Table]:
]

def tabulate(self, name: str | None = None) -> pd.DataFrame:
return self.backend.optimization.tables.tabulate(name=name)
return self.backend.optimization.tables.tabulate(run_id=self._run.id, name=name)
35 changes: 30 additions & 5 deletions tests/core/test_table.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pandas as pd
import pytest

from ixmp4 import Platform, Table
from ixmp4.core import Platform, Table

from ..utils import all_platforms

Expand Down Expand Up @@ -216,8 +216,6 @@ def test_table_add_data(self, test_mp, request):
def test_list_tables(self, test_mp, request):
test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore
run = test_mp.runs.create("Model", "Scenario")
# Per default, list() lists scalars for `default` version runs:
run.set_as_default()
_ = run.optimization.indexsets.create("Indexset")
_ = run.optimization.indexsets.create("Indexset 2")
table = run.optimization.tables.create(
Expand All @@ -235,11 +233,23 @@ def test_list_tables(self, test_mp, request):
list_id = [table.id for table in run.optimization.tables.list(name="Table")]
assert not (set(expected_id) ^ set(list_id))

# Test that only Tables belonging to this Run are listed
run_2 = test_mp.runs.create("Model", "Scenario")
indexset_3 = run_2.optimization.indexsets.create("Indexset 3")
indexset_4 = run_2.optimization.indexsets.create("Indexset 4")
table_3 = run_2.optimization.tables.create(
"Table", constrained_to_indexsets=[indexset_3.name, indexset_4.name]
)
table_4 = run_2.optimization.tables.create(
"Table 2", constrained_to_indexsets=[indexset_3.name, indexset_4.name]
)
expected_ids = [table_3.id, table_4.id]
list_ids = [table.id for table in run_2.optimization.tables.list()]
assert not (set(expected_ids) ^ set(list_ids))

def test_tabulate_table(self, test_mp, request):
test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore
run = test_mp.runs.create("Model", "Scenario")
# Per default, tabulate() lists scalars for `default` version runs:
run.set_as_default()
indexset = run.optimization.indexsets.create("Indexset")
indexset_2 = run.optimization.indexsets.create("Indexset 2")
table = run.optimization.tables.create(
Expand All @@ -266,6 +276,21 @@ def test_tabulate_table(self, test_mp, request):
run.optimization.tables.tabulate(),
)

# Test that only Tables belonging to this Run are listed
run_2 = test_mp.runs.create("Model", "Scenario")
indexset_3 = run_2.optimization.indexsets.create("Indexset 3")
indexset_4 = run_2.optimization.indexsets.create("Indexset 4")
table_3 = run_2.optimization.tables.create(
"Table", constrained_to_indexsets=[indexset_3.name, indexset_4.name]
)
table_4 = run_2.optimization.tables.create(
"Table 2", constrained_to_indexsets=[indexset_3.name, indexset_4.name]
)
pd.testing.assert_frame_equal(
df_from_list([table_3, table_4]),
run_2.optimization.tables.tabulate(),
)

def test_table_docs(self, test_mp, request):
test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore
run = test_mp.runs.create("Model", "Scenario")
Expand Down
47 changes: 40 additions & 7 deletions tests/data/test_optimization_table.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pandas as pd
import pytest

from ixmp4 import Platform, Table
from ixmp4.core import Platform, Table

from ..utils import all_platforms

Expand Down Expand Up @@ -101,7 +101,7 @@ def test_create_table(self, test_mp, request):
assert table_3.columns[1].dtype == "int64"

def test_get_table(self, test_mp, request):
test_mp = request.getfixturevalue(test_mp)
test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore
run = test_mp.backend.runs.create("Model", "Scenario")
_ = test_mp.backend.optimization.indexsets.create(
run_id=run.id, name="Indexset"
Expand Down Expand Up @@ -267,10 +267,8 @@ def test_table_add_data(self, test_mp, request):
assert table_5.data == test_data_5

def test_list_table(self, test_mp, request):
test_mp = request.getfixturevalue(test_mp)
test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore
run = test_mp.backend.runs.create("Model", "Scenario")
# Per default, list() lists scalars for `default` version runs:
test_mp.backend.runs.set_as_default_version(run.id)
_ = test_mp.backend.optimization.indexsets.create(
run_id=run.id, name="Indexset"
)
Expand All @@ -287,11 +285,27 @@ def test_list_table(self, test_mp, request):

assert [table] == test_mp.backend.optimization.tables.list(name="Table")

# Test listing of Tables when specifying a Run
run_2 = test_mp.backend.runs.create("Model", "Scenario")
indexset_3 = test_mp.backend.optimization.indexsets.create(
run_id=run_2.id, name="Indexset 3"
)
indexset_4 = test_mp.backend.optimization.indexsets.create(
run_id=run_2.id, name="Indexset 4"
)
table_3 = test_mp.backend.optimization.tables.create(
run_id=run_2.id, name="Table", constrained_to_indexsets=[indexset_3.name]
)
table_4 = test_mp.backend.optimization.tables.create(
run_id=run_2.id, name="Table 2", constrained_to_indexsets=[indexset_4.name]
)
assert [table_3, table_4] == test_mp.backend.optimization.tables.list(
run_id=run_2.id
)

def test_tabulate_table(self, test_mp, request):
test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore
run = test_mp.backend.runs.create("Model", "Scenario")
# Per default, tabulate() lists scalars for `default` version runs:
test_mp.backend.runs.set_as_default_version(run.id)
indexset = test_mp.backend.optimization.indexsets.create(
run_id=run.id, name="Indexset"
)
Expand Down Expand Up @@ -334,3 +348,22 @@ def test_tabulate_table(self, test_mp, request):
df_from_list([table, table_2]),
test_mp.backend.optimization.tables.tabulate(),
)

# Test tabulation of Tables when specifying a Run
run_2 = test_mp.backend.runs.create("Model", "Scenario")
indexset_3 = test_mp.backend.optimization.indexsets.create(
run_id=run_2.id, name="Indexset 3"
)
indexset_4 = test_mp.backend.optimization.indexsets.create(
run_id=run_2.id, name="Indexset 4"
)
table_3 = test_mp.backend.optimization.tables.create(
run_id=run_2.id, name="Table", constrained_to_indexsets=[indexset_3.name]
)
table_4 = test_mp.backend.optimization.tables.create(
run_id=run_2.id, name="Table 2", constrained_to_indexsets=[indexset_4.name]
)
pd.testing.assert_frame_equal(
df_from_list([table_3, table_4]),
test_mp.backend.optimization.tables.tabulate(run_id=run_2.id),
)

0 comments on commit 35ec82d

Please sign in to comment.