Skip to content

Commit b95fc4a

Browse files
BUG: Fix bug with volume (#12544)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 6368a0b commit b95fc4a

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

doc/changes/devel/12544.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug with :func:`mne.SourceSpaces.export_volume` where the ``img.affine`` was not set properly, by `Eric Larson`_.

mne/source_space/_source_space.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,10 +668,11 @@ def export_volume(
668668

669669
# Figure out how to get from our input source space to output voxels
670670
fro_dst_t = invert_transform(transform)
671-
dest = transform["to"]
672671
if coords == "head":
673672
head_mri_t = _get_trans(trans, "head", "mri")[0]
674-
fro_dst_t = combine_transforms(head_mri_t, fro_dst_t, "head", dest)
673+
fro_dst_t = combine_transforms(
674+
head_mri_t, fro_dst_t, "head", transform["to"]
675+
)
675676
else:
676677
fro_dst_t = fro_dst_t
677678

mne/source_space/tests/test_source_space.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,27 @@ def test_combine_source_spaces(tmp_path):
874874
with pytest.warns(RuntimeWarning, match="2 surf vertices lay outside"):
875875
src.export_volume(image_fname, mri_resolution="sparse", overwrite=True)
876876

877+
# gh-12495
878+
image_fname = tmp_path / "temp-image.nii"
879+
lh_cereb = mne.setup_volume_source_space(
880+
"sample",
881+
mri=aseg_fname,
882+
volume_label="Left-Cerebellum-Cortex",
883+
add_interpolator=False,
884+
subjects_dir=subjects_dir,
885+
)
886+
lh_cereb.export_volume(image_fname, mri_resolution=True)
887+
aseg = nib.load(str(aseg_fname))
888+
out = nib.load(str(image_fname))
889+
assert_allclose(out.affine, aseg.affine)
890+
src_data = _get_img_fdata(out).astype(bool)
891+
aseg_data = _get_img_fdata(aseg) == 8
892+
n_src = src_data.sum()
893+
n_aseg = aseg_data.sum()
894+
assert n_aseg == n_src
895+
n_overlap = (src_data & aseg_data).sum()
896+
assert n_src == n_overlap
897+
877898

878899
@testing.requires_testing_data
879900
def test_morph_source_spaces():

0 commit comments

Comments
 (0)