Spaces:
Sleeping
Sleeping
Update rules_engine.py
Browse files- rules_engine.py +51 -73
rules_engine.py
CHANGED
|
@@ -1,91 +1,69 @@
|
|
| 1 |
# rules_engine.py
|
| 2 |
|
| 3 |
-
def compute_bmi(
|
| 4 |
-
"""Compute
|
| 5 |
if height_cm <= 0:
|
| 6 |
-
return 0
|
| 7 |
-
|
| 8 |
-
return round(weight_kg / (
|
| 9 |
|
| 10 |
|
| 11 |
-
def evaluate_risks(age, bmi, glucose,
|
| 12 |
-
"""
|
| 13 |
risks = {}
|
| 14 |
|
| 15 |
-
# Type 2 Diabetes
|
| 16 |
-
risks["Type 2 Diabetes Risk"] = min(
|
| 17 |
|
| 18 |
-
# Hypertension
|
| 19 |
-
risks["Hypertension Risk"] = min(
|
| 20 |
|
| 21 |
-
# Depression/
|
| 22 |
-
risks["Depression/Mood Concern"] =
|
| 23 |
|
| 24 |
-
# Migraine
|
| 25 |
-
risks["Migraine Risk"] =
|
| 26 |
|
| 27 |
-
# Sleep Apnea
|
| 28 |
-
risks["Sleep Apnea Risk"] =
|
| 29 |
|
| 30 |
-
# Anemia
|
| 31 |
-
if
|
| 32 |
-
risks["Anemia Risk"] = min(100, (13.5 - hemoglobin) * 15 + 0.1 * age)
|
| 33 |
-
else:
|
| 34 |
-
risks["Anemia Risk"] = 0
|
| 35 |
|
| 36 |
return risks
|
| 37 |
|
| 38 |
-
# rules_engine.py
|
| 39 |
-
|
| 40 |
-
# rules_engine.py
|
| 41 |
-
|
| 42 |
-
def compute_bmi(weight, height):
|
| 43 |
-
"""Compute BMI given weight (kg) and height (cm)."""
|
| 44 |
-
if height <= 0:
|
| 45 |
-
return None
|
| 46 |
-
return round(weight / ((height / 100) ** 2), 2)
|
| 47 |
-
|
| 48 |
|
| 49 |
-
def rules_risk_assessment(age, sex,
|
| 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 |
-
# BMI
|
| 82 |
-
if bmi and bmi > 30:
|
| 83 |
-
risk_score += 1
|
| 84 |
-
|
| 85 |
-
# Categorize
|
| 86 |
-
if risk_score <= 2:
|
| 87 |
-
return "Low Risk"
|
| 88 |
-
elif risk_score <= 5:
|
| 89 |
-
return "Moderate Risk"
|
| 90 |
-
else:
|
| 91 |
-
return "High Risk"
|
|
|
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|