Skip to content

Commit

Permalink
✨ feat: Remove Column class completely
Browse files Browse the repository at this point in the history
  • Loading branch information
glatterf42 committed Jan 31, 2025
1 parent 0733b76 commit 9c36f7a
Show file tree
Hide file tree
Showing 43 changed files with 476 additions and 765 deletions.
19 changes: 8 additions & 11 deletions ixmp4/core/optimization/equation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import TYPE_CHECKING, Any, ClassVar
from typing import TYPE_CHECKING, Any, ClassVar, cast

if TYPE_CHECKING:
from . import InitKwargs
Expand All @@ -13,7 +13,6 @@
from ixmp4.data.abstract import Docs as DocsModel
from ixmp4.data.abstract import Equation as EquationModel
from ixmp4.data.abstract import Run
from ixmp4.data.abstract.optimization import Column

from .base import Creator, Lister, Retriever, Tabulator

Expand All @@ -36,7 +35,7 @@ def run_id(self) -> int:
return self._model.run__id

@property
def data(self) -> dict[str, Any]:
def data(self) -> dict[str, list[float] | list[int] | list[str]]:
return self._model.data

def add(self, data: dict[str, Any] | pd.DataFrame) -> None:
Expand All @@ -57,21 +56,19 @@ def remove_data(self) -> None:

@property
def levels(self) -> list[float]:
levels: list[float] = self._model.data.get("levels", [])
return levels
return cast(list[float], self._model.data.get("levels", []))

@property
def marginals(self) -> list[float]:
marginals: list[float] = self._model.data.get("marginals", [])
return marginals
return cast(list[float], self._model.data.get("marginals", []))

@property
def constrained_to_indexsets(self) -> list[str]:
return [column.indexset.name for column in self._model.columns]
def indexsets(self) -> list[str]:
return self._model.indexsets

@property
def columns(self) -> list[Column]:
return self._model.columns
def column_names(self) -> list[str] | None:
return self._model.column_names

@property
def created_at(self) -> datetime | None:
Expand Down
10 changes: 4 additions & 6 deletions ixmp4/core/optimization/parameter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import TYPE_CHECKING, Any, ClassVar
from typing import TYPE_CHECKING, Any, ClassVar, cast

if TYPE_CHECKING:
from . import InitKwargs
Expand Down Expand Up @@ -35,7 +35,7 @@ def run_id(self) -> int:
return self._model.run__id

@property
def data(self) -> dict[str, Any]:
def data(self) -> dict[str, list[float] | list[int] | list[str]]:
return self._model.data

def add(self, data: dict[str, Any] | pd.DataFrame) -> None:
Expand All @@ -49,13 +49,11 @@ def add(self, data: dict[str, Any] | pd.DataFrame) -> None:

@property
def values(self) -> list[float]:
values: list[float] = self._model.data.get("values", [])
return values
return cast(list[float], self._model.data.get("values", []))

@property
def units(self) -> list[Unit]:
units: list[Unit] = self._model.data.get("units", [])
return units
return cast(list[Unit], self._model.data.get("units", []))

@property
def indexsets(self) -> list[str]:
Expand Down
11 changes: 5 additions & 6 deletions ixmp4/core/optimization/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from ixmp4.data.abstract import Docs as DocsModel
from ixmp4.data.abstract import Run
from ixmp4.data.abstract import Table as TableModel
from ixmp4.data.abstract.optimization import Column

from .base import Creator, Lister, Retriever, Tabulator

Expand All @@ -36,7 +35,7 @@ def run_id(self) -> int:
return self._model.run__id

@property
def data(self) -> dict[str, Any]:
def data(self) -> dict[str, list[float] | list[int] | list[str]]:
return self._model.data

def add(self, data: dict[str, Any] | pd.DataFrame) -> None:
Expand All @@ -47,12 +46,12 @@ def add(self, data: dict[str, Any] | pd.DataFrame) -> None:
)

@property
def constrained_to_indexsets(self) -> list[str]:
return [column.indexset.name for column in self._model.columns]
def indexsets(self) -> list[str]:
return self._model.indexsets

@property
def columns(self) -> list[Column]:
return self._model.columns
def column_names(self) -> list[str] | None:
return self._model.column_names

