Repository: mne-tools/mne-python

Issue:
Several questions about source localization workflow
Hello,
I am a student working with MNE and ran into several issues while working on source localization. I was wondering if anyone had any suggestions.

Firstly, I created my BEM surfaces using freesurfer's mri_watershed, and used these to try to make a source space. However, it appears as though the BEM surfaces are not scaled or located properly with respect to the brain, and the brain sticks out the back of the head. Here is plot_alignment with the BEM surfaces and the pial surface:
<img width="590" alt="Screen Shot 2021-01-01 at 2 53 48 AM" src="https://user-images.githubusercontent.com/27247614/103437440-b5357580-4bdc-11eb-8686-be17583423ca.png">
Here is the T1.mgz with the BEM inner skull in freeview:
<img width="1523" alt="Screen Shot 2021-01-01 at 3 10 15 AM" src="https://user-images.githubusercontent.com/27247614/103437662-e44ce680-4bde-11eb-85be-ed7fd873f8be.png">
This is not as far off as the pial surface, but the BEM is still quite clearly misaligned, particularly in the back of the head, and this leads to many sources being discarded since they are outside the inner skull.
I was wondering if anyone had any ideas on what is going wrong here? Is this something I would have to fix on the freesurfer side, or is there a quick fix i can implement in my MNE python code?

My second question is more simple: I want to get an average source estimate for each brain region in the atlas I use for my volume source space. I was wondering if there was a way to do this automatically, or if there was a way to output the number of sources that correspond to each label region, so that using these numbers and the order of labels in the atlas I could just average groups of rows in the final source estimate matrix.

Thank You!


Discussion:
there is clearly a pb

the white and pial surfaces are displayed correctly in freeview?

are you loading the correct surfaces?

can you try uwing the command line like:

mne freeview_bem_surfaces -s sample

where you replace sample by your subject name?

>

The white and pial are also misaligned in freeview, and mne freeview_bem_surfaces gives the below. They seem to have gotten even more misaligned, but this is most likely a mistake I made in the image in the original post since the files have not changed:
<img width="1626" alt="Screen Shot 2021-01-04 at 10 19 18 AM" src="https://user-images.githubusercontent.com/27247614/103566233-83d7d680-4e76-11eb-9a33-d031abcd5a09.png">
The surfaces are the ones that I generated using mri_watershed. They initially saved into the mri folder and were named incorrectly, so I changed them to the standard names (outer_skin.surf, etc) and moved them to the bem folder. 1 thing I noticed was that after the initial output from mri_watershed the names were in the format lh.name.surf_outer_skin_surface, but the surfaces had both hemispheres. The line I used was mri_watershed -surf braintest.surf T1.mgz braintest.mgz (-atlas also gave misaligned output)

Below is white, pial, T1, and the bem surfaces together:
<img width="1719" alt="Screen Shot 2021-01-04 at 11 13 01 AM" src="https://user-images.githubusercontent.com/27247614/103570399-e41e4680-4e7d-11eb-8db1-0fbf2e148432.png">

can you share the script you used to produce these? did you use

https://mne.tools/dev/generated/commands.html#mne-watershed-bem

>

When I tried mne watershed_bem it gave an error that it could not read the T1 volume, so I used the freesurfer function directly:
`mri_watershed -surf test.surf T1.mgz test.mgz`
adding -atlas still results in a shifted bem model.

The error that came up for mne watershed_bem is below. Should I change the parameters?
```
$mne watershed_bem -d . -s EM --overwrite

Running mri_watershed for BEM segmentation with the following parameters:

Results dir = ./EM/bem/watershed
Command = mri_watershed -useSRAS -surf ./EM/bem/watershed/EM ./EM/mri/T1.mgz ./EM/bem/watershed/ws

Running subprocess: mri_watershed -useSRAS -surf ./EM/bem/watershed/EM ./EM/mri/T1.mgz ./EM/bem/watershed/ws
error: mghRead(./EM/mri/T1.mgz, -1): could not open file



Mode:          use surfaceRAS to save surface vertex positions

Mode:          Saving out BEM surfaces



*********************************************************

The input file is ./EM/mri/T1.mgz

The output file is ./EM/bem/watershed/ws



mri_watershed Error: read failed



('\nMode:          use surfaceRAS to save surface vertex positions\nMode:          Saving out BEM surfaces\n\n*********************************************************\nThe input file is ./EM/mri/T1.mgz\nThe output file is ./EM/bem/watershed/ws\n\nmri_watershed Error: read failed\n\n', 'error: mghRead(./EM/mri/T1.mgz, -1): could not open file\x00\n')
```
can you provide an absolute path for subjects dir not just . ?

I apologize as I was away for several weeks. Using the absolute path worked, thank you so much!

Does anyone have any ideas for the second issue? I will paste it again here:
I want to get an average source estimate for each brain region in the atlas I use for my volume source space. I was wondering if there was a way to do this automatically, or if there was a way to output the number of sources that correspond to each label region, so that using these numbers and the order of labels in the atlas I could just average groups of rows in the final source estimate matrix.
thank you!

Resolving patch:
diff --git a/mne/bem.py b/mne/bem.py
--- a/mne/bem.py
+++ b/mne/bem.py
@@ -1632,6 +1632,8 @@ def _prepare_env(subject, subjects_dir):
     _validate_type(subject, "str")
 
     subjects_dir = get_subjects_dir(subjects_dir, raise_error=True)
+    subjects_dir = op.abspath(subjects_dir)  # force use of an absolute path
+    subjects_dir = op.expanduser(subjects_dir)
     if not op.isdir(subjects_dir):
         raise RuntimeError('Could not find the MRI data directory "%s"'
                            % subjects_dir)


Test patch: