File size: 12,950 Bytes
af3091e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
"""Tabular classification bundles: Titanic, Heart Disease, Wine Quality.

Each bundle is trained lazily on first access from CSVs in ``sample_data/``.
Per-request contributions use:

- Titanic (LogisticRegression): signed ``coef * (x_scaled)`` per feature
- Heart / Wine (GradientBoostingClassifier): LOCO-style approximation β€”
  substitute each feature with the training mean/mode and compute Ξ”prob.

All three bundles expose a uniform ``predict(payload)`` callable returning the
unified response schema consumed by the ``/tabular/*`` endpoints.
"""

from __future__ import annotations

import os
import threading
from dataclasses import dataclass
from typing import Any, Callable

import numpy as np
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler


_DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "sample_data")
_lock = threading.Lock()
_bundles: dict[str, "TabularBundle"] = {}


@dataclass
class TabularBundle:
    name: str
    pipeline: Pipeline
    feature_order: list[str]
    categorical: list[str]
    numeric: list[str]
    class_labels: list[str]
    model_label: str
    target_type: str  # "binary" or "multiclass"
    baseline: dict[str, Any]  # training means / modes for LOCO substitution
    positive_class: str | None = None  # for binary display

    def predict(self, payload: dict[str, Any]) -> dict[str, Any]:
        """Run prediction + contributions on a single row payload."""
        row = {f: payload.get(f, self.baseline[f]) for f in self.feature_order}
        x_df = pd.DataFrame([row], columns=self.feature_order)
        proba = self.pipeline.predict_proba(x_df)[0]
        classes = [str(c) for c in self.pipeline.classes_]
        probabilities = {
            label_for_class(self.class_labels, classes, c): float(p)
            for c, p in zip(classes, proba)
        }
        top_idx = int(np.argmax(proba))
        top_class_raw = classes[top_idx]
        top_class_label = label_for_class(self.class_labels, classes, top_class_raw)

        # Contributions
        if isinstance(self.pipeline.named_steps["clf"], LogisticRegression):
            contributions = _logreg_contributions(
                self.pipeline, x_df, self.feature_order, self.numeric, self.categorical
            )
        else:
            contributions = _loco_contributions(
                self.pipeline,
                x_df,
                self.feature_order,
                self.baseline,
                top_idx,
            )

        # Top-1 probability for gauge
        confidence = float(proba[top_idx])

        # Build feature_importance (signed) sorted by |contribution|
        fi_list = [
            {
                "feature": feat,
                "value": _display_value(row[feat]),
                "contribution": float(contrib),
            }
            for feat, contrib in contributions.items()
        ]
        fi_list.sort(key=lambda d: abs(d["contribution"]), reverse=True)

        return {
            "prediction": top_class_label,
            "confidence": confidence,
            "probabilities": probabilities,
            "feature_importance": fi_list,
            "feature_order": self.feature_order,
            "model": self.model_label,
            "target_type": self.target_type,
        }


def label_for_class(class_labels: list[str], classes: list[str], raw: str) -> str:
    """Map model class output (may be '0','1' or string) to display label."""
    try:
        idx = classes.index(raw)
        if idx < len(class_labels):
            return class_labels[idx]
    except ValueError:
        pass
    return raw


def _display_value(v: Any) -> Any:
    if isinstance(v, float):
        if v != v:  # NaN
            return None
        return round(float(v), 3)
    return v


# ─── Contribution helpers ───

def _logreg_contributions(
    pipeline: Pipeline,
    x_df: pd.DataFrame,
    feature_order: list[str],
    numeric: list[str],
    categorical: list[str],
) -> dict[str, float]:
    """Compute signed coef * x_scaled contribution per original feature.

    For one-hot encoded categoricals we sum over all dummy columns tied to the
    original feature so the contribution aggregates cleanly for the UI.
    """
    preprocessor: ColumnTransformer = pipeline.named_steps["prep"]
    clf: LogisticRegression = pipeline.named_steps["clf"]
    x_trans = preprocessor.transform(x_df)
    if hasattr(x_trans, "toarray"):
        x_trans = x_trans.toarray()
    x_trans = np.asarray(x_trans).ravel()

    coef = clf.coef_
    # Binary LR β†’ single row of coefs; multiclass β†’ use class 1 row (positive)
    if coef.shape[0] == 1:
        coefs = coef[0]
    else:
        coefs = coef[-1]

    # Map transformed column index β†’ original feature name
    feature_names_out = _get_feature_names_out(preprocessor, numeric, categorical)
    per_feat: dict[str, float] = {f: 0.0 for f in feature_order}
    for i, col_name in enumerate(feature_names_out):
        original = _strip_prefix(col_name, numeric + categorical)
        if original in per_feat:
            per_feat[original] += float(coefs[i] * x_trans[i])
    return per_feat


