mirror of
https://github.com/Dasemu/skipredictor.git
synced 2026-11-26 07:16:15 +00:00
- Remove internal YOLO counts (cars/people/buses/total_vehicles) from crowd_analysis table - Keep only crowd_density and confidence_scores for persistence (no ML training impact) - Upgrade from YOLOv8n to YOLOv11m for better person detection (fewer false positives) - Add regional holidays: Día de Castilla y León (4/23), Santiago Apóstol (7/25, shared with Galicia) - Add Easter Monday (moveable, 2018-2035) for Castilla y León and Galicia - Update all tests to match simplified crowd_analysis schema Tests: all 38 SQLite/crowd_analyzer tests + 13 feature tests passing.
132 lines
4.0 KiB
Python
132 lines
4.0 KiB
Python
"""Unit tests for the SQLite-backed persistence layer."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from storage import SQLiteStore
|
|
|
|
|
|
def test_current_forecasts_roundtrip(tmp_path):
|
|
"""Forecasts must round-trip through current_forecasts table."""
|
|
store = SQLiteStore(tmp_path / "skipredictor.db")
|
|
forecasts = [
|
|
{
|
|
"station_id": "pajares",
|
|
"prediction_date": "2026-03-14",
|
|
"open_probability": 0.82,
|
|
"km_open": 12.4,
|
|
"crowd_level": "medium",
|
|
"weather_summary": "Temp -2-5 C",
|
|
"rule_notes": ["Wind below threshold."],
|
|
"prediction_source": "ml",
|
|
}
|
|
]
|
|
|
|
store.save_current_forecasts(forecasts, run_hour=10)
|
|
loaded = store.load_current_forecasts()
|
|
|
|
assert len(loaded) == 1
|
|
row = loaded[0]
|
|
assert row["station_id"] == "pajares"
|
|
assert row["prediction_date"] == "2026-03-14"
|
|
assert row["open_probability"] == 0.82
|
|
assert row["km_open"] == 12.4
|
|
|
|
|
|
def test_archive_moves_to_past_forecasts(tmp_path):
|
|
"""Archiving must move rows from current to past_forecasts."""
|
|
store = SQLiteStore(tmp_path / "skipredictor.db")
|
|
forecasts = [
|
|
{
|
|
"station_id": "pajares",
|
|
"prediction_date": "2026-03-14",
|
|
"open_probability": 0.79,
|
|
"km_open": 10.1,
|
|
"crowd_level": "low",
|
|
"weather_summary": "Temp 0-5 C",
|
|
"rule_notes": [],
|
|
"prediction_source": "ml",
|
|
}
|
|
]
|
|
|
|
store.save_current_forecasts(forecasts, run_hour=5)
|
|
archived = store.archive_current_forecasts()
|
|
|
|
assert archived == 1
|
|
assert store.load_current_forecasts() == []
|
|
past = store.load_past_forecasts()
|
|
assert len(past) == 1
|
|
assert past[0]["station_id"] == "pajares"
|
|
|
|
|
|
def test_feature_importances_replace_latest_values(tmp_path):
|
|
"""Saving importances twice for the same target must replace older rows."""
|
|
store = SQLiteStore(tmp_path / "skipredictor.db")
|
|
|
|
store.save_feature_importances(
|
|
target="station_open",
|
|
importances={"snow_depth": 0.9, "wind_gusts_10m_max": 0.5},
|
|
)
|
|
store.save_feature_importances(
|
|
target="station_open",
|
|
importances={"snow_depth": 0.2, "temperature_2m_mean": 0.8},
|
|
)
|
|
|
|
assert store.load_feature_importances("station_open") == [
|
|
("temperature_2m_mean", 0.8),
|
|
("snow_depth", 0.2),
|
|
]
|
|
|
|
|
|
def test_evaluation_run_roundtrip(tmp_path):
|
|
"""Evaluation reports must be persisted and reloaded from SQLite."""
|
|
store = SQLiteStore(tmp_path / "skipredictor.db")
|
|
report = {
|
|
"station_open": {"accuracy": 0.91},
|
|
"km_open": {"mae": 1.2},
|
|
}
|
|
|
|
run_id = store.save_evaluation_run(
|
|
dataset_table="historical_featured_dataset",
|
|
report=report,
|
|
models_dir="ml/models",
|
|
)
|
|
|
|
loaded = store.load_latest_evaluation_run()
|
|
assert loaded is not None
|
|
assert loaded["id"] == run_id
|
|
assert loaded["dataset_table"] == "historical_featured_dataset"
|
|
assert loaded["report"] == report
|
|
assert loaded["models_dir"] == "ml/models"
|
|
|
|
|
|
def test_crowd_analysis_roundtrip(tmp_path):
|
|
"""Crowd analysis rows must be queryable with parsed confidence scores."""
|
|
store = SQLiteStore(tmp_path / "skipredictor.db")
|
|
store.save_crowd_analysis(
|
|
{
|
|
"timestamp": "2026-03-13T11:00:00",
|
|
"station_id": "sanisidro",
|
|
"image_path": "/tmp/frame.jpg",
|
|
"crowd_density": "low",
|
|
"confidence_scores": [0.91, 0.88],
|
|
}
|
|
)
|
|
store.save_crowd_analysis(
|
|
{
|
|
"timestamp": "2026-03-13T12:00:00",
|
|
"station_id": "sanisidro",
|
|
"image_path": "/tmp/frame2.jpg",
|
|
"crowd_density": "medium",
|
|
"confidence_scores": [0.77],
|
|
}
|
|
)
|
|
|
|
rows = store.load_crowd_analyses(
|
|
station_id="sanisidro",
|
|
analysis_date="2026-03-13",
|
|
)
|
|
|
|
assert len(rows) == 2
|
|
assert rows.iloc[0]["confidence_scores"] == [0.91, 0.88]
|
|
assert rows.iloc[1]["crowd_density"] == "medium"
|