ClarusC64 commited on
Commit
2e75fcc
·
verified ·
1 Parent(s): e6c882a

Create baseline_heuristic.py

Browse files
Files changed (1) hide show
  1. baseline_heuristic.py +112 -0
baseline_heuristic.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import json
3
+ import sys
4
+ from typing import Dict, List
5
+
6
+
7
+ DEFAULT_INPUT_PATH = "data/tester.csv"
8
+
9
+
10
+ def _safe_float(value, default: float = 0.0) -> float:
11
+ try:
12
+ return float(value)
13
+ except (TypeError, ValueError):
14
+ return default
15
+
16
+
17
+ def _read_csv(path: str) -> List[Dict[str, str]]:
18
+ with open(path, "r", encoding="utf-8") as f:
19
+ return list(csv.DictReader(f))
20
+
21
+
22
+ def _detect_id_column(fieldnames: List[str]) -> str:
23
+ preferred = ["id", "row_id", "sample_id", "case_id", "record_id"]
24
+ for col in preferred:
25
+ if col in fieldnames:
26
+ return col
27
+ return ""
28
+
29
+
30
+ def heuristic_score(row: Dict[str, str]) -> float:
31
+ intervention_intensity = _safe_float(row.get("intervention_intensity"))
32
+ physiologic_instability_index = _safe_float(row.get("physiologic_instability_index"))
33
+ recovery_signal_strength = _safe_float(row.get("recovery_signal_strength"))
34
+ overshoot_risk_score = _safe_float(row.get("overshoot_risk_score"))
35
+ drift_gradient = _safe_float(row.get("drift_gradient"))
36
+ stop_point_margin = _safe_float(row.get("stop_point_margin"))
37
+ intervention_taper_alignment = _safe_float(row.get("intervention_taper_alignment"))
38
+ coordination_stability_score = _safe_float(row.get("coordination_stability_score"))
39
+
40
+ score = 0.0
41
+ score += intervention_intensity * 1.0
42
+ score += physiologic_instability_index * 1.2
43
+ score += overshoot_risk_score * 1.5
44
+ score += max(0.0, drift_gradient) * 1.4
45
+ score += max(0.0, 0.20 - stop_point_margin) * 2.0
46
+ score -= recovery_signal_strength * 1.0
47
+ score -= intervention_taper_alignment * 0.9
48
+ score -= coordination_stability_score * 0.8
49
+
50
+ if score < 0.0:
51
+ return 0.0
52
+ if score > 1.0:
53
+ return 1.0
54
+ return round(score, 6)
55
+
56
+
57
+ def generate_predictions(
58
+ input_path: str = DEFAULT_INPUT_PATH,
59
+ output_path: str = "predictions.csv",
60
+ threshold: float = 0.5,
61
+ ) -> Dict[str, object]:
62
+ rows = _read_csv(input_path)
63
+ if not rows:
64
+ raise ValueError("Input file is empty.")
65
+
66
+ fieldnames = list(rows[0].keys())
67
+ id_col = _detect_id_column(fieldnames)
68
+
69
+ output_rows = []
70
+ positive_predictions = 0
71
+
72
+ for idx, row in enumerate(rows):
73
+ row_id = row[id_col] if id_col else str(idx)
74
+ pred_score = heuristic_score(row)
75
+ pred_label = 1 if pred_score >= threshold else 0
76
+ positive_predictions += pred_label
77
+
78
+ output_rows.append(
79
+ {
80
+ "id": row_id,
81
+ "prediction_score": pred_score,
82
+ "prediction": pred_label,
83
+ }
84
+ )
85
+
86
+ with open(output_path, "w", encoding="utf-8", newline="") as f:
87
+ writer = csv.DictWriter(f, fieldnames=["id", "prediction_score", "prediction"])
88
+ writer.writeheader()
89
+ writer.writerows(output_rows)
90
+
91
+ return {
92
+ "input_path": input_path,
93
+ "output_path": output_path,
94
+ "rows_processed": len(rows),
95
+ "threshold_used": threshold,
96
+ "predicted_positive_support": positive_predictions,
97
+ "predicted_negative_support": len(rows) - positive_predictions,
98
+ "note": "This is a dataset-specific baseline heuristic, not the canonical evaluation scorer.",
99
+ }
100
+
101
+
102
+ if __name__ == "__main__":
103
+ input_path = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_INPUT_PATH
104
+ output_path = sys.argv[2] if len(sys.argv) > 2 else "predictions.csv"
105
+ threshold = float(sys.argv[3]) if len(sys.argv) > 3 else 0.5
106
+
107
+ result = generate_predictions(
108
+ input_path=input_path,
109
+ output_path=output_path,
110
+ threshold=threshold,
111
+ )
112
+ print(json.dumps(result, indent=2))