File size: 3,584 Bytes
8b5e3b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""TrendeX expected greenness, Central Asia (v2-ca): CPU inference. Imports only numpy, pandas, xgboost.

Files next to this module: model_ca.ubj, feature_schema.json, climatology.parquet (cell_id, month, clim_ndvi),
weather_climatology.parquet (cell_id, month, <feature>_mean), conformal.json.

    from predict_ca import load
    m = load("path/to/repo")
    m.predict(df)                                                  # df has feature_schema.json columns
    m.what_if(cells, cell_ids, month=7, precip_pct=-30, temp_c=1)  # cells = ca-cube cells.parquet rows

Expected greenness from weather and terrain alone; the gap to observed NDVI is change NOT explained by
precipitation, air temperature, snow and soil moisture. Not a cause, 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


class ExpectedGreennessCA:
    def __init__(self, root):
        root = Path(root)
        s = json.loads((root / "feature_schema.json").read_text())
        self.features, self.static, self.weather = s["features"], s["static"], s["weather"]
        self.booster = xgb.Booster()
        self.booster.load_model(str(root / "model_ca.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()}

    def predict(self, df: pd.DataFrame) -> pd.DataFrame:
        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].to_numpy(dtype="float32")
        pred = self.booster.predict(xgb.DMatrix(X, feature_names=self.features))
        months = df["month"].astype(int).to_numpy()
        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"].to_numpy() + pred
        return pd.DataFrame({"pred_anom": pred, "expected": exp, "p10": exp + lo, "p90": exp + hi}, index=df.index)

    def what_if(self, cells: pd.DataFrame, cell_ids, month: int, precip_pct: float = 0.0, temp_c: float = 0.0,
                soil_pct: float = 0.0) -> pd.DataFrame:
        ids = np.asarray(cell_ids)
        key = pd.MultiIndex.from_arrays([ids, np.full(len(ids), int(month))])
        w = self.wclim.reindex(key)
        st = cells.set_index("cell_id").reindex(ids)

        def frame(pp, tc, sp):
            f = pd.DataFrame({k: st[k].to_numpy() for k in self.static})
            f["month"] = float(month)
            f["clim_ndvi"] = self.clim["clim_ndvi"].reindex(key).to_numpy()
            for k in ("precip_m", "precip_2m", "precip_winter"):
                f[k] = w[f"{k}_mean"].to_numpy() * pp / 100.0
            f["tair_m"] = f["tair_prev"] = tc
            f["soil_m"] = w["soil_m_mean"].to_numpy() * sp / 100.0
            f["swe_apr"] = 0.0
            return f

        res = self.predict(frame(precip_pct, temp_c, soil_pct))
        base = self.predict(frame(0.0, 0.0, 0.0))
        res.insert(0, "cell_id", ids)
        res["expected_typical_weather"] = base["expected"].to_numpy()
        res["change_vs_typical"] = res["expected"].to_numpy() - base["expected"].to_numpy()
        return res


def load(root=Path(__file__).parent) -> ExpectedGreennessCA:
    return ExpectedGreennessCA(root)