Exemplo n.º 1
0
def create_resting(subject, working_dir, data_dir, freesurfer_dir, out_dir,
                   vol_to_remove, TR, epi_resolution, highpass, lowpass,
                   echo_space, pe_dir, standard_brain,
                   standard_brain_resampled, standard_brain_mask,
                   standard_brain_mask_resampled, fwhm_smoothing):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    func_preproc = Workflow(name='resting_postscrub')
    func_preproc.base_dir = working_dir
    func_preproc.config['execution'][
        'crashdump_dir'] = func_preproc.base_dir + "/crash_files"
    # select files
    templates = {
        'epi_scrubbed_interp':
        'preprocessing/preprocessed/{subject}/scrubbed_interpolated/rest2anat_denoised_scrubbed_intep.nii.gz',
        'anat_head':
        'preprocessing/preprocessed/{subject}/structural/T1.nii.gz',
        'anat_brain':
        'preprocessing/preprocessed/{subject}/structural/brain.nii.gz',
        'brain_mask':
        'preprocessing/preprocessed/{subject}/structural/T1_brain_mask.nii.gz',
        'ants_affine':
        'preprocessing/preprocessed/{subject}/structural/transforms2mni/transform0GenericAffine.mat',
        'ants_warp':
        'preprocessing/preprocessed/{subject}/structural/transforms2mni/transform1Warp.nii.gz'
    }

    selectfiles = Node(nio.SelectFiles(templates, base_directory=data_dir),
                       name="selectfiles")
    selectfiles.inputs.subject = subject

    # node to remove first volumes
    #    remove_vol = Node(util.Function(input_names=['in_file', 't_min'],
    #                                    output_names=["out_file"],
    #                                    function=strip_rois_func),
    #                      name='remove_vol')
    #    remove_vol.inputs.t_min = vol_to_remove
    #    # workflow for motion correction
    #    moco = create_moco_pipeline()
    #
    #    # workflow for fieldmap correction and coregistration
    #    topup_coreg = create_topup_coreg_pipeline()
    #    topup_coreg.inputs.inputnode.fs_subjects_dir = freesurfer_dir
    #    topup_coreg.inputs.inputnode.fs_subject_id = subject
    #    topup_coreg.inputs.inputnode.echo_space = echo_space
    #    topup_coreg.inputs.inputnode.pe_dir = pe_dir
    #
    #    # workflow for applying transformations to timeseries
    #    transform_ts = create_transform_pipeline()
    #    transform_ts.inputs.inputnode.resolution = epi_resolution
    #
    #
    #    # workflow to denoise timeseries
    denoise = create_denoise_pipeline()
    denoise.inputs.inputnode.highpass_sigma = 1. / (2 * TR * highpass)
    denoise.inputs.inputnode.lowpass_sigma = 1. / (2 * TR * lowpass)
    # https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=ind1205&L=FSL&P=R57592&1=FSL&9=A&I=-3&J=on&d=No+Match%3BMatch%3BMatches&z=4
    denoise.inputs.inputnode.tr = TR

    # workflow to transform timeseries to MNI
    ants_registration = create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref = standard_brain
    ants_registration.inputs.inputnode.tr_sec = TR

    # FL added fullspectrum
    # workflow to transform fullspectrum timeseries to MNI
    #ants_registration_full = create_ants_registration_pipeline('ants_registration_full')
    #ants_registration_full.inputs.inputnode.ref = standard_brain
    #ants_registration_full.inputs.inputnode.tr_sec = TR

    # workflow to smooth
    smoothing = create_smoothing_pipeline()
    smoothing.inputs.inputnode.fwhm = fwhm_smoothing

    # visualize registration results
    visualize = create_visualize_pipeline()
    visualize.inputs.inputnode.mni_template = standard_brain

    # sink to store files
    sink = Node(nio.DataSink(
        parameterization=False,
        base_directory=out_dir,
        substitutions=[('rest_denoised_bandpassed_norm_trans.nii.gz',
                        'rest_scrubbed_int_mni_unsmoothed.nii.gz'),
                       ('rest_denoised_bandpassed_norm_trans_smooth.nii.gz',
                        'rest_scrubbed_int_mni_smoothed.nii.gz'),
                       ('rest_denoised_bandpassed_norm.nii.gz',
                        'rest_denoised_scrubbed_int_bp.nii.gz')]),
                name='sink')

    # connections
    func_preproc.connect([
        # remove the first volumes
        #        (selectfiles, remove_vol, [('func', 'in_file')]),
        #
        #        # align volumes and motion correction
        #        (remove_vol, moco, [('out_file', 'inputnode.epi')]),
        #
        #        # prepare field map
        #        (selectfiles, topup_coreg, [('ap', 'inputnode.ap'),
        #                                   ('pa', 'inputnode.pa'),
        #                                   ('anat_head', 'inputnode.anat_head'),
        #                                   ('anat_brain', 'inputnode.anat_brain')
        #                                   ]),
        #        (moco, topup_coreg, [('outputnode.epi_mean', 'inputnode.epi_mean')]),
        #
        #        # transform timeseries
        #        (remove_vol, transform_ts, [('out_file', 'inputnode.orig_ts')]),
        #        (selectfiles, transform_ts, [('anat_head', 'inputnode.anat_head')]),
        #        (selectfiles, transform_ts, [('brain_mask', 'inputnode.brain_mask')]),
        #        (moco, transform_ts, [('outputnode.mat_moco', 'inputnode.mat_moco')]),
        #        (topup_coreg, transform_ts, [('outputnode.fmap_fullwarp', 'inputnode.fullwarp')]),
        #
        #        # correct slicetiming
        #        # FIXME slice timing?
        #        # (transform_ts, slicetiming, [('outputnode.trans_ts_masked', 'inputnode.ts')]),
        #        # (slicetiming, denoise, [('outputnode.ts_slicetcorrected', 'inputnode.epi_coreg')]),
        #        (transform_ts, denoise, [('outputnode.trans_ts_masked', 'inputnode.epi_coreg')]),

        # denoise data
        (selectfiles, denoise, [('brain_mask', 'inputnode.brain_mask'),
                                ('anat_brain', 'inputnode.anat_brain'),
                                ('epi_scrubbed_interp',
                                 'inputnode.epi_denoised')]),
        (denoise, ants_registration, [('outputnode.normalized_file',
                                       'inputnode.denoised_ts')]),

        # registration to MNI space
        (selectfiles, ants_registration, [('ants_affine',
                                           'inputnode.ants_affine')]),
        (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')
                                          ]),

        # FL added fullspectrum
        #(selectfiles, ants_registration_full, [('epi_scrubbed_interp', 'inputnode.denoised_ts')]),
        #(selectfiles, ants_registration_full, [('ants_affine', 'inputnode.ants_affine')]),
        #(selectfiles, ants_registration_full, [('ants_warp', 'inputnode.ants_warp')]),
        (ants_registration, smoothing, [('outputnode.ants_reg_ts',
                                         'inputnode.ts_transformed')]),
        (smoothing, visualize, [('outputnode.ts_smoothed',
                                 'inputnode.ts_transformed')]),

        ##all the output
        #        (moco, sink, [  # ('outputnode.epi_moco', 'realign.@realigned_ts'),
        #                        ('outputnode.par_moco', 'realign.@par'),
        #                        ('outputnode.rms_moco', 'realign.@rms'),
        #                        ('outputnode.mat_moco', 'realign.MAT.@mat'),
        #                        ('outputnode.epi_mean', 'realign.@mean'),
        #                        ('outputnode.rotplot', 'realign.plots.@rotplot'),
        #                        ('outputnode.transplot', 'realign.plots.@transplot'),
        #                        ('outputnode.dispplots', 'realign.plots.@dispplots'),
        #                        ('outputnode.tsnr_file', 'realign.@tsnr')]),
        #        (topup_coreg, sink, [('outputnode.fmap', 'coregister.transforms2anat.@fmap'),
        #                            # ('outputnode.unwarpfield_epi2fmap', 'coregister.@unwarpfield_epi2fmap'),
        #                            ('outputnode.unwarped_mean_epi2fmap', 'coregister.@unwarped_mean_epi2fmap'),
        #                            ('outputnode.epi2fmap', 'coregister.@epi2fmap'),
        #                            # ('outputnode.shiftmap', 'coregister.@shiftmap'),
        #                            ('outputnode.fmap_fullwarp', 'coregister.transforms2anat.@fmap_fullwarp'),
        #                            ('outputnode.epi2anat', 'coregister.@epi2anat'),
        #                            ('outputnode.epi2anat_mat', 'coregister.transforms2anat.@epi2anat_mat'),
        #                            ('outputnode.epi2anat_dat', 'coregister.transforms2anat.@epi2anat_dat'),
        #                            ('outputnode.epi2anat_mincost', 'coregister.@epi2anat_mincost')
        #                            ]),
        #
        #        (transform_ts, sink, [('outputnode.trans_ts_masked', 'coregister.@full_transform_ts'),
        #                              ('outputnode.trans_ts_mean', 'coregister.@full_transform_mean'),
        #                              ('outputnode.resamp_brain', 'coregister.@resamp_brain')]),
        (
            denoise,
            sink,
            [
                ('outputnode.normalized_file', 'denoise.@normalized'),
                # FL added fullspectrum
            ]),
        (ants_registration, sink, [('outputnode.ants_reg_ts',
                                    'ants.@antsnormalized')]),
        #(ants_registration_full, sink, [('outputnode.ants_reg_ts', 'ants.@antsnormalized_fullspectrum')]),
        (smoothing, sink, [('outputnode.ts_smoothed', '@smoothed.FWHM6')]),
    ])

    func_preproc.write_graph(dotfilename='func_preproc.dot',
                             graph2use='colored',
                             format='pdf',
                             simple_form=True)
    func_preproc.run()
