File size: 2,225 Bytes
f559cc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "backend"))

from app.ml.runtime_stack import (
    RUNTIME_STACK_VERSION,
    build_runtime_stack_prediction,
    decision_threshold_for_source,
    hb_archive_weight_for_source,
    risk_archive_weight_for_source,
)


def test_decision_threshold_defaults_are_source_aware() -> None:
    assert decision_threshold_for_source("roi_original") == 0.495
    assert decision_threshold_for_source("palpebral") == 0.65
    assert decision_threshold_for_source("forniceal_palpebral") == 0.65


def test_runtime_stack_prediction_keeps_archive_signal_without_secondary_model() -> None:
    result = build_runtime_stack_prediction(
        {
            "anemia_risk": 0.58,
            "predicted_hemoglobin": 11.4,
            "uncertainty": 0.21,
        },
        source_hint="roi_original",
    )

    assert result["anemia_risk"] == 0.58
    assert result["predicted_hemoglobin"] == 11.4
    assert result["uncertainty"] == 0.21
    assert result["decision_threshold"] == 0.495


def test_runtime_stack_blends_archive_and_efficientnet_for_roi() -> None:
    result = build_runtime_stack_prediction(
        {
            "anemia_risk": 0.7,
            "predicted_hemoglobin": 10.8,
            "uncertainty": 0.18,
        },
        efficientnet_prediction={
            "anemia_risk": 0.3,
            "predicted_hemoglobin": 12.0,
            "uncertainty": 0.24,
        },
        source_hint="roi_original",
    )

    assert round(result["anemia_risk"], 4) == 0.5138
    assert round(result["predicted_hemoglobin"], 4) == 11.16
    assert round(result["decision_threshold"], 4) == 0.495
    assert round(result["uncertainty"], 4) == 0.2424


def test_runtime_stack_weights_are_source_aware() -> None:
    assert risk_archive_weight_for_source("roi_original") == 0.55
    assert risk_archive_weight_for_source("palpebral") == 1.0
    assert hb_archive_weight_for_source("roi_original") == 0.70
    assert hb_archive_weight_for_source("palpebral") == 1.0


def test_runtime_stack_version_is_declared() -> None:
    assert RUNTIME_STACK_VERSION == "archive-evidence-fusion-v4"