|
22 | 22 | # |
23 | 23 |
|
24 | 24 | import json |
| 25 | +import re |
25 | 26 | from pathlib import Path |
26 | 27 |
|
27 | 28 | import nibabel as nb |
28 | 29 | import numpy as np |
29 | 30 | import pytest |
30 | 31 | from nitransforms.linear import Affine |
31 | 32 |
|
32 | | -from nifreeze.data.pet import PET, _compute_frame_duration, _compute_uptake_statistic, from_nii |
| 33 | +from nifreeze.data.pet import ( |
| 34 | + ARRAY_ATTRIBUTE_ABSENCE_ERROR_MSG, |
| 35 | + ARRAY_ATTRIBUTE_NDIM_ERROR_MSG, |
| 36 | + ARRAY_ATTRIBUTE_OBJECT_ERROR_MSG, |
| 37 | + ATTRIBUTE_SHAPE_MISMATCH_ERROR_MSG, |
| 38 | + PET, |
| 39 | + SCALAR_ATTRIBUTE_OBJECT_ERROR_MSG, |
| 40 | + _compute_frame_duration, |
| 41 | + _compute_uptake_statistic, |
| 42 | + from_nii, |
| 43 | +) |
33 | 44 | from nifreeze.utils.ndimage import load_api |
34 | 45 |
|
35 | 46 |
|
@@ -63,6 +74,114 @@ def random_nifti_file(tmp_path, setup_random_uniform_spatial_data) -> Path: |
63 | 74 | return _filename |
64 | 75 |
|
65 | 76 |
|
| 77 | +@pytest.mark.random_uniform_spatial_data((2, 2, 2, 4), 0.0, 1.0) |
| 78 | +@pytest.mark.parametrize( |
| 79 | + "midframe, expected_exc, expected_msg", |
| 80 | + [ |
| 81 | + (None, ValueError, ARRAY_ATTRIBUTE_ABSENCE_ERROR_MSG), |
| 82 | + (1, TypeError, ARRAY_ATTRIBUTE_OBJECT_ERROR_MSG), |
| 83 | + ], |
| 84 | +) |
| 85 | +def test_pet_attribute_basic_errors( |
| 86 | + setup_random_uniform_spatial_data, midframe, expected_exc, expected_msg |
| 87 | +): |
| 88 | + data, affine = setup_random_uniform_spatial_data |
| 89 | + with pytest.raises(expected_exc, match=expected_msg.format(attribute="midframe")): |
| 90 | + PET(dataobj=data, affine=affine, midframe=midframe) # type: ignore[arg-type] |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.random_uniform_spatial_data((2, 2, 2, 4), 0.0, 1.0) |
| 94 | +@pytest.mark.parametrize("size", ((2, 1), (1, 2), (3, 1), (3, 2))) |
| 95 | +def test_pet_midframe_shape_error(setup_random_uniform_spatial_data, size): |
| 96 | + data, affine = setup_random_uniform_spatial_data |
| 97 | + midframe = np.zeros(size, dtype=np.float32) |
| 98 | + with pytest.raises( |
| 99 | + ValueError, match=ARRAY_ATTRIBUTE_NDIM_ERROR_MSG.format(attribute="midframe") |
| 100 | + ): |
| 101 | + PET(dataobj=data, affine=affine, midframe=midframe) |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.random_uniform_spatial_data((2, 2, 2, 4), 0.0, 1.0) |
| 105 | +@pytest.mark.parametrize("size", ((2, 1), (1, 1))) |
| 106 | +def test_pet_total_duration_error(request, setup_random_uniform_spatial_data, size): |
| 107 | + data, affine = setup_random_uniform_spatial_data |
| 108 | + midframe = np.zeros(data.shape[-1], dtype=np.float32) |
| 109 | + total_duration = request.node.rng.uniform(5.0, 20.0, size=size) |
| 110 | + with pytest.raises( |
| 111 | + ValueError, match=SCALAR_ATTRIBUTE_OBJECT_ERROR_MSG.format(attribute="total_duration") |
| 112 | + ): |
| 113 | + PET(dataobj=data, affine=affine, midframe=midframe, total_duration=total_duration) |
| 114 | + |
| 115 | + |
| 116 | +@pytest.mark.random_uniform_spatial_data((2, 2, 2, 4), 0.0, 1.0) |
| 117 | +@pytest.mark.parametrize("size", ((2, 1), (1, 2), (3, 1), (3, 2))) |
| 118 | +def test_pet_uptake_shape_error(setup_random_uniform_spatial_data, size): |
| 119 | + data, affine = setup_random_uniform_spatial_data |
| 120 | + midframe = np.zeros(data.shape[-1], dtype=np.float32) |
| 121 | + total_duration = 16.2 |
| 122 | + uptake = np.zeros(size, dtype=np.float32) |
| 123 | + with pytest.raises( |
| 124 | + ValueError, match=ARRAY_ATTRIBUTE_NDIM_ERROR_MSG.format(attribute="uptake") |
| 125 | + ): |
| 126 | + PET( |
| 127 | + dataobj=data, |
| 128 | + affine=affine, |
| 129 | + midframe=midframe, |
| 130 | + total_duration=total_duration, |
| 131 | + uptake=uptake, |
| 132 | + ) |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.random_uniform_spatial_data((2, 2, 2, 4), 0.0, 1.0) |
| 136 | +def test_pet_midframe_length_mismatch(setup_random_uniform_spatial_data): |
| 137 | + data, affine = setup_random_uniform_spatial_data |
| 138 | + total_duration = 16.2 |
| 139 | + data_frames = data.shape[-1] |
| 140 | + attr_len = data_frames + 1 |
| 141 | + midframe = np.zeros(attr_len, dtype=np.float32) |
| 142 | + uptake = np.zeros(data_frames, dtype=np.float32) |
| 143 | + with pytest.raises( |
| 144 | + ValueError, |
| 145 | + match=re.escape( |
| 146 | + ATTRIBUTE_SHAPE_MISMATCH_ERROR_MSG.format( |
| 147 | + attribute="midframe", attr_len=attr_len, data_frames=data_frames |
| 148 | + ) |
| 149 | + ), |
| 150 | + ): |
| 151 | + PET( |
| 152 | + dataobj=data, |
| 153 | + affine=affine, |
| 154 | + midframe=midframe, |
| 155 | + total_duration=total_duration, |
| 156 | + uptake=uptake, |
| 157 | + ) |
| 158 | + |
| 159 | + |
| 160 | +@pytest.mark.random_uniform_spatial_data((2, 2, 2, 4), 0.0, 1.0) |
| 161 | +def test_pet_uptake_length_mismatch(setup_random_uniform_spatial_data): |
| 162 | + data, affine = setup_random_uniform_spatial_data |
| 163 | + total_duration = 16.2 |
| 164 | + data_frames = data.shape[-1] |
| 165 | + midframe = np.zeros(data_frames, dtype=np.float32) |
| 166 | + attr_len = data_frames + 1 |
| 167 | + uptake = np.zeros(attr_len, dtype=np.float32) |
| 168 | + with pytest.raises( |
| 169 | + ValueError, |
| 170 | + match=re.escape( |
| 171 | + ATTRIBUTE_SHAPE_MISMATCH_ERROR_MSG.format( |
| 172 | + attribute="uptake", attr_len=attr_len, data_frames=data_frames |
| 173 | + ) |
| 174 | + ), |
| 175 | + ): |
| 176 | + PET( |
| 177 | + dataobj=data, |
| 178 | + affine=affine, |
| 179 | + midframe=midframe, |
| 180 | + total_duration=total_duration, |
| 181 | + uptake=uptake, |
| 182 | + ) |
| 183 | + |
| 184 | + |
66 | 185 | @pytest.mark.parametrize( |
67 | 186 | "midframe, expected", |
68 | 187 | [ |
|
0 commit comments