Issues with make_scalp_surfaces

Greetings all,

I am in the process of trying to perform coregistration, and I seem to be running into an error. I know that coreg looks for three files (head.fif, lh.seghead, and lh.smseghead). I have done the MRI reconstruction (FreeSurfer) and do not have these files. However, I understand that they can be generated using mne.bem.make_scalp_surfaces.

When I try to use this function (mne.bem.make_scalp_surfaces(ā€˜sampleā€™,subjects_dir=subjects_dir)), I run into an error (subjdir: undefined variable). I am wondering if there is some function/output I am supposed to run/generate before running make_scalp_surfaces, or if I just have some kind of syntax error leading to the undefined variable.

MNE Python version 0.24.0
OS: macOS Monterey v12.0.1

Hello @ktyner and welcome to the forum!

Are you sure the variable you pass ā€” subjects_dir ā€” is defined? Can you share more context of your script?

Best wishes,
Richard

Hi Richard,

I believe that variable subjects_dir is defined. In my terminal, I run:
export SUBJECTS_DIR=/path/to/subject (the folder I map to contains the ā€œsampleā€ folder, which contains bem, mri, surf, etc.

Earlier in my code, I generate bem (using mne.bem.make_watershed_bem) which takes subjects_dir as an argument, and the code runs just fine (bem is generated).

Can you please share more of your code and the full error message?

In command terminal, I run:

export FREESURFER_HOME=/Applications/freesurfer/7.2.0
export SUBJECTS_DIR=/path/to/subject/dir
source $FREESURFER_HOME/SetUpFreeSurfer.sh

conda activate mne
spyder

In spyder, I run:

import os.path as op
import mne

subjects_dir = ā€˜/path/to/subject/dirā€™
subject = op.join(subjects_dir,ā€˜sampleā€™)

mne.bem.make_watershed_bem(ā€˜sampleā€™,subjects_dir=subjects_dir,overwrite=True,volume=ā€˜T1ā€™,show=True,brainmask=ā€˜ws.mgzā€™)

mne.bem.make_scalp_surfaces(ā€˜sampleā€™,subjects_dir=subjects_dir)

When I run the make_scalp_surfaces, I see:

  1. Creating a dense scalp tessellation with mkheadsurfā€¦
    Running subprocess: mkheadsurf -subjid sample -srcvol T1.mgz
    subjdir: Undefined variable.
    CalledProcessError: Command ā€˜[ā€˜mkheadsurfā€™, ā€˜-subjidā€™, ā€˜sampleā€™, ā€˜-srcvolā€™, ā€˜T1.mgzā€™]ā€™ returned non-zero exit status 1.

I have tried altering ā€˜sampleā€™ as an input argument for make_scalp_surfaces, and I receive various errors about pathing.

Thank you! Can you try to run the following code:

# %%
from pathlib import Path
import mne


sample_dir = mne.datasets.sample.data_path()
subjects_dir = Path(sample_dir) / 'subjects'

mne.bem.make_scalp_surfaces(
    subject='sample',
    subjects_dir=subjects_dir,
    force=True,
    overwrite=True
)

That worked perfect! Gave no errors!

I think the issue is that when you do export SUBJECTS_DIR=whatever and then later run spyder, the ipython terminal within spyder is a new terminal and therefore doesnā€™t inherit your exported environment variables. You can probably fix that by putting the export lines in your .bashrc file so that they get declared when spyder spawns its embedded terminals too.

That may be true, but @ktyner also explicitly passes subjects_dir, which overrides the SUBJECTS_DIR environment variable, so I suspect the issue must be somewhere elseā€¦

This suggests that subjects_dir passed in via python is not being passed to the subprocess.

But if that were the case, the MWE ā€“ which runs successfully ā€“ wouldnā€™t work either, no?

This is what I thought too Richard. If I pass subjects_dir, there should be an issue with each function I pass it to, not just the make_scalp_surfaces function.

@ktyner Can you try to run the code outside of Spyder, directly from the console? Save it to a file and run python your_script.py.

Hi Richard,

I can successfully run the mne watershed_bem in the command terminalā€¦

However, I run into the same error in the command terminal as I did in spyder when I run make_scalp_surfacesā€¦

You need to pass $SUBJECT and $SUBJECTS_DIR (with dollar signs) instead of SUBJECT and SUBJECTS_DIR (without dollar signs) on the command line. Or, better yet, pass dir subject name and subjects directory directly. Unless a directory named SUBJECT_DIR actually exists??

We passed $SUBJECT and $SUBJECTS_DIR and received the same error. We also tried passing with the absolute paths for -s and -d and ran into the same error. I do not have a directory named SUBJECT_DIR.

Your first screenshot, however, indicates that you do :smiley: Can you check? It appears you have a directory named /Users/kevin/SUBJECTS_DIR.

Here is what I have under my directory:

Thanks @ktyner!

Can you try to run the following code on the command line please?

mne make_scalp_surfaces -s sample -d ~/mne_data/MNE-sample-data/subjects --overwrite --force

In my terminal, I first activate mne, then I export and source freesurfer. When I run that line of code, I donā€™t seem to run into any issues. The terminal output is attached.