-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #147 add ml model * add migration for datamodel * add model filter + tests * PR comment, rename function
- Loading branch information
1 parent
b46fef5
commit 127e7e4
Showing
11 changed files
with
235 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Add ml models table | ||
Revision ID: 34891d466985 | ||
Revises: fb27362e3b6b | ||
Create Date: 2024-08-07 12:26:23.631105 | ||
""" | ||
import sqlalchemy as sa | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "34891d466985" | ||
down_revision = "fb27362e3b6b" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"ml_model", | ||
sa.Column( | ||
"model_uuid", sa.UUID(), server_default=sa.text("gen_random_uuid()"), nullable=False | ||
), | ||
sa.Column("name", sa.String(), nullable=True), | ||
sa.Column("version", sa.String(), nullable=True), | ||
sa.Column("created_utc", sa.DateTime(), nullable=True), | ||
sa.PrimaryKeyConstraint("model_uuid"), | ||
) | ||
op.add_column( | ||
"forecast_values", | ||
sa.Column( | ||
"ml_model_uuid", | ||
sa.UUID(), | ||
nullable=True, | ||
comment="The ML Model this forcast value belongs to", | ||
), | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_column("forecast_values", "ml_model_uuid") | ||
op.drop_table("ml_model") | ||
# ### end Alembic commands ### |
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,52 @@ | ||
""" Read functions for getting ML models. """ | ||
import logging | ||
from typing import Optional | ||
|
||
from sqlalchemy.orm import Session | ||
|
||
from pvsite_datamodel.sqlmodels import MLModelSQL | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_or_create_model(session: Session, name: str, version: Optional[str] = None) -> MLModelSQL: | ||
""" | ||
Get model object from name and version. | ||
A new model is made if it doesn't not exists | ||
: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
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.