IDKHowToCodeFr/tinyml-backend
1
1from typing import Dict, Any2import pandas as pd3import numpy as np4from preprocessing import preprocess_data5 6def evaluate(ensemble_model, data) -> Dict[str, Any]:7 # Domain orchestrator: PatientData -> InferenceResult8 raw_df = pd.DataFrame([data.model_dump(by_alias=True)])9 10 # Mapping properties to expected feature names11 raw_df = raw_df.rename(columns={12 "Heart_Rate": "Heart Rate (bpm)",13 "SpO2_Level": "SpO2 Level (%)",14 "Systolic_BP": "Systolic Blood Pressure (mmHg)",15 "Diastolic_BP": "Diastolic Blood Pressure (mmHg)",16 "Body_Temp": "Body Temperature (°C)",17 "Fall_Detection": "Fall Detection"18 })19 20 # Drop anything the model hasn't seen21 unseen = ["Physical Activity Level", "Age", "Gender", "Stress Level", "Cholesterol Level (mg/dL)", 22 "Physical_Activity_Level", "Stress_Level", "Cholesterol_Level", "Fall_Detection"]23 for f in unseen:24 if f in raw_df.columns:25 raw_df = raw_df.drop(columns=[f])26 27 # Feature engineering28 processed_df, _ = preprocess_data(raw_df, is_training=False)29 30 if ensemble_model is None:31 return {"error": "Ensemble model not loaded"}32 33 # Ensemble Prediction34 disease_label, conf, ind_preds, class_probs, weights, ind_conf = ensemble_model.predict(processed_df)35 is_at_risk = (disease_label.lower() not in ["healthy", "normal", "none"])36 37 return {38 "prediction": is_at_risk,39 "prediction_label": disease_label,40 "probability": float(conf),41 "ensemble_prediction": is_at_risk,42 "model_outputs": ind_preds,43 "model_probs": ind_conf,44 "disease_probs": class_probs,45 "weights": weights46 }47 