|
32 | 32 | from nifreeze.data import load |
33 | 33 | from nifreeze.data.dmri import ( |
34 | 34 | DWI, |
| 35 | + GRADIENT_ABSENCE_ERROR_MSG, |
35 | 36 | GRADIENT_BVAL_BVEC_PRIORITY_WARN_MSG, |
36 | 37 | GRADIENT_DATA_MISSING_ERROR, |
37 | 38 | GRADIENT_EXPECTED_COLUMNS_ERROR_MSG, |
38 | 39 | GRADIENT_NDIM_ERROR_MSG, |
| 40 | + GRADIENT_OBJECT_ERROR_MSG, |
39 | 41 | GRADIENT_VOLUME_DIMENSIONALITY_MISMATCH_ERROR, |
40 | 42 | find_shelling_scheme, |
| 43 | + format_gradients, |
41 | 44 | from_nii, |
42 | 45 | transform_fsl_bvec, |
| 46 | + validate_gradients, |
43 | 47 | ) |
44 | 48 | from nifreeze.utils.ndimage import load_api |
45 | 49 |
|
@@ -88,6 +92,70 @@ def test_main(datadir): |
88 | 92 | assert isinstance(load(input_file), DWI) |
89 | 93 |
|
90 | 94 |
|
| 95 | +@pytest.mark.parametrize( |
| 96 | + "value, expected_exc, expected_msg", |
| 97 | + [ |
| 98 | + (np.array([[1], [2]], dtype=object), ValueError, GRADIENT_EXPECTED_COLUMNS_ERROR_MSG), |
| 99 | + (np.zeros((2, 3)), ValueError, GRADIENT_EXPECTED_COLUMNS_ERROR_MSG), |
| 100 | + ], |
| 101 | +) |
| 102 | +def test_validate_gradients(value, expected_exc, expected_msg): |
| 103 | + with pytest.raises(expected_exc, match=re.escape(str(expected_msg))): |
| 104 | + validate_gradients(None, "gradients", value) |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.parametrize( |
| 108 | + "value, expected_exc, expected_msg", |
| 109 | + [ |
| 110 | + (None, ValueError, GRADIENT_ABSENCE_ERROR_MSG), |
| 111 | + (3.14, ValueError, GRADIENT_NDIM_ERROR_MSG), |
| 112 | + ([1, 2, 3, 4], ValueError, GRADIENT_NDIM_ERROR_MSG), |
| 113 | + (np.arange(24).reshape(4, 3, 2), ValueError, GRADIENT_NDIM_ERROR_MSG), |
| 114 | + ([[1, 2], [3, 4, 5]], (TypeError, ValueError), GRADIENT_OBJECT_ERROR_MSG), # Ragged |
| 115 | + ], |
| 116 | +) |
| 117 | +def test_format_gradients_errors(value, expected_exc, expected_msg): |
| 118 | + with pytest.raises(expected_exc, match=str(expected_msg)): |
| 119 | + format_gradients(value) |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.parametrize( |
| 123 | + "value, expect_transpose", |
| 124 | + [ |
| 125 | + # 2D arrays where first dim == 4 and second dim == 4 -> NO transpose |
| 126 | + (np.arange(16).reshape(4, 4), False), |
| 127 | + # 2D arrays where first dim == 4 and second dim != 4 -> transpose |
| 128 | + (np.arange(12).reshape(4, 3), True), |
| 129 | + (np.arange(4).reshape(4, 1), True), |
| 130 | + (np.empty((4, 0)), True), # zero columns -> still triggers transpose |
| 131 | + (np.arange(20).reshape(4, 5), True), |
| 132 | + # 2D arrays where first dim != 4 -> NO transpose |
| 133 | + (np.arange(12).reshape(3, 4), False), |
| 134 | + (np.arange(20).reshape(5, 4), False), |
| 135 | + # List of lists |
| 136 | + ([[1, 2, 3, 4], [5, 6, 7, 8]], False), |
| 137 | + ([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], True), |
| 138 | + ], |
| 139 | +) |
| 140 | +def test_format_gradients_basic(value, expect_transpose): |
| 141 | + obtained = format_gradients(value) |
| 142 | + |
| 143 | + assert isinstance(obtained, np.ndarray) |
| 144 | + if expect_transpose: |
| 145 | + assert obtained.shape == np.asarray(value).T.shape |
| 146 | + assert np.allclose(obtained, np.asarray(value).T) |
| 147 | + else: |
| 148 | + assert obtained.shape == np.asarray(value).shape |
| 149 | + assert np.allclose(obtained, np.asarray(value)) |
| 150 | + |
| 151 | + |
| 152 | +@pytest.mark.random_uniform_spatial_data((2, 2, 2, 4), 0.0, 1.0) |
| 153 | +def test_gradients_absence_error(setup_random_uniform_spatial_data): |
| 154 | + data, affine = setup_random_uniform_spatial_data |
| 155 | + with pytest.raises(ValueError, match=GRADIENT_ABSENCE_ERROR_MSG): |
| 156 | + DWI(dataobj=data, affine=affine) |
| 157 | + |
| 158 | + |
91 | 159 | @pytest.mark.random_gtab_data(10, (1000, 2000), 2) |
92 | 160 | @pytest.mark.random_dwi_data(50, (34, 36, 24), True) |
93 | 161 | @pytest.mark.parametrize("row_major_gradients", (False, True)) |
|
0 commit comments