feat: initial ski predictor system

Hybrid ML + rules prediction system for 3 Asturian ski stations
(Pajares, San Isidro, Fuentes de Invierno).

- Weather data from Open-Meteo (historical + forecast)
- Station scraping from infonieve.es (live + 16 seasons historical TSV)
- Feature engineering: rolling snow, calendar, temperature/pressure trends
- ML models: RandomForest (open/close), GradientBoosting (km), LightGBM (crowd)
- Rules engine: wind closure, snow depth, rain+heat, heavy snow
- YOLOv8 webcam crowd detection (cars, people in parking/lifts)
- Telegram bot with /pajares, /sanisidro, /fuentes commands
- Daily cron pipeline for automated data collection and predictions
- Deployment guide with systemd service and cron setup
This commit is contained in:
dasemu committed 2026-03-12 14:51:32 +01:00
commit 1afe2b73e0
34 files changed
+7201

No files matched your search

+165
View File
@@ -0,0 +1,165 @@
"""Unit tests for the RulesEngine in predictor/rules.py.
Tests cover the physical and operational constraints that must override ML
predictions when conditions are deterministic:
- Extreme wind forces the station closed regardless of other conditions.
- Insufficient snow depth forces the station closed.
- Normal conditions produce no forced outcome.
- High temperature combined with rain reduces the estimated_km_factor.
"""
import pytest
from unittest.mock import patch
# ---------------------------------------------------------------------------
# Fixtures – station config
# ---------------------------------------------------------------------------
@pytest.fixture()
def station_config():
"""Minimal station configuration mirroring config/stations.yaml structure."""
return {
"pajares": {
"name": "Valgrande-Pajares",
"wind_closure_threshold": 80, # km/h
"minimum_snow_depth": 30, # cm
"max_km_skiable": 22.0,
}
}
@pytest.fixture()
def rules_engine(station_config):
"""RulesEngine instance with mocked config loading."""
with patch("predictor.rules._load_station_configs", return_value=station_config):
from predictor.rules import RulesEngine
engine = RulesEngine()
return engine
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _weather(
wind_gust: float = 40.0,
snow_depth: int = 60,
temperature: float = 2.0,
precipitation: float = 0.0,
) -> dict:
"""Build a minimal weather observation dict for testing."""
return {
"wind_gusts_10m_max": wind_gust,
"snow_depth": snow_depth,
"temperature_2m_max": temperature,
"precipitation_sum": precipitation,
}
# ---------------------------------------------------------------------------
# Tests – wind closure
# ---------------------------------------------------------------------------
class TestWindClosure:
"""Rules triggered by wind gust exceeding the station threshold."""
def test_wind_above_threshold_forces_closed(self, rules_engine):
"""Wind gust above 80 km/h must set force_closed to True."""
weather = _weather(wind_gust=85.0)
result = rules_engine.evaluate("pajares", weather)
assert result["force_closed"] is True
def test_wind_at_threshold_forces_closed(self, rules_engine):
"""Wind gust exactly at the threshold (80 km/h) must force closure."""
weather = _weather(wind_gust=80.0)
result = rules_engine.evaluate("pajares", weather)
assert result["force_closed"] is True
def test_wind_below_threshold_does_not_force_closed(self, rules_engine):
"""Wind gust below 80 km/h must not set force_closed."""
weather = _weather(wind_gust=75.0)
result = rules_engine.evaluate("pajares", weather)
assert result["force_closed"] is False
# ---------------------------------------------------------------------------
# Tests – snow depth
# ---------------------------------------------------------------------------
class TestSnowDepth:
"""Rules triggered by insufficient snow depth."""
def test_snow_below_minimum_forces_closed(self, rules_engine):
"""Snow depth below 30 cm must set force_closed to True."""
weather = _weather(snow_depth=20)
result = rules_engine.evaluate("pajares", weather)
assert result["force_closed"] is True
def test_snow_at_minimum_does_not_force_closed(self, rules_engine):
"""Snow depth exactly at 30 cm is acceptable and must not force closure."""
weather = _weather(snow_depth=30)
result = rules_engine.evaluate("pajares", weather)
assert result["force_closed"] is False
def test_snow_above_minimum_does_not_force_closed(self, rules_engine):
"""Snow depth well above the minimum must not force closure."""
weather = _weather(snow_depth=80)
result = rules_engine.evaluate("pajares", weather)
assert result["force_closed"] is False
# ---------------------------------------------------------------------------
# Tests – normal conditions
# ---------------------------------------------------------------------------
class TestNormalConditions:
"""No rules should trigger under benign conditions."""
def test_normal_conditions_produce_no_force(self, rules_engine):
"""Standard winter conditions must leave all force flags unset."""
weather = _weather(wind_gust=30.0, snow_depth=60, temperature=-2.0, precipitation=0.0)
result = rules_engine.evaluate("pajares", weather)
assert result["force_closed"] is False
assert result["estimated_km_factor"] == pytest.approx(1.0)
def test_normal_conditions_return_dict(self, rules_engine):
"""evaluate() must always return a dict with the expected keys."""
weather = _weather()
result = rules_engine.evaluate("pajares", weather)
assert isinstance(result, dict)
assert "force_closed" in result
assert "estimated_km_factor" in result
# ---------------------------------------------------------------------------
# Tests – rain + high temperature reduces estimated_km_factor
# ---------------------------------------------------------------------------
class TestRainAndHighTemperature:
"""Rain at positive temperatures should reduce the estimated open km."""
def test_high_temp_and_rain_reduces_km_factor(self, rules_engine):
"""Temperature > 10 C combined with precipitation must reduce estimated_km_factor below 1.0."""
weather = _weather(temperature=12.0, precipitation=8.0)
result = rules_engine.evaluate("pajares", weather)
assert result["estimated_km_factor"] < 1.0
def test_rain_without_high_temp_does_not_reduce_km_factor(self, rules_engine):
"""Rain at sub-zero temperatures must not reduce estimated_km_factor."""
weather = _weather(temperature=-3.0, precipitation=8.0)
result = rules_engine.evaluate("pajares", weather)
assert result["estimated_km_factor"] == pytest.approx(1.0)
def test_high_temp_without_rain_does_not_reduce_km_factor(self, rules_engine):
"""High temperature without precipitation must not reduce estimated_km_factor."""
weather = _weather(temperature=12.0, precipitation=0.0)
result = rules_engine.evaluate("pajares", weather)
assert result["estimated_km_factor"] == pytest.approx(1.0)