McFlirt Error when trying to output the mean image


I am trying to run McFlirt and am getting the following error…

No such file or directory ‘/scratch/06953/jes6785/CPT/output/preproc_ses2_scan1/mcflirt/20200207.005.2_func_task-CPTPNND_run-01_dir-AP_20200207181451_15_brain_mcf_mean_reg.nii.gz’ for output ‘mean_img’ of a MCFLIRT interface

I noticed that the discussion above, but I noticed that the code (including the version that I am using has been fixed). I am using python 6 and am calling for a mean image output, but it looks like the problem was “fixed” in the code. The source code that I am running for Mcflirt appears to contain the fix. I am running FSL 6.03. I can’t currently install FSL 5.0 and I would rather not go back to an old version. I also get the same error message when I gunzip the functional file and specify the output type for each node to NIFTI (and also if I don’t do this).

I am at the testing phase of running my script and I would like to be able to run this file on our test scan. Could someone help on what is going wrong and what I can do to fix this? Surely someone else must have come across this problem too? I am pretty new to nipype, but have been researching this for a few days now and it is very frustrating.

Does anyone have any idea why I might STILL be getting this error?

Also, what is the trick to output the command line from a node, having trouble finding this online?

Here is actual code…

#Initiate Gunzip node
gunzip_func = Node(Gunzip(in_file = func_file),

bet = Node(BET(args="-F",
output_type =‘NIFTI’,

This is currently working but it is throwing an error

mcflirt = Node(MCFLIRT(mean_vol=True,
output_type = ‘NIFTI’),

preproc_ses2_scan1_test.connect([(gunzip_func, bet, [(‘out_file’, ‘in_file’)])])
preproc_ses2_scan1_test.connect([(bet, mcflirt, [(‘out_file’, ‘in_file’)])])

except(RuntimeError) as err:
print(“RuntimeError”, err)


To add to this, this error clearly arises because McFlirt doesn’t like the weird naming output for the mean image. It seems to output filename.nii.gz.nii.gz and throws an error. Weirdly the scan output looks fine, the name is just off. I have tried telling the program to output a file ending in .nii or nii_gz and this doesn’t seem to stop the error.

I can also run the interface command output by nipype in the terminal window.
Please please please help


Also, the error doesn’t appear when I don’t ask for the mean image output.

I have the same problem

I installed FSL 6.0.4 under Ubuntu 20.4, and tried nipype 1.6.0 with this excellent tutorial. I encounter the identical error when nipype try to run a workflow including mcflirt with -meanvol.

traits.trait_errors.TraitError: The 'mean_img' trait of a MCFLIRTOutputSpec instance must be a pathlike object or string representing an existing file, but a value of '/data/trynipype/output/working_dir/ex1/motioncor/sub-01_ses-test_task-fingerfootlips_bold_roi_mcf_mean_reg.nii.gz' <class 'str'> was specified.

In the working directory, I found the file do exist but with different name: sub-01_ses-test_task-fingerfootlips_bold_roi_mcf.nii.gz_mean_reg.nii.gz. It has a redundant “nii.gz” before the suffix “mean_reg”.

A similar error has been reported in previous thread. It suggests that FSL 6 generated file with name {basename}{_mean_reg}.nii.gz rather than redundant “.nii.gz” in {basename}.nii.gz{_mean_reg}.nii.gz. And it should be fixed.

However, in my case, I do have FSL 6.0.4. But my mcflirt generate filename like FSL 5. It has redudant “nii.gz”.

I also check my FSL with the following code:

from nipype.interfaces import fsl

I ran the command line

!mcflirt -in /data/trynipype/output/working_dir/ex1/skipdummy/sub-01_ses-test_task-fingerfootlips_bold_roi.nii.gz -meanvol -out /data/trynipype/output/working_dir/ex1/motioncor/sub-01_ses-test_task-fingerfootlips_bold_roi_mcf.nii.gz

It still generate the filename with redundant “.nii.gz” like {basename}.nii.gz{_mean_reg}*.nii.gz.

However, if I ran the command line specifying output with only basename and not extension “.nii.gz” like the following line:

!mcflirt -in /data/trynipype/output/working_dir/ex1/skipdummy/sub-01_ses-test_task-fingerfootlips_bold_roi.nii.gz -meanvol -out /data/trynipype/output/working_dir/ex1/motioncor/sub-01_ses-test_task-fingerfootlips_bold_roi_mcf

It generate mean_reg with correct filename have no redundant “nii.gz”.

I guess it is more like a bug in FSL 6. So I would like to ask if anyone know how to fix the redundant “.nii.gz” under FSL 6.0.4.

I also wonder if any others do have FSL 6 and generate the filename with the redundant “nii.gz”. If it is the case, nipype might need to change the code that can catch the correct output filename for either “{basename}{_mean_reg}.nii.gz" or "{basename}.nii.gz{_mean_reg}.nii.gz” regardless of FSL version.

So glad to hear that someone out there had the same problem. I eventually just gave up trying to run this with code as no one ever was able to help me with this. It was incredibly frustrating.

I was going through the same tutorial as you. Sorry that I couldn’t be of more help than this, but please let me know if you ever get any assistance.

After struggling the error for a day, I have a workaround that modify the original nipype code. The original code specified the filename depending on what the FSL version is. I would like to modify the code that it specifies the filename by looking for what the output filename is. Here, I assume that the code would run after the nypipe executes the mcflirt command and then my customized code can look for what the actual output is. Fortunately, it works in my case.

I navigated to where mcflirt interface deal with filename. It should be under the file nipype/interfaces/fsl/ at line 929. And I commented out lines 936 to 944 and add the following lines:

        # The mean image created if -stats option is specified ('meanvol')
        # is missing the top and bottom slices. Therefore we only expose the
        # mean image created by -meanvol option ('mean_reg') which isn't
        # corrupted.
        # Note that the same problem holds for the std and variance image.

        if isdefined(self.inputs.mean_vol) and self.inputs.mean_vol:
            #if LooseVersion(Info.version()) < LooseVersion("6.0.0"):
            #    # FSL <6.0 outputs have .nii.gz_mean_img.nii.gz as extension
            #    outputs["mean_img"] = self._gen_fname(
            #        outputs["out_file"] + "_mean_reg.ext", cwd=output_dir
            #    )
            #    outputs["mean_img"] = self._gen_fname(
            #        outputs["out_file"], suffix="_mean_reg", cwd=output_dir
            #    )

            if op.exists(self._gen_fname(
                    outputs["out_file"] + "_mean_reg.ext", cwd=output_dir
                outputs["mean_img"] = self._gen_fname(
                    outputs["out_file"] + "_mean_reg.ext", cwd=output_dir
                outputs["mean_img"] = self._gen_fname(
                    outputs["out_file"], suffix="_mean_reg", cwd=output_dir

If anyone encounter the same problem. Hope this workaround might help you.

1 Like

Thank you for this! That’s great!