@property
def created_at(self) -> datetime | None:
Expand Down
23 changes: 8 additions & 15 deletions ixmp4/core/optimization/variable.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import TYPE_CHECKING, Any, ClassVar
from typing import TYPE_CHECKING, Any, ClassVar, cast

if TYPE_CHECKING:
from . import InitKwargs
Expand All @@ -13,7 +13,6 @@
from ixmp4.data.abstract import Docs as DocsModel
from ixmp4.data.abstract import OptimizationVariable as VariableModel
from ixmp4.data.abstract import Run
from ixmp4.data.abstract.optimization import Column

from .base import Lister, Retriever, Tabulator

Expand All @@ -36,7 +35,7 @@ def run_id(self) -> int:
return self._model.run__id

@property
def data(self) -> dict[str, Any]:
def data(self) -> dict[str, list[float] | list[int] | list[str]]:
return self._model.data

def add(self, data: dict[str, Any] | pd.DataFrame) -> None:
Expand All @@ -57,25 +56,19 @@ def remove_data(self) -> None:

@property
def levels(self) -> list[float]:
levels: list[float] = self._model.data.get("levels", [])
return levels
return cast(list[float], self._model.data.get("levels", []))

@property
def marginals(self) -> list[float]:
marginals: list[float] = self._model.data.get("marginals", [])
return marginals
return cast(list[float], self._model.data.get("marginals", []))

@property
def constrained_to_indexsets(self) -> list[str]:
return (
[column.indexset.name for column in self._model.columns]
if self._model.columns
else []
)
def indexsets(self) -> list[str] | None:
return self._model.indexsets

@property
def columns(self) -> list[Column] | None:
return self._model.columns
def column_names(self) -> list[str] | None:
return self._model.column_names

@property
def created_at(self) -> datetime | None:
Expand Down
1 change: 0 additions & 1 deletion ixmp4/data/abstract/optimization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from ..annotations import HasIdFilter, HasNameFilter, HasRunIdFilter
from .base import BackendBaseRepository
from .column import Column
from .equation import Equation, EquationRepository
from .indexset import IndexSet, IndexSetRepository
from .parameter import Parameter, ParameterRepository
Expand Down
35 changes: 0 additions & 35 deletions ixmp4/data/abstract/optimization/column.py

This file was deleted.

13 changes: 7 additions & 6 deletions ixmp4/data/abstract/optimization/equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from .. import base
from ..docs import DocsRepository
from .base import BackendBaseRepository
from .column import Column


class Equation(base.BaseModel, Protocol):
Expand All @@ -24,8 +23,10 @@ class Equation(base.BaseModel, Protocol):
"""Unique name of the Equation."""
data: types.JsonDict
"""Data stored in the Equation."""
columns: types.Mapped[list[Column]]
"""Data specifying this Equation's Columns."""
indexsets: types.Mapped[list[str]]
"""List of the names of the IndexSets the Equation is bound to."""
column_names: types.Mapped[list[str] | None]
"""List of the Equation's column names, if distinct from the IndexSet names."""