Exemplo n.º 2
0
def create_lemon_resting(subject, working_dir, data_dir, freesurfer_dir,
                         out_dir, vol_to_remove, TR, epi_resolution, highpass,
                         lowpass, echo_space, te_diff, pe_dir, standard_brain,
                         standard_brain_resampled, standard_brain_mask,
                         standard_brain_mask_resampled, fwhm_smoothing):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    func_preproc = Workflow(name='lemon_resting')
    func_preproc.base_dir = working_dir
    func_preproc.config['execution'][
        'crashdump_dir'] = func_preproc.base_dir + "/crash_files"
    # select files
    templates = {
        'func': 'func/EPI_t2.nii',
        'fmap_phase': 'unwarp/B0_ph.nii',
        'fmap_mag': 'unwarp/B0_mag.nii',
        'anat_head':
        'preprocessed/mod/anat/T1.nii.gz',  #either with mod or without
        'anat_brain':
        'preprocessed/mod/anat/brain.nii.gz',  #new version with brain_extraction from freesurfer  #T1_brain_brain.nii.gz',
        'brain_mask':
        'preprocessed/mod/anat/T1_brain_mask.nii.gz',  #T1_brain_brain_mask.nii.gz',
        'ants_affine':
        'preprocessed/mod/anat/transforms2mni/transform0GenericAffine.mat',
        'ants_warp':
        'preprocessed/mod/anat/transforms2mni/transform1Warp.nii.gz'
    }

    selectfiles = Node(nio.SelectFiles(templates, base_directory=data_dir),
                       name="selectfiles")

    # node to remove first volumes
    remove_vol = Node(util.Function(input_names=['in_file', 't_min'],
                                    output_names=["out_file"],
                                    function=strip_rois_func),
                      name='remove_vol')
    remove_vol.inputs.t_min = vol_to_remove
    # workflow for motion correction
    moco = create_moco_pipeline()
    # workflow for fieldmap correction and coregistration
    fmap_coreg = create_fmap_coreg_pipeline()
    fmap_coreg.inputs.inputnode.fs_subjects_dir = freesurfer_dir
    fmap_coreg.inputs.inputnode.fs_subject_id = subject
    fmap_coreg.inputs.inputnode.echo_space = echo_space
    fmap_coreg.inputs.inputnode.te_diff = te_diff
    fmap_coreg.inputs.inputnode.pe_dir = pe_dir
    # workflow for applying transformations to timeseries
    transform_ts = create_transform_pipeline()
    transform_ts.inputs.inputnode.resolution = epi_resolution

    #workflow to convert signal into percent signal change
    normalize = create_normalize_pipeline()
    normalize.inputs.inputnode.tr = TR

    #workflow to transform timeseries to MNI
    ants_registration = create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref = standard_brain_resampled

    #workflow to smooth
    smoothing = create_smoothing_pipeline()
    smoothing.inputs.inputnode.fwhm = fwhm_smoothing

    #workflow to correct slice timing
    slicetiming = create_slice_timing_pipeline()

    #visualize registration results
    visualize = create_visualize_pipeline()
    visualize.inputs.inputnode.mni_template = standard_brain_resampled

    #sink to store files
    sink = Node(nio.DataSink(
        parameterization=False,
        base_directory=out_dir,
        substitutions=[
            ('fmap_phase_fslprepared', 'fieldmap'),
            ('fieldmap_fslprepared_fieldmap_unmasked_vsm', 'shiftmap'),
            ('plot.rest_coregistered', 'outlier_plot'),
            ('filter_motion_comp_norm_compcor_art_dmotion',
             'nuissance_matrix'),
            ('rest_realigned.nii.gz_abs.rms', 'rest_realigned_abs.rms'),
            ('rest_realigned.nii.gz.par', 'rest_realigned.par'),
            ('rest_realigned.nii.gz_rel.rms', 'rest_realigned_rel.rms'),
            ('rest_realigned.nii.gz_abs_disp', 'abs_displacement_plot'),
            ('rest_realigned.nii.gz_rel_disp', 'rel_displacment_plot'),
            ('art.rest_coregistered_outliers', 'outliers'),
            ('global_intensity.rest_coregistered', 'global_intensity'),
            ('norm.rest_coregistered', 'composite_norm'),
            ('stats.rest_coregistered', 'stats'),
            ('rest_denoised_bandpassed_norm.nii.gz',
             'rest_preprocessed.nii.gz'),
            ('rest_denoised_bandpassed_norm_trans.nii.gz', 'rest_mni.nii.gz'),
            ('rest2anat_masked_st_norm_trans_smooth.nii',
             'rest_mni_smoothed.nii')
        ]),
                name='sink')

    # connections
    func_preproc.connect([
        #remove the first volumes
        (selectfiles, remove_vol, [('func', 'in_file')]),

        #align volumes and motion correction
        (remove_vol, moco, [('out_file', 'inputnode.epi')]),

        #prepare field map
        (selectfiles, fmap_coreg, [('fmap_phase', 'inputnode.phase'),
                                   ('fmap_mag', 'inputnode.mag'),
                                   ('anat_head', 'inputnode.anat_head'),
                                   ('anat_brain', 'inputnode.anat_brain')]),
        (moco, fmap_coreg, [('outputnode.epi_mean', 'inputnode.epi_mean')]),
        (remove_vol, transform_ts, [('out_file', 'inputnode.orig_ts')]),
        (selectfiles, transform_ts, [('anat_head', 'inputnode.anat_head')]),
        (selectfiles, transform_ts, [('brain_mask', 'inputnode.brain_mask')]),
        (moco, transform_ts, [('outputnode.mat_moco', 'inputnode.mat_moco')]),
        (fmap_coreg, transform_ts, [('outputnode.fmap_fullwarp',
                                     'inputnode.fullwarp')]),

        ##add slice time correction after applying motion realignement + unwarping
        (transform_ts, slicetiming, [('outputnode.trans_ts_masked',
                                      'inputnode.ts')]),
        (slicetiming, normalize, [('outputnode.ts_slicetcorrected',
                                   'inputnode.epi_coreg')]),
        (normalize, ants_registration, [('outputnode.normalized_file',
                                         'inputnode.denoised_ts')]),

        #registration to MNI space
        (selectfiles, ants_registration, [('ants_affine',
                                           'inputnode.ants_affine')]),
        (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')
                                          ]),
        (ants_registration, smoothing, [('outputnode.ants_reg_ts',
                                         'inputnode.ts_transformed')]),
        (smoothing, visualize, [('outputnode.ts_smoothed',
                                 'inputnode.ts_transformed')]),

        ##all the output
        (
            moco,
            sink,
            [  #('outputnode.epi_moco', 'realign.@realigned_ts'),
                ('outputnode.par_moco', 'realign.@par'),
                ('outputnode.rms_moco', 'realign.@rms'),
                ('outputnode.mat_moco', 'realign.MAT.@mat'),
                ('outputnode.epi_mean', 'realign.@mean'),
                ('outputnode.rotplot', 'realign.plots.@rotplot'),
                ('outputnode.transplot', 'realign.plots.@transplot'),
                ('outputnode.dispplots', 'realign.plots.@dispplots'),
                ('outputnode.tsnr_file', 'realign.@tsnr')
            ]),
        (
            fmap_coreg,
            sink,
            [
                ('outputnode.fmap', 'coregister.transforms2anat.@fmap'),
                #('outputnode.unwarpfield_epi2fmap', 'coregister.@unwarpfield_epi2fmap'),
                ('outputnode.unwarped_mean_epi2fmap',
                 'coregister.@unwarped_mean_epi2fmap'),
                ('outputnode.epi2fmap', 'coregister.@epi2fmap'),
                #('outputnode.shiftmap', 'coregister.@shiftmap'),
                ('outputnode.fmap_fullwarp',
                 'coregister.transforms2anat.@fmap_fullwarp'),
                ('outputnode.epi2anat', 'coregister.@epi2anat'),
                ('outputnode.epi2anat_mat',
                 'coregister.transforms2anat.@epi2anat_mat'),
                ('outputnode.epi2anat_dat',
                 'coregister.transforms2anat.@epi2anat_dat'),
                ('outputnode.epi2anat_mincost', 'coregister.@epi2anat_mincost')
            ]),
        (
            transform_ts,
            sink,
            [  #('outputnode.trans_ts', 'coregister.@full_transform_ts'),
                ('outputnode.trans_ts_mean',
                 'coregister.@full_transform_mean'),
                ('outputnode.resamp_brain', 'coregister.@resamp_brain')
            ]),
        (ants_registration, sink, [('outputnode.ants_reg_ts',
                                    'ants.@antsnormalized')]),
        (smoothing, sink, [('outputnode.ts_smoothed', '@smoothed.FWHM6')]),
    ])

    #func_preproc.write_graph(dotfilename='func_preproc.dot', graph2use='colored', format='pdf', simple_form=True)
    func_preproc.run()
