Files
ML-py/tests/test_predict.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

126 lines
4.3 KiB
Python

from __future__ import annotations
import pandas as pd
SENSOR = "hn-stadtgalerie-01"
def _assert_points(points: list[dict]) -> None:
assert points
for point in points:
assert set(point) == {"t", "v", "lo", "hi"}
assert point["t"].endswith("Z")
assert point["v"] >= 0
assert point["lo"] <= point["v"] <= point["hi"]
def test_predict_7d(client):
response = client.post(
"/predict", json={"sensor": SENSOR, "horizon": "7d", "scenario": "base"}
)
assert response.status_code == 200
payload = response.json()
assert payload["sensor"] == SENSOR
assert payload["horizon"] == "7d"
assert payload["scenario"] == "base"
assert len(payload["points"]) == 168
assert payload["model_version"].startswith("histgb-")
_assert_points(payload["points"])
def test_predict_all_horizons(client):
expected = {"15m": 1, "1h": 4, "48h": 48, "7d": 168}
for horizon, count in expected.items():
response = client.post("/predict", json={"sensor": SENSOR, "horizon": horizon})
assert response.status_code == 200, response.text
assert len(response.json()["points"]) == count, horizon
def test_predict_weather_override_changes_values(client):
cold = client.post(
"/predict",
json={"sensor": SENSOR, "horizon": "48h", "weather": {"temp_c": -8.0, "precip_mm": 6.0}},
)
warm = client.post(
"/predict",
json={"sensor": SENSOR, "horizon": "48h", "weather": {"temp_c": 30.0, "precip_mm": 0.0}},
)
assert cold.status_code == warm.status_code == 200
assert cold.json()["points"] != warm.json()["points"]
def test_predict_unknown_sensor(client):
response = client.post("/predict", json={"sensor": "nope", "horizon": "7d"})
assert response.status_code == 404
assert response.json()["error"]["code"] == "not_found"
def test_predict_bad_horizon_and_scenario(client):
response = client.post("/predict", json={"sensor": SENSOR, "horizon": "9d"})
assert response.status_code == 400
assert response.json()["error"]["code"] == "bad_request"
response = client.post("/predict", json={"sensor": SENSOR, "scenario": "snow"})
assert response.status_code == 400
assert response.json()["error"]["code"] == "bad_request"
def test_predict_validation_error_shape(client):
response = client.post("/predict", json={"horizon": "7d"})
assert response.status_code == 400
assert response.json()["error"]["code"] == "bad_request"
assert "sensor" in response.json()["error"]["message"]
def test_simulate_all_sensors_and_scenarios(client):
response = client.post(
"/simulate",
json={
"scenario": "sunny-saturday-market",
"weather": {"temp_delta_c": 4, "precip_mm": 0},
"sensors": "all",
},
)
assert response.status_code == 200
payload = response.json()
assert payload["scenario"] == "sunny-saturday-market"
assert len(payload["forecasts"]) == 8
for sensor_forecast in payload["forecasts"].values():
assert set(sensor_forecast) == {"15m", "1h", "48h", "7d"}
assert len(sensor_forecast["15m"]) == 1
assert len(sensor_forecast["1h"]) == 4
assert len(sensor_forecast["48h"]) == 48
assert len(sensor_forecast["7d"]) == 168
_assert_points(sensor_forecast["7d"])
def test_simulate_changes_saturday_store_hours(client):
def points(scenario: str):
response = client.post(
"/simulate", json={"scenario": scenario, "sensors": [SENSOR]}
)
assert response.status_code == 200
return response.json()["forecasts"][SENSOR]["7d"]
base = points("base")
sunny = points("sunny-saturday-market")
changed = []
for left, right in zip(base, sunny, strict=True):
local = pd.Timestamp(left["t"]).tz_convert("Europe/Berlin")
if local.day_of_week == 5 and 10 <= local.hour < 21:
changed.append(left["v"] != right["v"])
assert changed and any(changed)
rainy = points("rainy-saturday")
rainy_changed = [
left["v"] != right["v"]
for left, right in zip(sunny, rainy, strict=True)
]
assert any(rainy_changed)
def test_simulate_unknown_sensor(client):
response = client.post("/simulate", json={"scenario": "base", "sensors": ["nope"]})
assert response.status_code == 404
assert response.json()["error"]["code"] == "not_found"