-
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.
* Add abstraction layer for core optimization reusability * Add annotations
- Loading branch information
1 parent
26abf0e
commit 0deac5b
Showing
16 changed files
with
232 additions
and
185 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from typing import TYPE_CHECKING, Generic, TypeVar | ||
|
||
if TYPE_CHECKING: | ||
from . import InitKwargs | ||
|
||
|
||
import pandas as pd | ||
|
||
# TODO Import this from typing when dropping Python 3.11 | ||
from typing_extensions import TypedDict, Unpack | ||
|
||
from ixmp4.core.base import BaseFacade, BaseModelFacade | ||
from ixmp4.data import abstract | ||
|
||
FacadeOptimizationModelType = TypeVar( | ||
"FacadeOptimizationModelType", bound=BaseModelFacade | ||
) | ||
AbstractOptimizationModelType = TypeVar( | ||
"AbstractOptimizationModelType", bound=abstract.BaseModel | ||
) | ||
|
||
|
||
class OptimizationBaseRepository( | ||
BaseFacade, Generic[FacadeOptimizationModelType, AbstractOptimizationModelType] | ||
): | ||
_run: abstract.Run | ||
_backend_repository: abstract.BackendBaseRepository[AbstractOptimizationModelType] | ||
_model_type: type[FacadeOptimizationModelType] | ||
|
||
def __init__(self, _run: abstract.Run, **kwargs: Unpack["InitKwargs"]) -> None: | ||
super().__init__(**kwargs) | ||
self._run = _run | ||
|
||
|
||
class Creator( | ||
OptimizationBaseRepository[ | ||
FacadeOptimizationModelType, AbstractOptimizationModelType | ||
], | ||
abstract.Creator, | ||
): | ||
def create( | ||
self, name: str, **kwargs: Unpack["abstract.optimization.base.CreateKwargs"] | ||
) -> FacadeOptimizationModelType: | ||
model = self._backend_repository.create( | ||
run_id=self._run.id, name=name, **kwargs | ||
) | ||
return self._model_type(_backend=self.backend, _model=model) | ||
|
||
|
||
class Retriever( | ||
OptimizationBaseRepository[ | ||
FacadeOptimizationModelType, AbstractOptimizationModelType | ||
], | ||
abstract.Retriever, | ||
): | ||
def get(self, name: str) -> FacadeOptimizationModelType: | ||
model = self._backend_repository.get(run_id=self._run.id, name=name) | ||
return self._model_type(_backend=self.backend, _model=model) | ||
|
||
|
||
class Lister( | ||
OptimizationBaseRepository[ | ||
FacadeOptimizationModelType, AbstractOptimizationModelType | ||
], | ||
abstract.Lister, | ||
): | ||
def list(self, name: str | None = None) -> list[FacadeOptimizationModelType]: | ||
models = self._backend_repository.list(run_id=self._run.id, name=name) | ||
return [self._model_type(_backend=self.backend, _model=m) for m in models] | ||
|
||
|
||
class Tabulator( | ||
OptimizationBaseRepository[ | ||
FacadeOptimizationModelType, AbstractOptimizationModelType | ||
], | ||
abstract.Tabulator, | ||
): | ||
def tabulate(self, name: str | None = None) -> pd.DataFrame: | ||
return self._backend_repository.tabulate(run_id=self._run.id, name=name) | ||
|
||
|
||
class EnumerateKwargs(TypedDict, total=False): | ||
name: str | None | ||
|
||
|
||
class Enumerator( | ||
Lister[FacadeOptimizationModelType, AbstractOptimizationModelType], | ||
Tabulator[FacadeOptimizationModelType, AbstractOptimizationModelType], | ||
abstract.Enumerator, | ||
): | ||
def enumerate( | ||
self, table: bool = False, **kwargs: Unpack[EnumerateKwargs] | ||
) -> list[FacadeOptimizationModelType] | pd.DataFrame: | ||
return self.tabulate(**kwargs) if table else self.list(**kwargs) |
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
Oops, something went wrong.