Exemplo n.º 3
0
def create_hc_connec(subject, working_dir, data_dir,
                  freesurfer_dir, 
                  out_dir,epi_resolution, 
                  standard_brain,
                  standard_brain_resampled, 
                  standard_brain_mask,
                  standard_brain_mask_resampled,
                  fwhm_smoothing,
                  side, half):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    hc_connec = Workflow(name='transform_hc_mni')
    hc_connec.base_dir = working_dir
    hc_connec.config['execution']['crashdump_dir'] = hc_connec.base_dir + "/crash_files"
    
    
    # select files
    templates={
    'input': 'hc_connectivity_thr099/'+side+'/hc_masks/'+half+'/hc_mask_thr0.99.nii.gz',
    #'hc_connectivity/left/hc_masks/anterior_hc/hc_mask.nii.gz',
    #
    'ants_affine':  'structural/transforms2mni/transform0GenericAffine.mat',
    'ants_warp':    'structural/transforms2mni/transform1Warp.nii.gz',

    }
         
    selectfiles = Node(nio.SelectFiles(templates, base_directory=data_dir),    name="selectfiles")

    
    #workflow to tranform correlations to MNI space
    ants_registration=create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref=standard_brain #_resampled: 2x2x2mm brain for RSV
    
    #sink to store files
    sink = Node(nio.DataSink
    (parameterization=True,
    base_directory=out_dir),
#   substitutions=[('_binarize', 'binarize'), -> don't really seem to work and I don't know why.
#                   #('_binarize', 'anterior_hc'),
#                   ('_ants_reg1', 'posterior_hc'),
#                   #('_ants_reg', 'anterior_hc'),
#                   ('_smooth1', 'posterior_hc'),
#                   ('_smooth0', 'anterior_hc'),
#                   ('corr_Z_trans', 'corr_Z_MNI')],
    name='sink')
    
    sink.inputs.substitutions=[('hc_mask_trans', 'hc_mask2mni'),
                               ('_ants_reg0', 'MNI')]
    
    
    
    # connections
    hc_connec.connect([
    (selectfiles, ants_registration,    [('input', 'inputnode.corr_Z')]),
    (selectfiles, ants_registration, [('ants_affine', 'inputnode.ants_affine')] ),
    (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')] ),

    (ants_registration, sink, [('outputnode.ants_reg_corr_Z', 'hc_connectivity_thr099.'+side+'.hc_masks.' + half)])
        
    ])

    
    hc_connec.run() #it can't run in multiproc as in one moment one file is hardcoded and saved to the disk which is
Exemplo n.º 4
0
def create_hc_connec(subject, working_dir, data_dir, freesurfer_dir, out_dir,
                     epi_resolution, standard_brain, standard_brain_resampled,
                     standard_brain_mask, standard_brain_mask_resampled,
                     fwhm_smoothing, side, TR, highpass, lowpass):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    hc_connec = Workflow(name='hc_connec_thr099_scrubbed')
    hc_connec.base_dir = working_dir
    hc_connec.config['execution'][
        'crashdump_dir'] = hc_connec.base_dir + "/crash_files"

    # select files
    templates = {
        #'rest_head': 'resting_state/denoise/rest_preprocessed_nativespace.nii.gz', #denoised and bandpass-filtered native space (2x2x2mm) image
        'rest2anat_scrubbed':
        'preprocessing/preprocessed/{subject}/scrubbed_interpolated/rest2anat_denoised_scrubbed_intep.nii.gz',  #denoised, scrubbed, interp, bp-filtered native space
        'ants_affine':
        'preprocessing/preprocessed/{subject}/structural/transforms2mni/transform0GenericAffine.mat',
        'ants_warp':
        'preprocessing/preprocessed/{subject}/structural/transforms2mni/transform1Warp.nii.gz',
        'scrubvols': 'quality_reports/poldrack_reports/{subject}/scrubvols.txt'
    }

    selectfiles = Node(nio.SelectFiles(templates, base_directory=data_dir),
                       name="selectfiles")
    selectfiles.inputs.subject = subject

    denoise = create_denoise_pipeline()
    denoise.inputs.inputnode.highpass_sigma = 1. / (2 * TR * highpass)
    denoise.inputs.inputnode.lowpass_sigma = 1. / (2 * TR * lowpass)
    # https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=ind1205&L=FSL&P=R57592&1=FSL&9=A&I=-3&J=on&d=No+Match%3BMatch%3BMatches&z=4
    denoise.inputs.inputnode.tr = TR

    #drop scrubbed volumes
    scrub_volumes = Node(util.Function(
        input_names=['scrubvols', 'in_file', 'working_dir'],
        output_names=['filename_scrubbed_img'],
        function=scrub_timepoints),
                         name='scrub_volumes')
    scrub_volumes.inputs.working_dir = working_dir

    #get T1 brainmask
    get_T1_brainmask = create_get_T1_brainmask()
    get_T1_brainmask.inputs.inputnode.fs_subjects_dir = freesurfer_dir
    get_T1_brainmask.inputs.inputnode.fs_subject_id = subject

    #workflow to extract HC and transform into individual space
    transform_hc = create_transform_hc()
    transform_hc.inputs.inputnode.fs_subjects_dir = freesurfer_dir
    transform_hc.inputs.inputnode.fs_subject_id = subject
    transform_hc.inputs.inputnode.resolution = 2
    transform_hc.inputs.inputnode.working_dir = working_dir

    #workflow to extract timeseries and correlate
    corr_ts = create_corr_ts()

    #workflow to tranform correlations to MNI space
    ants_registration = create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref = standard_brain  #_resampled: 2x2x2mm brain for RSV

    #
    smoothing = create_smoothing_pipeline()
    smoothing.inputs.inputnode.fwhm = fwhm_smoothing
    #sink to store files
    sink = Node(
        nio.DataSink(parameterization=True, base_directory=out_dir),
        #   substitutions=[('_binarize', 'binarize'), -> don't really seem to work and I don't know why.
        #                   #('_binarize', 'anterior_hc'),
        #                   ('_ants_reg1', 'posterior_hc'),
        #                   #('_ants_reg', 'anterior_hc'),
        #                   ('_smooth1', 'posterior_hc'),
        #                   ('_smooth0', 'anterior_hc'),
        #                   ('corr_Z_trans', 'corr_Z_MNI')],
        name='sink')

    sink.inputs.substitutions = [('_binarize0', 'posterior_hc'),
                                 ('_binarize1', 'anterior_hc'),
                                 ('_ants_reg0', 'posterior_hc'),
                                 ('_ants_reg1', 'anterior_hc'),
                                 ('_smooth0', 'posterior_hc'),
                                 ('_smooth1', 'anterior_hc'),
                                 ('_apply_FisherZ0', 'posterior_hc'),
                                 ('_apply_FisherZ1', 'anterior_hc')]

    # connections
    hc_connec.connect([
        #bandpass-filtering implemented after scrubbing and replacement!
        (selectfiles, scrub_volumes, [('scrubvols', 'scrubvols')]),
        (get_T1_brainmask, transform_hc, [('outputnode.T1',
                                           'inputnode.anat_head')]),
        (transform_hc, corr_ts, [('outputnode.hc_transformed_bin',
                                  'inputnode.hc_mask')]),
        (selectfiles, denoise, [('rest2anat_scrubbed',
                                 'inputnode.epi_denoised')]),
        (denoise, scrub_volumes, [('outputnode.normalized_file', 'in_file')]),
        (scrub_volumes, corr_ts, [('filename_scrubbed_img', 'inputnode.ts')]),
        (corr_ts, sink,
         [('outputnode.corrmap_z', 'hc_connectivity_thr099.scrubbed.' + side +
           '.corr.nativespace.@transformed')]),
        (corr_ts, ants_registration, [('outputnode.corrmap_z',
                                       'inputnode.corr_Z')]),
        (selectfiles, ants_registration, [('ants_affine',
                                           'inputnode.ants_affine')]),
        (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')
                                          ]),
        (ants_registration, sink,
         [('outputnode.ants_reg_corr_Z',
           'hc_connectivity_thr099.scrubbed.' + side + '.corr.ants')]),
        (ants_registration, smoothing, [('outputnode.ants_reg_corr_Z',
                                         'inputnode.ts_transformed')]),
        (smoothing, sink,
         [('outputnode.ts_smoothed',
           'hc_connectivity_thr099.scrubbed.' + side + '.corr.smoothed')]),
    ])

    hc_connec.run(
    )  #it can't run in multiproc as in one moment one file is hardcoded and saved to the disk which is
