Esempio n. 1
0
def filterFSL(input_file, highpass, tempMean):
    outputSFRGR = os.path.join(
        os.path.dirname(input_file),
        os.path.basename(input_file).split('SRGR')[0]) + 'SFRGR.nii.gz'
    myHP = fsl.TemporalFilter(in_file=input_file,
                              highpass_sigma=highpass,
                              args='-add ' + tempMean,
                              out_file=outputSFRGR)
    print(myHP.cmdline)
    myHP.run()
    input_file = outputSFRGR
    output_file = os.path.join(
        os.path.dirname(input_file),
        os.path.basename(input_file).split('.')[0]) + '_thres_mask.nii.gz'
    #input_file = getMean(input_file,'HPmean')
    thres = fsl.Threshold(in_file=input_file,
                          thresh=17,
                          out_file=output_file,
                          output_datatype='float',
                          use_robust_range=True,
                          args='-Tmean -bin')
    print(thres.cmdline)
    thres.run()

    return outputSFRGR
Esempio n. 2
0
def test_TemporalFilter(tmp_path):
    seed(a=0x4d3c732f)

    array = np.random.rand(10, 10, 10, 100) * 1000 + 10000
    img = nib.Nifti1Image(array, np.eye(4))

    os.chdir(str(tmp_path))

    in_file = "img.nii.gz"
    nib.save(img, in_file)

    instance = TemporalFilter()
    instance.inputs.in_file = in_file
    instance.inputs.lowpass_sigma = 12
    instance.inputs.highpass_sigma = 125
    result = instance.run()

    r0 = nib.load(result.outputs.out_file).get_fdata()

    instance = fsl.TemporalFilter()
    instance.inputs.in_file = in_file
    instance.inputs.lowpass_sigma = 12
    instance.inputs.highpass_sigma = 125
    result = instance.run()

    r1 = nib.load(result.outputs.out_file).get_fdata()
    assert np.allclose(r0, r1)
Esempio n. 3
0
def test_TemporalFilter(tmp_path):
    seed(a=0x4D3C732F)

    array = np.random.rand(10, 10, 10, 100) * 1000 + 10000
    img = nib.Nifti1Image(array, np.eye(4))
    assert isinstance(img.header, nib.Nifti1Header)
    img.header.set_data_dtype(np.float64)

    os.chdir(str(tmp_path))

    in_file = "img.nii.gz"
    nib.save(img, in_file)

    instance = TemporalFilter()
    instance.inputs.in_file = in_file
    instance.inputs.lowpass_sigma = 12
    instance.inputs.highpass_sigma = 125
    result = instance.run()
    assert result.outputs is not None

    r0 = nib.load(result.outputs.out_file).get_fdata()

    instance = fsl.TemporalFilter()
    instance.inputs.in_file = in_file
    instance.inputs.lowpass_sigma = 12
    instance.inputs.highpass_sigma = 125
    result = instance.run()
    assert result.outputs is not None

    r1 = nib.load(result.outputs.out_file).get_fdata()
    assert np.allclose(r0, r1)
Esempio n. 4
0
def mod_filter(in_file, algorithm, lowpass_freq, highpass_freq, tr):
    import os
    from nipype.utils.filemanip import fname_presuffix
    if algorithm == 'fsl':
        import nipype.interfaces.fsl as fsl
        filter = fsl.TemporalFilter()
        filter.inputs.in_file = in_file
        if highpass_freq < 0:
            filter.inputs.highpass_sigma = -1
        else:
            filter.inputs.highpass_sigma = 1 / (2 * tr * highpass_freq)
        if lowpass_freq < 0:
            filter.inputs.lowpass_sigma = -1
        else:
            filter.inputs.lowpass_sigma = 1 / (2 * tr * lowpass_freq)
        res = filter.run()
        out_file = res.outputs.out_file
    else:
        import nitime.fmri.io as io
        from nitime.analysis import FilterAnalyzer
        import nibabel as nib
        import numpy as np

        T = io.time_series_from_file(in_file, TR=tr)
        if highpass_freq < 0:
            highpass_freq = 0
        if lowpass_freq < 0:
            lowpass_freq = None
        filt_order = np.floor(T.shape[3] / 3)
        if filt_order % 2 == 1:
            filt_order -= 1
        F = FilterAnalyzer(T,
                           ub=lowpass_freq,
                           lb=highpass_freq,
                           filt_order=filt_order)
        if algorithm == 'IIR':
            Filtered_data = F.iir.data
            suffix = '_iir_filt'
        elif algorithm == 'Boxcar':
            Filtered_data = F.filtered_boxcar.data
            suffix = '_boxcar_filt'
        elif algorithm == 'Fourier':
            Filtered_data = F.filtered_fourier.data
            suffix = '_fourier_filt'
        elif algorithm == 'FIR':
            Filtered_data = F.fir.data
            suffix = '_fir_filt'
        else:
            raise ValueError('Unknown Nitime filtering algorithm: %s' %
                             algorithm)

        out_file = fname_presuffix(in_file, suffix=suffix, newpath=os.getcwd())

        out_img = nib.Nifti1Image(Filtered_data,
                                  nib.load(in_file).get_affine())
        out_img.to_filename(out_file)

    return out_file
Esempio n. 5
0
def create_filtering_workflow(name="filter",
                              hpf_cutoff=128,
                              TR=2,
                              output_name="timeseries"):
    """Scale and high-pass filter the timeseries."""
    inputnode = Node(IdentityInterface(["timeseries", "mask_file"]), "inputs")

    # Grand-median scale within the brain mask
    scale = MapNode(ScaleTimeseries(statistic="median", target=10000),
                    ["in_file", "mask_file"], "scale")

    # Gaussian running-line filter
    if hpf_cutoff is None:
        hpf_sigma = -1
    else:
        hpf_sigma = (hpf_cutoff / 2.0) / TR
    filter = MapNode(fsl.TemporalFilter(highpass_sigma=hpf_sigma), "in_file",
                     "filter")

    # Possibly replace the mean
    # (In later versions of FSL, the highpass filter removes the
    # mean component. Put it back, but be flexible so this isn't
    # broken on older versions of FSL).
    replacemean = MapNode(ReplaceMean(output_name=output_name),
                          ["orig_file", "filtered_file"], "replacemean")

    # Compute a final mean functional volume
    meanfunc = MapNode(fsl.MeanImage(out_file="mean_func.nii.gz"), "in_file",
                       "meanfunc")

    outputnode = Node(IdentityInterface(["timeseries", "mean_file"]),
                      "outputs")

    filtering = Workflow(name)
    filtering.connect([
        (inputnode, scale, [("timeseries", "in_file"),
                            ("mask_file", "mask_file")]),
        (scale, filter, [("out_file", "in_file")]),
        (scale, replacemean, [("out_file", "orig_file")]),
        (filter, replacemean, [("out_file", "filtered_file")]),
        (replacemean, meanfunc, [("out_file", "in_file")]),
        (replacemean, outputnode, [("out_file", "timeseries")]),
        (meanfunc, outputnode, [("out_file", "mean_file")]),
    ])

    return filtering
Esempio n. 6
0
def create_denoise_pipeline(name='denoise'):
    # workflow
    denoise = Workflow(name='denoise')
    # Define nodes
    inputnode = Node(interface=util.IdentityInterface(fields=[
        'anat_brain', 'brain_mask', 'epi_denoised', 'highpass_sigma',
        'lowpass_sigma', 'tr'
    ]),
                     name='inputnode')
    outputnode = Node(
        interface=util.IdentityInterface(fields=[  # FL added fullspectrum
            'normalized_file'
        ]),
        name='outputnode')

    # bandpass filter denoised file
    bandpass_filter = Node(
        fsl.TemporalFilter(out_file='rest_denoised_bandpassed.nii.gz'),
        name='bandpass_filter')
    bandpass_filter.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([
        (inputnode, bandpass_filter, [('highpass_sigma', 'highpass_sigma'),
                                      ('lowpass_sigma', 'lowpass_sigma')]),
        # (filter2, bandpass_filter, [('out_res', 'in_file')]),
        # (filter2, outputnode, [('out_res', 'ts_fullspectrum')]),
        (inputnode, bandpass_filter, [('epi_denoised', 'in_file')])
    ])
    # time-normalize scans
    normalize_time = Node(util.Function(input_names=['in_file', 'tr'],
                                        output_names=['out_file'],
                                        function=time_normalizer),
                          name='normalize_time')
    normalize_time.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([
        (inputnode, normalize_time, [('tr', 'tr')]),
        (bandpass_filter, normalize_time, [('out_file', 'in_file')]),
        (normalize_time, outputnode, [('out_file', 'normalized_file')])
    ])
    return denoise
