Spaces:
Sleeping
Sleeping
Create rules_engine.py
Browse files- rules_engine.py +69 -0
rules_engine.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# rules_engine.py
|
| 2 |
+
|
| 3 |
+
def compute_bmi(height_cm, weight_kg):
|
| 4 |
+
"""Compute BMI = weight (kg) / height (m^2)."""
|
| 5 |
+
if height_cm <= 0:
|
| 6 |
+
return 0.0
|
| 7 |
+
h_m = height_cm / 100
|
| 8 |
+
return round(weight_kg / (h_m * h_m), 2)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def evaluate_risks(age, bmi, glucose, sbp, dbp, hr=70, spo2=98):
|
| 12 |
+
"""Simple numeric risk scoring for conditions."""
|
| 13 |
+
risks = {}
|
| 14 |
+
|
| 15 |
+
# Type 2 Diabetes
|
| 16 |
+
risks["Type 2 Diabetes Risk"] = min(1.0, (glucose - 90) / 100 + bmi / 40)
|
| 17 |
+
|
| 18 |
+
# Hypertension
|
| 19 |
+
risks["Hypertension Risk"] = min(1.0, (sbp - 120) / 60 + (dbp - 80) / 40)
|
| 20 |
+
|
| 21 |
+
# Depression / Mood
|
| 22 |
+
risks["Depression/Mood Concern"] = 0.3 if age > 40 else 0.2
|
| 23 |
+
|
| 24 |
+
# Migraine
|
| 25 |
+
risks["Migraine Risk"] = 0.4 if sbp > 135 else 0.2
|
| 26 |
+
|
| 27 |
+
# Sleep Apnea
|
| 28 |
+
risks["Sleep Apnea Risk"] = 0.5 if bmi > 30 else 0.2
|
| 29 |
+
|
| 30 |
+
# Anemia
|
| 31 |
+
risks["Anemia Risk"] = 0.3 if spo2 < 92 else 0.1
|
| 32 |
+
|
| 33 |
+
return risks
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def rules_risk_assessment(age, sex, sbp, dbp, hr, spo2, glucose, bmi, symptoms):
|
| 37 |
+
"""
|
| 38 |
+
Return (scores, explanations).
|
| 39 |
+
scores: dict of condition → 0–1 float
|
| 40 |
+
explanations: dict of condition → reason string
|
| 41 |
+
"""
|
| 42 |
+
scores = {}
|
| 43 |
+
explain = {}
|
| 44 |
+
|
| 45 |
+
risks = evaluate_risks(age, bmi, glucose, sbp, dbp, hr, spo2)
|
| 46 |
+
for cond, val in risks.items():
|
| 47 |
+
scores[cond] = round(val, 3)
|
| 48 |
+
if cond == "Type 2 Diabetes Risk":
|
| 49 |
+
explain[cond] = f"Glucose={glucose}, BMI={bmi}"
|
| 50 |
+
elif cond == "Hypertension Risk":
|
| 51 |
+
explain[cond] = f"SBP={sbp}, DBP={dbp}"
|
| 52 |
+
elif cond == "Depression/Mood Concern":
|
| 53 |
+
explain[cond] = f"Age={age}, Sex={sex}"
|
| 54 |
+
elif cond == "Migraine Risk":
|
| 55 |
+
explain[cond] = "Headache symptom" if "Headache" in symptoms else "Vitals"
|
| 56 |
+
elif cond == "Sleep Apnea Risk":
|
| 57 |
+
explain[cond] = "Snoring / daytime sleepiness" if any(s in symptoms for s in ["Loud snoring", "Daytime sleepiness"]) else "BMI factor"
|
| 58 |
+
elif cond == "Anemia Risk":
|
| 59 |
+
explain[cond] = f"SpO₂={spo2}"
|
| 60 |
+
else:
|
| 61 |
+
explain[cond] = "Rule-based heuristic"
|
| 62 |
+
|
| 63 |
+
return scores, explain
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def explain_rules(explain_dict):
|
| 67 |
+
"""Format explanation dict into readable text."""
|
| 68 |
+
lines = [f"- **{cond}**: {reason}" for cond, reason in explain_dict.items()]
|
| 69 |
+
return "\n".join(lines)
|