-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add get models + tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * rename * refactor * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
4f51d06
commit b30a662
Showing
7 changed files
with
143 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
""" Read functions for models""" | ||
|
||
from datetime import datetime | ||
from typing import Optional | ||
|
||
from sqlalchemy.orm import Session | ||
|
||
from nowcasting_datamodel.models import MLModelSQL | ||
from nowcasting_datamodel.models.forecast import ForecastSQL | ||
from nowcasting_datamodel.read.read import logger | ||
|
||
|
||
def get_models( | ||
session, with_forecasts: Optional[bool] = False, forecast_created_utc: Optional[datetime] = None | ||
) -> MLModelSQL: | ||
""" | ||
Get all models from the database, distinct on name | ||
Can also filter if the model has forecasts, and if they are made after a certain time | ||
:param session: sql session | ||
:param with_forecasts: only get models with forecasts | ||
:param forecast_created_utc: only look at forecast created after this time | ||
:return: | ||
""" | ||
|
||
query = session.query(MLModelSQL) | ||
query = query.distinct(MLModelSQL.name) | ||
|
||
if with_forecasts: | ||
query = query.join(ForecastSQL) | ||
|
||
if forecast_created_utc: | ||
query = query.filter(ForecastSQL.created_utc > forecast_created_utc) | ||
|
||
# order by | ||
query = query.order_by(MLModelSQL.name) | ||
|
||
# get all results | ||
models = query.all() | ||
|
||
return models | ||
|
||
|
||
def get_model(session: Session, name: str, version: Optional[str] = None) -> MLModelSQL: | ||
""" | ||
Get model object from name and version | ||
:param session: database session | ||
:param name: name of the model | ||
:param version: version of the model | ||
return: Model object | ||
""" | ||
|
||
# start main query | ||
query = session.query(MLModelSQL) | ||
|
||
# filter on gsp_id | ||
query = query.filter(MLModelSQL.name == name) | ||
if version is not None: | ||
query = query.filter(MLModelSQL.version == version) | ||
|
||
# gets the latest version | ||
query = query.order_by(MLModelSQL.version.desc()) | ||
|
||
# get all results | ||
models = query.all() | ||
|
||
if len(models) == 0: | ||
logger.debug( | ||
f"Model for name {name} and version {version} does not exist so going to add it" | ||
) | ||
|
||
model = MLModelSQL(name=name, version=version) | ||
session.add(model) | ||
session.commit() | ||
|
||
else: | ||
model = models[0] | ||
|
||
return model |
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,56 @@ | ||
from nowcasting_datamodel.fake import make_fake_forecast | ||
|
||
from datetime import datetime, timedelta, timezone | ||
|
||
from nowcasting_datamodel.models import MLModel | ||
from nowcasting_datamodel.read.read_models import get_models, get_model | ||
|
||
|
||
def test_get_models(db_session): | ||
|
||
_ = make_fake_forecast(session=db_session, gsp_id=1) | ||
|
||
models = get_models(session=db_session) | ||
assert len(models) == 1 | ||
|
||
|
||
def test_get_models_no_models(db_session): | ||
models = get_models(session=db_session) | ||
assert len(models) == 0 | ||
|
||
|
||
def test_get_models_no_forecasts(db_session): | ||
get_model(session=db_session, name="test") | ||
|
||
models = get_models(session=db_session, with_forecasts=True) | ||
assert len(models) == 0 | ||
|
||
|
||
def test_get_models_multiple_models(db_session): | ||
|
||
_ = make_fake_forecast(session=db_session, gsp_id=1, model_name="test_1") | ||
_ = make_fake_forecast(session=db_session, gsp_id=1, model_name="test_2") | ||
|
||
models = get_models(session=db_session) | ||
assert len(models) == 2 | ||
|
||
|
||
def testget_models_after_created(db_session): | ||
_ = make_fake_forecast(session=db_session, gsp_id=1, model_name="test_1") | ||
|
||
models = get_models( | ||
session=db_session, | ||
with_forecasts=True, | ||
forecast_created_utc=datetime.now(tz=timezone.utc) + timedelta(days=1), | ||
) | ||
assert len(models) == 0 | ||
|
||
|
||
def test_get_model(db_session): | ||
model_read_1 = get_model(session=db_session, name="test_name", version="9.9.9") | ||
model_read_2 = get_model(session=db_session, name="test_name", version="9.9.9") | ||
|
||
assert model_read_1.name == model_read_2.name | ||
assert model_read_1.version == model_read_2.version | ||
|
||
_ = MLModel.from_orm(model_read_2) |
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