How to skip the error in calling nipype

Here is my error:

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

Here is my code:

sub  = 'sub-OAS30008'
sub_path = os.path.join(r'/media/alex/Expansion/Data/OASIS3/PET_processing/PET_BIDS', sub)
# ses = 'ses-M108'
# track = '18FAV45'
tracs = ['11CPIB', '18FFDG', '18FFTP', '18FAV45']
sess = os.listdir(sub_path)

for ses in sess:
    for track in tracs:
        in_path = os.path.join(sub_path, ses)
        path_in = os.path.join(in_path, 'pet', '{}_{}_trc-{}_pet.nii.gz'.format(sub, ses, track))
        out_file=os.path.join(r'/media/alex/Expansion/Data/OASIS3/PET_processing/PET_register', sub, ses, 
                                                                                'pet', '{}_{}_trc-{}_pet.nii.gz'.format(sub, ses, track))
        os.makedirs(os.path.join(r'/media/alex/Expansion/Data/OASIS3/PET_processing/PET_register', sub, ses, 'pet'), exist_ok=True)
        
        if os.path.exists(path_in) and os.path.exists(out_file) == False:
                split = pe.Node(Split(dimension='t', ignore_exception=True), name='split')
                split.inputs.in_file = path_in
                imaging_path = Path(os.path.join('/media/alex/Expansion/Data/OASIS3/PET_processing/preprocessed/subjects', sub, ses, 
                                                 't1/spm/segmentation/normalized_space'))
                t1_files = imaging_path.glob("*T1w.nii.gz")
                t1_files = sorted([str(files) for files in t1_files])
                t1_file = t1_files[0]
            
                flirt = pe.MapNode(FLIRT(reference=os.path.join(in_path, t1_file), ignore_exception=True), 
                                   iterfield=['in_file'], 
                                   name='flirt')
                
                merge = pe.Node(Merge(dimension='t', output_type='NIFTI_GZ', ignore_exception=True), name='merge')
                mean = pe.Node(ImageMaths(op_string='-Tmean', out_file=out_file, ignore_exception=True), name='mean')
                
                workflow = pe.Workflow(name='fsl_preprocessing', base_dir='/media/alex/work/temp/data_preprocess/fsl_preprocessing/{}/{}/{}'.format(sub,ses,track))
                workflow.config['execution'] = {'stop_on_first_crash': 'False',
                                           'stop_on_first_rerun': 'False'}
                workflow.connect([
                    (split, flirt, [('out_files', 'in_file')]),
                    (flirt, merge, [('out_file', 'in_files')]),
                    (merge, mean, [('merged_file', 'in_file')]),
                ])
                
                plugin_args = {'n_procs': 10, 'maxtasksperchild': 1, 'raise_insufficient': False}
        
                # Run the workflow
                workflow.run(plugin='MultiProc', plugin_args=plugin_args)

        else:
            print("The file path not exists.")

I try set ‘ignore_exception’ as True and using try, except syntax, but all of them did not work. My forlopp can not continue when it arise the error FileNotFoundError.

I figure out the reason why the code raise FileNotFoundError since the out_file is defined as nii.gz. However, the FSLOUTPUTTYPE is nifit. It is not a nii.gz So when I change path of outfile from nii.gz to nii, it did not raise such error, the for loop can go…