Esempio n. 7
0
def create_resting_preproc(name='restpreproc'):
    """Create a "resting" time series preprocessing workflow

    The noise removal is based on Behzadi et al. (2007)

    Parameters
    ----------

    name : name of workflow (default: restpreproc)

    Inputs::

        inputspec.func : functional run (filename or list of filenames)

    Outputs::

        outputspec.noise_mask_file : voxels used for PCA to derive noise components
        outputspec.filtered_file : bandpass filtered and noise-reduced time series

    Example
    -------

    >>> TR = 3.0
    >>> wf = create_resting_preproc()
    >>> wf.inputs.inputspec.func = 'f3.nii'
    >>> wf.inputs.inputspec.num_noise_components = 6
    >>> wf.inputs.inputspec.highpass_sigma = 100/(2*TR)
    >>> wf.inputs.inputspec.lowpass_sigma = 12.5/(2*TR)
    >>> wf.run() # doctest: +SKIP

    """

    restpreproc = pe.Workflow(name=name)

    # Define nodes
    inputnode = pe.Node(interface=util.IdentityInterface(fields=[
        'func', 'num_noise_components', 'highpass_sigma', 'lowpass_sigma'
    ]),
                        name='inputspec')
    outputnode = pe.Node(interface=util.IdentityInterface(fields=[
        'noise_mask_file', 'filtered_file', 'motion_rms_files',
        'motion_par_file', 'realigned_file', 'mask_file', 'outlier_files',
        'intensity_files', 'outlier_plots'
    ]),
                         name='outputspec')
    slicetimer = pe.Node(fsl.SliceTimer(), name='slicetimer')
    realigner = create_realign_flow()

    art_detector = pe.Node(ArtifactDetect(), name='art_detector')
    art_detector.inputs.parameter_source = 'FSL'
    art_detector.inputs.mask_type = 'spm_global'
    art_detector.inputs.global_threshold = .5
    art_detector.inputs.norm_threshold = .6
    art_detector.inputs.use_differences = [True,
                                           True]  ## [Movement, Intensity]
    art_detector.inputs.zintensity_threshold = 3
    art_detector.inputs.intersect_mask = True
    '''Mask smoother node, added by Pablo Polosecki to use EPI mask'''
    mask_smoother = pe.Node(util.Function(input_names=['vol_in'],
                                          output_names=['out_vol'],
                                          function=morph_open_close),
                            name='mask_smoother')
    tsnr = pe.Node(TSNR(regress_poly=2), name='tsnr')
    getthresh = pe.Node(interface=fsl.ImageStats(op_string='-k %s -p 98'),
                        name='getthreshold')
    threshold_stddev = pe.Node(fsl.Threshold(), name='threshold')
    ''' Mask conjunction, to limit noisy voxels to those inside brain mask'''
    conj_masker = pe.Node(fsl.BinaryMaths(operation='mul'), name='conj_masker')

    compcor = pe.Node(util.Function(
        input_names=['realigned_file', 'noise_mask_file', 'num_components'],
        output_names=['noise_components'],
        function=extract_noise_components),
                      name='compcorr')
    #   cat_regressors = pe.Node(util.Function(input_names=['file1',
    #                                                       'file2'],
    #                                          output_names=['out_fn'],
    #                                          function=concatetante_reg_files),
    #                            name='cat_regressors')
    remove_noise = pe.Node(fsl.FilterRegressor(filter_all=True),
                           name='remove_noise')
    bandpass_filter = pe.Node(fsl.TemporalFilter(), name='bandpass_filter')

    # Define connections
    restpreproc.connect(inputnode, 'func', slicetimer, 'in_file')
    restpreproc.connect(slicetimer, 'slice_time_corrected_file', realigner,
                        'inputspec.func')
    restpreproc.connect(realigner, 'outputspec.realigned_file', tsnr,
                        'in_file')
    restpreproc.connect(tsnr, 'stddev_file', threshold_stddev, 'in_file')
    restpreproc.connect(tsnr, 'stddev_file', getthresh, 'in_file')
    restpreproc.connect(mask_smoother, 'out_vol', getthresh, 'mask_file')
    restpreproc.connect(getthresh, 'out_stat', threshold_stddev, 'thresh')
    restpreproc.connect(realigner, 'outputspec.realigned_file', compcor,
                        'realigned_file')
    restpreproc.connect(inputnode, 'num_noise_components', compcor,
                        'num_components')
    restpreproc.connect(tsnr, 'detrended_file', remove_noise, 'in_file')
    # Combiinng compcorr with motion regressors:
    #restpreproc.connect(compcor, 'noise_components',
    #                    cat_regressors, 'file1')
    #restpreproc.connect(realigner, 'outputspec.par_file',
    #                    cat_regressors, 'file2')
    #restpreproc.connect(cat_regressors, 'out_fn',
    #                    remove_noise, 'design_file')
    restpreproc.connect(compcor, 'noise_components', remove_noise,
                        'design_file')
    restpreproc.connect(inputnode, 'highpass_sigma', bandpass_filter,
                        'highpass_sigma')
    restpreproc.connect(inputnode, 'lowpass_sigma', bandpass_filter,
                        'lowpass_sigma')
    restpreproc.connect(remove_noise, 'out_file', bandpass_filter, 'in_file')
    restpreproc.connect(conj_masker, 'out_file', outputnode, 'noise_mask_file')
    restpreproc.connect(bandpass_filter, 'out_file', outputnode,
                        'filtered_file')
    restpreproc.connect(realigner, 'outputspec.rms_files', outputnode,
                        'motion_rms_files')
    restpreproc.connect(realigner, 'outputspec.par_file', outputnode,
                        'motion_par_file')
    restpreproc.connect(realigner, 'outputspec.realigned_file', outputnode,
                        'realigned_file')
    restpreproc.connect(realigner, 'outputspec.realigned_file', art_detector,
                        'realigned_files')
    restpreproc.connect(realigner, 'outputspec.par_file', art_detector,
                        'realignment_parameters')
    restpreproc.connect(art_detector, 'mask_files', mask_smoother, 'vol_in')
    restpreproc.connect(mask_smoother, 'out_vol', outputnode, 'mask_file')
    restpreproc.connect(art_detector, 'outlier_files', outputnode,
                        'outlier_files')
    restpreproc.connect(art_detector, 'intensity_files', outputnode,
                        'intensity_files')
    #restpreproc.connect(art_detector, 'plot_files',
    #                    outputnode, 'outlier_plots')
    restpreproc.connect(mask_smoother, 'out_vol', conj_masker, 'in_file')
    restpreproc.connect(threshold_stddev, 'out_file', conj_masker,
                        'operand_file')
    restpreproc.connect(conj_masker, 'out_file', compcor, 'noise_mask_file')
    return restpreproc
Esempio n. 8
0
# ========================================================================================================
# In[17]:

# Global Intensity Normalization by multiplying by the scaling value
# the grand-mean intensity normalisation factor ( to give a median brain intensity of 10000 )
# grand mean scaling
Intensity_Normalization = Node(fsl.BinaryMaths(),
                               name='Intensity_Normalization')
Intensity_Normalization.inputs.operation = 'mul'

# ========================================================================================================
# In[18]:

#   fslmaths ${folder}_mcf_2highres_intnorm -bptf 25 -1 -add tempMean ${folder}_mcf_2highres_tempfilt;
# sigma[vol] = filter_width[secs]/(2*TR[secs])
high_pass_filter = Node(fsl.TemporalFilter(), name='high_pass_filter')
high_pass_filter.inputs.highpass_sigma = 22.5  # 90s / (2*2(TR))
# ========================================================================================================
# In[19]

# Get the mean image
Get_Mean_Image = Node(fsl.MeanImage(), name='Get_Mean_Image')
Get_Mean_Image.inputs.dimension = 'T'

# Add the mean image to the filtered image
Add_Mean_Image = Node(fsl.BinaryMaths(), name='Add_Mean_Image')
Add_Mean_Image.inputs.operation = 'add'

# ========================================================================================================
# In[20]:
Scale_Median_Intensity = Node(name = 'Scale_Median_Intensity',
                      interface = Function(input_names = ['median_intensity'],
                                           output_names = ['scaling'],
                                           function = Scale_Median_Intensity))

#-----------------------------------------------------------------------------------------------------
#Global Intensity Normalization by multiplying by the scaling value
#the grand-mean intensity normalisation factor ( to give a median brain intensity of 10000 )
#grand mean scaling
Intensity_Normalization = Node(fsl.BinaryMaths(), name = 'Intensity_Normalization')
Intensity_Normalization.inputs.operation = 'mul'


#-----------------------------------------------------------------------------------------------------
#   fslmaths ${folder}_mcf_2highres_intnorm -bptf 25 -1 -add tempMean ${folder}_mcf_2highres_tempfilt;
High_Pass_Filter = Node(fsl.TemporalFilter(), name = 'High_Pass_Filter')
High_Pass_Filter.inputs.highpass_sigma = 22.5 

#-----------------------------------------------------------------------------------------------------
#Get the mean image
Get_Mean_Image = Node(fsl.MeanImage(), name = 'Get_Mean_Image')
Get_Mean_Image.inputs.dimension = 'T'

#Add the mean image to the filtered image
Add_Mean_Image = Node(fsl.BinaryMaths(), name = 'Add_Mean_Image')
Add_Mean_Image.inputs.operation = 'add'