Exemplo n.º 5
0
def create_lemon_resting(subject, working_dir, data_dir, data_dir_WDR, freesurfer_dir, out_dir,
    vol_to_remove, TR, epi_resolution, highpass, lowpass,
    echo_space, te_diff, pe_dir, standard_brain, standard_brain_resampled, standard_brain_mask, 
    standard_brain_mask_resampled, fwhm_smoothing):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    func_preproc = Workflow(name='lemon_resting')
    func_preproc.base_dir = working_dir
    func_preproc.config['execution']['crashdump_dir'] = func_preproc.base_dir + "/crash_files"
    # select files
   
   
       # select files
    templates={
    'anat_brain' : 'preprocessed/mod/anat/brain.nii.gz', 
    'brain_mask' : 'preprocessed/mod/anat/T1_brain_mask.nii.gz',
    'ants_affine': 'preprocessed/mod/anat/transforms2mni/transform0GenericAffine.mat',
    'ants_warp':   'preprocessed/mod/anat/transforms2mni/transform1Warp.nii.gz',

    }
         
    templates_WDR={
    'par_moco': 'lemon_resting/motion_correction/mcflirt/rest_realigned.nii.gz.par',
    'trans_ts': 'lemon_resting/transform_timeseries/merge/rest2anat.nii.gz',
    'epi2anat_dat': 'lemon_resting/fmap_coreg/bbregister/rest2anat.dat',
    'unwarped_mean_epi2fmap': 'lemon_resting/fmap_coreg/applywarp0/rest_mean2fmap_unwarped.nii.gz',
     
    }
    
    selectfiles = Node(nio.SelectFiles(templates, base_directory=data_dir),    name="selectfiles")
    selectfiles_WDR = Node(nio.SelectFiles(templates_WDR, base_directory=data_dir_WDR),    name="selectfiles_WDR")
   
      
       
          
    # workflow to denoise timeseries
    denoise = create_denoise_pipeline()
    denoise.inputs.inputnode.highpass_sigma= 1./(2*TR*highpass)
    denoise.inputs.inputnode.lowpass_sigma= 1./(2*TR*lowpass)
    #https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=ind1205&L=FSL&P=R57592&1=FSL&9=A&I=-3&J=on&d=No+Match%3BMatch%3BMatches&z=4
    denoise.inputs.inputnode.tr = TR
    
    #workflow to transform timeseries to MNI
    ants_registration=create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref=standard_brain_resampled    
    
    #workflow to smooth
    smoothing = create_smoothing_pipeline() 
    smoothing.inputs.inputnode.fwhm=fwhm_smoothing
   
    #workflow to slice time in the end as a try
    slicetiming = create_slice_timing_pipeline() 
    
    #visualize registration results
    visualize = create_visualize_pipeline()
    visualize.inputs.inputnode.mni_template=standard_brain_resampled 
    

    
    #sink to store files
    sink = Node(nio.DataSink(parameterization=False,
    base_directory=out_dir,
    substitutions=[('fmap_phase_fslprepared', 'fieldmap'),
    ('fieldmap_fslprepared_fieldmap_unmasked_vsm', 'shiftmap'),
    ('plot.rest_coregistered', 'outlier_plot'),
    ('filter_motion_comp_norm_compcor_art_dmotion', 'nuissance_matrix'),
    ('rest_realigned.nii.gz_abs.rms', 'rest_realigned_abs.rms'),
    ('rest_realigned.nii.gz.par','rest_realigned.par'),
    ('rest_realigned.nii.gz_rel.rms', 'rest_realigned_rel.rms'),
    ('rest_realigned.nii.gz_abs_disp', 'abs_displacement_plot'),
    ('rest_realigned.nii.gz_rel_disp', 'rel_displacment_plot'),
    ('art.rest_coregistered_outliers', 'outliers'),
    ('global_intensity.rest_coregistered', 'global_intensity'),
    ('norm.rest_coregistered', 'composite_norm'),
    ('stats.rest_coregistered', 'stats'),
    ('rest_denoised_bandpassed_norm.nii.gz', 'rest_preprocessed_nativespace.nii.gz'),
    ('rest_denoised_bandpassed_norm_trans.nii.gz', 'rest_mni_unsmoothed.nii.gz'),
    ('rest_denoised_bandpassed_norm_trans_smooth.nii', 'rest_mni_smoothed.nii')]),
    name='sink')
    
    
    # connections
    func_preproc.connect([
    
    #correct slicetiming
    (selectfiles_WDR, slicetiming, [('trans_ts', 'inputnode.ts')]),
    (slicetiming, denoise, [('outputnode.ts_slicetcorrected','inputnode.epi_coreg')]),
    
    #denoise data
    (selectfiles, denoise, [('brain_mask', 'inputnode.brain_mask'),
    ('anat_brain', 'inputnode.anat_brain')]),
    (selectfiles_WDR, denoise, [('par_moco', 'inputnode.moco_par')]),
    (selectfiles_WDR, denoise, [('epi2anat_dat', 'inputnode.epi2anat_dat'),
    ('unwarped_mean_epi2fmap', 'inputnode.unwarped_mean')]),
    (denoise, ants_registration, [('outputnode.normalized_file', 'inputnode.denoised_ts')]),
        
   
    #registration to MNI space
    (selectfiles, ants_registration, [('ants_affine', 'inputnode.ants_affine')] ),
    (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')] ),

    (ants_registration, smoothing, [('outputnode.ants_reg_ts', 'inputnode.ts_transformed')]),

    (smoothing, visualize,  [('outputnode.ts_smoothed', 'inputnode.ts_transformed')]),


    ##all the output
    (denoise, sink, [
    ('outputnode.wmcsf_mask', 'denoise.mask.@wmcsf_masks'),
    ('outputnode.combined_motion','denoise.artefact.@combined_motion'),
    ('outputnode.outlier_files','denoise.artefact.@outlier'),
    ('outputnode.intensity_files','denoise.artefact.@intensity'),
    ('outputnode.outlier_stats','denoise.artefact.@outlierstats'),
    ('outputnode.outlier_plots','denoise.artefact.@outlierplots'),
    ('outputnode.mc_regressor', 'denoise.regress.@mc_regressor'),
    ('outputnode.comp_regressor', 'denoise.regress.@comp_regressor'),
    ('outputnode.mc_F', 'denoise.regress.@mc_F'),
    ('outputnode.mc_pF', 'denoise.regress.@mc_pF'),
    ('outputnode.comp_F', 'denoise.regress.@comp_F'),
    ('outputnode.comp_pF', 'denoise.regress.@comp_pF'),
    ('outputnode.brain_mask_resamp', 'denoise.mask.@brain_resamp'),
    ('outputnode.brain_mask2epi', 'denoise.mask.@brain_mask2epi'),
    ('outputnode.normalized_file', 'denoise.@normalized')
    ]),
    (ants_registration, sink, [('outputnode.ants_reg_ts', 'ants.@antsnormalized')
    ]),
    (smoothing, sink, [('outputnode.ts_smoothed', '@smoothed.FWHM6')]),
    ])

    #func_preproc.write_graph(dotfilename='func_preproc.dot', graph2use='colored', format='pdf', simple_form=True)
    func_preproc.run()
    # plugin='MultiProc'plugin='MultiProc'plugin='CondorDAGMan')
    #func_preproc.run()plugin='CondorDAGMan'plugin='CondorDAGMan'plugin='CondorDAGMan'plugin='CondorDAGMan'
#plugin='CondorDAGMan'
def normalization_fullspectrum(subject, working_dir, data_dir, freesurfer_dir, out_dir,
                       vol_to_remove, TR, epi_resolution, highpass, lowpass,
                       echo_space, te_diff, pe_dir, standard_brain, standard_brain_resampled, standard_brain_mask,
                       standard_brain_mask_resampled, fwhm_smoothing):
    from nipype.pipeline.engine import Node, Workflow
    import nipype.interfaces.io as nio
    import nipype.interfaces.fsl as fsl
    from ants_registration import create_ants_registration_pipeline

    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    func_preproc = Workflow(name='lemon_resting')
    func_preproc.base_dir = working_dir
    func_preproc.config['execution']['crashdump_dir'] = func_preproc.base_dir + "/crash_files"
    # select files
    templates = {
        'ts_fullspectrum': 'Lemon_mod/{subject_id}/lemon_resting/denoise/filternoise/rest2anat_denoised.nii.gz',
        # 'func': 'func/EPI_t2.nii',
        # 'fmap_phase': 'unwarp/B0_ph.nii',
        # 'fmap_mag': 'unwarp/B0_mag.nii',
        # 'anat_head': 'preprocessed/mod/anat/T1.nii.gz',  # either with mod or without
        # 'anat_brain': 'preprocessed/mod/anat/brain.nii.gz',
        # # new version with brain_extraction from freesurfer  #T1_brain_brain.nii.gz',
        # 'brain_mask': 'preprocessed/mod/anat/T1_brain_mask.nii.gz',  # T1_brain_brain_mask.nii.gz',
        'ants_affine': 'subjects/{subject_id}/preprocessed/mod/anat/transforms2mni/transform0GenericAffine.mat',
        'ants_warp': 'subjects/{subject_id}/preprocessed/mod/anat/transforms2mni/transform1Warp.nii.gz'
        }
    # base = /scr/kennedy2/data_fbeyer/genetics/
    selectfiles = Node(nio.SelectFiles(templates,
                                       base_directory=data_dir),
                       name="selectfiles")
    selectfiles.inputs.subject_id = subject



    # FL added fullspectrum
    # workflow to transform fullspectrum timeseries to MNI
    ants_registration_full = create_ants_registration_pipeline('ants_registration_full')
    ants_registration_full.inputs.inputnode.ref = standard_brain_resampled

    # FL added fullspectrum
    func_preproc.connect(selectfiles, 'ts_fullspectrum', ants_registration_full, 'inputnode.denoised_ts')
    func_preproc.connect(selectfiles, 'ants_affine', ants_registration_full, 'inputnode.ants_affine')
    func_preproc.connect(selectfiles, 'ants_warp', ants_registration_full, 'inputnode.ants_warp')


    # sink to store files
    sink = Node(nio.DataSink(parameterization=False,
                             base_directory=out_dir,
                             substitutions=[
                                 # ('fmap_phase_fslprepared', 'fieldmap'),
                                 # ('fieldmap_fslprepared_fieldmap_unmasked_vsm', 'shiftmap'),
                                 # ('plot.rest_coregistered', 'outlier_plot'),
                                 # ('filter_motion_comp_norm_compcor_art_dmotion', 'nuissance_matrix'),
                                 # ('rest_realigned.nii.gz_abs.rms', 'rest_realigned_abs.rms'),
                                 # ('rest_realigned.nii.gz.par', 'rest_realigned.par'),
                                 # ('rest_realigned.nii.gz_rel.rms', 'rest_realigned_rel.rms'),
                                 # ('rest_realigned.nii.gz_abs_disp', 'abs_displacement_plot'),
                                 # ('rest_realigned.nii.gz_rel_disp', 'rel_displacment_plot'),
                                 # ('art.rest_coregistered_outliers', 'outliers'),
                                 # ('global_intensity.rest_coregistered', 'global_intensity'),
                                 # ('norm.rest_coregistered', 'composite_norm'),
                                 # ('stats.rest_coregistered', 'stats'),
                                 # ('rest_denoised_bandpassed_norm.nii.gz',
                                 #  'rest_preprocessed_nativespace.nii.gz'),
                                 # ('rest_denoised_bandpassed_norm_trans.nii.gz',
                                 #  'rest_mni_unsmoothed.nii.gz'),
                                 # ('rest_denoised_bandpassed_norm_trans_smooth.nii',
                                 #  'rest_mni_smoothed.nii'),
                                 # FL added
                                 ('rest2anat_denoised.nii.gz', 'rest_preprocessed_nativespace_fullspectrum.nii.gz'),
                                 ('rest2anat_denoised_trans.nii.gz', 'rest_mni_unsmoothed_fullspectrum.nii.gz')
                             ]),
                name='sink')

    func_preproc.connect(ants_registration_full, 'outputnode.ants_reg_ts', sink, 'ants.@antsnormalized_fullspectrum')


    func_preproc.write_graph(dotfilename='func_preproc.dot', graph2use='colored', format='pdf', simple_form=True)
    func_preproc.run(plugin='CondorDAGMan')
Exemplo n.º 7
0
def create_hc_connec(subject, working_dir, data_dir, freesurfer_dir, out_dir,
                     epi_resolution, standard_brain, standard_brain_resampled,
                     standard_brain_mask, standard_brain_mask_resampled,
                     fwhm_smoothing, side):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    hc_connec = Workflow(name='hc_connec_thr099')
    hc_connec.base_dir = working_dir
    hc_connec.config['execution'][
        'crashdump_dir'] = hc_connec.base_dir + "/crash_files"

    # select files
    templates = {
        'rest_head':
        'resting_state/denoise/rest_preprocessed_nativespace.nii.gz',  #denoised and bandpass-filtered native space (2x2x2mm) image
        #'rest_head': 'scrubbed_interpolated/denoise/rest_denoised_scrubbed_int_bp.nii.gz', #denoised, scrubbed, interp, bp-filtered native space
        'ants_affine':
        'structural/transforms2mni/transform0GenericAffine.mat',
        'ants_warp':
        'structural/transforms2mni/transform1Warp.nii.gz',
        'ts_smoothed_nativ':
        'resting_state/denoise/rest_preprocessed_nativespace.nii.gz'
    }

    selectfiles = Node(nio.SelectFiles(templates, base_directory=data_dir),
                       name="selectfiles")

    #get T1 brainmask
    get_T1_brainmask = create_get_T1_brainmask()
    get_T1_brainmask.inputs.inputnode.fs_subjects_dir = freesurfer_dir
    get_T1_brainmask.inputs.inputnode.fs_subject_id = subject

    #workflow to extract HC and transform into individual space
    transform_hc = create_transform_hc()
    transform_hc.inputs.inputnode.fs_subjects_dir = freesurfer_dir
    transform_hc.inputs.inputnode.fs_subject_id = subject
    transform_hc.inputs.inputnode.resolution = 2
    transform_hc.inputs.inputnode.working_dir = working_dir

    #workflow to extract timeseries and correlate
    corr_ts = create_corr_ts()

    #workflow to tranform correlations to MNI space
    ants_registration = create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref = standard_brain  #_resampled: 2x2x2mm brain for RSV

    #
    smoothing = create_smoothing_pipeline()
    smoothing.inputs.inputnode.fwhm = fwhm_smoothing
    #sink to store files
    sink = Node(
        nio.DataSink(parameterization=True, base_directory=out_dir),
        #   substitutions=[('_binarize', 'binarize'), -> don't really seem to work and I don't know why.
        #                   #('_binarize', 'anterior_hc'),
        #                   ('_ants_reg1', 'posterior_hc'),
        #                   #('_ants_reg', 'anterior_hc'),
        #                   ('_smooth1', 'posterior_hc'),
        #                   ('_smooth0', 'anterior_hc'),
        #                   ('corr_Z_trans', 'corr_Z_MNI')],
        name='sink')

    sink.inputs.substitutions = [('_binarize0', 'posterior_hc'),
                                 ('_binarize1', 'anterior_hc'),
                                 ('_ants_reg0', 'posterior_hc'),
                                 ('_ants_reg1', 'anterior_hc'),
                                 ('_smooth0', 'posterior_hc'),
                                 ('_smooth1', 'anterior_hc'),
                                 ('_apply_FisherZ0', 'posterior_hc'),
                                 ('_apply_FisherZ1', 'anterior_hc')]

    # connections
    hc_connec.connect([
        (get_T1_brainmask, transform_hc, [('outputnode.T1',
                                           'inputnode.anat_head')]),
        #(selectfiles, transform_hc, [('rest_head', 'inputnode.anat_head')]),
        (selectfiles, corr_ts, [('ts_smoothed_nativ', 'inputnode.ts')]),
        (transform_hc, corr_ts, [('outputnode.hc_transformed_bin',
                                  'inputnode.hc_mask')]),
        (transform_hc, sink, [('outputnode.hc_transformed_bin',
                               'hc_connectivity_thr099.' + side + '.hc_masks')
                              ]),
        (corr_ts, sink, [('outputnode.corrmap_z', 'hc_connectivity_thr099.' +
                          side + '.corr.nativespace.@transformed')]),
        (corr_ts, ants_registration, [('outputnode.corrmap_z',
                                       'inputnode.corr_Z')]),
        (selectfiles, ants_registration, [('ants_affine',
                                           'inputnode.ants_affine')]),
        (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')
                                          ]),
        (ants_registration, sink,
         [('outputnode.ants_reg_corr_Z',
           'hc_connectivity_thr099.' + side + '.corr.ants')]),
        (ants_registration, smoothing, [('outputnode.ants_reg_corr_Z',
                                         'inputnode.ts_transformed')]),
        (smoothing, sink,
         [('outputnode.ts_smoothed',
           'hc_connectivity_thr099.' + side + '.corr.smoothed')]),
    ])

    hc_connec.run(
    )  #it can't run in multiproc as in one moment one file is hardcoded and saved to the disk which is
