File size: 1,712 Bytes
4cea933
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72f5b06
4cea933
 
 
 
 
 
 
 
72f5b06
4cea933
 
 
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
from typing import Dict, Any
import pandas as pd
import numpy as np
from preprocessing import preprocess_data

def evaluate(ensemble_model, data) -> Dict[str, Any]:
    # Domain orchestrator: PatientData -> InferenceResult
    raw_df = pd.DataFrame([data.model_dump(by_alias=True)])
    
    # Mapping properties to expected feature names
    raw_df = raw_df.rename(columns={
        "Heart_Rate": "Heart Rate (bpm)",
        "SpO2_Level": "SpO2 Level (%)",
        "Systolic_BP": "Systolic Blood Pressure (mmHg)",
        "Diastolic_BP": "Diastolic Blood Pressure (mmHg)",
        "Body_Temp": "Body Temperature (°C)",
        "Fall_Detection": "Fall Detection"
    })

    # Drop anything the model hasn't seen
    unseen = ["Physical Activity Level", "Age", "Gender", "Stress Level", "Cholesterol Level (mg/dL)", 
              "Physical_Activity_Level", "Stress_Level", "Cholesterol_Level", "Fall_Detection"]
    for f in unseen:
        if f in raw_df.columns:
            raw_df = raw_df.drop(columns=[f])

    # Feature engineering
    processed_df, _ = preprocess_data(raw_df, is_training=False)

    if ensemble_model is None:
        return {"error": "Ensemble model not loaded"}

    # Ensemble Prediction
    disease_label, conf, ind_preds, class_probs, weights, ind_conf = ensemble_model.predict(processed_df)
    is_at_risk = (disease_label.lower() not in ["healthy", "normal", "none"])

    return {
        "prediction": is_at_risk,
        "prediction_label": disease_label,
        "probability": float(conf),
        "ensemble_prediction": is_at_risk,
        "model_outputs": ind_preds,
        "model_probs": ind_conf,
        "disease_probs": class_probs,
        "weights": weights
    }