"""Precompute what-if scenarios for the static site (no model server needed). uv run --python 3.12 --with xgboost --with pandas --with pyarrow --with huggingface_hub \ python scripts/55_scenarios.py --out data/scenarios Uses the published model (TrendeX90/expected-greenness@v1, its own predict.py) and cells.parquet from TrendeX90/naryn-cube. For every pasture cell, month (May-Sep) and scenario, it computes expected NDVI and the change vs typical weather, then averages over blocks, districts and the oblast. Scenario grid: precipitation -30..+30 % (all precipitation inputs), temperature -2..+2 deg C (day and night land-surface temperature together). These are model sensitivities, not causal effects. """ from __future__ import annotations import argparse import importlib.util import json import sys import time from pathlib import Path import numpy as np import pandas as pd from huggingface_hub import hf_hub_download, snapshot_download MODEL, CUBE = "TrendeX90/expected-greenness", "TrendeX90/naryn-cube" MONTHS = [5, 6, 7, 8, 9] PRECIP = [-30, -20, -10, 0, 10, 20, 30] TEMP = [-2, -1, 0, 1, 2] def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("--out", type=Path, default=Path("data/scenarios")) a = ap.parse_args() a.out.mkdir(parents=True, exist_ok=True) t0 = time.time() root = Path(snapshot_download(MODEL, revision="v1", local_dir=a.out.parent / "model_v1")) spec = importlib.util.spec_from_file_location("predict", root / "predict.py") mod = importlib.util.module_from_spec(spec) sys.modules["predict"] = mod spec.loader.exec_module(mod) model = mod.load(root) cells = pd.read_parquet(hf_hub_download(CUBE, "cells.parquet", repo_type="dataset")) past = cells[cells["in_oblast"] & cells["mask_reason"].isna()] ids = past["cell_id"].values print(f"{len(ids)} pasture cells, model v1 loaded ({time.time() - t0:.0f}s)", flush=True) rows = [] for mo in MONTHS: for p in PRECIP: for t in TEMP: r = model.what_if(cells, ids, month=mo, precip_pct=p, lst_day_c=t, lst_night_c=t) rows.append(pd.DataFrame({"cell_id": ids, "month": mo, "precip_pct": p, "temp_c": t, "typical": r["expected_typical_weather"].values, "change": r["change_vs_typical"].values})) cellwise = pd.concat(rows, ignore_index=True).merge(past[["cell_id", "block_id", "district"]], on="cell_id") cellwise.to_parquet(a.out / "what_if_cells.parquet", index=False) print(f"{len(cellwise)} cell-scenario rows ({time.time() - t0:.0f}s)", flush=True) def nested(df: pd.DataFrame) -> dict: g = df.groupby(["month", "precip_pct", "temp_c"])["change"].mean() change = [[[round(float(g[(mo, p, t)]), 4) for t in TEMP] for p in PRECIP] for mo in MONTHS] typ = df[(df["precip_pct"] == 0) & (df["temp_c"] == 0)].groupby("month")["typical"].mean() return {"n_cells": int(df["cell_id"].nunique()), "typical": [round(float(typ[mo]), 4) for mo in MONTHS], "change": change} out = {"model": f"{MODEL}@v1", "source_cells": f"{CUBE}/cells.parquet (in_oblast and mask_reason is null)", "units": "NDVI", "months": MONTHS, "precip_pct": PRECIP, "temp_c": TEMP, "layout": "change[month_index][precip_index][temp_index]; expected = typical[month_index] + change", "meaning": "Expected greenness change vs typical weather for that month if precipitation and land-surface " "temperature differ by the given amounts. Model sensitivity, not a causal effect; " "not forage quality. Wetter-than-normal does not raise NDVI in this model (see model card).", "oblast": nested(cellwise), "districts": {d: nested(g) for d, g in cellwise.groupby("district")}, "blocks": {b: nested(g) for b, g in cellwise.groupby("block_id")}} path = a.out / "what_if.json" path.write_text(json.dumps(out, separators=(",", ":"))) ob = out["oblast"] jul = MONTHS.index(7) print(f"oblast July typical {ob['typical'][jul]}, -30% precip {ob['change'][jul][0][TEMP.index(0)]}, " f"+2C {ob['change'][jul][PRECIP.index(0)][-1]}") print(f"what_if.json {path.stat().st_size / 1e6:.2f} MB, {len(out['blocks'])} blocks, " f"{len(out['districts'])} districts ({time.time() - t0:.0f}s)") print("SCENARIOS_DONE") if __name__ == "__main__": main()