dhj
dhj

Reputation: 11

Niilearn workflow giving improper output

I am using niilearn to preprocess MRI Image. But, it is not working because the file name that it is generating is rest_mcf.nii_mean_reg.nii . As you can see .nii extension is coming two times, rather than one. This file should be rest_mcf_mean_reg.nii . Since, the filename isn't proper, my workflow isn't proceeding.

I have attached the code below, Why is this error occuring? What to do so that this doesn't happen again?

func_template = 'cocaine_dataset/sub-{subject_id}/func/sub-{subject_id}_task-{task_name}.nii.gz'

templates = {
    'func': func_template,
}

# Infosource: Iterate over subject and task
infosource = Node(IdentityInterface(fields=['subject_id', 'task_name']),
                  name="infosource")
infosource.iterables = [('subject_id', subject_list),
                        ('task_name', task_list)]

# SelectFiles: Template-based file selection
selectfiles = Node(SelectFiles(templates), name="selectfiles")

# MCFLIRT: Motion Correction (realignment)
realign = Node(MCFLIRT(
    mean_vol=True,      # Create a mean image for reference
    save_plots=True,    # Save motion correction plots
    output_type='NIFTI', # Save output as NIFTI
    out_file='rest_mcf.nii'), name="realign")

# DataSink: Organize outputs
datasink = Node(DataSink(base_directory=experiment_dir,
                         container=output_dir),
                name="datasink")

# Define substitutions for datasink output
substitutions = [
    ('_subject_id_', 'sub-'), 
    ('_task_name_', 'task-'),
    ('.nii_mean_reg.nii', '_mean.nii.gz'),  # Handle the double extension
    ('_mcf.nii.gz', '_mcf'),                     # Remove additional .nii.gz
    ('.mat', '.par'),
    ('_flirt', '')                               # Remove unwanted suffix
]

datasink.inputs.substitutions = substitutions

# Create the workflow
realignment_wf = Workflow(name="realignment")
realignment_wf.base_dir = opj(experiment_dir, working_dir)

# Connect the nodes
realignment_wf.connect([
    (infosource, selectfiles, [('subject_id', 'subject_id'),
                               ('task_name', 'task_name')]),
    (selectfiles, realign, [('func', 'in_file')]),
    (realign, datasink, [('out_file', 'realignment.@realigned'),
                         ('par_file', 'realignment.@motion_parameters')])
])

# Run the workflow
realignment_wf.run('MultiProc', plugin_args={'n_procs': 1})

Upvotes: 0

Views: 13

Answers (0)

Related Questions