app.models module#

Declarative SQLAlchemy ORM models used across the application.

This module defines the core database entities for the platform: coordinates for data collection, time-stamped weather observations, a singleton table for training status, and an append-only log of ML training runs. These models are the canonical schema definition for the application and are used by both the FastAPI backend and the standalone Slurm-executed training jobs for reading and writing domain data.

See Also#

app.database

Engine/session factories and declarative Base.

app.schemas

Pydantic schemas mirroring these ORM models.

app.ml_utils

Read-only helpers that query TrainingLog.

app.ml_train

Slurm-run training job that writes TrainingLog entries.

app.coordinates_manager

Utilities for seeding coordinate grids.

app.imputation

Imputation routines operating on WeatherObservation.

Notes#

  • Primary role: provide ORM mappings for coordinates, observations, training status, and training logs bound to app.database.Base.

  • Key dependencies: a configured SQLAlchemy engine via app.database.engine and corresponding session factories. The schema is created via app.database.ensure_database_schema().

  • Invariants: WeatherObservation uses a composite primary key (timestamp, latitude, longitude). TrainingStatus is treated as a singleton with primary key id=1. TrainingLog.horizon is a non-null string key identifying coordinate+horizon groupings.

Examples#

>>> # Basic query pattern (requires an initialized DB)
>>> from app.database import SessionLocal, ensure_database_schema
>>> from app.models import Coordinate, TrainingLog
>>> ensure_database_schema()
>>> with SessionLocal() as session:
...     count = session.query(Coordinate).count()
...     latest = (session.query(TrainingLog)
...               .order_by(TrainingLog.timestamp.desc())
...               .first())
class app.models.Coordinate(*args: Any, **kwargs: Any)[source]#

Bases: Base

Geographic point used for weather data collection.

Attributes:
idint

Surrogate primary key.

latitudefloat

Coordinate latitude in decimal degrees.

longitudefloat

Coordinate longitude in decimal degrees.

labelstr | None

Optional human-readable label for the coordinate.

is_centralbool

Marks the central coordinate used as a reference point.

Notes

  • No uniqueness constraint is enforced at the ORM level for (latitude, longitude); duplicates are possible unless prevented by a database constraint or application logic.

class app.models.TrainingLog(*args: Any, **kwargs: Any)[source]#

Bases: Base

Append-only log of ML training runs and scores.

Each row represents one training execution for a given horizon and (optionally) a specific coordinate. Scores from both a Scikit-learn model and a PyTorch model are recorded, along with the number of data points used.

Attributes:
idstr

Primary key string. The training pipeline typically assigns a uuid4 string explicitly when inserting a row. The default defined here is a string constant created at import time and should be overridden by callers to avoid collisions.

timestampdatetime

Completion time of the training run in UTC.

horizonstr

Non-empty key identifying the grouping (often "<coord>_<horizon_label>").

sklearn_scorefloat

R^2 score from the Scikit-learn model.

pytorch_scorefloat

R^2 score from the PyTorch model.

data_countint

Number of samples used for training/evaluation for this run.

coord_latitudefloat | None

Coordinate latitude associated with the run, if available.

coord_longitudefloat | None

Coordinate longitude associated with the run, if available.

horizon_labelstr | None

Human-friendly label for the horizon (e.g., "5min", "1h").

class app.models.TrainingStatus(*args: Any, **kwargs: Any)[source]#

Bases: Base

Snapshot of the current ML training state.

This singleton table reflects whether a training job is running, when the last successful training completed, and which horizon is currently being processed. The application typically ensures that there is exactly one row with id=1 and updates it transactionally during training flows.

Attributes:
idint

Primary key. Conventionally set to 1 to model a singleton row.

is_trainingbool

Flag indicating whether a training job is currently running.

last_trained_atdatetime | None

Timestamp of the last completed training job in UTC, if any.

train_countint

Monotonic counter of completed training runs.

current_horizonstr | None

Human-readable horizon label (e.g., "5min") or status message.

class app.models.WeatherObservation(*args: Any, **kwargs: Any)[source]#

Bases: Base

Nowcast weather values for a specific time and location.

This table mirrors the structure of MET Norway Nowcast 2.0 fields consumed by the application. The composite primary key (timestamp, latitude, longitude) uniquely identifies each observation in time and space.

Attributes:
timestampdatetime

Observation timestamp in UTC (part of the composite primary key).

latitudefloat

Coordinate latitude in decimal degrees (part of the primary key).

longitudefloat

Coordinate longitude in decimal degrees (part of the primary key).

air_temperaturefloat | None

Air temperature in degrees Celsius.

wind_speedfloat | None

Wind speed in meters per second.

wind_directionfloat | None

Wind direction in degrees (from which the wind is coming).

cloud_area_fractionfloat | None

Fraction of the sky covered by clouds (0–1).

precipitation_amountfloat | None

Precipitation amount in millimeters for the interval.

is_imputedbool

Whether this record was imputed by preprocessing routines.