#-----------------------------------------------------------------------------------------------------
# In[15]:
#Fit the design to the voxels time-series
Esempio n. 10
0
def create_denoise_pipeline(name='denoise'):
    # workflow
    denoise = Workflow(name='denoise')
    # Define nodes
    inputnode = Node(interface=util.IdentityInterface(fields=[
        'anat_brain', 'brain_mask', 'flirt_mat', 'unwarped_mean', 'epi_coreg',
        'highpass_sigma', 'tr'
    ]),
                     name='inputnode')
    outputnode = Node(interface=util.IdentityInterface(fields=[
        'wmcsf_mask', 'brain2epi', 'wmcsf_mask2epi', 'combined_motion',
        'comp_regressor', 'comp_F', 'comp_pF', 'out_betas', 'ts_fullspectrum',
        'normalized_file'
    ]),
                      name='outputnode')
    # run fast to get tissue probability classes
    fast = Node(fsl.FAST(), name='fast')
    denoise.connect([(inputnode, fast, [('anat_brain', 'in_files')])])

    # functions to select tissue classes
    def selectindex(files, idx):
        import numpy as np
        from nipype.utils.filemanip import filename_to_list, list_to_filename
        return list_to_filename(
            np.array(filename_to_list(files))[idx].tolist())

    def selectsingle(files, idx):
        return files[idx]

    # binarize tissue classes
    binarize_tissue = MapNode(
        fsl.ImageMaths(op_string='-nan -thr 0.99 -ero -bin'),
        iterfield=['in_file'],
        name='binarize_tissue')
    denoise.connect([
        (fast, binarize_tissue, [(('partial_volume_files', selectindex,
                                   [0, 2]), 'in_file')]),
    ])
    # combine tissue classes to noise mask
    wmcsf_mask = Node(fsl.BinaryMaths(operation='add',
                                      out_file='wmcsf_mask.nii'),
                      name='wmcsf_mask')
    denoise.connect([(binarize_tissue, wmcsf_mask,
                      [(('out_file', selectsingle, 0), 'in_file'),
                       (('out_file', selectsingle, 1), 'operand_file')]),
                     (wmcsf_mask, outputnode, [('out_file', 'wmcsf_mask')])])

    # project wm_csf mask from anatomical to original epi space using inverse FLIRT-matrix
    invmat = Node(fsl.ConvertXFM(), name='invmat')
    invmat.inputs.invert_xfm = True

    apply_inv = Node(fsl.ApplyXfm(), name='apply_inv')
    apply_inv.inputs.apply_xfm = True
    denoise.connect([(inputnode, invmat, [('flirt_mat', 'in_file')]),
                     (invmat, apply_inv, [('out_file', 'in_matrix_file')]),
                     (inputnode, apply_inv, [('unwarped_mean', 'reference')]),
                     (wmcsf_mask, apply_inv, [('out_file', 'in_file')]),
                     (apply_inv, outputnode, [('out_file', 'wmcsf_mask2epi')])
                     ])
    #project brain to epi space as a checkup
    apply_inv_brain = Node(fsl.ApplyXfm(), name='apply_inv_brain')
    apply_inv_brain.inputs.apply_xfm = True
    denoise.connect([
        (invmat, apply_inv_brain, [('out_file', 'in_matrix_file')]),
        (inputnode, apply_inv_brain, [('unwarped_mean', 'reference')]),
        (inputnode, apply_inv_brain, [('anat_brain', 'in_file')]),
        (apply_inv_brain, outputnode, [('out_file', 'brain2epi')])
    ])

    #no artifact detection and motion regression done because of AROMA

    # create filter with compcor components
    createfilter2 = Node(util.Function(input_names=[
        'realigned_file', 'mask_file', 'num_components', 'extra_regressors'
    ],
                                       output_names=['out_files'],
                                       function=extract_noise_components),
                         name='makecompcorfilter')
    createfilter2.inputs.num_components = 6
    createfilter2.inputs.extra_regressors = None
    createfilter2.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([
        (inputnode, createfilter2, [('epi_coreg', 'realigned_file')]),
        (apply_inv, createfilter2, [('out_file', 'mask_file')]),
        (createfilter2, outputnode, [('out_files', 'comp_regressor')]),
    ])
    # regress compcor and other noise components
    filter2 = Node(fsl.GLM(out_f_name='F_noise.nii.gz',
                           out_pf_name='pF_noise.nii.gz',
                           out_res_name='rest2anat_denoised.nii.gz',
                           output_type='NIFTI_GZ',
                           demean=True),
                   name='filternoise')
    filter2.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([(inputnode, filter2, [('epi_coreg', 'in_file')]),
                     (createfilter2, filter2, [('out_files', 'design')]),
                     (inputnode, filter2, [('brain_mask', 'mask')]),
                     (filter2, outputnode, [('out_f', 'comp_F'),
                                            ('out_pf', 'comp_pF'),
                                            ('out_file', 'out_betas')])])

    # write TR into header again (glms remove it)
    # do not use mri_convert interface as it has a bug (already fixed in niyppe master)
    fix_tr = Node(util.Function(input_names=['in_file', 'TR_sec'],
                                output_names=['out_file'],
                                function=fix_TR_fs),
                  name='fix_tr')
    denoise.connect(inputnode, 'tr', fix_tr, 'TR_sec')
    denoise.connect(filter2, 'out_res', fix_tr, 'in_file')

    #use only highpass filter (because high-frequency content (otherwise filtered by lowpass is already considered in AROMA))
    highpass_filter = Node(
        fsl.TemporalFilter(out_file='rest_denoised_highpassed.nii'),
        name='highpass_filter')
    highpass_filter.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([(inputnode, highpass_filter, [('highpass_sigma',
                                                    'highpass_sigma')]),
                     (fix_tr, highpass_filter, [('out_file', 'in_file')]),
                     (fix_tr, outputnode, [('out_file', 'ts_fullspectrum')])])

    # time-normalize scans (could be set to percent change etc.  but here NO normalization is used
    #                 http://nipy.org/nitime/api/generated/nitime.fmri.io.html)
    normalize_time = Node(util.Function(input_names=['in_file', 'tr'],
                                        output_names=['out_file'],
                                        function=time_normalizer),
                          name='normalize_time')
    normalize_time.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([
        (inputnode, normalize_time, [('tr', 'tr')]),
        (highpass_filter, normalize_time, [('out_file', 'in_file')]),
        (normalize_time, outputnode, [('out_file', 'normalized_file')])
    ])
    return denoise
Esempio n. 11
0
NodeHash_f58d230.inputs.dimension = 3
NodeHash_f58d230.inputs.fwhm = 5
NodeHash_f58d230.inputs.use_median = 1

#Wraps command **fslmaths**
NodeHash_10dc3c10 = pe.MapNode(interface = fsl.ApplyMask(), name = 'NodeName_10dc3c10', iterfield = ['in_file', 'mask_file'])

#Custom interface wrapping function Getinormscale
NodeHash_1c9d2280 = pe.MapNode(interface = firstlevelhelpers.Getinormscale, name = 'NodeName_1c9d2280', iterfield = ['medianval'])

#Wraps command **fslmaths**
NodeHash_2ee27f90 = pe.MapNode(interface = fsl.BinaryMaths(), name = 'NodeName_2ee27f90', iterfield = ['in_file', 'operand_value'])
NodeHash_2ee27f90.inputs.operation = 'mul'

#Wraps command **fslmaths**
NodeHash_c0d1e30 = pe.MapNode(interface = fsl.TemporalFilter(), name = 'NodeName_c0d1e30', iterfield = ['in_file'])
NodeHash_c0d1e30.inputs.highpass_sigma = 25

#Wraps command **fslmaths**
NodeHash_17446a20 = pe.MapNode(interface = fsl.MeanImage(), name = 'NodeName_17446a20', iterfield = ['in_file'])
NodeHash_17446a20.inputs.dimension = 'T'

#Wraps command **fslmaths**
NodeHash_b5a5810 = pe.MapNode(interface = fsl.BinaryMaths(), name = 'NodeName_b5a5810', iterfield = ['in_file', 'operand_file'])
NodeHash_b5a5810.inputs.operation = 'add'

#Makes a model specification compatible with spm/fsl designers.
NodeHash_1e7a3420 = pe.MapNode(interface = modelgen.SpecifyModel(), name = 'NodeName_1e7a3420', iterfield = ['functional_runs', 'subject_info'])
NodeHash_1e7a3420.inputs.high_pass_filter_cutoff = 0
NodeHash_1e7a3420.inputs.input_units = 'secs'
NodeHash_1e7a3420.inputs.time_repetition = 2.0
                               name='NodeName_1becb6b0',
                               iterfield=['in_file', 'mask_file'])

#Custom interface wrapping function Getinormscale
NodeHash_1dce7160 = pe.MapNode(interface=firstlevelhelpers.Getinormscale,
                               name='NodeName_1dce7160',
                               iterfield=['medianval'])

#Wraps command **fslmaths**
NodeHash_1de5d180 = pe.MapNode(interface=fsl.BinaryMaths(),
                               name='NodeName_1de5d180',
                               iterfield=['in_file', 'operand_value'])
NodeHash_1de5d180.inputs.operation = 'mul'

#Wraps command **fslmaths**
NodeHash_1e98cad0 = pe.MapNode(interface=fsl.TemporalFilter(),
                               name='NodeName_1e98cad0',
                               iterfield=['in_file'])
NodeHash_1e98cad0.inputs.highpass_sigma = 25

#Wraps command **fslmaths**
NodeHash_1e558730 = pe.MapNode(interface=fsl.MeanImage(),
                               name='NodeName_1e558730',
                               iterfield=['in_file'])
NodeHash_1e558730.inputs.dimension = 'T'

#Wraps command **fslmaths**
NodeHash_1fdac460 = pe.MapNode(interface=fsl.BinaryMaths(),
                               name='NodeName_1fdac460',
                               iterfield=['in_file', 'operand_file'])
NodeHash_1fdac460.inputs.operation = 'add'
NodeHash_1a5fa380.inputs.dimension = 3
NodeHash_1a5fa380.inputs.fwhm = 5
NodeHash_1a5fa380.inputs.use_median = 1

#Wraps command **fslmaths**
NodeHash_1ba8b6a0 = pe.MapNode(interface = fsl.ApplyMask(), name = 'NodeName_1ba8b6a0', iterfield = ['in_file', 'mask_file'])

#Custom interface wrapping function Getinormscale
NodeHash_1b378470 = pe.MapNode(interface = firstlevelhelpers.Getinormscale, name = 'NodeName_1b378470', iterfield = ['medianval'])

#Wraps command **fslmaths**
NodeHash_1b3754f0 = pe.MapNode(interface = fsl.BinaryMaths(), name = 'NodeName_1b3754f0', iterfield = ['in_file', 'operand_value'])
NodeHash_1b3754f0.inputs.operation = 'mul'

#Wraps command **fslmaths**
NodeHash_1b252520 = pe.MapNode(interface = fsl.TemporalFilter(), name = 'NodeName_1b252520', iterfield = ['in_file'])
NodeHash_1b252520.inputs.highpass_sigma = 25

#Wraps command **fslmaths**
NodeHash_1cb2c540 = pe.MapNode(interface = fsl.MeanImage(), name = 'NodeName_1cb2c540', iterfield = ['in_file'])
NodeHash_1cb2c540.inputs.dimension = 'T'

#Wraps command **fslmaths**
NodeHash_1e361a00 = pe.MapNode(interface = fsl.BinaryMaths(), name = 'NodeName_1e361a00', iterfield = ['in_file', 'operand_file'])
NodeHash_1e361a00.inputs.operation = 'add'

