File size: 5,015 Bytes
3185f24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-
"""Desertification_Forecasting.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1t1WFmaiHgQ-9ukBo3NhJ3RZ1Qp-IvGxD
"""

import numpy as np
import pandas as pd
import joblib
from sklearn.base import BaseEstimator, TransformerMixin

DYNAMIC_FEATURES = [
    "ndvi", "t2m_c", "td2m_c",
    "rh_pct", "tp_m", "ssrd_jm2"
]

STATIC_FEATURES = [
    "sand", "silt", "clay", "soc", "ph",
    "bdod", "cec", "nitrogen",
    "phosphorus", "potassium", "lc_type1"
]

LAGS = [1, 2, 3]

REQUIRED_DYNAMIC = [
    "year", "month",
    "ndvi", "t2m_c", "td2m_c",
    "rh_pct", "tp_m", "ssrd_jm2"
]

# UTILS
class FeatureFilter(BaseEstimator, TransformerMixin):
    def __init__(self, expected_features):
        self.expected_features = expected_features

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        X = X.copy()
        X = X[[c for c in self.expected_features if c in X.columns]]
        for c in self.expected_features:
            if c not in X.columns:
                X[c] = np.nan
        return X[self.expected_features]


def validate_api_payload(payload):
    if len(payload) < 3:
        raise ValueError("At least 3 months of data are required")

    for item in payload:
        for f in REQUIRED_DYNAMIC:
            if f not in item["features"]:
                raise ValueError(f"Missing required feature: {f}")


def sanitize_features(df):
    df["ndvi"] = df["ndvi"].clip(-1, 1)
    df["rh_pct"] = df["rh_pct"].clip(0, 100)
    df["tp_m"] = df["tp_m"].clip(lower=0)
    df["ssrd_jm2"] = df["ssrd_jm2"].clip(lower=0)
    return df

forecast_models = {
    f: joblib.load(f"xgb_forecast_{f}.pkl")
    for f in DYNAMIC_FEATURES
}

cls_model = joblib.load("xgb_desertification_pipeline.pkl")
label_encoder = joblib.load("label_encoder.pkl")
feature_order = joblib.load("feature_names.pkl")

# API → DataFrame
def api_payload_to_df(payload):
    rows = []
    for item in payload:
        row = item["features"].copy()
        row["location_name"] = item["metadata"]["location_name"]
        rows.append(row)

    df = pd.DataFrame(rows)

    df = df.rename(columns={"LC_Type1": "lc_type1"})
    df = df.drop(columns=["latitude", "longitude"], errors="ignore")

    return df

# PREPARE TIME SERIES
def prepare_time_series(df):
    df = df.sort_values(
        ["location_name", "year", "month"]
    ).reset_index(drop=True)

    df["month_sin"] = np.sin(2 * np.pi * df["month"] / 12)
    df["month_cos"] = np.cos(2 * np.pi * df["month"] / 12)

    for col in DYNAMIC_FEATURES:
        for lag in LAGS:
            df[f"{col}_lag{lag}"] = (
                df.groupby("location_name")[col].shift(lag)
            )

    df = df.dropna().reset_index(drop=True)
    return df

# FORECAST
def forecast_future(df, years=5):
    months = years * 12

    forecast_feature_cols = (
        DYNAMIC_FEATURES +
        ["month_sin", "month_cos"] +
        [f"{c}_lag{l}" for c in DYNAMIC_FEATURES for l in LAGS]
    )

    last_row = df.iloc[-1].copy()
    forecasts = []

    year = int(last_row["year"])
    month = int(last_row["month"])

    for _ in range(months):
        month += 1
        if month > 12:
            month = 1
            year += 1

        last_row["month_sin"] = np.sin(2 * np.pi * month / 12)
        last_row["month_cos"] = np.cos(2 * np.pi * month / 12)

        row_pred = {"year": year, "month": month}

        for feature in DYNAMIC_FEATURES:
            X = last_row[forecast_feature_cols].values.reshape(1, -1)
            pred = forecast_models[feature].predict(X)[0]

            if feature == "ssrd_jm2":
                pred = np.expm1(pred)

            row_pred[feature] = pred

            last_row[f"{feature}_lag3"] = last_row[f"{feature}_lag2"]
            last_row[f"{feature}_lag2"] = last_row[f"{feature}_lag1"]
            last_row[f"{feature}_lag1"] = pred

        forecasts.append(row_pred)

    return pd.DataFrame(forecasts)

# CLASSIFICATION
def classify_risk(future_df, static_row):
    for col in STATIC_FEATURES:
        future_df[col] = static_row[col]

    feature_filter = FeatureFilter(feature_order)
    X = feature_filter.transform(future_df)

    encoded = cls_model.predict(X)
    labels = label_encoder.inverse_transform(encoded)
    proba = cls_model.predict_proba(X).max(axis=1)

    future_df["risk_level"] = labels
    future_df["risk_confidence"] = proba

    return future_df

# MAIN PIPELINE
def run_pipeline(api_payload, years=5):
    validate_api_payload(api_payload)

    df = api_payload_to_df(api_payload)
    df = sanitize_features(df)

    ts_df = prepare_time_series(df)

    future = forecast_future(ts_df, years)
    static_row = ts_df.iloc[-1]

    final = classify_risk(future, static_row)

    return {
        "success": True,
        "forecast": final[
            ["year", "month"] +
            DYNAMIC_FEATURES +
            ["risk_level", "risk_confidence"]
        ].to_dict(orient="records")
    }