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,

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

conda activate mne

In spyder, I run:

import os.path as op
import mne

subjects_dir = ‘/path/to/subject/dir’
subject = op.join(subjects_dir,‘sample’)



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'


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

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.