Spaces:
Sleeping
Sleeping
File size: 3,630 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 | """Risk Mapper — produce a flood risk report from a trained FloodLSTM."""
from __future__ import annotations
import json
import sys
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
import numpy as np
import torch
if __package__ is None or __package__ == "":
sys.path.append(str(Path(__file__).resolve().parents[1]))
from src.config import load_default_config, resolve_project_path
from src.pipeline.saving.checkpoint import load_model
def risk_class(prob: float) -> str:
if prob < 0.30:
return "Low"
if prob <= 0.70:
return "Moderate"
return "High"
def confidence(prob: float) -> float:
return abs(prob - 0.5) * 2.0
def _make_tabular_stub(cfg: dict[str, Any], device: torch.device) -> torch.Tensor:
"""Return a random normalised input tensor for demo inference.
Shape: (1, window_size, 20) — mimics one sliding-window sample.
If `inference.default_features` points to a JSON file containing a dict
of {feature_name: value} pairs, those values are used instead.
"""
from src.pipeline.ingestion.loader import FEATURE_COLUMNS # noqa: PLC0415
window_size = int(cfg["data"]["window_size"])
num_features = len(FEATURE_COLUMNS)
feature_path_str = str(cfg.get("inference", {}).get("default_features") or "")
if feature_path_str:
feat_path = resolve_project_path(feature_path_str)
if feat_path.exists():
raw = json.loads(feat_path.read_text(encoding="utf-8"))
row = np.array(
[float(raw.get(col, 0.0)) for col in FEATURE_COLUMNS],
dtype=np.float32,
)
# Tile the same feature vector over the window
seq = np.tile(row, (window_size, 1))
return torch.from_numpy(seq).unsqueeze(0).to(device) # (1, W, F)
# No features file → use random stub
rng = np.random.default_rng(int(cfg["project"]["seed"]))
seq = rng.standard_normal((window_size, num_features)).astype(np.float32)
return torch.from_numpy(seq).unsqueeze(0).to(device)
def main() -> None:
cfg = load_default_config()
for key in ("scaler", "checkpoints", "insight_reports"):
cfg["paths"][key] = str(resolve_project_path(cfg["paths"][key]))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = load_model(cfg, device)
features = _make_tabular_stub(cfg, device)
with torch.no_grad():
prob = float(model(features).item())
report = {
"timestamp_utc": datetime.now(timezone.utc).isoformat(),
"probability": round(prob, 6),
"risk_class": risk_class(prob),
"confidence": round(confidence(prob), 6),
"model_version": cfg["project"]["name"],
"window_size": cfg["data"]["window_size"],
"flood_threshold": cfg["data"]["flood_threshold"],
"data_provenance": {
"dataset": "flood.csv (Kaggle GFD tabular)",
"features": 20,
"label": "FloodProbability (regression)",
},
}
infer_cfg = cfg.get("inference", {})
output_str = str(infer_cfg.get("output_path", "")).strip()
if output_str:
output = resolve_project_path(output_str)
else:
output = (
resolve_project_path(cfg["paths"]["insight_reports"])
/ f"risk_report_{datetime.now(timezone.utc).strftime('%Y%m%dT%H%M%S')}.json"
)
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text(json.dumps(report, indent=2), encoding="utf-8")
print(json.dumps(report, indent=2))
if __name__ == "__main__":
main()
|