Test mixed source space morphing.
(_mixed_morph_srcs, vector)
| 1028 | @pytest.mark.slowtest |
| 1029 | @pytest.mark.parametrize("vector", (False, True)) |
| 1030 | def test_mixed_source_morph(_mixed_morph_srcs, vector): |
| 1031 | """Test mixed source space morphing.""" |
| 1032 | nib = pytest.importorskip("nibabel") |
| 1033 | pytest.importorskip("dipy") |
| 1034 | morph, src, src_fs = _mixed_morph_srcs |
| 1035 | # Test some basic properties in the subject's own space |
| 1036 | lut, _ = read_freesurfer_lut() |
| 1037 | ids = [lut[s["seg_name"]] for s in src[2:]] |
| 1038 | del lut |
| 1039 | vertices = [s["vertno"] for s in src] |
| 1040 | n_vertices = sum(len(v) for v in vertices) |
| 1041 | data = np.zeros((n_vertices, 3, 1)) |
| 1042 | data[:, 1] = 1.0 |
| 1043 | klass = mne.MixedVectorSourceEstimate |
| 1044 | if not vector: |
| 1045 | data = data[:, 1] |
| 1046 | klass = klass._scalar_class |
| 1047 | stc = klass(data, vertices, 0, 1, "sample") |
| 1048 | vol_info = _get_mri_info_data(fname_aseg, data=True) |
| 1049 | rrs = np.concatenate([src[2]["rr"][sp["vertno"]] for sp in src[2:]]) |
| 1050 | n_want = np.isin(_get_atlas_values(vol_info, rrs), ids).sum() |
| 1051 | img = _get_img_fdata(stc.volume().as_volume(src, mri_resolution=False)) |
| 1052 | assert img.astype(bool).sum() == n_want |
| 1053 | img_res = nib.load(fname_aseg) |
| 1054 | n_want = np.isin(_get_img_fdata(img_res), ids).sum() |
| 1055 | img = _get_img_fdata(stc.volume().as_volume(src, mri_resolution=True)) |
| 1056 | assert img.astype(bool).sum() > n_want # way more get interpolated into |
| 1057 | |
| 1058 | with pytest.raises(TypeError, match="stc_from must be an instance"): |
| 1059 | morph.apply(1.0) |
| 1060 | |
| 1061 | # Now actually morph |
| 1062 | stc_fs = morph.apply(stc) |
| 1063 | vol_info = _get_mri_info_data(fname_aseg_fs, data=True) |
| 1064 | rrs = np.concatenate([src_fs[2]["rr"][sp["vertno"]] for sp in src_fs[2:]]) |
| 1065 | n_want = np.isin(_get_atlas_values(vol_info, rrs), ids).sum() |
| 1066 | with pytest.raises(ValueError, match=r"stc\.subject does not match src s"): |
| 1067 | stc_fs.volume().as_volume(src, mri_resolution=False) |
| 1068 | img = _get_img_fdata(stc_fs.volume().as_volume(src_fs, mri_resolution=False)) |
| 1069 | assert img.astype(bool).sum() == n_want # correct number of voxels |
| 1070 | |
| 1071 | # Morph separate parts and compare to morphing the entire one |
| 1072 | stc_fs_surf = morph.apply(stc.surface()) |
| 1073 | stc_fs_vol = morph.apply(stc.volume()) |
| 1074 | stc_fs_2 = stc_fs.__class__( |
| 1075 | np.concatenate([stc_fs_surf.data, stc_fs_vol.data]), |
| 1076 | stc_fs_surf.vertices + stc_fs_vol.vertices, |
| 1077 | stc_fs.tmin, |
| 1078 | stc_fs.tstep, |
| 1079 | stc_fs.subject, |
| 1080 | ) |
| 1081 | assert_allclose(stc_fs.data, stc_fs_2.data) |
| 1082 | |
| 1083 | |
| 1084 | def _rand_affine(rng): |
nothing calls this directly
no test coverage detected