#Makes a model specification compatible with spm/fsl designers.
NodeHash_1f14a780 = pe.MapNode(interface = modelgen.SpecifyModel(), name = 'NodeName_1f14a780', iterfield = ['functional_runs', 'subject_info'])
NodeHash_1f14a780.inputs.high_pass_filter_cutoff = 0
NodeHash_1f14a780.inputs.input_units = 'secs'
NodeHash_1f14a780.inputs.time_repetition = 2.0
Esempio n. 14
0
def create_denoise_pipeline(working_dir, ds_dir, name='denoise'):
    # workflow
    denoise_wf = Workflow(name=name)
    denoise_wf.base_dir = os.path.join(working_dir, 'LeiCA_resting',
                                       'rsfMRI_preprocessing')

    # I/O NODES
    inputnode = Node(interface=util.IdentityInterface(fields=[
        'subject_id', 'epi', 'mean_epi', 'par_moco', 'struct_2_epi_mat',
        'MNI_2_epi_warp', 'lat_ventricle_mask_MNI', 'wm_mask', 'csf_mask',
        'brain_mask_epiSpace', 'TR_ms', 'lp_cutoff_freq', 'hp_cutoff_freq'
    ]),
                     name='inputnode')

    outputnode = Node(interface=util.IdentityInterface(
        fields=['outlier_files', 'rs_preprocessed']),
                      name='outputnode')

    ds = Node(nio.DataSink(base_directory=ds_dir), name='ds')
    ds.inputs.substitutions = [('_TR_id_', 'TR_')]

    ######### TRANSFORM MASKS

    wm_mask_epiSpace = Node(fsl.ApplyXfm(apply_xfm=True,
                                         interp='nearestneighbour'),
                            name='wm_mask_epiSpace')
    wm_mask_epiSpace.inputs.out_file = 'wm_mask_epiSpace.nii.gz'
    denoise_wf.connect([(inputnode, wm_mask_epiSpace,
                         [('wm_mask', 'in_file'), ('mean_epi', 'reference'),
                          ('struct_2_epi_mat', 'in_matrix_file')])])

    denoise_wf.connect(wm_mask_epiSpace, 'out_file', ds,
                       'masks.wm_mask_epiSpace')

    csf_mask_epiSpace = Node(fsl.ApplyXfm(apply_xfm=True,
                                          interp='nearestneighbour'),
                             name='csf_mask_epiSpace')
    denoise_wf.connect([(inputnode, csf_mask_epiSpace,
                         [('csf_mask', 'in_file'), ('mean_epi', 'reference'),
                          ('struct_2_epi_mat', 'in_matrix_file')])])

    # MOVE LATERAL VENTRICLE MASK INTO EPI SPACE
    lat_ventricle_mask_epiSpace = Node(fsl.ApplyWarp(interp='nn'),
                                       name='lat_ventricle_mask_epiSpace')
    denoise_wf.connect(inputnode, 'lat_ventricle_mask_MNI',
                       lat_ventricle_mask_epiSpace, 'in_file')
    denoise_wf.connect(inputnode, 'mean_epi', lat_ventricle_mask_epiSpace,
                       'ref_file')
    denoise_wf.connect(inputnode, 'MNI_2_epi_warp',
                       lat_ventricle_mask_epiSpace, 'field_file')

    # CONFINE INDIVIDUAL CSF MASK TO LATERAL VENTRICLES
    csf_mask_lat_ventricles_epiSpace = Node(
        fsl.maths.BinaryMaths(operation='mul'),
        name='csf_mask_lat_ventricles_epiSpace')
    csf_mask_lat_ventricles_epiSpace.inputs.out_file = 'csf_mask_epiSpace.nii.gz'
    denoise_wf.connect(csf_mask_epiSpace, 'out_file',
                       csf_mask_lat_ventricles_epiSpace, 'in_file')
    denoise_wf.connect(lat_ventricle_mask_epiSpace, 'out_file',
                       csf_mask_lat_ventricles_epiSpace, 'operand_file')
    denoise_wf.connect(csf_mask_lat_ventricles_epiSpace, 'out_file', ds,
                       'masks.csf_mask_lat_ventr_epiSpace')

    # TR CONVERSION
    def get_TR_in_sec_fct(TR_ms):
        return TR_ms / 1000.0

    get_TR_in_sec = Node(util.Function(input_names=['TR_ms'],
                                       output_names=['TR_sec'],
                                       function=get_TR_in_sec_fct),
                         name='get_TR_in_sec')

    denoise_wf.connect(inputnode, 'TR_ms', get_TR_in_sec, 'TR_ms')

    # RUN ARTIFACT DETECTION
    artifact = Node(rapidart.ArtifactDetect(save_plot=True,
                                            use_norm=True,
                                            parameter_source='FSL',
                                            mask_type='file',
                                            norm_threshold=1,
                                            zintensity_threshold=3,
                                            use_differences=[True, False]),
                    name='artifact')
    artifact.plugin_args = {'submit_specs': 'request_memory = 17000'}

    denoise_wf.connect(inputnode, 'epi', artifact, 'realigned_files')
    denoise_wf.connect([(inputnode, artifact, [('par_moco',
                                                'realignment_parameters')])])
    denoise_wf.connect(inputnode, 'brain_mask_epiSpace', artifact, 'mask_file')

    denoise_wf.connect([
        (artifact, ds, [('norm_files', 'denoise.artefact.@combined_motion'),
                        ('outlier_files', 'denoise.artefact.@outlier'),
                        ('intensity_files', 'denoise.artefact.@intensity'),
                        ('statistic_files', 'denoise.artefact.@outlierstats'),
                        ('plot_files', 'denoise.artefact.@outlierplots')])
    ])
    denoise_wf.connect(artifact, 'outlier_files', outputnode, 'outlier_files')

    ############################

    def combine_motion_parameters_with_outliers_fct(motion_params,
                                                    outliers_file, spike_reg):
        """Adapted from rom https://github.com/nipy/nipype/blob/master/examples/
        rsfmri_vol_surface_preprocessing_nipy.py#L261
        """

        import numpy as np
        import os
        if spike_reg:
            out_params = np.genfromtxt(motion_params)
            try:
                outlier_val = np.genfromtxt(outliers_file)
            except IOError:
                outlier_val = np.empty((0))
            for index in np.atleast_1d(outlier_val):
                outlier_vector = np.zeros((out_params.shape[0], 1))
                outlier_vector[index] = 1
                out_params = np.hstack((out_params, outlier_vector))

            out_file = os.path.join(os.getcwd(), "motion_outlier_regressor.txt"
                                    )  #"filter_regressor%02d.txt" % idx)
            np.savetxt(out_file, out_params, fmt="%.8f")
        else:
            out_file = motion_params

        return out_file

    ############################

    # COMPUTE MOTION DERIVATIVES (FRISTON24)
    friston24 = Node(util.Function(
        input_names=['in_file'],
        output_names=['friston_par'],
        function=cpac_generate_motion_statistics.calc_friston_twenty_four),
                     name='friston24')
    denoise_wf.connect(inputnode, 'par_moco', friston24, 'in_file')

    # CREATE OUTLIER DUMMY REGRESSOR AND COMBINE WITH FRISTON24
    motion_and_outlier_regressor = Node(util.Function(
        input_names=['motion_params', 'outliers_file', 'spike_reg'],
        output_names=['out_file'],
        function=combine_motion_parameters_with_outliers_fct),
                                        name='motion_and_outlier_regressor')
    motion_and_outlier_regressor.inputs.spike_reg = True
    denoise_wf.connect(friston24, 'friston_par', motion_and_outlier_regressor,
                       'motion_params')
    denoise_wf.connect(artifact, 'outlier_files', motion_and_outlier_regressor,
                       'outliers_file')

    # EXTRACT SIGNAL FROM WM AND CSF FOR COMPCOR
    wm_sig = Node(util.Function(input_names=['data_file', 'mask_file'],
                                output_names=['out_file'],
                                function=extract_signal_from_tissue),
                  name='wm_sig')

    denoise_wf.connect(inputnode, 'epi', wm_sig, 'data_file')
    denoise_wf.connect(wm_mask_epiSpace, 'out_file', wm_sig, 'mask_file')

    csf_sig = Node(util.Function(input_names=['data_file', 'mask_file'],
                                 output_names=['out_file'],
                                 function=extract_signal_from_tissue),
                   name='csf_sig')

    denoise_wf.connect(inputnode, 'epi', csf_sig, 'data_file')
    denoise_wf.connect(csf_mask_lat_ventricles_epiSpace, 'out_file', csf_sig,
                       'mask_file')

    nuisance_selector = {
        'compcor': True,
        'wm': False,
        'csf': False,
        'gm': False,
        'global': False,
        'pc1': False,
        'motion': True,
        'linear': True,
        'quadratic': True
    }

    nuisance_reg = Node(util.Function(
        input_names=[
            'subject', 'selector', 'wm_sig_file', 'csf_sig_file',
            'gm_sig_file', 'motion_file', 'compcor_ncomponents'
        ],
        output_names=['residual_file', 'regressors_file'],
        function=cpac_nuisance.calc_residuals),
                        name='nuisance_reg')

    nuisance_reg.inputs.compcor_ncomponents = 5
    nuisance_reg.inputs.selector = nuisance_selector
    denoise_wf.connect(inputnode, 'epi', nuisance_reg, 'subject')
    denoise_wf.connect(wm_sig, 'out_file', nuisance_reg, 'wm_sig_file')
    denoise_wf.connect(csf_sig, 'out_file', nuisance_reg, 'csf_sig_file')
    denoise_wf.connect(motion_and_outlier_regressor, 'out_file', nuisance_reg,
                       'motion_file')
    denoise_wf.connect(nuisance_reg, 'regressors_file', ds,
                       'denoise.regression.regressor')
    denoise_wf.connect(nuisance_reg, 'residual_file', ds, 'epis.01_denoised')

    ############################

    # BANDPASS FILTER
    # sigma calculation see
    # 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
    def calc_bp_sigma_fct(TR_sec, cutoff_freq):
        sigma = 1. / (2 * TR_sec * cutoff_freq)
        return sigma

    calc_lp_sigma = Node(util.Function(input_names=['TR_sec', 'cutoff_freq'],
                                       output_names=['sigma'],
                                       function=calc_bp_sigma_fct),
                         name='calc_lp_sigma')
    denoise_wf.connect(get_TR_in_sec, 'TR_sec', calc_lp_sigma, 'TR_sec')
    denoise_wf.connect(inputnode, 'lp_cutoff_freq', calc_lp_sigma,
                       'cutoff_freq')

    calc_hp_sigma = Node(util.Function(input_names=['TR_sec', 'cutoff_freq'],
                                       output_names=['sigma'],
                                       function=calc_bp_sigma_fct),
                         name='calc_hp_sigma')
    denoise_wf.connect(get_TR_in_sec, 'TR_sec', calc_hp_sigma, 'TR_sec')
    denoise_wf.connect(inputnode, 'hp_cutoff_freq', calc_hp_sigma,
                       'cutoff_freq')

    bp_filter = Node(fsl.TemporalFilter(), name='bp_filter')
    bp_filter.plugin_args = {'submit_specs': 'request_memory = 4000'}

    denoise_wf.connect(nuisance_reg, 'residual_file', bp_filter, 'in_file')
    denoise_wf.connect(calc_lp_sigma, 'sigma', bp_filter, 'lowpass_sigma')
    denoise_wf.connect(calc_hp_sigma, 'sigma', bp_filter, 'highpass_sigma')
    denoise_wf.connect(bp_filter, 'out_file', ds, 'epis.02_denoised_BP')

    # TIME-NORMALIZE SCAN
    normalize_time = Node(util.Function(input_names=['in_file', 'tr'],
                                        output_names=['out_file'],
                                        function=time_normalizer),
                          name='normalize_time')
    #fixme req mem needed?
    #normalize_time.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise_wf.connect(bp_filter, 'out_file', normalize_time, 'in_file')
    denoise_wf.connect(get_TR_in_sec, 'TR_sec', normalize_time, 'tr')

    denoise_wf.connect(normalize_time, 'out_file', outputnode,
                       'rs_preprocessed')
    denoise_wf.connect(normalize_time, 'out_file', ds,
                       'epis.03_denoised_BP_tNorm')

    denoise_wf.write_graph(dotfilename=denoise_wf.name,
                           graph2use='flat',
                           format='pdf')

    return denoise_wf
