Spaces:
Running on Zero
Running on Zero
File size: 4,974 Bytes
99da741 | 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | import pytest
from pathlib import Path
import numpy as np
import nibabel as nib
from cad_benchmark_space.validator import validate_submission
@pytest.fixture
def mock_gt():
return {
"canonical_orientation": "RAS+",
"cases": {
"case001": {
"labels": {"2c": 1, "2d": 0},
"shape": (32, 64, 64),
"spacing": (1.5, 0.75, 0.75),
"orientation": ("R", "A", "S"),
"findings": {
"case001_f0": {"class": "2c", "mask_path": "masks/case001_f0.nii.gz"}
},
"gt_masks": {"2c": np.zeros((32, 64, 64), dtype=bool), "2d": np.zeros((32, 64, 64), dtype=bool)},
},
"case002": {
"labels": {"2c": 0, "2d": 1},
"shape": (32, 64, 64),
"spacing": (1.5, 0.75, 0.75),
"orientation": ("R", "A", "S"),
"findings": {
"case002_f0": {"class": "2d", "mask_path": "masks/case002_f0.nii.gz"}
},
"gt_masks": {"2c": np.zeros((32, 64, 64), dtype=bool), "2d": np.zeros((32, 64, 64), dtype=bool)},
},
},
}
def test_reject_invalid_model_type(mock_gt):
manifest = {"model_name": "TestModel", "model_type": "invalid_type"}
res = validate_submission(manifest, mock_gt)
assert not res.is_valid
assert any("model_type" in err for err in res.errors)
def test_classification_reject_floats(mock_gt):
manifest = {
"model_name": "TestModel",
"model_type": "classification",
"predictions": [
{"scan_id": "case001", "2c": 0.85, "2d": 0}
],
}
res = validate_submission(manifest, mock_gt)
assert not res.is_valid
assert any("must be int 0 or 1" in err for err in res.errors)
def test_classification_reject_unknown_scan_id(mock_gt):
manifest = {
"model_name": "TestModel",
"model_type": "classification",
"predictions": [
{"scan_id": "case999", "2c": 1, "2d": 0}
],
}
res = validate_submission(manifest, mock_gt)
assert not res.is_valid
assert any("case999" in err for err in res.errors)
def test_classification_reject_duplicates(mock_gt):
manifest = {
"model_name": "TestModel",
"model_type": "classification",
"predictions": [
{"scan_id": "case001", "2c": 1, "2d": 0},
{"scan_id": "case001", "2c": 0, "2d": 1},
],
}
res = validate_submission(manifest, mock_gt)
assert not res.is_valid
assert any("duplicate scan_id" in err for err in res.errors)
def test_classification_valid(mock_gt):
manifest = {
"model_name": "TestModel",
"model_type": "classification",
"predictions": [
{"scan_id": "case001", "2c": 1, "2d": 0},
{"scan_id": "case002", "2c": 0, "2d": 1},
],
}
res = validate_submission(manifest, mock_gt)
assert res.is_valid
assert len(res.classification_samples) == 2
def test_segmentation_validation(mock_gt, tmp_path):
shape = (32, 64, 64)
spacing = (1.5, 0.75, 0.75)
affine = np.diag([spacing[0], spacing[1], spacing[2], 1.0])
mask_data = np.zeros(shape, dtype=np.uint8)
mask_data[10:15, 20:30, 20:30] = 1
mask_file = tmp_path / "case001_f0.nii.gz"
nii = nib.Nifti1Image(mask_data, affine)
nib.save(nii, str(mask_file))
manifest = {
"model_name": "SegModel",
"model_type": "segmentation",
"predictions": [
{
"scan_id": "case001",
"finding_id": "case001_f0",
"class": "2c",
"mask_path": "case001_f0.nii.gz",
"is_soft_mask": False,
}
],
}
res = validate_submission(manifest, mock_gt, files_dir=tmp_path)
assert res.is_valid
assert len(res.localization_samples) == 1
def test_segmentation_reject_shape_mismatch(mock_gt, tmp_path):
wrong_shape = (20, 64, 64)
spacing = (1.5, 0.75, 0.75)
affine = np.diag([spacing[0], spacing[1], spacing[2], 1.0])
mask_data = np.zeros(wrong_shape, dtype=np.uint8)
mask_file = tmp_path / "case001_wrong.nii.gz"
nii = nib.Nifti1Image(mask_data, affine)
nib.save(nii, str(mask_file))
manifest = {
"model_name": "SegModel",
"model_type": "segmentation",
"predictions": [
{
"scan_id": "case001",
"finding_id": "case001_f0",
"class": "2c",
"mask_path": "case001_wrong.nii.gz",
}
],
}
res = validate_submission(manifest, mock_gt, files_dir=tmp_path)
assert not res.is_valid
assert any("mask shape" in err for err in res.errors)
|