Files
ML-py/tests/test_artifacts.py
T
Noel Sabia 939a8db482 feat: complete ML pipeline, FastAPI service, small model, tests, image
- finish the global HistGB bundle, scenario forecasts and 28-day ablation
- export sensors/series/forecasts/metrics/context plus baked weather frames
- add FastAPI health/meta/predict/simulate and background /train jobs
- add the p75 busy classifier, retraining from CSV, and hot-swap persistence
- add pytest coverage, multi-stage non-root Dockerfile, README and model card

Holdout: weather 7d MAE 3.32 / WAPE 27.5% / R2 0.777 vs seasonal naive
MAE 4.23 / WAPE 35.0% / R2 0.656 (5,350 eval rows).
2026-11-14 05:01:16 +01:00

79 lines
3.0 KiB
Python

from __future__ import annotations
import json
from pulsecast import config
def _load(name: str):
return json.loads((config.artifacts_dir() / name).read_text())
def test_sensors_schema():
sensors = _load("sensors.json")
assert len(sensors) == 8
assert {sensor["id"] for sensor in sensors} == set(config.USABLE_SENSORS)
assert [sensor["id"] for sensor in sensors] == sorted(sensor["id"] for sensor in sensors)
for sensor in sensors:
for key in ("id", "name", "lat", "lon", "mean", "p95", "battery"):
assert key in sensor
assert sensor["p95"] >= sensor["mean"]
assert sensor["battery"] is None or 0 <= sensor["battery"] <= 100
def test_series_schema():
series = _load("series.json")
assert series["step"] == "1h"
assert set(series["series"]) == set(config.USABLE_SENSORS)
for points in series["series"].values():
assert len(points) > 1000
assert {"t", "v"} == set(points[0])
assert points[0]["t"].endswith("Z")
assert all(point["v"] >= 0 for point in points)
def test_forecasts_schema():
forecasts = _load("forecasts.json")
assert forecasts["forecasts"].keys() == set(config.USABLE_SENSORS)
for by_horizon in forecasts["forecasts"].values():
assert set(by_horizon) == {"48h", "7d"}
assert set(by_horizon["7d"]) == set(config.SCENARIOS)
assert len(by_horizon["7d"]["base"]) == 168
assert len(by_horizon["48h"]["base"]) == 48
for points in by_horizon["7d"].values():
assert all({"t", "v", "lo", "hi"} == set(point) for point in points)
assert all(point["lo"] <= point["v"] <= point["hi"] for point in points)
def test_context_schema():
context = _load("context.json")
assert context["cached"] is False
assert context["updated_at"].endswith("Z")
now = context["weather"]["now"]
for key in ("t", "temp_c", "precip_mm", "cloud_pct", "wind_kmh", "humidity_pct"):
assert key in now
assert len(context["weather"]["forecast"]) == 168
holiday_dates = {holiday["date"] for holiday in context["holidays"]}
assert "2026-11-01" in holiday_dates
assert "2025-01-01" in holiday_dates
assert "2026-12-26" in holiday_dates
def test_metrics_and_audit():
metrics = _load("metrics.json")
assert metrics["ablation"][0]["name"] == "seasonal_naive"
assert metrics["ablation"][-1]["name"] == "week_weather"
assert metrics["metrics"]["mae"] < metrics["metrics"]["baseline_mae"]
assert metrics["metrics"]["wape"] < metrics["metrics"]["baseline_wape"]
assert len(metrics["features"]) == 10
assert len(metrics["limits"]) >= 4
audit = _load("data_audit.json")
assert audit["all_checks_ok"] is True
assert audit["raw_rows"] == 192_464
assert audit["usable_sensors"] == 8
assert set(audit["density_01_devices"]) == {"863f75b0", "f8c19730"}
assert (config.artifacts_dir() / "model.joblib").exists()
assert (config.artifacts_dir() / "small_model.joblib").exists()