test(models): real-artifact sanity for MRI DL 2D (skips when absent)
Browse files
tests/models/test_mri_dl_2d_real.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Real-artifact sanity. Skipped unless the checkpoint is present.
|
| 2 |
+
|
| 3 |
+
When the user drops their trained best_model.pt at the canonical path, this
|
| 4 |
+
test runs automatically — catches class-order or input-shape drift.
|
| 5 |
+
"""
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
import pytest
|
| 12 |
+
from PIL import Image
|
| 13 |
+
|
| 14 |
+
from src.models import mri_dl_2d
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
REAL_CKPT = Path("data/processed/mri_dl_2d/best_model.pt")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@pytest.mark.skipif(not REAL_CKPT.exists(), reason="real MRI checkpoint not present")
|
| 21 |
+
def test_real_checkpoint_loads_and_predicts(tmp_path):
|
| 22 |
+
model = mri_dl_2d.load(REAL_CKPT)
|
| 23 |
+
arr = (np.random.RandomState(0).rand(170, 170, 3) * 255).astype(np.uint8)
|
| 24 |
+
img = tmp_path / "scan.png"
|
| 25 |
+
Image.fromarray(arr).save(str(img))
|
| 26 |
+
result = mri_dl_2d.predict_image(model, img)
|
| 27 |
+
|
| 28 |
+
assert result["label_text"] in mri_dl_2d.CLASS_TO_IDX
|
| 29 |
+
s = sum(p["probability"] for p in result["probabilities"])
|
| 30 |
+
assert abs(s - 1.0) < 1e-5
|