Question about calling fsl in nipype

Hi, I try to run the code:

from nipype.interfaces.fsl import Split
sm = Split()
sm.inputs.dimension = 'z'
sm.inputs.out_base_name = 'NIFTI_GZ'
sm.inputs.in_file = '/media/alex/Expansion/Data/OASIS3/PET_processing/PET_BIDS/sub-OAS30001/ses-M078/pet/sub-OAS30001_ses-M078_trc-18FAV45_pet.nii.gz'
res = sm.run() 

However, it return error :

OSError: No command "fslsplit" found on host alex-MS-7D75. Please check that the corresponding package is installed.

I check the fsl was installed correctly in my local PC ny which fslmaths command.

(torch1_10) alex@alex-MS-7D75:~$ which fslmaths
/usr/local/fsl/bin/fslmaths

And I can get the GUI of fsl by inputting fsl in terminal. So why the nipype can not find it? Any suggestions? thanks

Hi @alienx-zero,

Please add the FSL bin to your path in your python script before executing nipype workflow, for example:

os.environ["PATH"] += os.pathsep + "/usr/local/fsl/bin"

Best,
Steven

Thanks, steven, it works.

Hi, Steven, I have a new question. Here is my code:

import os
os.environ["PATH"] += os.pathsep + "/usr/local/fsl/bin"
os.environ['FSLOUTPUTTYPE'] = 'NIFTI'

import nipype.pipeline.engine as pe
from nipype.interfaces.fsl import Split, FLIRT, Merge, ImageMaths

split = pe.Node(Split(dimension='t'), name='split')
split.inputs.in_file = '/media/alex/Expansion/Data/OASIS3/PET_processing/PET_BIDS/sub-OAS30001/ses-M078/pet/sub-OAS30001_ses-M078_trc-18FAV45_pet.nii.gz'

flirt = pe.MapNode(FLIRT(reference='/media/alex/Expansion/Data/OASIS3/PET_processing/PET_BIDS/sub-OAS30001/ses-M078/anat/sub-OAS30001_ses-M078_run-01_T1w.nii.gz'), 
                   iterfield=['in_file'], 
                   name='flirt')

merge = pe.Node(Merge(dimension='t', output_type='NIFTI_GZ'), name='merge')
mean = pe.Node(ImageMaths(op_string='-Tmean', out_file=r'/media/alex/Expansion/Data/OASIS3/PET_processing/PET_BIDS/sub-OAS30001/ses-M078/pet/averaged_3d_image.nii.gz'), name='mean')

workflow = pe.Workflow(name='fsl_preprocessing', base_dir='/media/alex/work/temp/data_preprocess')

workflow.connect([
    (split, flirt, [('out_files', 'in_file')]),
    (flirt, merge, [('out_file', 'in_files')]),
    (merge, mean, [('merged_file', 'in_file')]),
])

# Run the workflow
workflow.run()

Finally, it generated ‘averaged_3d_image.nii’ at the path I defined. However, it also raise error in python console.



Cmdline:
	fslmaths /media/alex/work/temp/data_preprocess/fsl_preprocessing/merge/vol0000_flirt_merged.nii.gz -Tmean /media/alex/Expansion/Data/OASIS3/PET_processing/PET_BIDS/sub-OAS30001/ses-M078/pet/averaged_3d_image.nii.gz
Stdout:

Stderr:

