# Aligning volume source space with mri

**URL:** <https://mne.discourse.group/t/aligning-volume-source-space-with-mri/10703>\
**Category:** Support & Discussions\
**Tags:** ﻿source-localization, meg, visualization\
**Created:** [January 14, 2025, 7:47am UTC](https://mne.discourse.group/t/aligning-volume-source-space-with-mri/10703 "2025-01-14T07:47:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ezekiel7811a](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/ezekiel7811a/32/4086_2.png) [@Ezekiel7811a](https://mne.discourse.group/u/Ezekiel7811a)\
**Post date:** [January 14, 2025, 7:47am UTC](https://mne.discourse.group/t/aligning-volume-source-space-with-mri/10703/1 "2025-01-14T07:47:02Z")

</div>

- MNE version: 1.8.0
- operating system: Debian GNU/Linux 9.8 (stretch)

Dear MNE community,  
I’m writing you about some difficulty I am experiencing on aligning properly the volume source space (created using MNE) and the T1 image used to create the source space.

```python
src = mne.setup_volume_source_space(subject, bem=bem_sol, pos=5)
fwd = mne.make_forward_solution(
        raw.info, src=src, bem=bem_sol, trans=trans_sol, meg=True, eeg=False
)
mri_img = nib.load(f"{subjects_dir}/{subject}/mri/T1.mgz")
src_pts = fwd['src'][0]['rr'] * 1e3
trans_coords = mne.transforms.apply_trans(trans, src_pts)
voxel_coords = nib.affines.apply_affine(
    np.linalg.inv(mri_img.affine),
    trans_coords
)

current_ornt = io_orientation(mri_img.affine)
ras_ornt = axcodes2ornt(('R', 'A', 'S'))
transform = ornt_transform(current_ornt, ras_ornt)
img_ras = nib.as_closest_canonical(mri_img)
mri_ras_data = img_ras.get_fdata()

subset = voxel_coords[(voxel_coords[:, 0] > 110) & (voxel_coords[:, 0] < 120)].copy().round()
x = subset[:, 1]
y = subset[:, 2]
mri_slice = mri_ras_data[118, :, :]
fig, ax = plt.subplots(figsize=(6, 6))
ax.imshow(mri_slice)
ax.scatter(x, y)

```

With this code I’m able to plot both, the src positions and mri slice at the same place but they don’t align perfectly. I’d say there is like a 15-20% missalignement whereas when I use

```python
mne.viz.plot_bem(src=src, subjects_dir=subjects_dir, subject=subject, brain_surfaces='white', orientation='coronal') 

```

The aligment is absolutely perfect but I can’t seem to reproduce the same transformations to get to the same result.  
Thank you,

---

<div class="post-metadata">

**Author:** ![Ezekiel7811a](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/ezekiel7811a/32/4086_2.png) [@Ezekiel7811a](https://mne.discourse.group/u/Ezekiel7811a)\
**Post date:** [January 14, 2025, 9:37am UTC](https://mne.discourse.group/t/aligning-volume-source-space-with-mri/10703/2 "2025-01-14T09:37:20Z")

</div>

I finally soved my problem it was due to some transpose missing on the mri slice because of how imshow works (inversing cols and rows (y, x) instead of (x, y)). Also I’ve figured that using the src[‘rr’] directly is faster as it is in MRI coords already. Hope that will help someone one day

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [January 21, 2025, 9:37am UTC](https://mne.discourse.group/t/aligning-volume-source-space-with-mri/10703/3 "2025-01-21T09:37:55Z")

</div>

Closed on January 21, 2025, 9:37am UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
