File size: 5,608 Bytes
d7c810d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""TrendeX expected-greenness model: CPU inference. Imports only numpy, pandas and xgboost.

Files expected next to this module (the HF model repo TrendeX90/expected-greenness):
  model_a.ubj                  XGBoost model (predicts the NDVI anomaly)
  feature_schema.json          feature order
  climatology.parquet          cell_id, month, clim_ndvi (2001-2025 mean NDVI of that cell and month)
  weather_climatology.parquet  cell_id, month, <feature>_mean (raw weather value minus this = model anomaly)
  conformal.json               month -> [q10, q90] of residuals (P10/P90 band)

    from predict import load
    m = load("path/to/repo")
    out = m.predict(features_df)                 # features in feature_schema order
    out = m.what_if(cells_df, cell_ids, month=7, precip_pct=-30, lst_day_c=+1.5)

Output: pred_anom, expected (= clim_ndvi + pred_anom), p10, p90. NDVI units.
Meaning: greenness expected from weather and terrain alone. The gap to observed NDVI is change NOT
explained by precipitation, temperature, snow and soil moisture. It is not a cause and not forage quality.
"""
from __future__ import annotations

import json
from pathlib import Path

import numpy as np
import pandas as pd
import xgboost as xgb

STATIC = ["elev_mean", "elev_std", "slope_mean", "steep_share", "aspect_sin", "aspect_cos", "tpi",
          "grass_frac", "barren_frac", "lc_type1"]
WEATHER = ["precip_m", "precip_3m", "precip_winter", "lst_day_m", "lst_night_m", "lst_day_prev",
           "snow_spring", "snow_m", "soil_m", "swe_apr"]


class ExpectedGreenness:
    def __init__(self, root: str | Path):
        root = Path(root)
        self.schema = json.loads((root / "feature_schema.json").read_text())
        self.features = self.schema["features"]
        self.booster = xgb.Booster()
        self.booster.load_model(str(root / "model_a.ubj"))
        self.booster.set_param({"device": "cpu"})
        self.clim = pd.read_parquet(root / "climatology.parquet").set_index(["cell_id", "month"])
        self.wclim = pd.read_parquet(root / "weather_climatology.parquet").set_index(["cell_id", "month"])
        self.conf = {int(k): v for k, v in json.loads((root / "conformal.json").read_text()).items()}

    # ------------------------------------------------------------------ core
    def predict(self, df: pd.DataFrame) -> pd.DataFrame:
        """df must contain every feature in feature_schema.json (NaN allowed for weather)."""
        missing = [c for c in self.features if c not in df.columns]
        if missing:
            raise ValueError(f"missing features: {missing}")
        X = df[self.features].astype("float32")
        pred = self.booster.predict(xgb.DMatrix(X, feature_names=self.features))
        months = df["month"].astype(int).values
        lo = np.array([self.conf[m][0] for m in months])
        hi = np.array([self.conf[m][1] for m in months])
        exp = df["clim_ndvi"].values + pred
        return pd.DataFrame({"pred_anom": pred, "expected": exp, "p10": exp + lo, "p90": exp + hi}, index=df.index)

    # ------------------------------------------------------------------ helpers
    def features_from_raw(self, cells: pd.DataFrame, raw: pd.DataFrame) -> pd.DataFrame:
        """raw: cell_id, month and raw weather values named like WEATHER (mm, deg C, fractions, kg m-2).

        precip_3m = this + 2 previous months; precip_winter = Oct..Apr sum; snow_spring = Mar-May mean;
        swe_apr = April SWE. cells: rows of the cube's cells.parquet (for static features).
        """
        key = pd.MultiIndex.from_arrays([raw["cell_id"].values, raw["month"].astype(int).values])
        out = raw[["cell_id", "month"]].copy()
        for w in WEATHER:
            out[w] = raw[w].values - self.wclim[f"{w}_mean"].reindex(key).values if w in raw else np.nan
        out["clim_ndvi"] = self.clim["clim_ndvi"].reindex(key).values
        out = out.merge(cells[["cell_id"] + STATIC], on="cell_id", how="left")
        return out

    def what_if(self, cells: pd.DataFrame, cell_ids, month: int, precip_pct: float = 0.0,
                lst_day_c: float = 0.0, lst_night_c: float = 0.0, snow_spring_delta: float = 0.0,
                soil_pct: float = 0.0) -> pd.DataFrame:
        """Scenario vs typical weather: e.g. precip_pct=-30 means every precipitation input 30% below normal."""
        ids = np.asarray(cell_ids)
        key = pd.MultiIndex.from_arrays([ids, np.full(len(ids), int(month))])
        w = self.wclim.reindex(key)

        def frame(pp, ld, ln, ss, sp):
            f = pd.DataFrame({"cell_id": ids, "month": float(month)})
            for k in ("precip_m", "precip_3m", "precip_winter"):
                f[k] = w[f"{k}_mean"].values * pp / 100.0
            f["lst_day_m"] = f["lst_day_prev"] = ld
            f["lst_night_m"] = ln
            f["snow_spring"] = ss
            f["snow_m"] = 0.0
            f["soil_m"] = w["soil_m_mean"].values * sp / 100.0
            f["swe_apr"] = 0.0
            f["clim_ndvi"] = self.clim["clim_ndvi"].reindex(key).values
            return f.merge(cells[["cell_id"] + STATIC], on="cell_id", how="left")

        res = self.predict(frame(precip_pct, lst_day_c, lst_night_c, snow_spring_delta, soil_pct))
        base = self.predict(frame(0.0, 0.0, 0.0, 0.0, 0.0))
        res.insert(0, "cell_id", ids)
        res["expected_typical_weather"] = base["expected"].values
        res["change_vs_typical"] = res["expected"].values - base["expected"].values  # the scenario effect
        return res


def load(root: str | Path = Path(__file__).parent) -> ExpectedGreenness:
    return ExpectedGreenness(root)