Esempio n. 15
0
def init_bold_filt_wf(variant=None, memcalc=MemoryCalculator()):
    assert variant is not None

    name = make_variant_bold_filt_wf_name(variant)

    workflow = pe.Workflow(name=name)

    inputnode = pe.Node(
        niu.IdentityInterface(fields=[
            *in_attrs_from_func_preproc_wf, *in_attrs_from_anat_preproc_wf,
            "metadata"
        ]),
        name="inputnode",
    )
    workflow.add_nodes([inputnode])

    metadatanode = pe.Node(niu.IdentityInterface(fields=["repetition_time"]),
                           name="metadatanode")
    workflow.connect([(inputnode, metadatanode, [
        (("metadata", get_repetition_time), "repetition_time")
    ])])

    bandpass = None

    ortendpoint = None

    boldfileendpoint = (inputnode, "bold_std")

    tagdict = dict(variant)

    # smoothing is done first
    if "smoothed" in tagdict:
        fwhm = float(tagdict["smoothed"])
        smooth_workflow = init_smooth_wf(fwhm=fwhm)
        workflow.connect(inputnode, "bold_mask_std", smooth_workflow,
                         "inputnode.mask_file")
        workflow.connect(*boldfileendpoint, smooth_workflow,
                         "inputnode.in_file")
        boldfileendpoint = (smooth_workflow, "outputnode.out_file")

    if "grand_mean_scaled" in tagdict:
        grand_mean = tagdict["grand_mean_scaled"]
        assert isinstance(grand_mean, float)
        grandmeanscaling = pe.Node(
            interface=GrandMeanScaling(grand_mean=grand_mean),
            name="grandmeanscaling")
        workflow.connect(*boldfileendpoint, grandmeanscaling, "in_file")
        workflow.connect(inputnode, "bold_mask_std", grandmeanscaling,
                         "mask_file")
        boldfileendpoint = (grandmeanscaling, "out_file")

    need_to_add_mean = False
    boldfileendpoint_for_meanfunc = boldfileendpoint

    # if we use gaussian band-pass filtering, we cannot orthogonalize these regressors
    # with respect to the filter, as afni tproject doesn't support this filter type
    # as such we need to remove them before to not re-introduce filtered-out variance
    simultaneous_bandpass_and_ort = True
    if "band_pass_filtered" in tagdict:
        type = first(tagdict["band_pass_filtered"])
        if type == "gaussian":
            simultaneous_bandpass_and_ort = False

    confounds_to_remove_before_filtering = set(("aroma_motion_[0-9]+", ))
    if (not simultaneous_bandpass_and_ort and "confounds_removed" in tagdict
            and not confounds_to_remove_before_filtering.isdisjoint(
                tagdict["confounds_removed"])):
        confoundsremovedset = set(tagdict["confounds_removed"])
        preconfoundsremoved = confounds_to_remove_before_filtering & confoundsremovedset
        postconfoundsremoved = confoundsremovedset - confounds_to_remove_before_filtering
        if len(postconfoundsremoved) == 0:
            del tagdict["confounds_removed"]
        else:
            tagdict["confounds_removed"] = postconfoundsremoved

        ortendpoint, _ = make_confoundsendpoint("pre", workflow,
                                                boldfileendpoint,
                                                list(preconfoundsremoved),
                                                memcalc)
        tproject = pe.Node(afni.TProject(polort=1, out_file="tproject.nii"),
                           name="pretproject")
        workflow.connect(*boldfileendpoint, tproject, "in_file")
        workflow.connect(metadatanode, "repetition_time", tproject, "TR")
        workflow.connect(*ortendpoint, tproject, "ort")
        boldfileendpoint = (tproject, "out_file")
        need_to_add_mean = True

    if "band_pass_filtered" in tagdict:
        type = first(tagdict["band_pass_filtered"])
        if type == "frequency_based":
            bandpass = tagdict["band_pass_filtered"][1:]
        elif type == "gaussian":

            def calc_highpass_sigma(temporal_filter_width=None,
                                    repetition_time=None):
                highpass_sigma = temporal_filter_width / (2.0 *
                                                          repetition_time)
                return highpass_sigma

            calchighpasssigma = pe.Node(
                interface=niu.Function(
                    input_names=["temporal_filter_width", "repetition_time"],
                    output_names=["highpass_sigma"],
                    function=calc_highpass_sigma,
                ),
                name="calchighpasssigma",
            )
            workflow.connect(metadatanode, "repetition_time",
                             calchighpasssigma, "repetition_time")
            calchighpasssigma.inputs.temporal_filter_width = second(
                tagdict["band_pass_filtered"])
            highpass = pe.Node(fsl.TemporalFilter(), name="gaussianfilter")
            workflow.connect(calchighpasssigma, "highpass_sigma", highpass,
                             "highpass_sigma")
            workflow.connect(*boldfileendpoint, highpass, "in_file")
            need_to_add_mean = True

    if "confounds_removed" in tagdict:
        confoundnames = tagdict["confounds_removed"]
        if len(confoundnames) > 0:
            ortendpoint, _ = make_confoundsendpoint("post", workflow,
                                                    boldfileendpoint,
                                                    confoundnames, memcalc)

    if bandpass is not None or ortendpoint is not None:
        tproject = pe.Node(afni.TProject(polort=1, out_file="tproject.nii"),
                           name="tproject")
        workflow.connect(*boldfileendpoint, tproject, "in_file")
        workflow.connect(metadatanode, "repetition_time", tproject, "TR")
        if bandpass is not None:
            tproject.inputs.bandpass = bandpass
        if ortendpoint is not None:
            workflow.connect(*ortendpoint, tproject, "ort")
        boldfileendpoint = (tproject, "out_file")
        need_to_add_mean = True

    if need_to_add_mean is True:
        meanfunc = pe.Node(interface=fsl.ImageMaths(op_string="-Tmean",
                                                    suffix="_mean"),
                           name="meanfunc")
        workflow.connect(*boldfileendpoint_for_meanfunc, meanfunc, "in_file")
        addmean = pe.Node(interface=fsl.BinaryMaths(operation="add"),
                          name="addmean")
        workflow.connect(*boldfileendpoint, addmean, "in_file")
        workflow.connect(meanfunc, "out_file", addmean, "operand_file")
        boldfileendpoint = (addmean, "out_file")

    applymask = pe.Node(
        interface=fsl.ApplyMask(),
        name="applymask",
        mem_gb=memcalc.volume_std_gb,
    )
    workflow.connect(*boldfileendpoint, applymask, "in_file")
    workflow.connect(inputnode, "bold_mask_std", applymask, "mask_file")
    boldfileendpoint = (applymask, "out_file")

    endpoints = [boldfileendpoint]  # boldfile is finished

    if "confounds_extract" in tagdict:  # last
        confoundnames = tagdict["confounds_extract"]
        confoundsextractendpoint, confoundsextractendpointwithheader = make_confoundsendpoint(
            "extract", workflow, boldfileendpoint, confoundnames, memcalc)
        endpoints.append(confoundsextractendpoint)
        endpoints.append(confoundsextractendpointwithheader)

    outnames = [f"out{i+1}" for i in range(len(endpoints))]

    outputnode = pe.Node(
        niu.IdentityInterface(fields=[*outnames, "mask_file"]),
        name="outputnode",
    )
    workflow.connect(inputnode, "bold_mask_std", outputnode, "mask_file")

    for outname, endpoint in zip(outnames, endpoints):
        workflow.connect(*endpoint, outputnode, outname)

    return workflow
Esempio n. 16
0
#pipe.connect(changedim, 'out_file', compcor, 'inputspec.func')

########################################################################################################################
# median angle correction

from mypype.workflows.preproc.median_angle import create_median_angle_correction

medang = create_median_angle_correction()
medang.inputs.inputspec.target_angle = 90

pipe.connect(changedim, 'out_file', medang, 'inputspec.subject')

########################################################################################################################
# node for bandpass filter
bandpass_filter = pe.MapNode(
    fsl.TemporalFilter(),  # TODO: into preprocess workflow
    name='07_bandpass_filter',
    iterfield=['in_file'])

bandpass_filter.inputs.highpass_sigma = 100 / (2 * _TR_)
bandpass_filter.inputs.lowpass_sigma = 12.5 / (2 * _TR_)
#pipe.connect(compcor, 'outputspec.residual_file', bandpass_filter, 'in_file')
pipe.connect(medang, 'outputspec.subject', bandpass_filter, 'in_file')

########################################################################################################################
# node for reho
reho = reho.create_reho(wf_name="08_reho")
reho.inputs.inputspec.cluster_size = 27
pipe.connect(bandpass_filter, 'out_file', reho, 'inputspec.rest_res_filt')
pipe.connect(preproc, 'outputspec.func_brain_mask', reho,
             'inputspec.rest_mask')
Esempio n. 17
0
my_fsl_Threshold.inputs.args = '-bin'

#Anatomical compcor: for inputs and outputs, see CompCor.
my_confounds_ACompCor = pe.Node(interface=confounds.ACompCor(),
                                name='my_confounds_ACompCor',
                                iterfield=[''])
my_confounds_ACompCor.inputs.num_components = 2

#Wraps command **fsl_regfilt**
my_fsl_FilterRegressor = pe.Node(interface=fsl.FilterRegressor(),
                                 name='my_fsl_FilterRegressor',
                                 iterfield=[''])
my_fsl_FilterRegressor.inputs.filter_columns = [1, 2]

#Wraps command **fslmaths**
my_fsl_TemporalFilter = pe.Node(interface=fsl.TemporalFilter(),
                                name='my_fsl_TemporalFilter',
                                iterfield=[''])
my_fsl_TemporalFilter.inputs.highpass_sigma = 25

#Change the name of a file based on a mapped format string.
my_utility_Rename = pe.Node(interface=utility.Rename(),
                            name='my_utility_Rename',
                            iterfield=[''])
my_utility_Rename.inputs.format_string = "/output/filtered.nii.gz"

#Create a workflow to connect all those nodes
analysisflow = nipype.Workflow('MyWorkflow')
analysisflow.connect(my_io_S3DataGrabber, "outfiles", my_fsl_SliceTimer,
                     "in_file")