def _get_feature_names_out(
    preprocessor: ColumnTransformer,
    numeric: list[str],
    categorical: list[str],
) -> list[str]:
    """Best-effort retrieval of column names from fitted ColumnTransformer."""
    try:
        return list(preprocessor.get_feature_names_out())
    except Exception:
        pass
    names: list[str] = []
    for name, transformer, cols in preprocessor.transformers_:
        if name == "num":
            names.extend([f"num__{c}" for c in cols])
        elif name == "cat":
            try:
                ohe_names = transformer.get_feature_names_out(cols)
            except Exception:
                ohe_names = [f"cat__{c}" for c in cols]
            names.extend(ohe_names)
    return names


def _strip_prefix(col_name: str, originals: list[str]) -> str:
    # sklearn formats: "num__age", "cat__sex_female"
    for o in originals:
        if col_name == f"num__{o}" or col_name.startswith(f"cat__{o}_"):
            return o
        if col_name == o or col_name.startswith(f"{o}_"):
            return o
    return col_name.split("__", 1)[-1].split("_", 1)[0]


def _loco_contributions(
    pipeline: Pipeline,
    x_df: pd.DataFrame,
    feature_order: list[str],
    baseline: dict[str, Any],
    target_idx: int,
) -> dict[str, float]:
    """LOCO approximation β€” substitute each feature with its baseline value and
    compute delta in predicted probability for the predicted class."""
    base_proba = pipeline.predict_proba(x_df)[0][target_idx]
    contributions: dict[str, float] = {}
    for feat in feature_order:
        substituted = x_df.copy()
        substituted[feat] = baseline[feat]
        new_proba = pipeline.predict_proba(substituted)[0][target_idx]
        # contribution = base - new_with_feature_neutralised
        # β†’ positive means feature pushed probability UP
        contributions[feat] = float(base_proba - new_proba)
    return contributions


# ─── Bundle builders ───

def _build_titanic() -> TabularBundle:
    path = os.path.join(_DATA_DIR, "titanic.csv")
    df = pd.read_csv(path)
    # Clean
    df = df[["pclass", "sex", "age", "sibsp", "parch", "fare", "embarked", "survived"]].copy()
    df["age"] = df["age"].fillna(df["age"].median())
    df["fare"] = df["fare"].fillna(df["fare"].median())
    df["embarked"] = df["embarked"].fillna(df["embarked"].mode().iloc[0])
    df = df.dropna(subset=["survived"])

    numeric = ["age", "sibsp", "parch", "fare"]
    categorical = ["pclass", "sex", "embarked"]
    feature_order = ["pclass", "sex", "age", "sibsp", "parch", "fare", "embarked"]

    preprocessor = ColumnTransformer(
        transformers=[
            ("num", StandardScaler(), numeric),
            ("cat", OneHotEncoder(handle_unknown="ignore"), categorical),
        ]
    )
    pipeline = Pipeline([
        ("prep", preprocessor),
        ("clf", LogisticRegression(max_iter=500, C=1.0)),
    ])
    pipeline.fit(df[feature_order], df["survived"].astype(int))

    baseline: dict[str, Any] = {
        "pclass": int(df["pclass"].mode().iloc[0]),
        "sex": df["sex"].mode().iloc[0],
        "age": float(df["age"].median()),
        "sibsp": int(df["sibsp"].median()),
        "parch": int(df["parch"].median()),
        "fare": float(df["fare"].median()),
        "embarked": df["embarked"].mode().iloc[0],
    }

    return TabularBundle(
        name="titanic-survival",
        pipeline=pipeline,
        feature_order=feature_order,
        categorical=categorical,
        numeric=numeric,
        class_labels=["Did not survive", "Survived"],
        model_label="LogisticRegression (Titanic)",
        target_type="binary",
        baseline=baseline,
        positive_class="Survived",
    )


