# Issues with make\_scalp\_surfaces

**URL:** <https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016>\
**Category:** Support & Discussions\
**Tags:** coregistration\
**Created:** [November 23, 2021, 7:59pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016 "2021-11-23T19:59:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ktyner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/ktyner/32/989_2.png) [@ktyner](https://mne.discourse.group/u/ktyner)\
**Post date:** [November 23, 2021, 7:59pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/1 "2021-11-23T19:59:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [November 23, 2021, 8:22pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/2 "2021-11-23T20:22:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ktyner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/ktyner/32/989_2.png) [@ktyner](https://mne.discourse.group/u/ktyner)\
**Post date:** [November 23, 2021, 8:29pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/3 "2021-11-23T20:29:05Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [November 23, 2021, 8:43pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/4 "2021-11-23T20:43:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ktyner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/ktyner/32/989_2.png) [@ktyner](https://mne.discourse.group/u/ktyner)\
**Post date:** [November 23, 2021, 9:06pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/5 "2021-11-23T21:06:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [November 23, 2021, 9:23pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/6 "2021-11-23T21:23:19Z")

</div>

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

```python
# %%
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
)

```

---

<div class="post-metadata">

**Author:** ![ktyner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/ktyner/32/989_2.png) [@ktyner](https://mne.discourse.group/u/ktyner)\
**Post date:** [November 23, 2021, 9:30pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/7 "2021-11-23T21:30:25Z")

</div>

That worked perfect! Gave no errors!

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [November 23, 2021, 9:38pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/8 "2021-11-23T21:38:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [November 23, 2021, 10:46pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/9 "2021-11-23T22:46:05Z")

</div>

> [@drammock](#):
>
> 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

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…

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [November 23, 2021, 10:47pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/10 "2021-11-23T22:47:11Z")

</div>

> [@ktyner](#):
>
> CalledProcessError: Command ‘[‘mkheadsurf’, ‘-subjid’, ‘sample’, ‘-srcvol’, ‘T1.mgz’]’ returned non-zero exit status 1.

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

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [November 23, 2021, 10:51pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/11 "2021-11-23T22:51:45Z")

</div>

> [@drammock](#):
>
> This suggests that `subjects_dir` passed in via python is _not_ being passed to the subprocess.

But if that were the case, the [MWE](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/6) – which runs successfully – wouldn’t work either, no?

---

<div class="post-metadata">

**Author:** ![ktyner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/ktyner/32/989_2.png) [@ktyner](https://mne.discourse.group/u/ktyner)\
**Post date:** [November 23, 2021, 10:58pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/12 "2021-11-23T22:58:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [November 23, 2021, 11:08pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/13 "2021-11-23T23:08:09Z")

</div>

@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`.

---

<div class="post-metadata">

**Author:** ![ktyner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/ktyner/32/989_2.png) [@ktyner](https://mne.discourse.group/u/ktyner)\
**Post date:** [November 24, 2021, 5:11pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/14 "2021-11-24T17:11:34Z")

</div>

Hi Richard,

I can successfully run the mne watershed\_bem in the command terminal…

 ![watershed_bem](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/762c1055195393a0f6239c4ebda3d87adc687f4b.png)

However, I run into the same error in the command terminal as I did in spyder when I run make\_scalp\_surfaces…

 ![make_scalp_surfaces](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/bb50ecdd5c64f8acd922fdf5c2b144cf3f5e772b.png)

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [November 24, 2021, 5:13pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/15 "2021-11-24T17:13:38Z")

</div>

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??

---

<div class="post-metadata">

**Author:** ![ktyner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/ktyner/32/989_2.png) [@ktyner](https://mne.discourse.group/u/ktyner)\
**Post date:** [November 24, 2021, 5:23pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/16 "2021-11-24T17:23:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [November 24, 2021, 5:27pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/17 "2021-11-24T17:27:22Z")

</div>

> [@ktyner](#):
>
> I do not have a directory named SUBJECT\_DIR.

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

---

<div class="post-metadata">

**Author:** ![ktyner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/ktyner/32/989_2.png) [@ktyner](https://mne.discourse.group/u/ktyner)\
**Post date:** [November 24, 2021, 5:29pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/18 "2021-11-24T17:29:14Z")

</div>

Here is what I have under my directory:

 ![Screen Shot 2021-11-24 at 11.28.27 AM](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/917ad681532d537d6a0b3ab363d9d96673e69c53.jpeg)

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [November 24, 2021, 7:26pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/19 "2021-11-24T19:26:10Z")

</div>

Thanks @ktyner!

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

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

```

---

<div class="post-metadata">

**Author:** ![ktyner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/ktyner/32/989_2.png) [@ktyner](https://mne.discourse.group/u/ktyner)\
**Post date:** [November 24, 2021, 10:58pm UTC](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016/20 "2021-11-24T22:58:53Z")

</div>

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.

 ![Screen Shot 2021-11-24 at 4.56.37 PM](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/d/dec39101a8f4c0f28a7c0fbf0f67807ad055cd94.jpeg)

[Next page](https://mne.discourse.group/t/issues-with-make-scalp-surfaces/4016.md?page=2)
