Error: Command not found: mkheadsurf

  • freesurfer version: 1.7.4
  • operating system: e.g. macOS Ventura 13.0

Hello everyone,

I am stuck on this error message: Command not found: mkheadsurf. If someone can provide some advice on how to fix it, it would be greatly appreciated.

import os
import mne

subjects_dir=“/Applications/freesurfer/7.4.1/subjects/pilot_data”

subject=“sub-01”

os.environ[“FREESURFER_HOME”] = “/Applications/freesurfer/7.4.1”

mne.bem.make_scalp_surfaces(
subject=“sub-01”,
subjects_dir=“pilot_data”,
force=True,
overwrite=False,
no_decimate=False,
threshold=20,
mri=‘T1.mgz’,
verbose=None
)

  1. Creating a dense scalp tessellation with mkheadsurf

    Running subprocess: mkheadsurf -subjid sub-01 -srcvol T1.mgz -thresh1 20 -thresh2 20
    Command not found: mkheadsurf
    Traceback (most recent call last):

Cell In[5], line 5
mne.bem.make_scalp_surfaces(

File :12 in make_scalp_surfaces

File /Applications/MNE-Python/.mne-python/lib/python3.10/site-packages/mne/bem.py:2443 in make_scalp_surfaces
run_subprocess(

File :12 in run_subprocess

File /Applications/MNE-Python/.mne-python/lib/python3.10/site-packages/mne/utils/misc.py:131 in run_subprocess
with running_subprocess(command, *args, **kwargs) as p:

File /Applications/MNE-Python/.mne-python/lib/python3.10/contextlib.py:135 in enter
return next(self.gen)

File /Applications/MNE-Python/.mne-python/lib/python3.10/site-packages/mne/utils/misc.py:263 in running_subprocess
p = subprocess.Popen(command, *args, **kwargs)

File /Applications/MNE-Python/.mne-python/lib/python3.10/subprocess.py:971 in init
self._execute_child(args, executable, preexec_fn, close_fds,

File /Applications/MNE-Python/.mne-python/lib/python3.10/subprocess.py:1847 in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)

FileNotFoundError: [Errno 2] No such file or directory: ‘mkheadsurf’

I have verified that the file/script ‘mkheadsurf’ exists in the freesurfer/bin folder.

Advice appreciated. Thank in advance.

did you install freesurfer on your machine? if not you should

Alex

Like @agramfort said, simply setting FREESURFER_HOME doesn’t do the trick. We assume that the FreeSurfer commands are actually in your path, and we try to call them like that. For example, when invoking mkheadsurf, we literally call mkheadsurf and don’t construct $FREESURFER_HOME/bin/mkheadsurf first.

@larsoner Is this something we should reconsider?

Richard

Our assumption is that the user has their environment properly set up to run FreeSurfer commands, including stuff like adding the freesurfer bin/ to their PATH according to the FreeSurfer install instructions. I think we should continue to make that assumption rather than trying to hack stuff in, it risks things like them changing their installation instructions and us having to keep track etc.

1 Like

I agree. Maybe the error message could be improved though?

To give slightly more detail: part of the freesurfer installation process involves setting up your .bashrc file so that when the terminal is opened, $FREESURFER_HOME gets added to the system PATH, allowing commands like mkheadsurf to “just work” from anywhere (rather than having to specify the full path to the executable). If that isn’t done, MNE commands that call freesurfer commands will fail in exactly the way you’re seeing.

Check your .bashrc or .zshrc file for something like this (the paths will differ):

export SUBJECTS_DIR=/data/MEG/structurals
export FREESURFER_HOME=/opt/freesurfer
source $FREESURFER_HOME/SetUpFreeSurfer.sh >/dev/null
1 Like

Yes, I have installed freesurfer version 1.7.4. Thanks for your reply.

Hello,

Thank you for your reply.

So here is what I have in my terminal after entering:
export FREESURFER_HOME=/Applications/freesurfer/7.4.1
export SUBJECTS_DIR=$FREESURFER_HOME/subjects/pilot_data
source $FREESURFER_HOME/SetUpFreeSurfer.sh

-------- freesurfer-macOS-darwin_x86_64-7.4.1-20230614-7eb8460 --------
Setting up environment for FreeSurfer/FS-FAST (and FSL)
FREESURFER_HOME /Applications/freesurfer/7.4.1
FSFAST_HOME /Applications/freesurfer/7.4.1/fsfast
FSF_OUTPUT_FORMAT nii.gz
SUBJECTS_DIR /Applications/freesurfer/7.4.1/subjects/pilot_data
MNI_DIR /Applications/freesurfer/7.4.1/mni

What do you think? I have just run the mne.bem.make_scalp_surfaces() command and it works for some reason. So I am quite baffled why some commands run but not others.

those export and source commands need to be run each time a new terminal opens (which is why Freesurfer suggests during installation to add them to your .bashrc file). It’s not clear from your answer whether you indeed see those commands in the RC file or not — but I’m guessing not (otherwise it wouldn’t have failed previously).

Running the commands once in a terminal and then running mne.bem.make_scalp_surfaces() in that same terminal window is expected to work — you’re doing manually what the correct lines in .bashrc would have done automatically.

If you’re still confused I suggest reading up on what the PATH variable does / is used for.

Hello,
So I tried setting the paths:

#Setting PATH for Python 3.11
#The original version is saved in .zprofile.pysave
PATH=“/Library/Frameworks/Python.framework/Versions/3.11/bin:${PATH}”
export PATH

freesurfer setup
export FREESURFER_HOME=/Applications/freesurfer/7.4.1
export SUBJECTS_DIR=/Applications/freesurfer/7.4.1/subjects/pilot_data
source $FREESURFER_HOME/SetUpFreeSurfer.sh

Then I tried running it in terminal but got this the first time:

/Users/binarybits/.zprofile:source:10: no such file or directory: /Applications/freesurfer/7.4.1^M/SetUpFreeSurfer.sh^M

Tried it again and got this:
/Users/binarybits/.zprofile:source:10: no such file or directory: /Applications/freesurfer/7.4.1^M/SetUpFreeSurfer.sh

The weird character ^M keeps showing up in different places each time. Not sure why. However it is not working still. Can you see what I am doing wrong?

It’s the carriage return control character. Windows uses carriage return and line feed to start a new line; macOS doesn’t use this. It almost appears as if those files were copied over from a Windows machine 


Richard

FYI, this is what I have in my ~/.zshrc:

export FREESURFER_HOME=/Applications/freesurfer/6.0.0
export SUBJECTS_DIR=$FREESURFER_HOME/subjects
source $FREESURFER_HOME/SetUpFreeSurfer.sh

That’s sufficient to get FreeSurfer running. You don’t need to fiddle with PATH manually. The SetUpFreeSurfer.sh script does all of that for you.

Richard

1 Like

Hello,

Thanks for your reply!

I am using MacOs. The carriage return control character keeps appearing for some reason. Do you think it might be something wrong with my Mac? It is a macbook air if that makes a difference?

Also, when I open my terminal window, it is in -zsh. Don’t know if this makes a difference.

have you tried editing your .zprofile to remove the carriage return characters? Note that any changes you make to a .zprofile or .zshrc file will require you to close and reopen the terminal before the changes will take effect.

Hello,
Thanks for your reply.
When you say to “remove the carriage return characters”, do you mean literally using the “delete button”?
I don’t know how to remove them. Hitting backspace does not work.

Your terminal may be opening the file with vim. once you open the .zprofile file, try pressing i on your keyboard. If you are using a vim editor then this will put you in insert mode. Now try to see if you can delete the character. If it works, when you are done editing the file, press the esc key to exit insert mode, then type :wq and hit enter to save and quit the file.

1 Like

@binary_bits This one is really difficult to debug. I’d suggest you come visit one of our online office hours where we could do a live screen sharing support session.

Richard