mirror of
https://github.com/Dasemu/skipredictor.git
synced 2026-11-26 07:16:15 +00:00
74 lines
3.3 KiB
Python
74 lines
3.3 KiB
Python
"""Analysis script to validate and derive deterministic operational rules from historical data.
|
|
|
|
This script scans the historical dataset to find categorical thresholds (e.g., wind speed,
|
|
snow depth) where the station's open status is 100% predictable. These values
|
|
should be used as 'hard rules' in config/stations.yaml.
|
|
"""
|
|
|
|
import logging
|
|
import pandas as pd
|
|
from storage import SQLiteStore, HISTORICAL_FEATURED_TABLE
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def analyze_station_thresholds(station_id: str, df: pd.DataFrame):
|
|
"""Find the highest wind speed where the station ever opened and the minimum snow depth."""
|
|
station_df = df[df["station_id"] == station_id].copy()
|
|
if station_df.empty:
|
|
logger.warning(f"No data for {station_id}")
|
|
return
|
|
|
|
# 1. Wind Threshold Analysis
|
|
# Find the maximum wind gust ever recorded while the station was OPEN (open_status=1)
|
|
open_days = station_df[station_df["open_status"] == 1]
|
|
if not open_days.empty:
|
|
# Use wind_gusts_10m_max if available, fallback to wind_speed_10m_max
|
|
wind_col = "wind_gusts_10m_max" if "wind_gusts_10m_max" in station_df.columns else "wind_speed_10m_max"
|
|
max_wind_when_open = open_days[wind_col].max()
|
|
logger.info(f"[{station_id}] Max wind recorded while OPEN: {max_wind_when_open:.1f} km/h")
|
|
|
|
# Find 100% closure zone: starting from max_wind_when_open, find the next threshold
|
|
# where we have enough samples and ZERO are open.
|
|
for w in range(int(max_wind_when_open), 130, 2):
|
|
total_days = len(station_df[station_df[wind_col] >= w])
|
|
if total_days >= 5: # Statistical significance
|
|
open_at_w = len(station_df[(station_df[wind_col] >= w) & (station_df["open_status"] == 1)])
|
|
if open_at_w == 0:
|
|
logger.info(f"[{station_id}] SUGGESTED WIND THRESHOLD: {w} km/h (Deterministic closure)")
|
|
break
|
|
|
|
# 2. Snow Depth Analysis
|
|
if not open_days.empty:
|
|
min_snow_when_open = open_days["snow_depth"].min()
|
|
logger.info(f"[{station_id}] Min snow depth while OPEN: {min_snow_when_open:.1f} cm")
|
|
|
|
# Find 100% closure zone for snow (below threshold)
|
|
for s in range(int(min_snow_when_open), 0, -2):
|
|
total_days = len(station_df[station_df["snow_depth"] <= s])
|
|
if total_days >= 5:
|
|
open_at_s = len(station_df[(station_df["snow_depth"] <= s) & (station_df["open_status"] == 1)])
|
|
if open_at_s == 0:
|
|
logger.info(f"[{station_id}] SUGGESTED MIN SNOW: {s} cm (Deterministic closure)")
|
|
break
|
|
|
|
def main():
|
|
logging.basicConfig(level=logging.INFO, format="%(message)s")
|
|
store = SQLiteStore()
|
|
|
|
# Check multiple tables to find best available historical data
|
|
df = store.read_dataframe(HISTORICAL_FEATURED_TABLE)
|
|
if df.empty:
|
|
from storage import HISTORICAL_MERGED_TABLE
|
|
df = store.read_dataframe(HISTORICAL_MERGED_TABLE)
|
|
|
|
if df.empty:
|
|
logger.error("No historical data found in SQLite. Run historical collection scripts first.")
|
|
return
|
|
|
|
logger.info("--- Data-Driven Rule Validation ---\n")
|
|
for station_id in df["station_id"].unique():
|
|
analyze_station_thresholds(station_id, df)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|