-
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 complex filters for RunMetaEntries (#26)
* split meta.py into model and repo * add __init__.py * add filter * add base filter * adjust abstract signature and docs * export meta base filter * adjust api and api client * adjust core api * adjust tests * add PATCH query endpoint * fix bug for non-default run meta entries * fix unused import * Fix invalid-escape-sequences * Add tests for meta-default-run filters * Add tests for meta-key filters --------- Co-authored-by: Fridolin Glatter <glatter@iiasa.ac.at> Co-authored-by: Pino Mussak <mussak@iiasa.ac.at>
- Loading branch information
1 parent
c482ef4
commit 64b65e3
Showing
15 changed files
with
258 additions
and
132 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,16 +1,14 @@ | ||
from typing import List | ||
|
||
import pandas as pd | ||
|
||
from .base import BaseFacade | ||
|
||
|
||
class MetaRepository(BaseFacade): | ||
def tabulate(self, run_ids: List[int]) -> pd.DataFrame: | ||
def tabulate(self, **kwargs) -> pd.DataFrame: | ||
# TODO: accept list of `Run` instances as arg | ||
# TODO: expand run-id to model-scenario-version-id columns | ||
return ( | ||
self.backend.meta.tabulate(run_ids=run_ids) | ||
self.backend.meta.tabulate(**kwargs) | ||
.drop(columns=["id", "type"]) | ||
.rename(columns={"run__id": "run_id"}) | ||
) |
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,23 @@ | ||
from typing import ClassVar | ||
|
||
from ixmp4.db import filters | ||
|
||
from .. import RunMetaEntry | ||
|
||
|
||
class RunMetaEntryFilter(filters.BaseFilter, metaclass=filters.FilterMeta): | ||
sqla_model: ClassVar[type] = RunMetaEntry | ||
|
||
id: filters.Id | ||
type: filters.String | ||
run__id: filters.Integer = filters.Field(None, alias="run_id") | ||
|
||
key: filters.String | ||
|
||
value_int: filters.Integer | ||
value_str: filters.String | ||
value_float: filters.Float | ||
value_bool: filters.Boolean | ||
|
||
def join(self, exc, **kwargs): | ||
return exc |
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,2 @@ | ||
from .model import RunMetaEntry | ||
from .repository import RunMetaEntryRepository |
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,19 @@ | ||
from ixmp4.data.db import filters as base | ||
from ixmp4.data.db.run import Run | ||
from ixmp4.db import filters, utils | ||
|
||
from .model import RunMetaEntry | ||
|
||
|
||
class RunFilter(base.RunFilter, metaclass=filters.FilterMeta): | ||
def join(self, exc, **kwargs): | ||
if not utils.is_joined(exc, Run): | ||
return exc.join(Run, onclause=RunMetaEntry.run__id == Run.id) | ||
else: | ||
return exc | ||
|
||
|
||
class RunMetaEntryFilter(base.RunMetaEntryFilter, metaclass=filters.FilterMeta): | ||
run: RunFilter = filters.Field( | ||
default=RunFilter(id=None, version=None, is_default=True) | ||
) |
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,76 @@ | ||
from typing import ClassVar | ||
|
||
from ixmp4 import db | ||
from ixmp4.core.exceptions import InvalidRunMeta | ||
from ixmp4.data import abstract, types | ||
|
||
from .. import base | ||
|
||
|
||
class RunMetaEntry(base.BaseModel): | ||
NotFound: ClassVar = abstract.RunMetaEntry.NotFound | ||
NotUnique: ClassVar = abstract.RunMetaEntry.NotUnique | ||
DeletionPrevented: ClassVar = abstract.RunMetaEntry.DeletionPrevented | ||
|
||
Type: ClassVar = abstract.RunMetaEntry.Type | ||
|
||
_column_map = { | ||
abstract.RunMetaEntry.Type.INT: "value_int", | ||
abstract.RunMetaEntry.Type.STR: "value_str", | ||
abstract.RunMetaEntry.Type.FLOAT: "value_float", | ||
abstract.RunMetaEntry.Type.BOOL: "value_bool", | ||
} | ||
|
||
__table_args__ = ( | ||
db.UniqueConstraint( | ||
"run__id", | ||
"key", | ||
), | ||
) | ||
updateable_columns = [ | ||
"type", | ||
"value_int", | ||
"value_str", | ||
"value_float", | ||
"value_bool", | ||
] | ||
|
||
run__id: types.Integer = db.Column( | ||
db.Integer, | ||
db.ForeignKey("run.id"), | ||
nullable=False, | ||
index=True, | ||
) | ||
run = db.relationship( | ||
"Run", | ||
backref="meta", | ||
foreign_keys=[run__id], | ||
) | ||
|
||
key: types.String = db.Column(db.String(1023), nullable=False) | ||
type: types.String = db.Column(db.String(20), nullable=False) | ||
|
||
value_int: types.Integer = db.Column(db.Integer, nullable=True) | ||
value_str: types.String = db.Column(db.String(1023), nullable=True) | ||
value_float: types.Float = db.Column(db.Float, nullable=True) | ||
value_bool: types.Boolean = db.Column(db.Boolean, nullable=True) | ||
|
||
@property | ||
def value(self) -> abstract.MetaValue: | ||
type_ = RunMetaEntry.Type(self.type) | ||
col = self._column_map[type_] | ||
return getattr(self, col) | ||
|
||
def __init__(self, *args, **kwargs) -> None: | ||
value = kwargs.pop("value") | ||
value_type = type(value) | ||
try: | ||
type_ = RunMetaEntry.Type.from_pytype(value_type) | ||
col = self._column_map[type_] | ||
except KeyError: | ||
raise InvalidRunMeta( | ||
f"Invalid type `{value_type}` for value of `RunMetaEntry`." | ||
) | ||
kwargs["type"] = type_ | ||
kwargs[col] = value | ||
super().__init__(*args, **kwargs) |
Oops, something went wrong.