run__id: types.Integer
"Foreign unique integer id of a run."
Expand Down Expand Up @@ -58,7 +59,7 @@ def create(
"""Creates an Equation.
Each column of the Equation needs to be constrained to an existing
:class:ixmp4.data.abstract.optimization.IndexSet. These are specified by name
:class:`ixmp4.data.abstract.optimization.IndexSet`. These are specified by name
and per default, these will be the column names. They can be overwritten by
specifying `column_names`, which needs to specify a unique name for each column.
Expand All @@ -80,7 +81,7 @@ def create(
------
:class:`ixmp4.data.abstract.optimization.Equation.NotUnique`:
If the Equation with `name` already exists for the Run with `run_id`.
ValueError
:class:`ixmp4.core.exceptions.OptimizationItemUsageError`:
If `column_names` are not unique or not enough names are given.
Returns
Expand Down Expand Up @@ -194,7 +195,7 @@ def add_data(self, equation_id: int, data: dict[str, Any] | pd.DataFrame) -> Non
Raises
------
ValueError:
:class:`ixmp4.core.exceptions.OptimizationItemUsageError`:
- If values are missing, `None`, or `NaN`
- If values are not allowed based on constraints to `Indexset`s
- If rows are not unique
Expand Down
13 changes: 7 additions & 6 deletions ixmp4/data/abstract/optimization/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from .. import base
from ..docs import DocsRepository
from .base import BackendBaseRepository
from .column import Column


class Table(base.BaseModel, Protocol):
Expand All @@ -24,8 +23,10 @@ class Table(base.BaseModel, Protocol):
"""Unique name of the Table."""
data: types.JsonDict
"""Data stored in the Table."""
columns: types.Mapped[list[Column]]
"""Data specifying this Table's Columns."""
indexsets: types.Mapped[list[str]]
"""List of the names of the IndexSets the Table is bound to."""
column_names: types.Mapped[list[str] | None]
"""List of the Table's column names, if distinct from the IndexSet names."""

run__id: types.Integer
"Foreign unique integer id of a run."
Expand Down Expand Up @@ -58,7 +59,7 @@ def create(
"""Creates a Table.
Each column of the Table needs to be constrained to an existing
:class:ixmp4.data.abstract.optimization.IndexSet. These are specified by name
:class:`ixmp4.data.abstract.optimization.IndexSet`. These are specified by name
and per default, these will be the column names. They can be overwritten by
specifying `column_names`, which needs to specify a unique name for each column.
Expand All @@ -80,7 +81,7 @@ def create(
------
:class:`ixmp4.data.abstract.optimization.Table.NotUnique`:
If the Table with `name` already exists for the Run with `run_id`.
ValueError
:class:`ixmp4.core.exceptions.OptimizationItemUsageError`:
If `column_names` are not unique or not enough names are given.
Returns
Expand Down Expand Up @@ -193,7 +194,7 @@ def add_data(self, table_id: int, data: dict[str, Any] | pd.DataFrame) -> None:
Raises
------
ValueError:
:class:`ixmp4.core.exceptions.OptimizationDataValidationError`:
- If values are missing, `None`, or `NaN`
- If values are not allowed based on constraints to `Indexset`s
- If rows are not unique
Expand Down
13 changes: 7 additions & 6 deletions ixmp4/data/abstract/optimization/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from .. import base
from ..docs import DocsRepository
from .base import BackendBaseRepository
from .column import Column


class Variable(base.BaseModel, Protocol):
Expand All @@ -23,8 +22,10 @@ class Variable(base.BaseModel, Protocol):
"""Unique name of the Variable."""
data: types.JsonDict
"""Data stored in the Variable."""
columns: types.Mapped[list[Column] | None]
"""Data specifying this Variable's Columns."""
indexsets: types.Mapped[list[str] | None]
"""List of the names of the IndexSets the Variable is bound to."""
column_names: types.Mapped[list[str] | None]
"""List of the Variable's column names, if distinct from the IndexSet names."""

run__id: types.Integer
"Foreign unique integer id of a run."
Expand Down Expand Up @@ -57,7 +58,7 @@ def create(
"""Creates a Variable.
Each column of the Variable needs to be constrained to an existing
:class:ixmp4.data.abstract.optimization.IndexSet. These are specified by name
:class:`ixmp4.data.abstract.optimization.IndexSet`. These are specified by name
and per default, these will be the column names. They can be overwritten by
specifying `column_names`, which needs to specify a unique name for each column.
Expand All @@ -80,7 +81,7 @@ def create(
------
:class:`ixmp4.data.abstract.optimization.Variable.NotUnique`:
If the Variable with `name` already exists for the Run with `run_id`.
ValueError
:class:`ixmp4.core.exceptions.OptimizationItemUsageError`:
If `column_names` are not unique or not enough names are given.
Returns
Expand Down Expand Up @@ -195,7 +196,7 @@ def add_data(self, variable_id: int, data: dict[str, Any] | pd.DataFrame) -> Non
Raises
------
ValueError:
:class:`ixmp4.core.exceptions.OptimizationItemUsageError`:
- If values are missing, `None`, or `NaN`
- If values are not allowed based on constraints to `Indexset`s
- If rows are not unique
Expand Down
23 changes: 0 additions & 23 deletions ixmp4/data/api/optimization/column.py

This file was deleted.

Loading

0 comments on commit 9c36f7a

Please sign in to comment.