Spaces:
Running
Running
Upload 2 files
Browse files- app/rules/ecg_rules.py +55 -0
- app/rules/engine.py +21 -0
app/rules/ecg_rules.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Dict, List
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def apply_rules(
|
| 5 |
+
patient_context: Dict[str, Any],
|
| 6 |
+
model_output: Dict[str, Any],
|
| 7 |
+
) -> Dict[str, Any]:
|
| 8 |
+
"""
|
| 9 |
+
Simple rule set over ECG model outputs.
|
| 10 |
+
"""
|
| 11 |
+
label = model_output.get("label")
|
| 12 |
+
score = float(model_output.get("score", 0.0))
|
| 13 |
+
hr = model_output.get("hr")
|
| 14 |
+
hr_int = int(hr) if hr is not None else None
|
| 15 |
+
|
| 16 |
+
explanations: List[str] = []
|
| 17 |
+
alert_level = "none"
|
| 18 |
+
age = patient_context.get("age")
|
| 19 |
+
has_prior_stroke = bool(patient_context.get("has_prior_stroke"))
|
| 20 |
+
|
| 21 |
+
# Arrhythmia label with high confidence
|
| 22 |
+
arrhythmia_labels = {"arrhythmia", "suspected_afib", "afib"}
|
| 23 |
+
if label in arrhythmia_labels and score >= 0.85:
|
| 24 |
+
alert_level = "escalate"
|
| 25 |
+
explanations.append("High-confidence arrhythmia detected.")
|
| 26 |
+
elif label in arrhythmia_labels and score >= 0.6:
|
| 27 |
+
alert_level = "notify"
|
| 28 |
+
explanations.append("Arrhythmia suspected; monitor closely.")
|
| 29 |
+
|
| 30 |
+
# Heart rate based thresholds
|
| 31 |
+
if hr_int is not None:
|
| 32 |
+
if hr_int >= 140:
|
| 33 |
+
alert_level = "escalate"
|
| 34 |
+
explanations.append("Severe tachycardia (hr >= 140).")
|
| 35 |
+
elif hr_int >= 120 and alert_level == "none":
|
| 36 |
+
alert_level = "notify"
|
| 37 |
+
explanations.append("Tachycardia (hr >= 120).")
|
| 38 |
+
|
| 39 |
+
# Patient risk factors escalate one level
|
| 40 |
+
if has_prior_stroke and alert_level != "none":
|
| 41 |
+
alert_level = "escalate"
|
| 42 |
+
explanations.append("Prior stroke history: escalate.")
|
| 43 |
+
elif has_prior_stroke and alert_level == "none":
|
| 44 |
+
alert_level = "notify"
|
| 45 |
+
explanations.append("Prior stroke history: monitor closely.")
|
| 46 |
+
|
| 47 |
+
if isinstance(age, int) and age >= 75 and alert_level == "notify":
|
| 48 |
+
alert_level = "escalate"
|
| 49 |
+
explanations.append("Age >= 75 with concerning signal: escalate.")
|
| 50 |
+
|
| 51 |
+
# If no triggers, add baseline explanation
|
| 52 |
+
if not explanations:
|
| 53 |
+
explanations.append("No rule-based alerts triggered.")
|
| 54 |
+
|
| 55 |
+
return {"alert_level": alert_level, "explanations": explanations}
|
app/rules/engine.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Dict
|
| 2 |
+
|
| 3 |
+
from app.rules import ecg_rules
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def evaluate_ecg_rules(
|
| 7 |
+
patient_context: Dict[str, Any],
|
| 8 |
+
model_output: Dict[str, Any],
|
| 9 |
+
) -> Dict[str, Any]:
|
| 10 |
+
"""
|
| 11 |
+
Apply ECG-specific rules to the model output and patient context.
|
| 12 |
+
|
| 13 |
+
Returns:
|
| 14 |
+
dict with keys:
|
| 15 |
+
- alert_level: str
|
| 16 |
+
- explanations: list[str]
|
| 17 |
+
"""
|
| 18 |
+
result = ecg_rules.apply_rules(patient_context, model_output)
|
| 19 |
+
alert_level = result.get("alert_level", "none")
|
| 20 |
+
explanations = result.get("explanations", [])
|
| 21 |
+
return {"alert_level": alert_level, "explanations": explanations}
|