Download _backup/runs_v2_ca/hf_model/predict_ca.py from TrendeX90/ca-cube: direct link, hf CLI and curl.
- Browser
- Download file 3.58 kB
-
https://huggingface.co/datasets/TrendeX90/ca-cube/resolve/main/_backup/runs_v2_ca/hf_model/predict_ca.py
- Command line
-
hf download hf://datasets/TrendeX90/ca-cube/_backup/runs_v2_ca/hf_model/predict_ca.py
-
curl -L -o predict_ca.py https://huggingface.co/datasets/TrendeX90/ca-cube/resolve/main/_backup/runs_v2_ca/hf_model/predict_ca.py
3.58 kB
| """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) | |