File size: 978 Bytes
21c7db9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import pickle
from pathlib import Path

from app.models.graph.infer import infer_graph_risk


class BrokenProbabilityModel:
    def predict_proba(self, _encoded):
        raise AttributeError("multi_class")


class FakeLabelBinarizer:
    classes_ = ["dizziness"]


def test_graph_inference_uses_deterministic_fallback_for_stale_model(tmp_path: Path) -> None:
    model_path = tmp_path / "graph_model.pkl"
    with model_path.open("wb") as handle:
        pickle.dump(
            {
                "severe_model": BrokenProbabilityModel(),
                "side_model": BrokenProbabilityModel(),
                "mlb": FakeLabelBinarizer(),
            },
            handle,
        )

    risk = infer_graph_risk(["warfarin", "aspirin"], model_path=model_path)

    assert 0.0 <= risk["severe_alert_probability"] <= 1.0
    assert isinstance(risk["side_effect_probs"], dict)
    assert "warfarin__aspirin" in risk["pairwise_ddi_severity"]