def _build_heart() -> TabularBundle:
    path = os.path.join(_DATA_DIR, "heart.csv")
    df = pd.read_csv(path)
    # UCI Cleveland: num 0 = no disease, 1-4 = presence β€” bin to binary
    df = df[[
        "age", "sex", "cp", "trestbps", "chol", "fbs",
        "restecg", "thalach", "exang", "oldpeak", "num",
    ]].copy()
    df = df.apply(pd.to_numeric, errors="coerce").dropna()
    df["target"] = (df["num"] > 0).astype(int)
    df = df.drop(columns=["num"])

    feature_order = [
        "age", "sex", "cp", "trestbps", "chol", "fbs",
        "restecg", "thalach", "exang", "oldpeak",
    ]
    numeric = feature_order[:]
    categorical: list[str] = []

    preprocessor = ColumnTransformer(
        transformers=[("num", StandardScaler(), numeric)]
    )
    pipeline = Pipeline([
        ("prep", preprocessor),
        ("clf", GradientBoostingClassifier(n_estimators=150, max_depth=3, random_state=0)),
    ])
    pipeline.fit(df[feature_order], df["target"])

    baseline = {f: float(df[f].mean()) for f in feature_order}

    return TabularBundle(
        name="heart-disease",
        pipeline=pipeline,
        feature_order=feature_order,
        categorical=categorical,
        numeric=numeric,
        class_labels=["Low risk", "Elevated risk"],
        model_label="GradientBoosting (UCI Cleveland Heart)",
        target_type="binary",
        baseline=baseline,
        positive_class="Elevated risk",
    )


def _build_wine() -> TabularBundle:
    path = os.path.join(_DATA_DIR, "wine_red.csv")
    df = pd.read_csv(path, sep=";")
    df.columns = [c.strip().replace(" ", "_") for c in df.columns]
    df = df.dropna()

    # Bin quality β†’ 3 classes
    def _bin(q: float) -> int:
        if q <= 4:
            return 0  # low
        if q <= 6:
            return 1  # medium
        return 2  # high

    df["target"] = df["quality"].apply(_bin)

    feature_order = [
        "alcohol", "volatile_acidity", "sulphates", "citric_acid",
        "residual_sugar", "chlorides", "pH", "density",
    ]
    numeric = feature_order[:]
    categorical: list[str] = []

    preprocessor = ColumnTransformer(
        transformers=[("num", StandardScaler(), numeric)]
    )
    pipeline = Pipeline([
        ("prep", preprocessor),
        ("clf", GradientBoostingClassifier(n_estimators=200, max_depth=3, random_state=0)),
    ])
    pipeline.fit(df[feature_order], df["target"])

    baseline = {f: float(df[f].mean()) for f in feature_order}

    return TabularBundle(
        name="wine-quality",
        pipeline=pipeline,
        feature_order=feature_order,
        categorical=categorical,
        numeric=numeric,
        class_labels=["Low (≀4)", "Medium (5–6)", "High (β‰₯7)"],
        model_label="GradientBoosting (Wine Quality β€” Red)",
        target_type="multiclass",
        baseline=baseline,
        positive_class=None,
    )


_BUILDERS: dict[str, Callable[[], TabularBundle]] = {
    "titanic-survival": _build_titanic,
    "heart-disease": _build_heart,
    "wine-quality": _build_wine,
}


def get_bundle(name: str) -> TabularBundle:
    if name in _bundles:
        return _bundles[name]
    with _lock:
        if name in _bundles:
            return _bundles[name]
        if name not in _BUILDERS:
            raise KeyError(f"Unknown tabular bundle: {name}")
        _bundles[name] = _BUILDERS[name]()
    return _bundles[name]


def schema_summary(name: str) -> dict[str, Any]:
    """Return baseline/metadata for warmup introspection."""
    bundle = get_bundle(name)
    return {
        "name": bundle.name,
        "model": bundle.model_label,
        "features": bundle.feature_order,
        "numeric": bundle.numeric,
        "categorical": bundle.categorical,
        "classes": bundle.class_labels,
        "target_type": bundle.target_type,
        "baseline": bundle.baseline,
    }