|
1 | 1 | """test_fieldmaps."""
|
| 2 | +from collections import namedtuple |
| 3 | +import shutil |
2 | 4 | import pytest
|
| 5 | +import bids |
3 | 6 | from .. import fieldmaps as fm
|
4 | 7 |
|
5 | 8 |
|
6 | 9 | def test_FieldmapFile(dsA_dir):
|
7 | 10 | """Test one existing file."""
|
8 |
| - fm.FieldmapFile(dsA_dir / "sub-01" / "anat" / "sub-01_T1w.nii.gz") |
| 11 | + f1 = fm.FieldmapFile(dsA_dir / "sub-01" / "anat" / "sub-01_T1w.nii.gz") |
| 12 | + f2 = fm.FieldmapFile(str(dsA_dir / "sub-01" / "anat" / "sub-01_T1w.nii.gz")) |
| 13 | + f3 = fm.FieldmapFile( |
| 14 | + bids.layout.BIDSFile(str(dsA_dir / "sub-01" / "anat" / "sub-01_T1w.nii.gz")) |
| 15 | + ) |
| 16 | + assert f1 == f2 == f3 |
| 17 | + |
| 18 | + with pytest.raises(ValueError): |
| 19 | + fm.FieldmapFile(dsA_dir / "sub-01" / "fmap" / "sub-01_dir-AP_epi.json") |
| 20 | + |
| 21 | + with pytest.raises(ValueError): |
| 22 | + fm.FieldmapFile(dsA_dir / "sub-01" / "anat" / "sub-01_FLAIR.nii.gz") |
9 | 23 |
|
10 | 24 |
|
11 | 25 | @pytest.mark.parametrize(
|
@@ -203,3 +217,78 @@ def test_FieldmapEstimationIdentifier(monkeypatch, dsA_dir):
|
203 | 217 | fm.get_identifier("file", by="invalid")
|
204 | 218 |
|
205 | 219 | fm.clear_registry()
|
| 220 | + |
| 221 | + |
| 222 | +def test_type_setter(): |
| 223 | + """Cover the _type_setter routine.""" |
| 224 | + obj = namedtuple("FieldmapEstimation", ("method",))(method=fm.EstimatorType.UNKNOWN) |
| 225 | + with pytest.raises(ValueError): |
| 226 | + fm._type_setter(obj, "method", 10) |
| 227 | + |
| 228 | + obj = namedtuple("FieldmapEstimation", ("method",))(method=fm.EstimatorType.PEPOLAR) |
| 229 | + assert ( |
| 230 | + fm._type_setter(obj, "method", fm.EstimatorType.PEPOLAR) |
| 231 | + == fm.EstimatorType.PEPOLAR |
| 232 | + ) |
| 233 | + |
| 234 | + |
| 235 | +def test_FieldmapEstimation_missing_files(tmpdir, dsA_dir): |
| 236 | + """Exercise some FieldmapEstimation checks.""" |
| 237 | + tmpdir.chdir() |
| 238 | + tmpdir.mkdir("data") |
| 239 | + |
| 240 | + # fieldmap - no magnitude |
| 241 | + path = dsA_dir / "sub-01" / "fmap" / "sub-01_fieldmap.nii.gz" |
| 242 | + shutil.copy(path, f"data/{path.name}") |
| 243 | + |
| 244 | + with pytest.raises( |
| 245 | + ValueError, |
| 246 | + match=r"A fieldmap or phase-difference .* \(magnitude file\) is missing.*", |
| 247 | + ): |
| 248 | + fm.FieldmapEstimation( |
| 249 | + [fm.FieldmapFile("data/sub-01_fieldmap.nii.gz", metadata={"Units": "Hz"})] |
| 250 | + ) |
| 251 | + |
| 252 | + # phase1/2 - no magnitude2 |
| 253 | + path = dsA_dir / "sub-01" / "fmap" / "sub-01_phase1.nii.gz" |
| 254 | + shutil.copy(path, f"data/{path.name}") |
| 255 | + |
| 256 | + path = dsA_dir / "sub-01" / "fmap" / "sub-01_magnitude1.nii.gz" |
| 257 | + shutil.copy(path, f"data/{path.name}") |
| 258 | + |
| 259 | + path = dsA_dir / "sub-01" / "fmap" / "sub-01_phase2.nii.gz" |
| 260 | + shutil.copy(path, f"data/{path.name}") |
| 261 | + |
| 262 | + with pytest.raises( |
| 263 | + ValueError, match=r".* \(phase1/2\) .* \(magnitude2 file\) is missing.*" |
| 264 | + ): |
| 265 | + fm.FieldmapEstimation( |
| 266 | + [ |
| 267 | + fm.FieldmapFile( |
| 268 | + "data/sub-01_phase1.nii.gz", metadata={"EchoTime": 0.004} |
| 269 | + ), |
| 270 | + fm.FieldmapFile( |
| 271 | + "data/sub-01_phase2.nii.gz", metadata={"EchoTime": 0.007} |
| 272 | + ), |
| 273 | + ] |
| 274 | + ) |
| 275 | + |
| 276 | + # pepolar - only one PE |
| 277 | + path = dsA_dir / "sub-01" / "fmap" / "sub-01_dir-AP_epi.nii.gz" |
| 278 | + shutil.copy(path, f"data/{path.name}") |
| 279 | + |
| 280 | + path = dsA_dir / "sub-01" / "dwi" / "sub-01_dir-AP_dwi.nii.gz" |
| 281 | + shutil.copy(path, f"data/{path.name}") |
| 282 | + with pytest.raises(ValueError, match="Only one phase-encoding direction <j>.*"): |
| 283 | + fm.FieldmapEstimation( |
| 284 | + [ |
| 285 | + fm.FieldmapFile( |
| 286 | + "data/sub-01_dir-AP_epi.nii.gz", |
| 287 | + metadata={"PhaseEncodingDirection": "j", "TotalReadoutTime": 0.004}, |
| 288 | + ), |
| 289 | + fm.FieldmapFile( |
| 290 | + "data/sub-01_dir-AP_dwi.nii.gz", |
| 291 | + metadata={"PhaseEncodingDirection": "j", "TotalReadoutTime": 0.004}, |
| 292 | + ), |
| 293 | + ] |
| 294 | + ) |
0 commit comments