analysisflow.connect(my_fsl_SliceTimer, "slice_time_corrected_file",
Esempio n. 18
0
#Wraps command **fslmaths**
NodeHash_30f6760 = pe.Node(interface=fsl.Threshold(), name='NodeName_30f6760')
NodeHash_30f6760.inputs.args = '-bin'

#Anatomical compcor: for inputs and outputs, see CompCor.
NodeHash_325da10 = pe.Node(interface=confounds.ACompCor(),
                           name='NodeName_325da10')
NodeHash_325da10.inputs.num_components = 2

#Wraps command **fsl_regfilt**
NodeHash_430d1e0 = pe.Node(interface=fsl.FilterRegressor(),
                           name='NodeName_430d1e0')
NodeHash_430d1e0.inputs.filter_columns = [1, 2]

#Wraps command **fslmaths**
NodeHash_77e3220 = pe.Node(interface=fsl.TemporalFilter(),
                           name='NodeName_77e3220')
NodeHash_77e3220.inputs.highpass_sigma = 25

#Generic datasink module to store structured outputs
NodeHash_99576b0 = pe.Node(interface=io.DataSink(), name='NodeName_99576b0')

#Create a workflow to connect all those nodes
analysisflow = nipype.Workflow('MyWorkflow')
analysisflow.connect(NodeHash_30f69e0, 'outfiles', NodeHash_1d000c0, 'in_file')
analysisflow.connect(NodeHash_1d000c0, 'slice_time_corrected_file',
                     NodeHash_22f2e80, 'in_file')
analysisflow.connect(NodeHash_22f2e80, 'out_file', NodeHash_50c02c0, 'in_file')
analysisflow.connect(NodeHash_50c02c0, 'stddev_file', NodeHash_3ac27f0,
                     'in_file')
analysisflow.connect(NodeHash_3ac27f0, 'out_stat', NodeHash_30f6760, 'thresh')
Esempio n. 19
0
def process_rest(wf_name='proc_rest'):
    def get_element(list, index):
        return list[index]

    rest = pe.Workflow(wf_name)
    inputnode = pe.Node(interface=util.IdentityInterface(
        fields=['preprocessed_func', 'func_brain_mask']),
                        name='inputspec')
    outputnode = pe.Node(interface=util.IdentityInterface(fields=['out']),
                         name='outputspec')

    ########################################################################################################################
    # node for bandpass filter
    bandpass_filter = pe.MapNode(
        fsl.TemporalFilter(),  # TODO: into preprocess workflow
        name='05_bandpass_filter_cc',
        iterfield=['in_file'])

    #bandpass_filter.inputs.highpass_sigma = 100 / (2 * _TR_)
    #bandpass_filter.inputs.lowpass_sigma = 12.5 / (2 * _TR_)

    # 1. frequency band: 0.1-0.05 Hz
    # 2. frequency band: 0.05-0.02 Hz
    # 3. frequency band: 0.02-0.01 Hz
    # 4. frequency band: 0.01-0.002 Hz
    # 3. frequency band: 0.1-0.01 Hz # standard broadband

    bandpass_filter.iterables = [('highpass_sigma', [
        20 / (2 * _TR_), 50 / (2 * _TR_), 100 / (2 * _TR_), 500 / (2 * _TR_),
        100 / (2 * _TR_)
    ]),
                                 ('lowpass_sigma', [
                                     10 / (2 * _TR_), 20 / (2 * _TR_),
                                     50 / (2 * _TR_), 100 / (2 * _TR_),
                                     10 / (2 * _TR_)
                                 ])]

    bandpass_filter.synchronize = True

    rest.connect(inputnode, 'preprocessed_func', bandpass_filter, 'in_file')
    #pipe.connect(changedim, 'out_file', bandpass_filter, 'in_file')

    ########################################################################################################################
    # node for reho
    import mypype.workflows.rest.reho as reho
    reho = reho.create_reho(wf_name="06_reho_cc")
    reho.inputs.inputspec.cluster_size = 27
    rest.connect(bandpass_filter, 'out_file', reho, 'inputspec.rest_res_filt')
    rest.connect(inputnode, 'func_brain_mask', reho, 'inputspec.rest_mask')

    ########################################################################################################################
    # smooth reho maps

    smooth = prpc.create_susan_smooth("08_smooth_reho_cc")
    smooth.inputs.inputnode.fwhm = 12.
    #smooth.get_node( "smooth").iterables=[('fwhm', [4., 6., 8., 10., 12., 14.])] #TODO: to sigma???

    rest.connect(reho, 'outputspec.raw_reho_map', smooth, 'inputnode.in_files')
    rest.connect(inputnode, 'func_brain_mask', smooth, 'inputnode.mask_file')

    ########################################################################################################################
    # compute centrality

    #graph=cent.create_resting_state_graphs(wf_name='07_centrality_cc', multipleTemplate=True)
    #graph.inputs.centrality_options.method_options=[True, True]
    #graph.inputs.inputspec.method_option=1
    #graph.get_node( "calculate_centrality").iterables=[('method_option', [0,1,2]),
    #												   ('threshold_option', [1,1,2]),
    #												   ('threshold', [0.3, 0.3,0.6])]
    #graph.get_node( "calculate_centrality").synchronize = True
    #graph.inputs.inputspec.template = '/opt/shared/etc/std/new/standard-wistar_5mm_brain_mask.nii.gz'
    #graph.inputs.inputspec.threshold=0.5
    #graph.inputs.inputspec.threshold_option=1
    #graph.inputs.inputspec.weight_options=[False, True]

    #rest.connect(bandpass_filter, 'out_file', graph, 'inputspec.subject')
    #rest.connect(inputnode, 'func_brain_mask', graph, 'inputspec.template')

    ########################################################################################################################
    # get fisrt element of results

    #get_element=pe.MapNode(interface=util.Function(input_names = ['list','index'],
    #								   output_names = ['out'],
    #								   function = get_element),
    #								   name = 'get_element',
    #									  iterfield=['list'])
    #get_element.inputs.index=0
    #rest.connect(graph, 'outputspec.centrality_outputs', get_element, 'list')

    ########################################################################################################################
    # smooth centrality maps

    #smoothc = prpc.create_susan_smooth("09_smooth_centrality_cc", separate_masks=True)
    #smooth.get_node( "smooth").iterables=[('fwhm', [4., 6., 8., 10., 12., 14.])] #TODO: to sigma???
    #smoothc.inputs.inputnode.fwhm=12.

    #rest.connect(inputnode, 'func_brain_mask', smoothc, 'inputnode.mask_file')
    #rest.connect(get_element, 'out', smoothc, 'inputnode.in_files')
    return rest
Esempio n. 20
0
def create_denoise_pipeline(name='denoise'):
    # workflow
    denoise = Workflow(name='denoise')
    # Define nodes
    inputnode = Node(interface=util.IdentityInterface(fields=[
        'anat_brain', 'brain_mask', 'epi2anat_dat', 'unwarped_mean',
        'epi_coreg', 'moco_par', 'highpass_sigma', 'lowpass_sigma', 'tr'
    ]),
                     name='inputnode')
    outputnode = Node(interface=util.IdentityInterface(fields=[
        'wmcsf_mask', 'brain_mask_resamp', 'brain_mask2epi', 'combined_motion',
        'outlier_files', 'intensity_files', 'outlier_stats', 'outlier_plots',
        'mc_regressor', 'mc_F', 'mc_pF', 'comp_regressor', 'comp_F', 'comp_pF',
        'normalized_file'
    ]),
                      name='outputnode')
    # run fast to get tissue probability classes
    fast = Node(fsl.FAST(), name='fast')
    denoise.connect([(inputnode, fast, [('anat_brain', 'in_files')])])

    # functions to select tissue classes
    def selectindex(files, idx):
        import numpy as np
        from nipype.utils.filemanip import filename_to_list, list_to_filename
        return list_to_filename(
            np.array(filename_to_list(files))[idx].tolist())

    def selectsingle(files, idx):
        return files[idx]

    # resample tissue classes
    resample_tissue = MapNode(afni.Resample(resample_mode='NN',
                                            outputtype='NIFTI_GZ'),
                              iterfield=['in_file'],
                              name='resample_tissue')
    denoise.connect([
        (inputnode, resample_tissue, [('epi_coreg', 'master')]),
        (fast, resample_tissue, [(('partial_volume_files', selectindex,
                                   [0, 2]), 'in_file')]),
    ])
    # binarize tissue classes
    binarize_tissue = MapNode(
        fsl.ImageMaths(op_string='-nan -thr 0.99 -ero -bin'),
        iterfield=['in_file'],
        name='binarize_tissue')
    denoise.connect([
        (resample_tissue, binarize_tissue, [('out_file', 'in_file')]),
    ])
    # combine tissue classes to noise mask
    wmcsf_mask = Node(fsl.BinaryMaths(operation='add',
                                      out_file='wmcsf_mask_lowres.nii.gz'),
                      name='wmcsf_mask')
    denoise.connect([(binarize_tissue, wmcsf_mask,
                      [(('out_file', selectsingle, 0), 'in_file'),
                       (('out_file', selectsingle, 1), 'operand_file')]),
                     (wmcsf_mask, outputnode, [('out_file', 'wmcsf_mask')])])
    # resample brain mask
    resample_brain = Node(afni.Resample(
        resample_mode='NN',
        outputtype='NIFTI_GZ',
        out_file='T1_brain_mask_lowres.nii.gz'),
                          name='resample_brain')
    denoise.connect([(inputnode, resample_brain, [('brain_mask', 'in_file'),
                                                  ('epi_coreg', 'master')]),
                     (resample_brain, outputnode, [('out_file',
                                                    'brain_mask_resamp')])])
    # project brain mask into original epi space fpr quality assessment
    brainmask2epi = Node(fs.ApplyVolTransform(
        interp='nearest',
        inverse=True,
        transformed_file='T1_brain_mask2epi.nii.gz',
    ),
                         name='brainmask2epi')
    denoise.connect([
        (inputnode, brainmask2epi, [('brain_mask', 'target_file'),
                                    ('epi2anat_dat', 'reg_file'),
                                    ('unwarped_mean', 'source_file')]),
        (brainmask2epi, outputnode, [('transformed_file', 'brain_mask2epi')])
    ])
    # perform artefact detection
    artefact = Node(ra.ArtifactDetect(save_plot=True,
                                      use_norm=True,
                                      parameter_source='FSL',
                                      mask_type='file',
                                      norm_threshold=1,
                                      zintensity_threshold=3,
                                      use_differences=[True, False]),
                    name='artefact')
    artefact.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([
        (inputnode, artefact, [('epi_coreg', 'realigned_files'),
                               ('moco_par', 'realignment_parameters')]),
        (resample_brain, artefact, [('out_file', 'mask_file')]),
        (artefact, outputnode, [('norm_files', 'combined_motion'),
                                ('outlier_files', 'outlier_files'),
                                ('intensity_files', 'intensity_files'),
                                ('statistic_files', 'outlier_stats'),
                                ('plot_files', 'outlier_plots')])
    ])
    # Compute motion regressors
    motreg = Node(util.Function(
        input_names=['motion_params', 'order', 'derivatives'],
        output_names=['out_files'],
        function=motion_regressors),
                  name='getmotionregress')
    motreg.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([(inputnode, motreg, [('moco_par', 'motion_params')])])
    # Create a filter to remove motion and art confounds
    createfilter1 = Node(util.Function(
        input_names=['motion_params', 'comp_norm', 'outliers', 'detrend_poly'],
        output_names=['out_files'],
        function=build_filter1),
                         name='makemotionbasedfilter')
    createfilter1.inputs.detrend_poly = 2
    createfilter1.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([
        (motreg, createfilter1, [('out_files', 'motion_params')]),
        (
            artefact,
            createfilter1,
            [  #('norm_files', 'comp_norm'),
                ('outlier_files', 'outliers')
            ]),
        (createfilter1, outputnode, [('out_files', 'mc_regressor')])
    ])
    # regress out motion and art confounds
    filter1 = Node(fsl.GLM(out_f_name='F_mcart.nii.gz',
                           out_pf_name='pF_mcart.nii.gz',
                           out_res_name='rest_mc_denoised.nii.gz',
                           demean=True),
                   name='filtermotion')
    filter1.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([(inputnode, filter1, [('epi_coreg', 'in_file')]),
                     (createfilter1, filter1,
                      [(('out_files', list_to_filename), 'design')]),
                     (filter1, outputnode, [('out_f', 'mc_F'),
                                            ('out_pf', 'mc_pF')])])
    # create filter with compcor components
    createfilter2 = Node(util.Function(input_names=[
        'realigned_file', 'mask_file', 'num_components', 'extra_regressors'
    ],
                                       output_names=['out_files'],
                                       function=extract_noise_components),
                         name='makecompcorfilter')
    createfilter2.inputs.num_components = 6
    createfilter2.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([
        (createfilter1, createfilter2, [(('out_files', list_to_filename),
                                         'extra_regressors')]),
        (filter1, createfilter2, [('out_res', 'realigned_file')]),
        (wmcsf_mask, createfilter2, [('out_file', 'mask_file')]),
        (createfilter2, outputnode, [('out_files', 'comp_regressor')]),
    ])
    # regress compcor and other noise components
    filter2 = Node(fsl.GLM(out_f_name='F_noise.nii.gz',
                           out_pf_name='pF_noise.nii.gz',
                           out_res_name='rest2anat_denoised.nii.gz',
                           demean=True),
                   name='filternoise')
    filter2.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([(filter1, filter2, [('out_res', 'in_file')]),
                     (createfilter2, filter2, [('out_files', 'design')]),
                     (resample_brain, filter2, [('out_file', 'mask')]),
                     (filter2, outputnode, [('out_f', 'comp_F'),
                                            ('out_pf', 'comp_pF')])])
    # bandpass filter denoised file
    bandpass_filter = Node(
        fsl.TemporalFilter(out_file='rest_denoised_bandpassed.nii.gz'),
        name='bandpass_filter')
    bandpass_filter.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([(inputnode, bandpass_filter,
                      [('highpass_sigma', 'highpass_sigma'),
                       ('lowpass_sigma', 'lowpass_sigma')]),
                     (filter2, bandpass_filter, [('out_res', 'in_file')])])
    # time-normalize scans
    normalize_time = Node(util.Function(input_names=['in_file', 'tr'],
                                        output_names=['out_file'],
                                        function=time_normalizer),
                          name='normalize_time')
    normalize_time.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([
        (inputnode, normalize_time, [('tr', 'tr')]),
        (bandpass_filter, normalize_time, [('out_file', 'in_file')]),
        (normalize_time, outputnode, [('out_file', 'normalized_file')])
    ])
    return denoise
Esempio n. 21
0
    def create_workflow(self, flow, inputnode, outputnode):

        smoothing_output = pe.Node(interface=util.IdentityInterface(fields=["smoothing_output"]),name="smoothing_output")
        if self.config.smoothing > 0.0:
            smoothing = pe.Node(interface=fsl.SpatialFilter(operation='mean',kernel_shape = 'gauss'),name="smoothing")
            smoothing.inputs.kernel_size = self.config.smoothing
            flow.connect([
                        (inputnode,smoothing,[("preproc_file","in_file")]),
                        (smoothing,smoothing_output,[("out_file","smoothing_output")])
                        ])
        else:
            flow.connect([
                        (inputnode,smoothing_output,[("preproc_file","smoothing_output")])
                        ])
        
        discard_output = pe.Node(interface=util.IdentityInterface(fields=["discard_output"]),name="discard_output")   
        if self.config.discard_n_volumes > 0:
            discard = pe.Node(interface=discard_tp(n_discard=self.config.discard_n_volumes),name='discard_volumes')
            flow.connect([
                        (smoothing_output,discard,[("smoothing_output","in_file")]),
                        (discard,discard_output,[("out_file","discard_output")])
                        ])
        else:
            flow.connect([
                        (smoothing_output,discard_output,[("smoothing_output","discard_output")])
                        ])
        
        nuisance_output = pe.Node(interface=util.IdentityInterface(fields=["nuisance_output"]),name="nuisance_output")      
        if self.config.wm or self.config.global_nuisance or self.config.csf or self.config.motion:
            nuisance = pe.Node(interface=nuisance_regression(),name="nuisance_regression")
            nuisance.inputs.global_nuisance=self.config.global_nuisance
            nuisance.inputs.csf_nuisance=self.config.csf
            nuisance.inputs.wm_nuisance=self.config.wm
            nuisance.inputs.motion_nuisance=self.config.motion
            nuisance.inputs.n_discard=self.config.discard_n_volumes
            flow.connect([
                        (discard_output,nuisance,[("discard_output","in_file")]),
                        (inputnode,nuisance,[("eroded_brain","brainfile")]),
                        (inputnode,nuisance,[("eroded_csf","csf_file")]),
                        (inputnode,nuisance,[("eroded_wm","wm_file")]),
                        (inputnode,nuisance,[("motion_par_file","motion_file")]),
                        (inputnode,nuisance,[("registered_roi_volumes","gm_file")]),
                        (nuisance,nuisance_output,[("out_file","nuisance_output")])
                        ])
        else:
            flow.connect([
                        (discard_output,nuisance_output,[("discard_output","nuisance_output")])
                        ])
        
        detrending_output = pe.Node(interface=util.IdentityInterface(fields=["detrending_output"]),name="detrending_output")
        if self.config.detrending:
            detrending = pe.Node(interface=Detrending(),name='detrending')
            flow.connect([
                        (nuisance_output,detrending,[("nuisance_output","in_file")]),
                        (inputnode,detrending,[("registered_roi_volumes","gm_file")]),
                        (detrending,detrending_output,[("out_file","detrending_output")])
                        ])
        else:
            flow.connect([
                        (nuisance_output,detrending_output,[("nuisance_output","detrending_output")])
                        ])
        
        filter_output = pe.Node(interface=util.IdentityInterface(fields=["filter_output"]),name="filter_output")
        if self.config.lowpass_filter > 0 or self.config.highpass_filter > 0:
            filtering = pe.Node(interface=fsl.TemporalFilter(),name='temporal_filter')
            filtering.inputs.lowpass_sigma = self.config.lowpass_filter
            filtering.inputs.highpass_sigma = self.config.highpass_filter
            flow.connect([
                        (detrending_output,filtering,[("detrending_output","in_file")]),
                        (filtering,filter_output,[("out_file","filter_output")])
                        ])
        else:
            flow.connect([
                        (detrending_output,filter_output,[("detrending_output","filter_output")])
                        ])
                            
        if self.config.scrubbing:
            scrubbing = pe.Node(interface=Scrubbing(),name='scrubbing')
            flow.connect([
                        (filter_output,scrubbing,[("filter_output","in_file")]),
                        (inputnode,scrubbing,[("registered_wm","wm_mask")]),
                        (inputnode,scrubbing,[("registered_roi_volumes","gm_file")]),
                        (inputnode,scrubbing,[("motion_par_file","motion_parameters")]),
                        (scrubbing,outputnode,[("fd_npy","FD")]),
                        (scrubbing,outputnode,[("dvars_npy","DVARS")])
                        ])
        
        flow.connect([
                        (filter_output,outputnode,[("filter_output","func_file")])
                        ])
Esempio n. 22
0
def create_denoise_pipeline(name='denoise'):
    # workflow
    denoise = Workflow(name='denoise')
    # Define nodes
    inputnode = Node(interface=util.IdentityInterface(fields=['brain_mask',
                                                              'epi_coreg',
                                                              'wmseg',
                                                              'csfseg',
                                                              'highpass_freq',
                                                              'tr']),
                     name='inputnode')
    outputnode = Node(interface=util.IdentityInterface(fields=['wmcsf_mask',
                                                               'combined_motion',
                                                               'comp_regressor',
                                                               'comp_F',
                                                               'comp_pF',
                                                               'out_betas',
                                                               'ts_fullspectrum',
                                                               'ts_filtered']),
                      name='outputnode')

    # combine tissue classes to noise mask
    wmcsf_mask = Node(fsl.BinaryMaths(operation='add',
                                      out_file='wmcsf_mask.nii'),
                      name='wmcsf_mask')
    denoise.connect([(inputnode, wmcsf_mask, [('wmseg', 'in_file'),
                                              ('csfseg', 'operand_file')])])
        
    #resample + binarize wm_csf mask to epi resolution.
   
    resample_wmcsf= Node(afni.Resample(resample_mode='NN',
    outputtype='NIFTI_GZ',
    out_file='wmcsf_mask_lowres.nii.gz'),
    name = 'resample_wmcsf')
    
    bin_wmcsf_mask=Node(fsl.utils.ImageMaths(), name="bin_wmcsf_mask")
    bin_wmcsf_mask.inputs.op_string='-nan -thr 0.99 -ero -bin'
    
    denoise.connect([(wmcsf_mask, resample_wmcsf, [('out_file', 'in_file')]),
                     (inputnode, resample_wmcsf, [('brain_mask', 'master')]),
                     (resample_wmcsf, bin_wmcsf_mask,[('out_file', 'in_file')]),
                     (bin_wmcsf_mask, outputnode, [('out_file', 'wmcsf_mask')])
                    ])
         
    #no other denoising filters created here because AROMA performs already well.
       
    compcor=Node(conf.ACompCor(), name="compcor")
    compcor.inputs.num_components=5 #https://www.sciencedirect.com/science/article/pii/S105381191400175X?via%3Dihub
    denoise.connect([
                     (inputnode, compcor, [('epi_coreg', 'realigned_file')]),
                     (bin_wmcsf_mask, compcor, [('out_file', 'mask_files')]),
                     
                     ])    
    
    def create_designs(compcor_regressors,epi_coreg,mask):
            import numpy as np
            import pandas as pd
            import os
            from nilearn.input_data import NiftiMasker
           
            brain_masker = NiftiMasker(mask_img = mask, 
                               smoothing_fwhm=None, standardize=False,
                               memory='nilearn_cache', 
                               memory_level=5, verbose=2)
    
            whole_brain = brain_masker.fit_transform(epi_coreg)
            avg_signal = np.mean(whole_brain,axis=1)
            
            all_regressors=pd.read_csv(compcor_regressors,sep='\t')
            
            #add global signal.
            all_regressors['global_signal']=avg_signal
            
            fn=os.getcwd()+'/all_regressors.txt'
            all_regressors.to_csv(fn, sep='\t', index=False)
            
            return [fn, compcor_regressors]
            
    #create a list of design to loop over.
    create_design = Node(util.Function(input_names=['compcor_regressors','epi_coreg','mask'], output_names=['reg_list'], function=create_designs),
              name='create_design')
    
    denoise.connect([
    (compcor, create_design, [('components_file', 'compcor_regressors')]),
    (inputnode, create_design, [('epi_coreg', 'epi_coreg')]),
    (inputnode, create_design, [('brain_mask', 'mask')])
    ])
    
    # regress compcor and other noise components
    filter2 = MapNode(fsl.GLM(out_f_name='F_noise.nii.gz',
                           out_pf_name='pF_noise.nii.gz',
                           out_res_name='rest2anat_denoised.nii.gz',
                           output_type='NIFTI_GZ',
                           demean=True), 
                    iterfield=['design'],
                    name='filternoise')
    filter2.plugin_args = {'submit_specs': 'request_memory = 17000'}
    
    denoise.connect([(inputnode, filter2, [('epi_coreg', 'in_file')]),
                     #(createfilter2, filter2, [('out_files', 'design')]),
                     #(compcor, filter2, [('components_file', 'design')]),
                     (create_design, filter2, [('reg_list', 'design')]),
                     (inputnode, filter2, [('brain_mask', 'mask')]),
                     (filter2, outputnode, [('out_f', 'comp_F'),
                                            ('out_pf', 'comp_pF'),
                                            ('out_file', 'out_betas'),
                                            ('out_res', 'ts_fullspectrum'),
                                            ])
                     ])



    def calc_sigma(TR,highpass):
        # 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
        sigma=1. / (2 * TR * highpass)
        return sigma

    calc_s=Node(util.Function(input_names=['TR', 'highpass'], output_names=['sigma'], function=calc_sigma),
                  name='calc_s')
    
    
    denoise.connect(inputnode, 'tr', calc_s, 'TR')
    denoise.connect(inputnode, 'highpass_freq', calc_s, 'highpass')
    
    #use only highpass filter (because high-frequency content is already somewhat filtered in AROMA)) 
    highpass_filter = MapNode(fsl.TemporalFilter(out_file='rest_denoised_highpassed.nii'),
                           name='highpass_filter', iterfield=['in_file'])
    highpass_filter.plugin_args = {'submit_specs': 'request_memory = 17000'}
    denoise.connect([(calc_s, highpass_filter, [('sigma', 'highpass_sigma')]),
                     (filter2, highpass_filter, [('out_res', 'in_file')]),
                     (highpass_filter, outputnode, [('out_file', 'ts_filtered')])
                     ])
    
    return denoise
Esempio n. 23
0
pipe.connect(getdim, 'out_pixdim2', changedim, 'ydim')
pipe.connect(getdim, 'out_pixdim3', changedim, 'zdim')

########################################################################################################################
# median angle correction

from mypype.workflows.preproc.median_angle import create_median_angle_correction

medang=create_median_angle_correction()
medang.inputs.inputspec.target_angle=66

pipe.connect(changedim, 'out_file', )

########################################################################################################################
# node for bandpass filter
bandpass_filter = pe.MapNode(fsl.TemporalFilter(),  # TODO: into preprocess workflow
							 name='07_bandpass_filter',
							 iterfield=['in_file'])

bandpass_filter.inputs.highpass_sigma = 100 / (2 * _TR_)
bandpass_filter.inputs.lowpass_sigma = 12.5 / (2 * _TR_)
#pipe.connect(changedim, 'out_file', bandpass_filter, 'in_file')
pipe.connect(medang, 'subject', bandpass_filter, 'in_file')

########################################################################################################################
# calculate_centrality

graph=cent.create_resting_state_graphs(wf_name='08_centrality')
#graph.inputs.centrality_options.method_options=[True, True]
#graph.inputs.inputspec.method_option=1
graph.get_node( "calculate_centrality").iterables=[('method_option', [1,1,1]),
Esempio n. 24
0
def create_resting_preproc(name='restpreproc'):
    """Create a "resting" time series preprocessing workflow

    The noise removal is based on Behzadi et al. (2007)

    Parameters
    ----------

    name : name of workflow (default: restpreproc)

    Inputs::

        inputspec.func : functional run (filename or list of filenames)

    Outputs::

        outputspec.noise_mask_file : voxels used for PCA to derive noise components
        outputspec.filtered_file : bandpass filtered and noise-reduced time series

    Example
    -------

    >>> TR = 3.0
    >>> wf = create_resting_preproc()
    >>> wf.inputs.inputspec.func = 'f3.nii'
    >>> wf.inputs.inputspec.num_noise_components = 6
    >>> wf.inputs.inputspec.highpass_sigma = 100/(2*TR)
    >>> wf.inputs.inputspec.lowpass_sigma = 12.5/(2*TR)
    >>> wf.run() # doctest: +SKIP

    """

    restpreproc = pe.Workflow(name=name)

    # Define nodes
    inputnode = pe.Node(interface=util.IdentityInterface(fields=[
        'func', 'num_noise_components', 'highpass_sigma', 'lowpass_sigma'
    ]),
                        name='inputspec')
    outputnode = pe.Node(interface=util.IdentityInterface(fields=[
        'noise_mask_file',
        'filtered_file',
    ]),
                         name='outputspec')
    slicetimer = pe.Node(fsl.SliceTimer(), name='slicetimer')
    realigner = create_realign_flow()
    tsnr = pe.Node(TSNR(regress_poly=2), name='tsnr')
    getthresh = pe.Node(interface=fsl.ImageStats(op_string='-p 98'),
                        name='getthreshold')
    threshold_stddev = pe.Node(fsl.Threshold(), name='threshold')
    compcor = pe.Node(util.Function(
        input_names=['realigned_file', 'noise_mask_file', 'num_components'],
        output_names=['noise_components'],
        function=extract_noise_components),
                      name='compcorr')
    remove_noise = pe.Node(fsl.FilterRegressor(filter_all=True),
                           name='remove_noise')
    bandpass_filter = pe.Node(fsl.TemporalFilter(), name='bandpass_filter')

    # Define connections
    restpreproc.connect(inputnode, 'func', slicetimer, 'in_file')
    restpreproc.connect(slicetimer, 'slice_time_corrected_file', realigner,
                        'inputspec.func')
    restpreproc.connect(realigner, 'outputspec.realigned_file', tsnr,
                        'in_file')
    restpreproc.connect(tsnr, 'stddev_file', threshold_stddev, 'in_file')
    restpreproc.connect(tsnr, 'stddev_file', getthresh, 'in_file')
    restpreproc.connect(getthresh, 'out_stat', threshold_stddev, 'thresh')
    restpreproc.connect(realigner, 'outputspec.realigned_file', compcor,
                        'realigned_file')
    restpreproc.connect(threshold_stddev, 'out_file', compcor,
                        'noise_mask_file')
    restpreproc.connect(inputnode, 'num_noise_components', compcor,
                        'num_components')
    restpreproc.connect(tsnr, 'detrended_file', remove_noise, 'in_file')
    restpreproc.connect(compcor, 'noise_components', remove_noise,
                        'design_file')
    restpreproc.connect(inputnode, 'highpass_sigma', bandpass_filter,
                        'highpass_sigma')
    restpreproc.connect(inputnode, 'lowpass_sigma', bandpass_filter,
                        'lowpass_sigma')
    restpreproc.connect(remove_noise, 'out_file', bandpass_filter, 'in_file')
    restpreproc.connect(threshold_stddev, 'out_file', outputnode,
                        'noise_mask_file')
    restpreproc.connect(bandpass_filter, 'out_file', outputnode,
                        'filtered_file')
    return restpreproc
Esempio n. 25
0
Threshold.inputs.args = '-bin'

#Anatomical compcor: for inputs and outputs, see CompCor.
NoiseComponents = pe.MapNode(interface=confounds.ACompCor(),
                             name='NoiseComponents',
                             iterfield=['realigned_file', 'mask_files'])
NoiseComponents.inputs.num_components = 2

#Wraps command **fsl_regfilt**
RegressionFilter = pe.MapNode(interface=fsl.FilterRegressor(),
                              name='RegressionFilter',
                              iterfield=['in_file', 'design_file'])
RegressionFilter.inputs.filter_columns = [1, 2]

#Wraps command **fslmaths**
BandpassFilter = pe.MapNode(interface=fsl.TemporalFilter(),
                            name='BandpassFilter',
                            iterfield=['in_file'])
BandpassFilter.inputs.highpass_sigma = 25

#Wraps the executable command ``bet``.
BrainExtraction = pe.Node(interface=fsl.BET(), name='BrainExtraction')

#Generic datasink module to store structured outputs
io_DataSink = pe.Node(interface=io.DataSink(), name='io_DataSink')
io_DataSink.inputs.base_directory = '/output/'

#Basic interface class generates identity mappings
Parameters = pe.Node(utility.IdentityInterface(fields=["subj_id", "run_num"]),
                     name='Parameters',
                     iterfield=['subj_id'])