File size: 3,729 Bytes
7881c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import csv
import json
import sys
from typing import Dict, List


DEFAULT_INPUT_PATH = "data/tester.csv"


def _safe_float(value, default: float = 0.0) -> float:
    try:
        return float(value)
    except (TypeError, ValueError):
        return default


def _read_csv(path: str) -> List[Dict[str, str]]:
    with open(path, "r", encoding="utf-8") as f:
        return list(csv.DictReader(f))


def _detect_id_column(fieldnames: List[str]) -> str:
    preferred = ["id", "row_id", "sample_id", "case_id", "record_id"]
    for col in preferred:
        if col in fieldnames:
            return col
    return ""


def heuristic_score(row: Dict[str, str]) -> float:
    narrative_coherence_score = _safe_float(row.get("narrative_coherence_score"))
    image_alignment_score = _safe_float(row.get("image_alignment_score"))
    interpretive_distortion_index = _safe_float(row.get("interpretive_distortion_index"))
    signal_fragmentation_score = _safe_float(row.get("signal_fragmentation_score"))
    drift_gradient = _safe_float(row.get("drift_gradient"))
    representation_stability_score = _safe_float(row.get("representation_stability_score"))
    context_integrity_score = _safe_float(row.get("context_integrity_score"))
    decision_readiness_score = _safe_float(row.get("decision_readiness_score"))

    score = 0.0
    score += max(0.0, 1.0 - narrative_coherence_score) * 1.1
    score += max(0.0, 1.0 - image_alignment_score) * 1.0
    score += interpretive_distortion_index * 1.3
    score += signal_fragmentation_score * 1.1
    score += max(0.0, drift_gradient) * 1.4
    score += max(0.0, 1.0 - representation_stability_score) * 1.1
    score += max(0.0, 1.0 - context_integrity_score) * 1.0
    score += max(0.0, 1.0 - decision_readiness_score) * 1.0

    if score < 0.0:
        return 0.0
    if score > 1.0:
        return 1.0
    return round(score, 6)


def generate_predictions(
    input_path: str = DEFAULT_INPUT_PATH,
    output_path: str = "predictions.csv",
    threshold: float = 0.5,
) -> Dict[str, object]:
    rows = _read_csv(input_path)
    if not rows:
        raise ValueError("Input file is empty.")

    fieldnames = list(rows[0].keys())
    id_col = _detect_id_column(fieldnames)

    output_rows = []
    positive_predictions = 0

    for idx, row in enumerate(rows):
        row_id = row[id_col] if id_col else str(idx)
        pred_score = heuristic_score(row)
        pred_label = 1 if pred_score >= threshold else 0
        positive_predictions += pred_label

        output_rows.append(
            {
                "id": row_id,
                "prediction_score": pred_score,
                "prediction": pred_label,
            }
        )

    with open(output_path, "w", encoding="utf-8", newline="") as f:
        writer = csv.DictWriter(f, fieldnames=["id", "prediction_score", "prediction"])
        writer.writeheader()
        writer.writerows(output_rows)

    return {
        "input_path": input_path,
        "output_path": output_path,
        "rows_processed": len(rows),
        "threshold_used": threshold,
        "predicted_positive_support": positive_predictions,
        "predicted_negative_support": len(rows) - positive_predictions,
        "note": "This is a dataset-specific baseline heuristic, not the canonical evaluation scorer.",
    }


if __name__ == "__main__":
    input_path = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_INPUT_PATH
    output_path = sys.argv[2] if len(sys.argv) > 2 else "predictions.csv"
    threshold = float(sys.argv[3]) if len(sys.argv) > 3 else 0.5

    result = generate_predictions(
        input_path=input_path,
        output_path=output_path,
        threshold=threshold,
    )
    print(json.dumps(result, indent=2))