Spaces:
Sleeping
Sleeping
File size: 8,935 Bytes
3d2a487 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | """Inference pipeline β coordinates β flood risk probability.
Flow:
1. Fetch last 72 h of hourly weather from Open-Meteo (weather_fetcher)
2. Engineer the same 13 features as the training ingestion stage
3. Apply the saved StandardScaler (fit on training data only)
4. Slice the last window_size (24) rows β tensor (1, 24, 13)
5. Run FloodLSTM β sigmoid probability in [0, 1]
6. Map to alert level LOW / MODERATE / HIGH / CRITICAL
Model and scaler are loaded once at startup and cached.
"""
from __future__ import annotations
import functools
from pathlib import Path
from typing import Any
import joblib
import numpy as np
import pandas as pd
import torch
from src.api.weather_fetcher import fetch_hourly
from src.config import load_default_config, PROJECT_ROOT
from src.pipeline.ingestion.loader import FEATURE_COLUMNS
from src.pipeline.training.model import build_model
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Alert thresholds
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _alert_level(prob: float) -> str:
"""Alert level for nowcast. Baseline for zero-signal inputs is ~0.5;
LOW boundary at 0.51 ensures uncertain OOD inputs show as LOW.
LOW prob < 0.51
MODERATE 0.51 β 0.72
HIGH 0.72 β 0.85
CRITICAL >= 0.85
"""
if prob < 0.51:
return "LOW"
if prob < 0.72:
return "MODERATE"
if prob < 0.85:
return "HIGH"
return "CRITICAL"
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Model + scaler β loaded once, cached for the lifetime of the process
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@functools.lru_cache(maxsize=1)
def _load_assets() -> tuple[Any, Any, dict]:
"""Return (model, scaler, cfg) β cached after first call."""
cfg = load_default_config()
device = torch.device("cpu")
# ββ Model βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ckpt_candidates = [
PROJECT_ROOT / cfg["paths"].get("models_dir", "models") / "best.pt",
PROJECT_ROOT / cfg["paths"]["checkpoints"] / "best.pt",
]
ckpt_path = next((p for p in ckpt_candidates if p.exists()), None)
model = build_model(cfg, num_features=len(FEATURE_COLUMNS)).to(device)
if ckpt_path is not None:
payload = torch.load(ckpt_path, map_location=device, weights_only=True)
model.load_state_dict(payload["model_state_dict"])
print(f"[inference] Loaded model from '{ckpt_path}' (epoch {payload.get('epoch', '?')})")
else:
print("[inference] WARNING: No checkpoint found. Using random weights β run 'dvc repro' first.")
model.eval()
# ββ Scaler ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
scaler_path = PROJECT_ROOT / cfg["paths"]["scaler"]
if not scaler_path.exists():
raise RuntimeError(
f"Scaler not found at '{scaler_path}'. Run 'dvc repro' (train stage) first."
)
scaler = joblib.load(scaler_path)
print(f"[inference] Loaded scaler from '{scaler_path}'")
return model, scaler, cfg
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Feature engineering (mirrors run_ingest_real.py but for a single location)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _engineer_features(df: pd.DataFrame) -> pd.DataFrame:
"""Apply the same 13-feature engineering as the ingestion stage.
Input df must have columns:
Precipitation_mm, Soil_Moisture, Temperature_C, Elevation_m, Timestamp
"""
df = df.copy().reset_index(drop=True)
p = df["Precipitation_mm"]
sm = df["Soil_Moisture"]
t = df["Temperature_C"]
# Rolling rain accumulation
df["Rain_3h"] = p.rolling(window=3, min_periods=1).sum()
df["Rain_6h"] = p.rolling(window=6, min_periods=1).sum()
df["Rain_12h"] = p.rolling(window=12, min_periods=1).sum()
df["Rain_24h"] = p.rolling(window=24, min_periods=1).sum()
# Interaction: precipitation Γ soil saturation
df["Precip_x_Soil"] = p * sm
# Soil moisture lags
df["Soil_lag1"] = sm.shift(1).fillna(sm.iloc[0])
df["Soil_lag3"] = sm.shift(3).fillna(sm.iloc[0])
# Soil rate of change
df["Soil_rate"] = sm - df["Soil_lag1"]
# Temperature lag
df["Temp_lag1"] = t.shift(1).fillna(t.iloc[0])
return df.dropna().reset_index(drop=True)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Public inference function
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def predict(lat: float, lon: float) -> dict:
"""Run the full inference pipeline for a geographic coordinate.
Args:
lat: Latitude (-90 to 90)
lon: Longitude (-180 to 180)
Returns:
dict with keys:
lat, lon, flood_probability, alert_level,
window_hours, latest_timestamp, features_snapshot
"""
model, scaler, cfg = _load_assets()
window_size = int(cfg["data"]["window_size"]) # 24
device = torch.device("cpu")
# ββ 1. Fetch weather ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
raw_df = fetch_hourly(lat, lon)
# ββ 2. Engineer features ββββββββββββββββββββββββββββββββββββββββββββββββββ
feat_df = _engineer_features(raw_df)
if len(feat_df) < window_size:
raise ValueError(
f"After feature engineering only {len(feat_df)} rows available; "
f"need at least {window_size}."
)
# Take the most recent window_size rows
window_df = feat_df.tail(window_size).reset_index(drop=True)
latest_ts = str(raw_df["Timestamp"].iloc[-1])
# ββ 3. Scale ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
X = window_df[FEATURE_COLUMNS].values.astype(np.float32) # (24, 13)
X_scaled = scaler.transform(X).astype(np.float32) # (24, 13)
# ββ 4. Inference ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
tensor = torch.from_numpy(X_scaled).unsqueeze(0).to(device) # (1, 24, 13)
with torch.no_grad():
prob: float = float(model(tensor).item())
# ββ 5. Feature snapshot (last row β most recent hour) βββββββββββββββββββββ
last_row = window_df.iloc[-1]
features_snapshot = {col: round(float(last_row[col]), 4) for col in FEATURE_COLUMNS}
return {
"lat": lat,
"lon": lon,
"flood_probability": prob,
"alert_level": _alert_level(prob),
"window_hours": window_size,
"latest_timestamp": latest_ts,
"features_snapshot": features_snapshot,
}
|