Advanced ML diagnostic models and clinical decision intelligence system
View the Project on GitHub Arkz-Deepak/medilong_decision_core
Project MediLong is an end-to-end multimodal clinical intelligence platform for Polycystic Ovary Syndrome (PCOS) risk screening, diagnosis assistance, and explainable reporting.
The ML Fusion & Clinical Safety Squad owns the decision_core contract slice. This module sits at the predictive heart of MediLong, combining:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β PROJECT MEDILONG β
ββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Squad β Contract Slice / Core Responsibilities β
ββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β π‘οΈ ML Fusion & Safety β decision_core (risk_probability, risk_category, β
β (Deepak, Manoj, β safety_override, shap_contributions)β
β Nambi) β β
ββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β βοΈ Backend & Cloud β Orchestrates /predict, Firebase persistence, signed β
β (Sridhar, Murasoli, β PDF summary generator, unified JSON contract schema β
β Kamalesh G) β β
ββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β π¬ Data Pipelines β pillar_2_ultrasound (follicles, volume, stub mode) β
β (Mani, Keerthi, β pillar_3_labs (10 biomarkers + source confidence) β
β Kamalesh C) β β
ββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β π» Frontend & UI/UX β Multi-step intake UI, SHAP waterfall chart, β
β (Maadhesh, Kishore, β safety override alert banner, stub disclosure β
β Sanjeevram, β β
β Adithyan) β β
ββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
The following flowchart details how patient information flows from the Three Diagnostic Pillars, through Backend Orchestration, into the ML Fusion Decision Core, and out to the Frontend Dashboard:
flowchart TD
%% Styling
classDef inputPillar fill:#e1f5fe,stroke:#0288d1,stroke-width:2px,color:#01579b;
classDef backendLayer fill:#ede7f6,stroke:#512da8,stroke-width:2px,color:#311b92;
classDef guardrailLayer fill:#ffebee,stroke:#d32f2f,stroke-width:2px,color:#b71c1c;
classDef mlEngine fill:#e8f5e9,stroke:#388e3c,stroke-width:2px,color:#1b5e20;
classDef outputLayer fill:#fff3e0,stroke:#f57c00,stroke-width:2px,color:#e65100;
classDef uiLayer fill:#fce4ec,stroke:#c2185b,stroke-width:2px,color:#880e4f;
%% Subgraph 1: Three Diagnostic Pillars
subgraph S1["Diagnostic Pillars (Client & Data Squads)"]
P1["Pillar 1: Demographics & Clinical Intake<br/>β’ Age, BMI, Cycle Regularity<br/>β’ Clinical Symptoms (Hirsutism, Acne)"]:::inputPillar
P2["Pillar 2: Ultrasound CV Stub / Analysis<br/>β’ Follicle Counts (L & R)<br/>β’ Endometrium & Ovarian Volume<br/>β’ Mode: 'stub' / 'live'"]:::inputPillar
P3["Pillar 3: Lab OCR Extraction (10 Biomarkers)<br/>β’ TSH, Prolactin, AMH, FSH, LH<br/>β’ FSH/LH Ratio, Vit D3, RBS, PRG, HCG<br/>β’ Extraction Confidence Score"]:::inputPillar
end
%% Subgraph 2: Backend Orchestrator
subgraph S2["Backend Orchestration (FastAPI)"]
B1["Backend /predict Route<br/>Validates Request & Unifies Payloads"]:::backendLayer
B2["Missing Field Handling<br/>(Imputes Training Baseline Medians)"]:::backendLayer
end
P1 --> B1
P2 --> B1
P3 --> B1
B1 --> B2
%% Subgraph 3: Decision Core Pipeline
subgraph S3["ML Fusion Decision Core (Our Squad)"]
direction TB
%% Step 1: Deterministic Guardrails
G1{"Step 1: Clinical Guardrails Check<br/>(FOGSI / Rotterdam Criteria)"}:::guardrailLayer
G_VETO["π¨ Safety Override Triggered!<br/>β’ TSH > 4.5: Hypothyroidism Differential<br/>β’ PRL > 25.0: Hyperprolactinemia Differential<br/>β’ Beta-HCG > 25: Pregnancy Differential"]:::guardrailLayer
G_PASS["Clinical Overrides Evaluated<br/>(Recorded in Output Payload)"]:::guardrailLayer
G1 -- "Rule Violation" --> G_VETO
G1 -- "Normal Range" --> G_PASS
%% Step 2: Feature Fusion & Vectorization
F1["Step 2: 41-Feature Clinical Vector Alignment<br/>Robust Type Coercion & Imputation"]:::mlEngine
G_VETO --> F1
G_PASS --> F1
%% Step 3: Calibrated Risk Classifier
M1["Step 3: Calibrated XGBoost Risk Engine<br/>β’ Base: XGBClassifier (Depth=4, n=100)<br/>β’ CalibratedClassifierCV (Isotonic, 5-Fold)"]:::mlEngine
F1 --> M1
%% Step 4: Categorization
C1["Risk Stratification<br/>β’ Low: < 0.30<br/>β’ Moderate: 0.30 - 0.65<br/>β’ High: > 0.65"]:::mlEngine
M1 --> C1
%% Step 5: SHAP Attribution
S_EXPL["Step 4: SHAP TreeExplainer<br/>β’ Base Value Log-Odds: -0.8109<br/>β’ Patient-Specific Local Attributions<br/>β’ Isolates Top Risk Enhancers & Reducers"]:::mlEngine
M1 --> S_EXPL
%% Step 6: Unified Decision Core Contract Slice
DC_PAYLOAD["Step 5: Unified decision_core Payload<br/>{<br/> risk_probability: float,<br/> risk_category: 'Low'|'Moderate'|'High',<br/> safety_override: { safety_override: bool, reasons: [...] },<br/> shap_contributions: [...],<br/> waterfall_items: [...]<br/>}"]:::outputLayer
C1 --> DC_PAYLOAD
S_EXPL --> DC_PAYLOAD
end
B2 --> G1
%% Subgraph 4: Frontend UI/UX Rendering
subgraph S4["Frontend & PDF Delivery (Frontend Squad)"]
UI_ALERT["β οΈ Differential Diagnosis Banner<br/>(Visible when safety_override == true)"]:::uiLayer
UI_RISK["π©Ί Calibrated Risk Meter & Badge<br/>(e.g., 98.4% β High Risk)"]:::uiLayer
UI_WATERFALL["π Interactive SHAP Waterfall Chart<br/>(Feature Impact Magnitude & Direction)"]:::uiLayer
UI_STUB["βΉοΈ Pillar 2 Disclosure Badge<br/>(Discloses Ultrasound Stub Mode)"]:::uiLayer
PDF_DOC["π Signed Clinical Summary PDF<br/>(Generated by Backend)"]:::uiLayer
end
DC_PAYLOAD --> UI_ALERT
DC_PAYLOAD --> UI_RISK
DC_PAYLOAD --> UI_WATERFALL
DC_PAYLOAD --> UI_STUB
DC_PAYLOAD --> PDF_DOC
The predictive engine was trained on the validated Kaggle Indian PCOS Dataset (541 patients, 41 clinical features) using an 80/20 stratified train-test split (random_state=44).
| Class | Clinical Status | Precision | Recall | F1-Score | Test Support |
|---|---|---|---|---|---|
| 0 | Non-PCOS | 0.88 | 0.95 | 0.91 | 73 |
| 1 | PCOS Positive | 0.87 | 0.75 | 0.81 | 36 |
| Accuracy | Overall Model Accuracy | β | β | 0.88 (88.07%) | 109 |
| Macro Avg | Unweighted Average | 0.88 | 0.85 | 0.86 | 109 |
| Weighted Avg | Class-Weighted Average | 0.88 | 0.88 | 0.88 | 109 |
| Metric | Score | Clinical Significance |
|---|---|---|
| ROC-AUC Score | 0.9463 |
Exceptional discriminative ability between PCOS and non-PCOS cases. |
| Calibration Method | Isotonic Regression | Aligns predicted scores with true posterior empirical probabilities via 5-fold cross validation. |
| Base Model | XGBoost Classifier | n_estimators=100, max_depth=4, eval_metric='logloss', random_state=42. |
| Inference Latency | < 70 ms |
Sub-100ms execution, well within Backendβs < 3.0s end-to-end latency budget. |
According to FOGSI and Rotterdam consensus guidelines, secondary endocrine etiologies that mimic PCOS must be screened out prior to confirming PCOS:
| Clinical Condition | Biomarker Rule | Trigger Threshold | Model Override Action | Recommended Clinical Differential |
|---|---|---|---|---|
| Suspected Hypothyroidism | Serum TSH | TSH > 4.5 mIU/L |
Flags safety_override: true |
Order full Thyroid profile (Free T3, Free T4, Anti-TPO antibodies). |
| Suspected Hyperprolactinemia | Serum Prolactin (PRL) | PRL > 25.0 ng/mL |
Flags safety_override: true |
Repeat fasting Prolactin; evaluate pituitary adenoma if persistent. |
| Pregnancy Differential | Serum Beta-HCG | Beta-HCG > 25.0 mIU/mL |
Flags safety_override: true |
Order confirmatory urine pregnancy test & obstetric ultrasound. |
SHAP attributions calculate the exact Shapley contribution of every clinical marker relative to the population base value:
E[f(x)] = -0.8109Follicle No. (R/L))Weight gain (Y/N))Cycle (R/I))hair growth: 0)medilong_decision_core/
βββ data/
β βββ PCOS_data_without_infertility.xlsx # Full clinical dataset (Kaggle Indian PCOS)
β βββ PCOS_infertility.csv # Infertility clinical cohort
βββ models/
β βββ calibrated_model.joblib # CalibratedClassifierCV (Isotonic)
β βββ base_xgb.joblib # Standalone XGBoost model (for SHAP)
β βββ feature_names.json # 41 feature canonical column list
β βββ feature_medians.json # Imputation baselines for missing labs
β βββ metrics.json # Serialized training and test metrics
βββ notebooks/
β βββ 0_eda.ipynb # Exploratory data analysis & prototype
βββ src/
β βββ __init__.py
β βββ train.py # Production training & artifact pipeline
β βββ decision_core.py # Multi-squad integration & FastAPI service
βββ tests/
β βββ __init__.py
β βββ test_decision_core.py # Unit tests (Guardrails, Stub, Repro)
βββ ML squad.pdf # ML Fusion & Safety Sprint Specification
βββ backendsquad.pdf # Backend & Cloud API Contract Specification
βββ datasquad.pdf # Data Pipelines (CV Stub & OCR) Specification
βββ frontendsquad.pdf # Frontend UI & Explanation Widget Spec
βββ requirements.txt # Project dependencies
βββ README.md # System documentation & architectural guide
/predict)Backend squad can import the decision core directly into their FastAPI service:
from src.decision_core import evaluate_patient, UnifiedIntakeRequest
# 1. Provide either 3-pillar JSON payload or dictionary
patient_payload = {
"pillar_1_demographics": {
"age": 26,
"weight_kg": 68,
"height_cm": 160,
"cycle_ri": 4, # Irregular
"weight_gain": 1,
"hair_growth": 1
},
"pillar_2_ultrasound": {
"mode": "stub",
"follicle_count": 18,
"confidence": 0.88
},
"pillar_3_labs": {
"tsh_miu_l": 6.2, # Triggers Hypothyroidism override
"prl_ng_ml": 18.0,
"amh_ng_ml": 8.4
}
}
# 2. Evaluate
decision_result = evaluate_patient(patient_payload)
print(decision_result["risk_category"]) # 'High'
print(decision_result["safety_override"]) # {'safety_override': True, 'reasons': [...]}
Mount directly into an existing FastAPI application:
from fastapi import FastAPI
from src.decision_core import router as decision_core_router
app = FastAPI(title="MediLong Core API")
app.include_router(decision_core_router, prefix="/api/v1")
To run the standalone Decision Core server:
uvicorn src.decision_core:app --host 0.0.0.0 --port 8000 --reload
Interactive Swagger documentation is available at: http://localhost:8000/docs
python -m unittest discover tests
Validates: Independent guardrail override logic, 3-pillar schema parsing, missing biomarker median imputation, and output reproducibility.
python src/train.py
Executes: Data cleaning, 5-fold isotonic calibration, metric calculation, and artifact persistence to models/.
This project is licensed under the MIT License β see the LICENSE file for details.
Copyright (c) 2026 Deepak (Arkz-Deepak). All rights reserved.