Skip to content

Commit

Permalink
Remove pointless indexsets.tabulate parameter (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
glatterf42 authored Dec 19, 2024
1 parent e252b7c commit 26abf0e
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 79 deletions.
6 changes: 2 additions & 4 deletions ixmp4/core/optimization/indexset.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ def list(self, name: str | None = None) -> list[IndexSet]:
for i in indexsets
]

def tabulate(
self, name: str | None = None, include_data: bool = False
) -> pd.DataFrame:
def tabulate(self, name: str | None = None) -> pd.DataFrame:
return self.backend.optimization.indexsets.tabulate(
run_id=self._run.id, name=name, include_data=include_data
run_id=self._run.id, name=name
)
7 changes: 1 addition & 6 deletions ixmp4/data/abstract/optimization/indexset.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,11 @@ def list(self, **kwargs: Unpack["EnumerateKwargs"]) -> list[IndexSet]:
"""
...

def tabulate(
self, *, include_data: bool = False, **kwargs: Unpack["EnumerateKwargs"]
) -> pd.DataFrame:
def tabulate(self, **kwargs: Unpack["EnumerateKwargs"]) -> pd.DataFrame:
r"""Tabulate IndexSets by specified criteria.
Parameters
----------
include_data : bool, optional
Whether to load all IndexSet data, which reduces loading speed. Defaults to
`False`.
\*\*kwargs: any
Any filter parameters as specified in
`ixmp4.data.db.optimization.indexset.filter.OptimizationIndexSetFilter`.
Expand Down
6 changes: 2 additions & 4 deletions ixmp4/data/api/optimization/indexset.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,10 @@ def list(
return super()._list(json=json)

def tabulate(
self,
include_data: bool = False,
**kwargs: Unpack[abstract.optimization.EnumerateKwargs],
self, **kwargs: Unpack[abstract.optimization.EnumerateKwargs]
) -> pd.DataFrame:
json = cast(abstract.annotations.OptimizationFilterAlias, kwargs)
return super()._tabulate(json=json, params={"include_data": include_data})
return super()._tabulate(json=json)

def add_data(
self,
Expand Down
2 changes: 0 additions & 2 deletions ixmp4/data/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ def tabulate(self, *args: Any, _raw: bool = False, **kwargs: Any) -> pd.DataFram

class PaginateKwargs(TypedDict):
_filter: filters.BaseFilter
include_data: NotRequired[bool]
join_parameters: NotRequired[bool | None]
join_runs: NotRequired[bool | None]
join_run_index: NotRequired[bool | None]
Expand All @@ -308,7 +307,6 @@ class PaginateKwargs(TypedDict):

class EnumerateKwargs(TypedDict):
_filter: filters.BaseFilter
include_data: NotRequired[bool]
join_parameters: NotRequired[bool | None]
join_runs: NotRequired[bool | None]
join_run_index: NotRequired[bool | None]
Expand Down
17 changes: 2 additions & 15 deletions ixmp4/data/db/optimization/indexset/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,8 @@ def list(self, **kwargs: Unpack["base.EnumerateKwargs"]) -> list[IndexSet]:
return super().list(**kwargs)

@guard("view")
def tabulate(
self, *, include_data: bool = False, **kwargs: Unpack["base.EnumerateKwargs"]
) -> pd.DataFrame:
if not include_data:
return (
super().tabulate(**kwargs).rename(columns={"_data_type": "data_type"})
)
else:
result = super().tabulate(**kwargs).drop(labels="_data_type", axis=1)
result.insert(
loc=0,
column="data",
value=[indexset.data for indexset in self.list(**kwargs)],
)
return result
def tabulate(self, **kwargs: Unpack["base.EnumerateKwargs"]) -> pd.DataFrame:
return super().tabulate(**kwargs).rename(columns={"_data_type": "data_type"})

@guard("edit")
def add_data(
Expand Down
2 changes: 0 additions & 2 deletions ixmp4/server/rest/optimization/indexset.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class DataInput(BaseModel):
def query(
filter: OptimizationIndexSetFilter = Body(OptimizationIndexSetFilter()),
table: bool = Query(False),
include_data: bool = Query(False),
pagination: Pagination = Depends(),
backend: Backend = Depends(deps.get_backend),
) -> EnumerationOutput[IndexSet]:
Expand All @@ -39,7 +38,6 @@ def query(
limit=pagination.limit,
offset=pagination.offset,
table=bool(table),
include_data=bool(include_data),
),
total=backend.optimization.indexsets.count(_filter=filter),
pagination=pagination,
Expand Down
32 changes: 9 additions & 23 deletions tests/core/test_optimization_indexset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..utils import create_indexsets_for_run


def df_from_list(indexsets: list[IndexSet], include_data: bool = False) -> pd.DataFrame:
def df_from_list(indexsets: list[IndexSet]) -> pd.DataFrame:
result = pd.DataFrame(
# Order is important here to avoid utils.assert_unordered_equality,
# which doesn't like lists
Expand All @@ -31,19 +31,14 @@ def df_from_list(indexsets: list[IndexSet], include_data: bool = False) -> pd.Da
"created_by",
],
)
if include_data:
result.insert(
loc=0, column="data", value=[indexset.data for indexset in indexsets]
)
else:
result.insert(
loc=0,
column="data_type",
value=[
type(indexset.data[0]).__name__ if indexset.data != [] else None
for indexset in indexsets
],
)
result.insert(
loc=0,
column="data_type",
value=[
type(indexset.data[0]).__name__ if indexset.data != [] else None
for indexset in indexsets
],
)
return result


Expand Down Expand Up @@ -143,15 +138,6 @@ def test_tabulate_indexsets(self, platform: ixmp4.Platform) -> None:
result = run.optimization.indexsets.tabulate(name="Indexset 2")
pdt.assert_frame_equal(expected, result)

# Test tabulating including the data
expected = df_from_list(indexsets=[indexset_2], include_data=True)
pdt.assert_frame_equal(
expected,
run.optimization.indexsets.tabulate(
name=indexset_2.name, include_data=True
),
)

def test_indexset_docs(self, platform: ixmp4.Platform) -> None:
run = platform.runs.create("Model", "Scenario")
(indexset_1,) = tuple(
Expand Down
32 changes: 9 additions & 23 deletions tests/data/test_optimization_indexset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..utils import create_indexsets_for_run


def df_from_list(indexsets: list[IndexSet], include_data: bool = False) -> pd.DataFrame:
def df_from_list(indexsets: list[IndexSet]) -> pd.DataFrame:
result = pd.DataFrame(
# Order is important here to avoid utils.assert_unordered_equality,
# which doesn't like lists
Expand All @@ -31,19 +31,14 @@ def df_from_list(indexsets: list[IndexSet], include_data: bool = False) -> pd.Da
"created_by",
],
)
if include_data:
result.insert(
loc=0, column="data", value=[indexset.data for indexset in indexsets]
)
else:
result.insert(
loc=0,
column="data_type",
value=[
type(indexset.data[0]).__name__ if indexset.data != [] else None
for indexset in indexsets
],
)
result.insert(
loc=0,
column="data_type",
value=[
type(indexset.data[0]).__name__ if indexset.data != [] else None
for indexset in indexsets
],
)
return result


Expand Down Expand Up @@ -138,15 +133,6 @@ def test_tabulate_indexsets(self, platform: ixmp4.Platform) -> None:
expected, platform.backend.optimization.indexsets.tabulate(run_id=run_2.id)
)

# Test tabulating including the data
expected = df_from_list(indexsets=[indexset_2], include_data=True)
pdt.assert_frame_equal(
expected,
platform.backend.optimization.indexsets.tabulate(
name=indexset_2.name, include_data=True
),
)

def test_add_data(self, platform: ixmp4.Platform) -> None:
test_data = ["foo", "bar"]
run = platform.backend.runs.create("Model", "Scenario")
Expand Down

0 comments on commit 26abf0e

Please sign in to comment.