Test volume source estimate morph, special cases and exceptions.
(tmp_path)
| 365 | @pytest.mark.slowtest |
| 366 | @testing.requires_testing_data |
| 367 | def test_volume_source_morph_basic(tmp_path): |
| 368 | """Test volume source estimate morph, special cases and exceptions.""" |
| 369 | nib = pytest.importorskip("nibabel") |
| 370 | pytest.importorskip("h5io") |
| 371 | pytest.importorskip("dipy") |
| 372 | inverse_operator_vol = read_inverse_operator(fname_inv_vol) |
| 373 | stc_vol = read_source_estimate(fname_vol_w, "sample") |
| 374 | |
| 375 | # check for invalid input type |
| 376 | with pytest.raises(TypeError, match="src must be"): |
| 377 | compute_source_morph(src=42) |
| 378 | |
| 379 | # check for raising an error if neither |
| 380 | # inverse_operator_vol['src'][0]['subject_his_id'] nor subject_from is set, |
| 381 | # but attempting to perform a volume morph |
| 382 | src = inverse_operator_vol["src"] |
| 383 | assert src._subject is None # already None on disk (old!) |
| 384 | |
| 385 | with pytest.raises(ValueError, match="subject_from could not be inferred"): |
| 386 | with pytest.warns(RuntimeWarning, match="recommend regenerating"): |
| 387 | compute_source_morph(src=src, subjects_dir=subjects_dir) |
| 388 | |
| 389 | # check infer subject_from from src[0]['subject_his_id'] |
| 390 | src[0]["subject_his_id"] = "sample" |
| 391 | |
| 392 | with pytest.raises(ValueError, match="Inter-hemispheric morphing"): |
| 393 | compute_source_morph(src=src, subjects_dir=subjects_dir, xhemi=True) |
| 394 | |
| 395 | with pytest.raises(ValueError, match="Only surface.*sparse morph"): |
| 396 | compute_source_morph(src=src, sparse=True, subjects_dir=subjects_dir) |
| 397 | |
| 398 | # terrible quality but fast |
| 399 | zooms = 20 |
| 400 | kwargs = dict(zooms=zooms, niter_sdr=(1,), niter_affine=(1,)) |
| 401 | source_morph_vol = compute_source_morph( |
| 402 | subjects_dir=subjects_dir, src=fname_inv_vol, subject_from="sample", **kwargs |
| 403 | ) |
| 404 | shape = (13,) * 3 # for the given zooms |
| 405 | |
| 406 | assert source_morph_vol.subject_from == "sample" |
| 407 | |
| 408 | # the brain used in sample data has shape (255, 255, 255) |
| 409 | assert tuple(source_morph_vol.sdr_morph.domain_shape) == shape |
| 410 | |
| 411 | assert tuple(source_morph_vol.pre_affine.domain_shape) == shape |
| 412 | |
| 413 | # proofs the above |
| 414 | assert_array_equal(source_morph_vol.zooms, (zooms,) * 3) |
| 415 | |
| 416 | # assure proper src shape |
| 417 | mri_size = (src[0]["mri_height"], src[0]["mri_depth"], src[0]["mri_width"]) |
| 418 | assert source_morph_vol.src_data["src_shape_full"] == mri_size |
| 419 | |
| 420 | fwd = read_forward_solution(fname_fwd_vol) |
| 421 | fwd["src"][0]["subject_his_id"] = "sample" # avoid further warnings |
| 422 | source_morph_vol = compute_source_morph( |
| 423 | fwd["src"], "sample", "sample", subjects_dir=subjects_dir, **kwargs |
| 424 | ) |
nothing calls this directly
no test coverage detected