File size: 10,321 Bytes
742cd9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Model loading and inference engine.

Loads XGBoost model, scaler, and SHAP explainer once at startup.
All prediction requests are handled through the Predictor class.

Design principle: load once, predict many times.
Loading a model on every request would be 10-100x slower.
"""

import joblib
import numpy as np
import pandas as pd
import shap
import logging
from pathlib import Path
from typing import Optional

from src.api.models import (
    PatientFeatures,
    PredictionResponse,
    RiskCategory,
    RiskFactor,
)

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Feature order must match exactly what the model was trained on
# Any mismatch causes silent wrong predictions β€” worse than an error
FEATURE_ORDER = [
    'los_days', 'los_days_log', 'admission_month', 'admission_dow',
    'is_emergency', 'n_admissions_prior_6m', 'n_admissions_prior_12m',
    'n_ed_visits_prior_6m', 'days_since_last_admission',
    'has_prior_admission', 'charlson_score', 'n_active_conditions',
    'has_heart_failure', 'has_diabetes', 'has_diabetes_complex',
    'has_copd', 'has_ckd', 'has_mi', 'has_cancer', 'has_dementia',
    'has_cerebrovascular', 'has_pvd', 'n_medications_capped',
    'is_high_polypharmacy', 'has_insulin', 'has_anticoagulant',
    'has_diuretic', 'has_ace_inhibitor', 'age_at_admission',
    'gender_male', 'income',
]

# Clinical descriptions for each feature
# Used to generate plain-English explanations in the response
FEATURE_DESCRIPTIONS = {
    'los_days':                   'Length of hospital stay',
    'los_days_log':               'Length of hospital stay (log scale)',
    'admission_month':            'Month of admission',
    'admission_dow':              'Day of week admitted',
    'is_emergency':               'Emergency admission',
    'n_admissions_prior_6m':      'Hospital admissions in past 6 months',
    'n_admissions_prior_12m':     'Hospital admissions in past 12 months',
    'n_ed_visits_prior_6m':       'Emergency department visits in past 6 months',
    'days_since_last_admission':  'Days since last hospital admission',
    'has_prior_admission':        'History of prior hospitalisation',
    'charlson_score':             'Charlson Comorbidity Index score',
    'n_active_conditions':        'Number of active medical conditions',
    'has_heart_failure':          'Heart failure diagnosis',
    'has_diabetes':               'Diabetes mellitus diagnosis',
    'has_diabetes_complex':       'Diabetes with complications',
    'has_copd':                   'Chronic obstructive pulmonary disease',
    'has_ckd':                    'Chronic kidney disease',
    'has_mi':                     'History of myocardial infarction',
    'has_cancer':                 'Active cancer diagnosis',
    'has_dementia':               'Dementia diagnosis',
    'has_cerebrovascular':        'Cerebrovascular disease',
    'has_pvd':                    'Peripheral vascular disease',
    'n_medications_capped':       'Number of active medications',
    'is_high_polypharmacy':       'High polypharmacy (>10 medications)',
    'has_insulin':                'Insulin therapy',
    'has_anticoagulant':          'Anticoagulant therapy',
    'has_diuretic':               'Diuretic therapy',
    'has_ace_inhibitor':          'ACE inhibitor therapy',
    'age_at_admission':           'Patient age at admission',
    'gender_male':                'Male gender',
    'income':                     'Household income',
}

# Risk thresholds β€” calibrated to our model's output distribution
# LOW:    < 20% β€” standard discharge
# MEDIUM: 20-50% β€” enhanced follow-up
# HIGH:   > 50% β€” intensive intervention
RISK_THRESHOLDS = {
    'LOW':    0.20,
    'MEDIUM': 0.50,
}

# Clinical recommendations by risk category
RECOMMENDATIONS = {
    RiskCategory.LOW: (
        "Standard discharge protocol. "
        "Schedule routine 30-day follow-up appointment. "
        "Provide patient with discharge summary and medication list."
    ),
    RiskCategory.MEDIUM: (
        "Enhanced discharge protocol. "
        "Schedule 14-day follow-up appointment. "
        "Conduct pharmacist medication reconciliation. "
        "Provide patient education on warning signs requiring ED return."
    ),
    RiskCategory.HIGH: (
        "High-risk discharge protocol. "
        "Schedule 7-day follow-up appointment. "
        "Assign care coordinator for post-discharge monitoring. "
        "Conduct pharmacist medication reconciliation before discharge. "
        "Consider social work referral if social support is limited. "
        "Confirm patient has transport and medication access post-discharge."
    ),
}


class Predictor:
    """
    Manages model lifecycle and inference.
    
    Loaded once at API startup via FastAPI lifespan events.
    Thread-safe for concurrent requests β€” models are read-only after loading.
    """

    def __init__(self, models_path: str = "models"):
        self.models_path   = Path(models_path)
        self.model         = None
        self.scaler        = None
        self.explainer     = None
        self.model_version = "xgboost_v1_auc0.891"
        self._loaded       = False

    def load(self) -> None:
        """
        Load model, scaler, and SHAP explainer from disk.
        Called once at API startup β€” not on every request.
        """
        logger.info("Loading model artifacts...")

        try:
            # Load XGBoost model
            model_path = self.models_path / "xgboost_model.pkl"
            self.model = joblib.load(model_path)
            logger.info(f"Model loaded from {model_path}")

            # Load scaler β€” must be the same scaler used in training
            scaler_path = self.models_path / "scaler.pkl"
            self.scaler = joblib.load(scaler_path)
            logger.info(f"Scaler loaded from {scaler_path}")

            # Build SHAP explainer β€” TreeExplainer for XGBoost
            # This is slow to build (~5s) but fast to query
            self.explainer = shap.TreeExplainer(self.model)
            logger.info("SHAP explainer ready")

            self._loaded = True
            logger.info(
                f"All artifacts loaded successfully. "
                f"Model version: {self.model_version}"
            )

        except FileNotFoundError as e:
            logger.error(f"Model file not found: {e}")
            raise
        except Exception as e:
            logger.error(f"Failed to load model: {e}")
            raise

    @property
    def is_loaded(self) -> bool:
        return self._loaded

    def _features_to_array(
        self, features: PatientFeatures
    ) -> pd.DataFrame:
        """Convert Pydantic model to DataFrame in correct feature order."""
        feature_dict = features.model_dump()
        ordered = {f: [feature_dict[f]] for f in FEATURE_ORDER}
        return pd.DataFrame(ordered)

    def _get_risk_category(
        self, probability: float
    ) -> RiskCategory:
        """Map probability to clinical risk category."""
        if probability < RISK_THRESHOLDS['LOW']:
            return RiskCategory.LOW
        elif probability < RISK_THRESHOLDS['MEDIUM']:
            return RiskCategory.MEDIUM
        else:
            return RiskCategory.HIGH

    def _compute_shap_factors(
        self,
        raw_array: np.ndarray,
        features: PatientFeatures,
        top_n: int = 5
    ) -> list[RiskFactor]:
        """
        Compute SHAP values and return top N risk factors
        with clinical plain-English descriptions.
        """
        shap_values = self.explainer.shap_values(raw_array)[0]

        feature_dict = features.model_dump()

        # Build list of (feature, value, shap_value)
        factors = [
            (feat, feature_dict[feat], shap_val)
            for feat, shap_val in zip(FEATURE_ORDER, shap_values)
        ]

        # Sort by absolute SHAP value β€” largest impact first
        factors_sorted = sorted(
            factors, key=lambda x: abs(x[2]), reverse=True
        )[:top_n]

        risk_factors = []
        for feat, val, shap_val in factors_sorted:
            risk_factors.append(RiskFactor(
                feature=feat,
                value=round(float(val), 3),
                impact=round(float(shap_val), 4),
                direction="increases" if shap_val > 0 else "decreases",
                description=FEATURE_DESCRIPTIONS.get(feat, feat)
            ))

        return risk_factors

    def predict(self, features: PatientFeatures) -> PredictionResponse:
        """
        Run full prediction pipeline for one patient.
        
        Steps:
        1. Convert features to array in correct order
        2. Scale features using training scaler
        3. Get probability from XGBoost
        4. Compute SHAP values for explainability
        5. Build structured clinical response
        
        Returns:
            PredictionResponse with risk score, category,
            SHAP factors, and clinical recommendation
        """
        if not self._loaded:
            raise RuntimeError(
                "Model not loaded. Call predictor.load() first."
            )

        # Step 1 β€” features to array
        raw_df = self._features_to_array(features)

        # Step 2 β€” scale
        scaled_array = self.scaler.transform(raw_df)

        # Step 3 β€” predict probability
        probability = float(
            self.model.predict_proba(scaled_array)[0][1]
        )

        # Step 4 β€” SHAP explanation
        risk_factors = self._compute_shap_factors(
            scaled_array, features, top_n=5
        )

        # Step 5 β€” build response
        risk_category = self._get_risk_category(probability)
        risk_percent  = round(probability * 100, 1)

        return PredictionResponse(
            readmission_risk   = round(probability, 4),
            risk_percent       = risk_percent,
            risk_category      = risk_category,
            risk_score_display = f"{risk_percent}% readmission risk",
            top_risk_factors   = risk_factors,
            recommendation     = RECOMMENDATIONS[risk_category],
            model_version      = self.model_version,
        )


# Global predictor instance
# Imported by routers β€” loaded once at startup
predictor = Predictor()