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.databaseEngine/session factories and declarative
Base.app.schemasPydantic schemas mirroring these ORM models.
app.ml_utilsRead-only helpers that query
TrainingLog.app.ml_trainSlurm-run training job that writes
TrainingLogentries.app.coordinates_managerUtilities for seeding coordinate grids.
app.imputationImputation 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.engineand corresponding session factories. The schema is created viaapp.database.ensure_database_schema().Invariants:
WeatherObservationuses a composite primary key(timestamp, latitude, longitude).TrainingStatusis treated as a singleton with primary keyid=1.TrainingLog.horizonis 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:
BaseGeographic point used for weather data collection.
- Attributes:
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:
BaseAppend-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:
- id
str Primary key string. The training pipeline typically assigns a
uuid4string 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.- timestamp
datetime Completion time of the training run in UTC.
- horizon
str Non-empty key identifying the grouping (often
"<coord>_<horizon_label>").- sklearn_score
float R^2 score from the Scikit-learn model.
- pytorch_score
float R^2 score from the PyTorch model.
- data_count
int Number of samples used for training/evaluation for this run.
- coord_latitude
float|None Coordinate latitude associated with the run, if available.
- coord_longitude
float|None Coordinate longitude associated with the run, if available.
- horizon_label
str|None Human-friendly label for the horizon (e.g.,
"5min","1h").
- id
- class app.models.TrainingStatus(*args: Any, **kwargs: Any)[source]#
Bases:
BaseSnapshot 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=1and updates it transactionally during training flows.- Attributes:
- id
int Primary key. Conventionally set to
1to model a singleton row.- is_trainingbool
Flag indicating whether a training job is currently running.
- last_trained_at
datetime|None Timestamp of the last completed training job in UTC, if any.
- train_count
int Monotonic counter of completed training runs.
- current_horizon
str|None Human-readable horizon label (e.g.,
"5min") or status message.
- id
- class app.models.WeatherObservation(*args: Any, **kwargs: Any)[source]#
Bases:
BaseNowcast 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:
- timestamp
datetime Observation timestamp in UTC (part of the composite primary key).
- latitude
float Coordinate latitude in decimal degrees (part of the primary key).
- longitude
float Coordinate longitude in decimal degrees (part of the primary key).
- air_temperature
float|None Air temperature in degrees Celsius.
- wind_speed
float|None Wind speed in meters per second.
- wind_direction
float|None Wind direction in degrees (from which the wind is coming).
- cloud_area_fraction
float|None Fraction of the sky covered by clouds (0–1).
- precipitation_amount
float|None Precipitation amount in millimeters for the interval.
- is_imputedbool
Whether this record was imputed by preprocessing routines.
- timestamp