Exemplo n.º 8
0
def create_lemon_resting(subject, working_dir, data_dir, freesurfer_dir, out_dir,
                         vol_to_remove, TR, epi_resolution, highpass, lowpass,
                         echo_space, te_diff, pe_dir, standard_brain, standard_brain_resampled, standard_brain_mask,
                         standard_brain_mask_resampled, fwhm_smoothing):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    func_preproc = Workflow(name='lemon_resting')
    func_preproc.base_dir = working_dir
    func_preproc.config['execution']['crashdump_dir'] = func_preproc.base_dir + "/crash_files"
    # select files
    templates = {'func': 'raw_data/{subject}/func/EPI_t2.nii',
                 'fmap_phase': 'raw_data/{subject}/unwarp/B0_ph.nii',
                 'fmap_mag': 'raw_data/{subject}/unwarp/B0_mag.nii',
                 'anat_head': 'preprocessed/{subject}/structural/T1.nii.gz',  # either with mod or without
                 'anat_brain': 'preprocessed/{subject}/structural/brain.nii.gz',
                 # new version with brain_extraction from freesurfer  #T1_brain_brain.nii.gz',
                 'brain_mask': 'preprocessed/{subject}/structural/T1_brain_mask.nii.gz',  # T1_brain_brain_mask.nii.gz',
                 'ants_affine': 'preprocessed/{subject}/structural/transforms2mni/transform0GenericAffine.mat',
                 'ants_warp': 'preprocessed/{subject}/structural/transforms2mni/transform1Warp.nii.gz'
                 }

    selectfiles = Node(nio.SelectFiles(templates,
                                       base_directory=data_dir),
                       name="selectfiles")
    selectfiles.inputs.subject = subject


    # node to remove first volumes
    remove_vol = Node(util.Function(input_names=['in_file', 't_min'],
                                    output_names=["out_file"],
                                    function=strip_rois_func),
                      name='remove_vol')
    remove_vol.inputs.t_min = vol_to_remove
    # workflow for motion correction
    moco = create_moco_pipeline()

    # workflow for fieldmap correction and coregistration
    fmap_coreg = create_fmap_coreg_pipeline()
    fmap_coreg.inputs.inputnode.fs_subjects_dir = freesurfer_dir
    fmap_coreg.inputs.inputnode.fs_subject_id = subject
    fmap_coreg.inputs.inputnode.echo_space = echo_space
    fmap_coreg.inputs.inputnode.te_diff = te_diff
    fmap_coreg.inputs.inputnode.pe_dir = pe_dir

    # workflow for applying transformations to timeseries
    transform_ts = create_transform_pipeline()
    transform_ts.inputs.inputnode.resolution = epi_resolution


    # workflow to denoise timeseries
    denoise = create_denoise_pipeline()
    denoise.inputs.inputnode.highpass_sigma = 1. / (2 * TR * highpass)
    denoise.inputs.inputnode.lowpass_sigma = 1. / (2 * TR * lowpass)
    # https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=ind1205&L=FSL&P=R57592&1=FSL&9=A&I=-3&J=on&d=No+Match%3BMatch%3BMatches&z=4
    denoise.inputs.inputnode.tr = TR

    # workflow to transform timeseries to MNI
    ants_registration = create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref = standard_brain_resampled
    ants_registration.inputs.inputnode.tr_sec = TR

    # FL added fullspectrum
    # workflow to transform fullspectrum timeseries to MNI
    ants_registration_full = create_ants_registration_pipeline('ants_registration_full')
    ants_registration_full.inputs.inputnode.ref = standard_brain_resampled
    ants_registration_full.inputs.inputnode.tr_sec = TR

    # workflow to smooth
    smoothing = create_smoothing_pipeline()
    smoothing.inputs.inputnode.fwhm = fwhm_smoothing

    # visualize registration results
    visualize = create_visualize_pipeline()
    visualize.inputs.inputnode.mni_template = standard_brain_resampled



    # sink to store files
    sink = Node(nio.DataSink(parameterization=False,
                             base_directory=out_dir,
                             substitutions=[('fmap_phase_fslprepared', 'fieldmap'),
                                            ('fieldmap_fslprepared_fieldmap_unmasked_vsm', 'shiftmap'),
                                            ('plot.rest_coregistered', 'outlier_plot'),
                                            ('filter_motion_comp_norm_compcor_art_dmotion', 'nuissance_matrix'),
                                            ('rest_realigned.nii.gz_abs.rms', 'rest_realigned_abs.rms'),
                                            ('rest_realigned.nii.gz.par', 'rest_realigned.par'),
                                            ('rest_realigned.nii.gz_rel.rms', 'rest_realigned_rel.rms'),
                                            ('rest_realigned.nii.gz_abs_disp', 'abs_displacement_plot'),
                                            ('rest_realigned.nii.gz_rel_disp', 'rel_displacment_plot'),
                                            ('art.rest_coregistered_outliers', 'outliers'),
                                            ('global_intensity.rest_coregistered', 'global_intensity'),
                                            ('norm.rest_coregistered', 'composite_norm'),
                                            ('stats.rest_coregistered', 'stats'),
                                            ('rest_denoised_bandpassed_norm.nii.gz',
                                             'rest_preprocessed_nativespace.nii.gz'),
                                            ('rest_denoised_bandpassed_norm_trans.nii.gz',
                                             'rest_mni_unsmoothed.nii.gz'),
                                            ('rest_denoised_bandpassed_norm_trans_smooth.nii',
                                             'rest_mni_smoothed.nii'),
                                            # FL added
                                            ('rest2anat_masked.nii.gz', 'rest_coregistered_nativespace.nii.gz'),
                                            ('rest2anat_denoised.nii.gz',
                                             'rest_preprocessed_nativespace_fullspectrum.nii.gz'),
                                            ('rest2anat_denoised_trans.nii.gz',
                                             'rest_mni_unsmoothed_fullspectrum.nii.gz')
                                            ]),
                name='sink')


    # connections
    func_preproc.connect([
        # remove the first volumes
        (selectfiles, remove_vol, [('func', 'in_file')]),

        # align volumes and motion correction
        (remove_vol, moco, [('out_file', 'inputnode.epi')]),

        # prepare field map
        (selectfiles, fmap_coreg, [('fmap_phase', 'inputnode.phase'),
                                   ('fmap_mag', 'inputnode.mag'),
                                   ('anat_head', 'inputnode.anat_head'),
                                   ('anat_brain', 'inputnode.anat_brain')
                                   ]),
        (moco, fmap_coreg, [('outputnode.epi_mean', 'inputnode.epi_mean')]),

        # transform timeseries
        (remove_vol, transform_ts, [('out_file', 'inputnode.orig_ts')]),
        (selectfiles, transform_ts, [('anat_head', 'inputnode.anat_head')]),
        (selectfiles, transform_ts, [('brain_mask', 'inputnode.brain_mask')]),
        (moco, transform_ts, [('outputnode.mat_moco', 'inputnode.mat_moco')]),
        (fmap_coreg, transform_ts, [('outputnode.fmap_fullwarp', 'inputnode.fullwarp')]),

        # correct slicetiming
        # FIXME slice timing?
        # (transform_ts, slicetiming, [('outputnode.trans_ts_masked', 'inputnode.ts')]),
        # (slicetiming, denoise, [('outputnode.ts_slicetcorrected', 'inputnode.epi_coreg')]),
        (transform_ts, denoise, [('outputnode.trans_ts_masked', 'inputnode.epi_coreg')]),

        # denoise data
        (selectfiles, denoise, [('brain_mask', 'inputnode.brain_mask'),
                                ('anat_brain', 'inputnode.anat_brain')]),
        (moco, denoise, [('outputnode.par_moco', 'inputnode.moco_par')]),
        (fmap_coreg, denoise, [('outputnode.epi2anat_dat', 'inputnode.epi2anat_dat'),
                               ('outputnode.unwarped_mean_epi2fmap', 'inputnode.unwarped_mean')]),
        (denoise, ants_registration, [('outputnode.normalized_file', 'inputnode.denoised_ts')]),

        # registration to MNI space
        (selectfiles, ants_registration, [('ants_affine', 'inputnode.ants_affine')]),
        (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')]),

        # FL added fullspectrum
        (denoise, ants_registration_full, [('outputnode.ts_fullspectrum', 'inputnode.denoised_ts')]),
        (selectfiles, ants_registration_full, [('ants_affine', 'inputnode.ants_affine')]),
        (selectfiles, ants_registration_full, [('ants_warp', 'inputnode.ants_warp')]),

        (ants_registration, smoothing, [('outputnode.ants_reg_ts', 'inputnode.ts_transformed')]),

        (smoothing, visualize, [('outputnode.ts_smoothed', 'inputnode.ts_transformed')]),

        ##all the output
        (moco, sink, [  # ('outputnode.epi_moco', 'realign.@realigned_ts'),
                        ('outputnode.par_moco', 'realign.@par'),
                        ('outputnode.rms_moco', 'realign.@rms'),
                        ('outputnode.mat_moco', 'realign.MAT.@mat'),
                        ('outputnode.epi_mean', 'realign.@mean'),
                        ('outputnode.rotplot', 'realign.plots.@rotplot'),
                        ('outputnode.transplot', 'realign.plots.@transplot'),
                        ('outputnode.dispplots', 'realign.plots.@dispplots'),
                        ('outputnode.tsnr_file', 'realign.@tsnr')]),
        (fmap_coreg, sink, [('outputnode.fmap', 'coregister.transforms2anat.@fmap'),
                            # ('outputnode.unwarpfield_epi2fmap', 'coregister.@unwarpfield_epi2fmap'),
                            ('outputnode.unwarped_mean_epi2fmap', 'coregister.@unwarped_mean_epi2fmap'),
                            ('outputnode.epi2fmap', 'coregister.@epi2fmap'),
                            # ('outputnode.shiftmap', 'coregister.@shiftmap'),
                            ('outputnode.fmap_fullwarp', 'coregister.transforms2anat.@fmap_fullwarp'),
                            ('outputnode.epi2anat', 'coregister.@epi2anat'),
                            ('outputnode.epi2anat_mat', 'coregister.transforms2anat.@epi2anat_mat'),
                            ('outputnode.epi2anat_dat', 'coregister.transforms2anat.@epi2anat_dat'),
                            ('outputnode.epi2anat_mincost', 'coregister.@epi2anat_mincost')
                            ]),

        (transform_ts, sink, [('outputnode.trans_ts_masked', 'coregister.@full_transform_ts'),
                              ('outputnode.trans_ts_mean', 'coregister.@full_transform_mean'),
                              ('outputnode.resamp_brain', 'coregister.@resamp_brain')]),

        (denoise, sink, [
            ('outputnode.wmcsf_mask', 'denoise.mask.@wmcsf_masks'),
            ('outputnode.combined_motion', 'denoise.artefact.@combined_motion'),
            ('outputnode.outlier_files', 'denoise.artefact.@outlier'),
            ('outputnode.intensity_files', 'denoise.artefact.@intensity'),
            ('outputnode.outlier_stats', 'denoise.artefact.@outlierstats'),
            ('outputnode.outlier_plots', 'denoise.artefact.@outlierplots'),
            ('outputnode.mc_regressor', 'denoise.regress.@mc_regressor'),
            ('outputnode.comp_regressor', 'denoise.regress.@comp_regressor'),
            ('outputnode.mc_F', 'denoise.regress.@mc_F'),
            ('outputnode.mc_pF', 'denoise.regress.@mc_pF'),
            ('outputnode.comp_F', 'denoise.regress.@comp_F'),
            ('outputnode.comp_pF', 'denoise.regress.@comp_pF'),
            ('outputnode.brain_mask_resamp', 'denoise.mask.@brain_resamp'),
            ('outputnode.brain_mask2epi', 'denoise.mask.@brain_mask2epi'),
            ('outputnode.normalized_file', 'denoise.@normalized'),
            # FL added fullspectrum
            ('outputnode.ts_fullspectrum', 'denoise.@ts_fullspectrum')
        ]),
        (ants_registration, sink, [('outputnode.ants_reg_ts', 'ants.@antsnormalized')]),
        (ants_registration_full, sink, [('outputnode.ants_reg_ts', 'ants.@antsnormalized_fullspectrum')]),
        (smoothing, sink, [('outputnode.ts_smoothed', '@smoothed.FWHM6')]),
    ])

    func_preproc.write_graph(dotfilename='func_preproc.dot', graph2use='colored', format='pdf', simple_form=True)
    func_preproc.run(plugin='CondorDAGMan', plugin_args={'initial_specs': 'request_memory = 1500'})
def create_lemon_resting(
    subject,
    working_dir,
    data_dir,
    freesurfer_dir,
    out_dir,
    vol_to_remove,
    TR,
    epi_resolution,
    highpass,
    lowpass,
    echo_space,
    te_diff,
    pe_dir,
    standard_brain,
    standard_brain_resampled,
    standard_brain_mask,
    standard_brain_mask_resampled,
    fwhm_smoothing,
):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type("NIFTI_GZ")
    # main workflow
    func_preproc = Workflow(name="lemon_resting")
    func_preproc.base_dir = working_dir
    func_preproc.config["execution"]["crashdump_dir"] = func_preproc.base_dir + "/crash_files"
    # select files
    templates = {
        "func": "func/EPI_t2.nii",
        "fmap_phase": "unwarp/B0_ph.nii",
        "fmap_mag": "unwarp/B0_mag.nii",
        "anat_head": "preprocessed/mod/anat/T1.nii.gz",  # either with mod or without
        "anat_brain": "preprocessed/mod/anat/brain.nii.gz",  # new version with brain_extraction from freesurfer  #T1_brain_brain.nii.gz',
        "brain_mask": "preprocessed/mod/anat/T1_brain_mask.nii.gz",  # T1_brain_brain_mask.nii.gz',
        "ants_affine": "preprocessed/mod/anat/transforms2mni/transform0GenericAffine.mat",
        "ants_warp": "preprocessed/mod/anat/transforms2mni/transform1Warp.nii.gz",
    }

    selectfiles = Node(nio.SelectFiles(templates, base_directory=data_dir), name="selectfiles")

    # node to remove first volumes
    remove_vol = Node(
        util.Function(input_names=["in_file", "t_min"], output_names=["out_file"], function=strip_rois_func),
        name="remove_vol",
    )
    remove_vol.inputs.t_min = vol_to_remove
    # workflow for motion correction
    moco = create_moco_pipeline()
    # workflow for fieldmap correction and coregistration
    fmap_coreg = create_fmap_coreg_pipeline()
    fmap_coreg.inputs.inputnode.fs_subjects_dir = freesurfer_dir
    fmap_coreg.inputs.inputnode.fs_subject_id = subject
    fmap_coreg.inputs.inputnode.echo_space = echo_space
    fmap_coreg.inputs.inputnode.te_diff = te_diff
    fmap_coreg.inputs.inputnode.pe_dir = pe_dir
    # workflow for applying transformations to timeseries
    transform_ts = create_transform_pipeline()
    transform_ts.inputs.inputnode.resolution = epi_resolution

    # workflow to convert signal into percent signal change
    normalize = create_normalize_pipeline()
    normalize.inputs.inputnode.tr = TR

    # workflow to transform timeseries to MNI
    ants_registration = create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref = standard_brain_resampled

    # workflow to smooth
    smoothing = create_smoothing_pipeline()
    smoothing.inputs.inputnode.fwhm = fwhm_smoothing

    # workflow to correct slice timing
    slicetiming = create_slice_timing_pipeline()

    # visualize registration results
    visualize = create_visualize_pipeline()
    visualize.inputs.inputnode.mni_template = standard_brain_resampled

    # sink to store files
    sink = Node(
        nio.DataSink(
            parameterization=False,
            base_directory=out_dir,
            substitutions=[
                ("fmap_phase_fslprepared", "fieldmap"),
                ("fieldmap_fslprepared_fieldmap_unmasked_vsm", "shiftmap"),
                ("plot.rest_coregistered", "outlier_plot"),
                ("filter_motion_comp_norm_compcor_art_dmotion", "nuissance_matrix"),
                ("rest_realigned.nii.gz_abs.rms", "rest_realigned_abs.rms"),
                ("rest_realigned.nii.gz.par", "rest_realigned.par"),
                ("rest_realigned.nii.gz_rel.rms", "rest_realigned_rel.rms"),
                ("rest_realigned.nii.gz_abs_disp", "abs_displacement_plot"),
                ("rest_realigned.nii.gz_rel_disp", "rel_displacment_plot"),
                ("art.rest_coregistered_outliers", "outliers"),
                ("global_intensity.rest_coregistered", "global_intensity"),
                ("norm.rest_coregistered", "composite_norm"),
                ("stats.rest_coregistered", "stats"),
                ("rest_denoised_bandpassed_norm.nii.gz", "rest_preprocessed.nii.gz"),
                ("rest_denoised_bandpassed_norm_trans.nii.gz", "rest_mni.nii.gz"),
                ("rest2anat_masked_st_norm_trans_smooth.nii", "rest_mni_smoothed.nii"),
            ],
        ),
        name="sink",
    )

    # connections
    func_preproc.connect(
        [
            # remove the first volumes
            (selectfiles, remove_vol, [("func", "in_file")]),
            # align volumes and motion correction
            (remove_vol, moco, [("out_file", "inputnode.epi")]),
            # prepare field map
            (
                selectfiles,
                fmap_coreg,
                [
                    ("fmap_phase", "inputnode.phase"),
                    ("fmap_mag", "inputnode.mag"),
                    ("anat_head", "inputnode.anat_head"),
                    ("anat_brain", "inputnode.anat_brain"),
                ],
            ),
            (moco, fmap_coreg, [("outputnode.epi_mean", "inputnode.epi_mean")]),
            (remove_vol, transform_ts, [("out_file", "inputnode.orig_ts")]),
            (selectfiles, transform_ts, [("anat_head", "inputnode.anat_head")]),
            (selectfiles, transform_ts, [("brain_mask", "inputnode.brain_mask")]),
            (moco, transform_ts, [("outputnode.mat_moco", "inputnode.mat_moco")]),
            (fmap_coreg, transform_ts, [("outputnode.fmap_fullwarp", "inputnode.fullwarp")]),
            ##add slice time correction after applying motion realignement + unwarping
            (transform_ts, slicetiming, [("outputnode.trans_ts_masked", "inputnode.ts")]),
            (slicetiming, normalize, [("outputnode.ts_slicetcorrected", "inputnode.epi_coreg")]),
            (normalize, ants_registration, [("outputnode.normalized_file", "inputnode.denoised_ts")]),
            # registration to MNI space
            (selectfiles, ants_registration, [("ants_affine", "inputnode.ants_affine")]),
            (selectfiles, ants_registration, [("ants_warp", "inputnode.ants_warp")]),
            (ants_registration, smoothing, [("outputnode.ants_reg_ts", "inputnode.ts_transformed")]),
            (smoothing, visualize, [("outputnode.ts_smoothed", "inputnode.ts_transformed")]),
            ##all the output
            (
                moco,
                sink,
                [  # ('outputnode.epi_moco', 'realign.@realigned_ts'),
                    ("outputnode.par_moco", "realign.@par"),
                    ("outputnode.rms_moco", "realign.@rms"),
                    ("outputnode.mat_moco", "realign.MAT.@mat"),
                    ("outputnode.epi_mean", "realign.@mean"),
                    ("outputnode.rotplot", "realign.plots.@rotplot"),
                    ("outputnode.transplot", "realign.plots.@transplot"),
                    ("outputnode.dispplots", "realign.plots.@dispplots"),
                    ("outputnode.tsnr_file", "realign.@tsnr"),
                ],
            ),
            (
                fmap_coreg,
                sink,
                [
                    ("outputnode.fmap", "coregister.transforms2anat.@fmap"),
                    # ('outputnode.unwarpfield_epi2fmap', 'coregister.@unwarpfield_epi2fmap'),
                    ("outputnode.unwarped_mean_epi2fmap", "coregister.@unwarped_mean_epi2fmap"),
                    ("outputnode.epi2fmap", "coregister.@epi2fmap"),
                    # ('outputnode.shiftmap', 'coregister.@shiftmap'),
                    ("outputnode.fmap_fullwarp", "coregister.transforms2anat.@fmap_fullwarp"),
                    ("outputnode.epi2anat", "coregister.@epi2anat"),
                    ("outputnode.epi2anat_mat", "coregister.transforms2anat.@epi2anat_mat"),
                    ("outputnode.epi2anat_dat", "coregister.transforms2anat.@epi2anat_dat"),
                    ("outputnode.epi2anat_mincost", "coregister.@epi2anat_mincost"),
                ],
            ),
            (
                transform_ts,
                sink,
                [  # ('outputnode.trans_ts', 'coregister.@full_transform_ts'),
                    ("outputnode.trans_ts_mean", "coregister.@full_transform_mean"),
                    ("outputnode.resamp_brain", "coregister.@resamp_brain"),
                ],
            ),
            (ants_registration, sink, [("outputnode.ants_reg_ts", "ants.@antsnormalized")]),
            (smoothing, sink, [("outputnode.ts_smoothed", "@smoothed.FWHM6")]),
        ]
    )

    # func_preproc.write_graph(dotfilename='func_preproc.dot', graph2use='colored', format='pdf', simple_form=True)
    func_preproc.run()
Exemplo n.º 10
0
def create_lemon_resting(subject, working_dir, data_dir, data_dir_WDR,
                         freesurfer_dir, out_dir, vol_to_remove, TR,
                         epi_resolution, highpass, lowpass, echo_space,
                         te_diff, pe_dir, standard_brain,
                         standard_brain_resampled, standard_brain_mask,
                         standard_brain_mask_resampled, fwhm_smoothing):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    func_preproc = Workflow(name='lemon_resting')
    func_preproc.base_dir = working_dir
    func_preproc.config['execution'][
        'crashdump_dir'] = func_preproc.base_dir + "/crash_files"
    # select files

    # select files
    templates = {
        'anat_brain': 'preprocessed/mod/anat/brain.nii.gz',
        'brain_mask': 'preprocessed/mod/anat/T1_brain_mask.nii.gz',
        'ants_affine':
        'preprocessed/mod/anat/transforms2mni/transform0GenericAffine.mat',
        'ants_warp':
        'preprocessed/mod/anat/transforms2mni/transform1Warp.nii.gz',
    }

    templates_WDR = {
        'par_moco':
        'lemon_resting/motion_correction/mcflirt/rest_realigned.nii.gz.par',
        'trans_ts':
        'lemon_resting/transform_timeseries/merge/rest2anat.nii.gz',
        'epi2anat_dat':
        'lemon_resting/fmap_coreg/bbregister/rest2anat.dat',
        'unwarped_mean_epi2fmap':
        'lemon_resting/fmap_coreg/applywarp0/rest_mean2fmap_unwarped.nii.gz',
    }

    selectfiles = Node(nio.SelectFiles(templates, base_directory=data_dir),
                       name="selectfiles")
    selectfiles_WDR = Node(nio.SelectFiles(templates_WDR,
                                           base_directory=data_dir_WDR),
                           name="selectfiles_WDR")

    # workflow to denoise timeseries
    denoise = create_denoise_pipeline()
    denoise.inputs.inputnode.highpass_sigma = 1. / (2 * TR * highpass)
    denoise.inputs.inputnode.lowpass_sigma = 1. / (2 * TR * lowpass)
    #https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=ind1205&L=FSL&P=R57592&1=FSL&9=A&I=-3&J=on&d=No+Match%3BMatch%3BMatches&z=4
    denoise.inputs.inputnode.tr = TR

    #workflow to transform timeseries to MNI
    ants_registration = create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref = standard_brain_resampled

    #workflow to smooth
    smoothing = create_smoothing_pipeline()
    smoothing.inputs.inputnode.fwhm = fwhm_smoothing

    #workflow to slice time in the end as a try
    slicetiming = create_slice_timing_pipeline()

    #visualize registration results
    visualize = create_visualize_pipeline()
    visualize.inputs.inputnode.mni_template = standard_brain_resampled

    #sink to store files
    sink = Node(nio.DataSink(
        parameterization=False,
        base_directory=out_dir,
        substitutions=[
            ('fmap_phase_fslprepared', 'fieldmap'),
            ('fieldmap_fslprepared_fieldmap_unmasked_vsm', 'shiftmap'),
            ('plot.rest_coregistered', 'outlier_plot'),
            ('filter_motion_comp_norm_compcor_art_dmotion',
             'nuissance_matrix'),
            ('rest_realigned.nii.gz_abs.rms', 'rest_realigned_abs.rms'),
            ('rest_realigned.nii.gz.par', 'rest_realigned.par'),
            ('rest_realigned.nii.gz_rel.rms', 'rest_realigned_rel.rms'),
            ('rest_realigned.nii.gz_abs_disp', 'abs_displacement_plot'),
            ('rest_realigned.nii.gz_rel_disp', 'rel_displacment_plot'),
            ('art.rest_coregistered_outliers', 'outliers'),
            ('global_intensity.rest_coregistered', 'global_intensity'),
            ('norm.rest_coregistered', 'composite_norm'),
            ('stats.rest_coregistered', 'stats'),
            ('rest_denoised_bandpassed_norm.nii.gz',
             'rest_preprocessed_nativespace.nii.gz'),
            ('rest_denoised_bandpassed_norm_trans.nii.gz',
             'rest_mni_unsmoothed.nii.gz'),
            ('rest_denoised_bandpassed_norm_trans_smooth.nii',
             'rest_mni_smoothed.nii')
        ]),
                name='sink')

    # connections
    func_preproc.connect([

        #correct slicetiming
        (selectfiles_WDR, slicetiming, [('trans_ts', 'inputnode.ts')]),
        (slicetiming, denoise, [('outputnode.ts_slicetcorrected',
                                 'inputnode.epi_coreg')]),

        #denoise data
        (selectfiles, denoise, [('brain_mask', 'inputnode.brain_mask'),
                                ('anat_brain', 'inputnode.anat_brain')]),
        (selectfiles_WDR, denoise, [('par_moco', 'inputnode.moco_par')]),
        (selectfiles_WDR, denoise, [('epi2anat_dat', 'inputnode.epi2anat_dat'),
                                    ('unwarped_mean_epi2fmap',
                                     'inputnode.unwarped_mean')]),
        (denoise, ants_registration, [('outputnode.normalized_file',
                                       'inputnode.denoised_ts')]),

        #registration to MNI space
        (selectfiles, ants_registration, [('ants_affine',
                                           'inputnode.ants_affine')]),
        (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')
                                          ]),
        (ants_registration, smoothing, [('outputnode.ants_reg_ts',
                                         'inputnode.ts_transformed')]),
        (smoothing, visualize, [('outputnode.ts_smoothed',
                                 'inputnode.ts_transformed')]),

        ##all the output
        (denoise, sink,
         [('outputnode.wmcsf_mask', 'denoise.mask.@wmcsf_masks'),
          ('outputnode.combined_motion', 'denoise.artefact.@combined_motion'),
          ('outputnode.outlier_files', 'denoise.artefact.@outlier'),
          ('outputnode.intensity_files', 'denoise.artefact.@intensity'),
          ('outputnode.outlier_stats', 'denoise.artefact.@outlierstats'),
          ('outputnode.outlier_plots', 'denoise.artefact.@outlierplots'),
          ('outputnode.mc_regressor', 'denoise.regress.@mc_regressor'),
          ('outputnode.comp_regressor', 'denoise.regress.@comp_regressor'),
          ('outputnode.mc_F', 'denoise.regress.@mc_F'),
          ('outputnode.mc_pF', 'denoise.regress.@mc_pF'),
          ('outputnode.comp_F', 'denoise.regress.@comp_F'),
          ('outputnode.comp_pF', 'denoise.regress.@comp_pF'),
          ('outputnode.brain_mask_resamp', 'denoise.mask.@brain_resamp'),
          ('outputnode.brain_mask2epi', 'denoise.mask.@brain_mask2epi'),
          ('outputnode.normalized_file', 'denoise.@normalized')]),
        (ants_registration, sink, [('outputnode.ants_reg_ts',
                                    'ants.@antsnormalized')]),
        (smoothing, sink, [('outputnode.ts_smoothed', '@smoothed.FWHM6')]),
    ])

    #func_preproc.write_graph(dotfilename='func_preproc.dot', graph2use='colored', format='pdf', simple_form=True)
    func_preproc.run()
    # plugin='MultiProc'plugin='MultiProc'plugin='CondorDAGMan')
    #func_preproc.run()plugin='CondorDAGMan'plugin='CondorDAGMan'plugin='CondorDAGMan'plugin='CondorDAGMan'


