-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include optimization parameter (#94)
* Include optimization parameter basis (#79) * Fix references to DB filters in docs * Streamline naming in tests * Fix and test parameter list and tabulate for specific runs * Make indexset-creation a test utility * Incorporate changes from #110 * Use pandas for updated add_data behaviour * Raise minimum pandas version to enable add_data upsert * Generalize UsageError for more optimization items * Use generalized UsageError for Table * Use own errors for Parameter
- Loading branch information
1 parent
353b744
commit 769a198
Showing
48 changed files
with
1,713 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
from datetime import datetime | ||
from typing import Any, ClassVar, Iterable | ||
|
||
import pandas as pd | ||
|
||
from ixmp4.core.base import BaseFacade, BaseModelFacade | ||
from ixmp4.data.abstract import Docs as DocsModel | ||
from ixmp4.data.abstract import Parameter as ParameterModel | ||
from ixmp4.data.abstract import Run | ||
from ixmp4.data.abstract.optimization import Column | ||
|
||
|
||
class Parameter(BaseModelFacade): | ||
_model: ParameterModel | ||
NotFound: ClassVar = ParameterModel.NotFound | ||
NotUnique: ClassVar = ParameterModel.NotUnique | ||
|
||
@property | ||
def id(self) -> int: | ||
return self._model.id | ||
|
||
@property | ||
def name(self) -> str: | ||
return self._model.name | ||
|
||
@property | ||
def run_id(self) -> int: | ||
return self._model.run__id | ||
|
||
@property | ||
def data(self) -> dict[str, Any]: | ||
return self._model.data | ||
|
||
def add(self, data: dict[str, Any] | pd.DataFrame) -> None: | ||
"""Adds data to an existing Parameter.""" | ||
self.backend.optimization.parameters.add_data( | ||
parameter_id=self._model.id, data=data | ||
) | ||
self._model.data = self.backend.optimization.parameters.get( | ||
run_id=self._model.run__id, name=self._model.name | ||
).data | ||
|
||
@property | ||
def values(self) -> list: | ||
return self._model.data.get("values", []) | ||
|
||
@property | ||
def units(self) -> list: | ||
return self._model.data.get("units", []) | ||
|
||
@property | ||
def constrained_to_indexsets(self) -> list[str]: | ||
return [column.indexset.name for column in self._model.columns] | ||
|
||
@property | ||
def columns(self) -> list[Column]: | ||
return self._model.columns | ||
|
||
@property | ||
def created_at(self) -> datetime | None: | ||
return self._model.created_at | ||
|
||
@property | ||
def created_by(self) -> str | None: | ||
return self._model.created_by | ||
|
||
@property | ||
def docs(self): | ||
try: | ||
return self.backend.optimization.parameters.docs.get(self.id).description | ||
except DocsModel.NotFound: | ||
return None | ||
|
||
@docs.setter | ||
def docs(self, description): | ||
if description is None: | ||
self.backend.optimization.parameters.docs.delete(self.id) | ||
else: | ||
self.backend.optimization.parameters.docs.set(self.id, description) | ||
|
||
@docs.deleter | ||
def docs(self): | ||
try: | ||
self.backend.optimization.parameters.docs.delete(self.id) | ||
# TODO: silently failing | ||
except DocsModel.NotFound: | ||
return None | ||
|
||
def __str__(self) -> str: | ||
return f"<Parameter {self.id} name={self.name}>" | ||
|
||
|
||
class ParameterRepository(BaseFacade): | ||
_run: Run | ||
|
||
def __init__(self, _run: Run, *args, **kwargs) -> None: | ||
super().__init__(*args, **kwargs) | ||
self._run = _run | ||
|
||
def create( | ||
self, | ||
name: str, | ||
constrained_to_indexsets: list[str], | ||
column_names: list[str] | None = None, | ||
) -> Parameter: | ||
model = self.backend.optimization.parameters.create( | ||
name=name, | ||
run_id=self._run.id, | ||
constrained_to_indexsets=constrained_to_indexsets, | ||
column_names=column_names, | ||
) | ||
return Parameter(_backend=self.backend, _model=model) | ||
|
||
def get(self, name: str) -> Parameter: | ||
model = self.backend.optimization.parameters.get(run_id=self._run.id, name=name) | ||
return Parameter(_backend=self.backend, _model=model) | ||
|
||
def list(self, name: str | None = None) -> Iterable[Parameter]: | ||
parameters = self.backend.optimization.parameters.list( | ||
run_id=self._run.id, name=name | ||
) | ||
return [ | ||
Parameter( | ||
_backend=self.backend, | ||
_model=i, | ||
) | ||
for i in parameters | ||
] | ||
|
||
def tabulate(self, name: str | None = None) -> pd.DataFrame: | ||
return self.backend.optimization.parameters.tabulate( | ||
run_id=self._run.id, name=name | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .column import Column | ||
from .indexset import IndexSet, IndexSetRepository | ||
from .parameter import Parameter, ParameterRepository | ||
from .scalar import Scalar, ScalarRepository | ||
from .table import Table, TableRepository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.