Traceback:
	Traceback (most recent call last):
	  File "/home/alex/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/interfaces/base/core.py", line 453, in aggregate_outputs
	    setattr(outputs, key, val)
	  File "/home/alex/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/interfaces/base/traits_extension.py", line 330, in validate
	    value = super(File, self).validate(objekt, name, value, return_pathlike=True)
	  File "/home/alex/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/interfaces/base/traits_extension.py", line 135, in validate
	    self.error(objekt, name, str(value))
	  File "/home/alex/anaconda3/envs/torch1_10/lib/python3.9/site-packages/traits/base_trait_handler.py", line 74, in error
	    raise TraitError(
	traits.trait_errors.TraitError: The 'out_file' trait of an ImageMathsOutputSpec instance must be a pathlike object or string representing an existing file, but a value of '/media/alex/Expansion/Data/OASIS3/PET_processing/PET_BIDS/sub-OAS30001/ses-M078/pet/averaged_3d_image.nii.gz' <class 'str'> was specified.

	During handling of the above exception, another exception occurred:

	Traceback (most recent call last):
	  File "/home/alex/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/interfaces/base/core.py", line 400, in run
	    outputs = self.aggregate_outputs(runtime)
	  File "/home/alex/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/interfaces/base/core.py", line 460, in aggregate_outputs
	    raise FileNotFoundError(msg)
	FileNotFoundError: No such file or directory '/media/alex/Expansion/Data/OASIS3/PET_processing/PET_BIDS/sub-OAS30001/ses-M078/pet/averaged_3d_image.nii.gz' for output 'out_file' of a ImageMaths interface


240827-21:50:26,732 nipype.workflow INFO:
	 ***********************************
240827-21:50:26,732 nipype.workflow ERROR:
	 could not run node: fsl_preprocessing.mean
240827-21:50:26,732 nipype.workflow INFO:
	 crashfile: /media/alex/Expansion/Data/OASIS3/PET_processing/crash-20240827-215026-alex-mean-4091aeaa-8048-47ab-96fe-c1e800be5df7.pklz
240827-21:50:26,733 nipype.workflow INFO:
	 ***********************************
Traceback (most recent call last):

  File ~/anaconda3/envs/torch1_10/lib/python3.9/site-packages/spyder_kernels/py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File /media/alex/Expansion/Data/OASIS3/PET_processing/Pet_4dTO3d.py:34
    workflow.run()

  File ~/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/pipeline/engine/workflows.py:638 in run
    runner.run(execgraph, updatehash=updatehash, config=self.config)

  File ~/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/pipeline/plugins/linear.py:82 in run
    raise error from cause

  File ~/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/pipeline/plugins/linear.py:47 in run
    node.run(updatehash=updatehash)

  File ~/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/pipeline/engine/nodes.py:527 in run
    result = self._run_interface(execute=True)

  File ~/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/pipeline/engine/nodes.py:645 in _run_interface
    return self._run_command(execute)

  File ~/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/pipeline/engine/nodes.py:771 in _run_command
    raise NodeExecutionError(msg)

NodeExecutionError: Exception raised while executing Node mean.

Cmdline:
	fslmaths /media/alex/work/temp/data_preprocess/fsl_preprocessing/merge/vol0000_flirt_merged.nii.gz -Tmean /media/alex/Expansion/Data/OASIS3/PET_processing/PET_BIDS/sub-OAS30001/ses-M078/pet/averaged_3d_image.nii.gz
Stdout:

Stderr:

Traceback:
	Traceback (most recent call last):
	  File "/home/alex/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/interfaces/base/core.py", line 453, in aggregate_outputs
	    setattr(outputs, key, val)
	  File "/home/alex/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/interfaces/base/traits_extension.py", line 330, in validate
	    value = super(File, self).validate(objekt, name, value, return_pathlike=True)
	  File "/home/alex/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/interfaces/base/traits_extension.py", line 135, in validate
	    self.error(objekt, name, str(value))
	  File "/home/alex/anaconda3/envs/torch1_10/lib/python3.9/site-packages/traits/base_trait_handler.py", line 74, in error
	    raise TraitError(
	traits.trait_errors.TraitError: The 'out_file' trait of an ImageMathsOutputSpec instance must be a pathlike object or string representing an existing file, but a value of '/media/alex/Expansion/Data/OASIS3/PET_processing/PET_BIDS/sub-OAS30001/ses-M078/pet/averaged_3d_image.nii.gz' <class 'str'> was specified.

	During handling of the above exception, another exception occurred:

	Traceback (most recent call last):
	  File "/home/alex/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/interfaces/base/core.py", line 400, in run
	    outputs = self.aggregate_outputs(runtime)
	  File "/home/alex/anaconda3/envs/torch1_10/lib/python3.9/site-packages/nipype/interfaces/base/core.py", line 460, in aggregate_outputs
	    raise FileNotFoundError(msg)
	FileNotFoundError: No such file or directory '/media/alex/Expansion/Data/OASIS3/PET_processing/PET_BIDS/sub-OAS30001/ses-M078/pet/averaged_3d_image.nii.gz' for output 'out_file' of a ImageMaths interface

I did not understand why it output the nii successfully in the path I defined, but also pop out error information. Is the output nifit file correct? Or it is incomplete? I try to visualize this output nifit, it looks good using mricroGL. If the code output file correctly, why it raise error?