#plugin='CondorDAGMan'
Exemplo n.º 11
0
def create_resting(subject, working_dir, data_dir, freesurfer_dir, out_dir,
                         vol_to_remove, TR, epi_resolution, highpass, lowpass,
                         echo_space, pe_dir, standard_brain, standard_brain_resampled, standard_brain_mask,
                         standard_brain_mask_resampled, fwhm_smoothing):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    func_preproc = Workflow(name='lemon_resting')
    func_preproc.base_dir = working_dir
    func_preproc.config['execution']['crashdump_dir'] = func_preproc.base_dir + "/crash_files"
    # select files
    templates = {'func': 'raw/{subject}/func/EPI_t2.nii',
                 'ap': 'raw/{subject}/topup/se.nii',
                 'pa': 'raw/{subject}/topup/seinv_ph.nii',
                 'anat_head': 'preprocessing/preprocessed/{subject}/structural/T1.nii.gz',  
                 'anat_brain': 'preprocessing/preprocessed/{subject}/structural/brain.nii.gz',
                 'brain_mask': 'preprocessing/preprocessed/{subject}/structural/T1_brain_mask.nii.gz',  
                 'ants_affine': 'preprocessing/preprocessed/{subject}/structural/transforms2mni/transform0GenericAffine.mat',
                 'ants_warp': 'preprocessing/preprocessed/{subject}/structural/transforms2mni/transform1Warp.nii.gz'
                 }

    selectfiles = Node(nio.SelectFiles(templates,
                                       base_directory=data_dir),
                       name="selectfiles")
    selectfiles.inputs.subject = subject


    # node to remove first volumes
    remove_vol = Node(util.Function(input_names=['in_file', 't_min'],
                                    output_names=["out_file"],
                                    function=strip_rois_func),
                      name='remove_vol')
    remove_vol.inputs.t_min = vol_to_remove
    # workflow for motion correction
    moco = create_moco_pipeline()

    # workflow for fieldmap correction and coregistration
    topup_coreg = create_topup_coreg_pipeline()
    topup_coreg.inputs.inputnode.fs_subjects_dir = freesurfer_dir
    topup_coreg.inputs.inputnode.fs_subject_id = subject
    topup_coreg.inputs.inputnode.echo_space = echo_space
    topup_coreg.inputs.inputnode.pe_dir = pe_dir

    # workflow for applying transformations to timeseries
    transform_ts = create_transform_pipeline()
    transform_ts.inputs.inputnode.resolution = epi_resolution


    # workflow to denoise timeseries
    denoise = create_denoise_pipeline()
    denoise.inputs.inputnode.highpass_sigma = 1. / (2 * TR * highpass)
    denoise.inputs.inputnode.lowpass_sigma = 1. / (2 * TR * lowpass)
    # https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=ind1205&L=FSL&P=R57592&1=FSL&9=A&I=-3&J=on&d=No+Match%3BMatch%3BMatches&z=4
    denoise.inputs.inputnode.tr = TR

    # workflow to transform timeseries to MNI
    ants_registration = create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref = standard_brain
    ants_registration.inputs.inputnode.tr_sec = TR

    # FL added fullspectrum
    # workflow to transform fullspectrum timeseries to MNI
    ants_registration_full = create_ants_registration_pipeline('ants_registration_full')
    ants_registration_full.inputs.inputnode.ref = standard_brain
    ants_registration_full.inputs.inputnode.tr_sec = TR

    # workflow to smooth
    smoothing = create_smoothing_pipeline()
    smoothing.inputs.inputnode.fwhm = fwhm_smoothing

    # visualize registration results
    visualize = create_visualize_pipeline()
    visualize.inputs.inputnode.mni_template = standard_brain



    # sink to store files
    sink = Node(nio.DataSink(parameterization=False,
                             base_directory=out_dir,
                             substitutions=[('fmap_phase_fslprepared', 'fieldmap'),
                                            ('fieldmap_fslprepared_fieldmap_unmasked_vsm', 'shiftmap'),
                                            ('plot.rest_coregistered', 'outlier_plot'),
                                            ('filter_motion_comp_norm_compcor_art_dmotion', 'nuissance_matrix'),
                                            ('rest_realigned.nii.gz_abs.rms', 'rest_realigned_abs.rms'),
                                            ('rest_realigned.nii.gz.par', 'rest_realigned.par'),
                                            ('rest_realigned.nii.gz_rel.rms', 'rest_realigned_rel.rms'),
                                            ('rest_realigned.nii.gz_abs_disp', 'abs_displacement_plot'),
                                            ('rest_realigned.nii.gz_rel_disp', 'rel_displacment_plot'),
                                            ('art.rest_coregistered_outliers', 'outliers'),
                                            ('global_intensity.rest_coregistered', 'global_intensity'),
                                            ('norm.rest_coregistered', 'composite_norm'),
                                            ('stats.rest_coregistered', 'stats'),
                                            ('rest_denoised_bandpassed_norm.nii.gz',
                                             'rest_preprocessed_nativespace.nii.gz'),
                                            ('rest_denoised_bandpassed_norm_trans.nii.gz',
                                             'rest_mni_unsmoothed.nii.gz'),
                                            ('rest_denoised_bandpassed_norm_trans_smooth.nii',
                                             'rest_mni_smoothed.nii'),
                                            # FL added
                                            ('rest2anat_masked.nii.gz', 'rest_coregistered_nativespace.nii.gz'),
                                            ('rest2anat_denoised.nii.gz',
                                             'rest_preprocessed_nativespace_fullspectrum.nii.gz'),
                                            ('rest2anat_denoised_trans.nii.gz',
                                             'rest_mni_unsmoothed_fullspectrum.nii.gz')
                                            ]),
                name='sink')


    # connections
    func_preproc.connect([
        # remove the first volumes
        (selectfiles, remove_vol, [('func', 'in_file')]),

        # align volumes and motion correction
        (remove_vol, moco, [('out_file', 'inputnode.epi')]),

        # prepare field map
        (selectfiles, topup_coreg, [('ap', 'inputnode.ap'),
                                   ('pa', 'inputnode.pa'),
                                   ('anat_head', 'inputnode.anat_head'),
                                   ('anat_brain', 'inputnode.anat_brain')
                                   ]),
        (moco, topup_coreg, [('outputnode.epi_mean', 'inputnode.epi_mean')]),

        # transform timeseries
        (remove_vol, transform_ts, [('out_file', 'inputnode.orig_ts')]),
        (selectfiles, transform_ts, [('anat_head', 'inputnode.anat_head')]),
        (selectfiles, transform_ts, [('brain_mask', 'inputnode.brain_mask')]),
        (moco, transform_ts, [('outputnode.mat_moco', 'inputnode.mat_moco')]),
        (topup_coreg, transform_ts, [('outputnode.fmap_fullwarp', 'inputnode.fullwarp')]),

        # correct slicetiming
        # FIXME slice timing?
        # (transform_ts, slicetiming, [('outputnode.trans_ts_masked', 'inputnode.ts')]),
        # (slicetiming, denoise, [('outputnode.ts_slicetcorrected', 'inputnode.epi_coreg')]),
        (transform_ts, denoise, [('outputnode.trans_ts_masked', 'inputnode.epi_coreg')]),

        # denoise data
        (selectfiles, denoise, [('brain_mask', 'inputnode.brain_mask'),
                                ('anat_brain', 'inputnode.anat_brain')]),
        (moco, denoise, [('outputnode.par_moco', 'inputnode.moco_par')]),
        (topup_coreg, denoise, [('outputnode.epi2anat_dat', 'inputnode.epi2anat_dat'),
                               ('outputnode.unwarped_mean_epi2fmap', 'inputnode.unwarped_mean')]),
        (denoise, ants_registration, [('outputnode.normalized_file', 'inputnode.denoised_ts')]),

        # registration to MNI space
        (selectfiles, ants_registration, [('ants_affine', 'inputnode.ants_affine')]),
        (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')]),

        # FL added fullspectrum
        (denoise, ants_registration_full, [('outputnode.ts_fullspectrum', 'inputnode.denoised_ts')]),
        (selectfiles, ants_registration_full, [('ants_affine', 'inputnode.ants_affine')]),
        (selectfiles, ants_registration_full, [('ants_warp', 'inputnode.ants_warp')]),

        (ants_registration, smoothing, [('outputnode.ants_reg_ts', 'inputnode.ts_transformed')]),

        (smoothing, visualize, [('outputnode.ts_smoothed', 'inputnode.ts_transformed')]),

        ##all the output
        (moco, sink, [  # ('outputnode.epi_moco', 'realign.@realigned_ts'),
                        ('outputnode.par_moco', 'realign.@par'),
                        ('outputnode.rms_moco', 'realign.@rms'),
                        ('outputnode.mat_moco', 'realign.MAT.@mat'),
                        ('outputnode.epi_mean', 'realign.@mean'),
                        ('outputnode.rotplot', 'realign.plots.@rotplot'),
                        ('outputnode.transplot', 'realign.plots.@transplot'),
                        ('outputnode.dispplots', 'realign.plots.@dispplots'),
                        ('outputnode.tsnr_file', 'realign.@tsnr')]),
        (topup_coreg, sink, [('outputnode.fmap', 'coregister.transforms2anat.@fmap'),
                            # ('outputnode.unwarpfield_epi2fmap', 'coregister.@unwarpfield_epi2fmap'),
                            ('outputnode.unwarped_mean_epi2fmap', 'coregister.@unwarped_mean_epi2fmap'),
                            ('outputnode.epi2fmap', 'coregister.@epi2fmap'),
                            # ('outputnode.shiftmap', 'coregister.@shiftmap'),
                            ('outputnode.fmap_fullwarp', 'coregister.transforms2anat.@fmap_fullwarp'),
                            ('outputnode.epi2anat', 'coregister.@epi2anat'),
                            ('outputnode.epi2anat_mat', 'coregister.transforms2anat.@epi2anat_mat'),
                            ('outputnode.epi2anat_dat', 'coregister.transforms2anat.@epi2anat_dat'),
                            ('outputnode.epi2anat_mincost', 'coregister.@epi2anat_mincost')
                            ]),

        (transform_ts, sink, [('outputnode.trans_ts_masked', 'coregister.@full_transform_ts'),
                              ('outputnode.trans_ts_mean', 'coregister.@full_transform_mean'),
                              ('outputnode.resamp_brain', 'coregister.@resamp_brain')]),

        (denoise, sink, [
            ('outputnode.wmcsf_mask', 'denoise.mask.@wmcsf_masks'),
            ('outputnode.combined_motion', 'denoise.artefact.@combined_motion'),
            ('outputnode.outlier_files', 'denoise.artefact.@outlier'),
            ('outputnode.intensity_files', 'denoise.artefact.@intensity'),
            ('outputnode.outlier_stats', 'denoise.artefact.@outlierstats'),
            ('outputnode.outlier_plots', 'denoise.artefact.@outlierplots'),
            ('outputnode.mc_regressor', 'denoise.regress.@mc_regressor'),
            ('outputnode.comp_regressor', 'denoise.regress.@comp_regressor'),
            ('outputnode.mc_F', 'denoise.regress.@mc_F'),
            ('outputnode.mc_pF', 'denoise.regress.@mc_pF'),
            ('outputnode.comp_F', 'denoise.regress.@comp_F'),
            ('outputnode.comp_pF', 'denoise.regress.@comp_pF'),
            ('outputnode.brain_mask_resamp', 'denoise.mask.@brain_resamp'),
            ('outputnode.brain_mask2epi', 'denoise.mask.@brain_mask2epi'),
            ('outputnode.normalized_file', 'denoise.@normalized'),
            # FL added fullspectrum
            ('outputnode.ts_fullspectrum', 'denoise.@ts_fullspectrum')
        ]),
        (ants_registration, sink, [('outputnode.ants_reg_ts', 'ants.@antsnormalized')]),
        (ants_registration_full, sink, [('outputnode.ants_reg_ts', 'ants.@antsnormalized_fullspectrum')]),
        (smoothing, sink, [('outputnode.ts_smoothed', '@smoothed.FWHM6')]),
    ])

    func_preproc.write_graph(dotfilename='func_preproc.dot', graph2use='colored', format='pdf', simple_form=True)
    func_preproc.run(plugin='MultiProc')