def test_detrend(): input_map = dict( args=dict(argstr='%s', ), environ=dict(usedefault=True, ), ignore_exception=dict(usedefault=True, ), in_file=dict( argstr='%s', mandatory=True, ), out_file=dict(argstr='-prefix %s', ), outputtype=dict(), ) instance = afni.Detrend() for key, metadata in input_map.items(): for metakey, value in metadata.items(): yield assert_equal, getattr(instance.inputs.traits()[key], metakey), value
name='fsl_bp_susan_sm{0}_'.format(kernel)) preproc_wf.connect(determine_bp_sigmas, ('out_sigmas', highpass_operand), fsl_bandpass, 'op_string') preproc_wf.connect(maskfunc2, 'out_file', fsl_bandpass, 'in_file') preproc_wf.connect(fsl_bandpass, 'out_file', outputspec, 'fsl_bp_susan_sm{0}_files'.format(kernel)) tsnr = pe.MapNode(conf.TSNR(), iterfield=['in_file'], name='fsl_bp_susan_sm{0}_tsnr_'.format(kernel)) preproc_wf.connect(fsl_bandpass, 'out_file', tsnr, 'in_file') preproc_wf.connect(tsnr, 'tsnr_file', outputspec, 'fsl_bp_susan_sm{0}_tsnr_files'.format(kernel)) # AFNI bandpass afni_detrend = pe.MapNode(afni.Detrend(outputtype='NIFTI_GZ', args='-polort 4'), iterfield=['in_file'], name='afni_bp_susan_sm{0}_'.format(kernel)) preproc_wf.connect(maskfunc2, 'out_file', afni_detrend, 'in_file') preproc_wf.connect(afni_detrend, 'out_file', outputspec, 'afni_bp_susan_sm{0}_files'.format(kernel)) tsnr = pe.MapNode(conf.TSNR(), iterfield=['in_file'], name='afni_bp_susan_sm{0}_tsnr_'.format(kernel)) preproc_wf.connect(afni_detrend, 'out_file', tsnr, 'in_file') preproc_wf.connect(tsnr, 'tsnr_file', outputspec, 'afni_bp_susan_sm{0}_tsnr_files'.format(kernel)) # Nipype bandpass nipype_bandpass = pe.Node(util.Function(
def temporal_variance_mask(threshold, by_slice=False, erosion=False, degree=1): threshold_method = "VAR" if isinstance(threshold, str): regex_match = { "SD": r"([0-9]+(\.[0-9]+)?)\s*SD", "PCT": r"([0-9]+(\.[0-9]+)?)\s*PCT", } for method, regex in regex_match.items(): matched = re.match(regex, threshold) if matched: threshold_method = method threshold_value = matched.groups()[0] try: threshold_value = float(threshold_value) except: raise ValueError( "Error converting threshold value {0} from {1} to a " "floating point number. The threshold value can " "contain SD or PCT for selecting a threshold based on " "the variance distribution, otherwise it should be a " "floating point number.".format(threshold_value, threshold)) if threshold_value < 0: raise ValueError( "Threshold value should be positive, instead of {0}.".format( threshold_value)) if threshold_method is "PCT" and threshold_value >= 100.0: raise ValueError( "Percentile should be less than 100, received {0}.".format( threshold_value)) threshold = threshold_value wf = pe.Workflow(name='tcompcor') input_node = pe.Node(util.IdentityInterface( fields=['functional_file_path', 'mask_file_path']), name='inputspec') output_node = pe.Node(util.IdentityInterface(fields=['mask']), name='outputspec') # C-PAC default performs linear regression while nipype performs quadratic regression detrend = pe.Node(afni.Detrend(args='-polort {0}'.format(degree), outputtype='NIFTI'), name='detrend') wf.connect(input_node, 'functional_file_path', detrend, 'in_file') std = pe.Node(afni.TStat(args='-nzstdev', outputtype='NIFTI'), name='std') wf.connect(input_node, 'mask_file_path', std, 'mask') wf.connect(detrend, 'out_file', std, 'in_file') var = pe.Node(afni.Calc(expr='a*a', outputtype='NIFTI'), name='var') wf.connect(std, 'out_file', var, 'in_file_a') if by_slice: slices = pe.Node(fsl.Slice(), name='slicer') wf.connect(var, 'out_file', slices, 'in_file') mask_slices = pe.Node(fsl.Slice(), name='mask_slicer') wf.connect(input_node, 'mask_file_path', mask_slices, 'in_file') mapper = pe.MapNode( util.IdentityInterface(fields=['out_file', 'mask_file']), name='slice_mapper', iterfield=['out_file', 'mask_file']) wf.connect(slices, 'out_files', mapper, 'out_file') wf.connect(mask_slices, 'out_files', mapper, 'mask_file') else: mapper_list = pe.Node(util.Merge(1), name='slice_mapper_list') wf.connect(var, 'out_file', mapper_list, 'in1') mask_mapper_list = pe.Node(util.Merge(1), name='slice_mask_mapper_list') wf.connect(input_node, 'mask_file_path', mask_mapper_list, 'in1') mapper = pe.Node( util.IdentityInterface(fields=['out_file', 'mask_file']), name='slice_mapper') wf.connect(mapper_list, 'out', mapper, 'out_file') wf.connect(mask_mapper_list, 'out', mapper, 'mask_file') if threshold_method is "PCT": threshold_node = pe.MapNode(Function( input_names=['in_file', 'mask', 'threshold_pct'], output_names=['threshold'], function=compute_pct_threshold, as_module=True), name='threshold_value', iterfield=['in_file', 'mask']) threshold_node.inputs.threshold_pct = threshold_value wf.connect(mapper, 'out_file', threshold_node, 'in_file') wf.connect(mapper, 'mask_file', threshold_node, 'mask') elif threshold_method is "SD": threshold_node = pe.MapNode(Function( input_names=['in_file', 'mask', 'threshold_sd'], output_names=['threshold'], function=compute_sd_threshold, as_module=True), name='threshold_value', iterfield=['in_file', 'mask']) threshold_node.inputs.threshold_sd = threshold_value wf.connect(mapper, 'out_file', threshold_node, 'in_file') wf.connect(mapper, 'mask_file', threshold_node, 'mask') else: threshold_node = pe.MapNode(Function( input_names=['in_file', 'mask', 'threshold'], output_names=['threshold'], function=compute_threshold, as_module=True), name='threshold_value', iterfield=['in_file', 'mask']) threshold_node.inputs.threshold = threshold_value wf.connect(mapper, 'out_file', threshold_node, 'in_file') wf.connect(mapper, 'mask_file', threshold_node, 'mask') threshold_mask = pe.MapNode(interface=fsl.maths.Threshold(), name='threshold', iterfield=['in_file', 'thresh']) threshold_mask.inputs.args = '-bin' wf.connect(mapper, 'out_file', threshold_mask, 'in_file') wf.connect(threshold_node, 'threshold', threshold_mask, 'thresh') merge_slice_masks = pe.Node(interface=fsl.Merge(), name='merge_slice_masks') merge_slice_masks.inputs.dimension = 'z' wf.connect(threshold_mask, 'out_file', merge_slice_masks, 'in_files') wf.connect(merge_slice_masks, 'merged_file', output_node, 'mask') return wf
# prepare the nuisance factors df = pd.concat([ dread['X'], dread['Y'], dread['Z'], dread['RotX'], dread['RotY'], dread['RotZ'], dread['FramewiseDisplacement'], dread['aCompCor00'], dread['aCompCor01'], dread['aCompCor02'], dread['aCompCor03'], dread['aCompCor04'], dread['aCompCor05'] ], axis=1) design_out = conf_fil[:-4] + '_small.csv' print(design_out) df.to_csv(design_out, sep='\t', index=False, header=False) # regress out nuisance factors using fls_glm glm = fsl.GLM(in_file=img_removed, mask=img_mask, design=design_out, demean=True, out_res_name=img_removed[:-7] + '_denois.nii.gz', output_type='NIFTI_GZ') glm.run() # detrend timeseries using afni-3dDetrend detrend = afni.Detrend() detrend.inputs.in_file = img_removed[:-7] + '_denois.nii.gz' detrend.inputs.args = '-polort 2' detrend.inputs.outputtype = 'NIFTI_GZ' detrend.inputs.out_file = img_removed[:-7] + '_denois_detrend.nii.gz' print(detrend.cmdline) detrend.run()
def create_resting(): # main workflow func_preproc = Workflow(name='resting') inputnode = Node(util.IdentityInterface(fields=[ 'subject', 'out_dir', 'freesurfer_dir', 'func', 'fmap_mag', 'fmap_phase', 'anat_head', 'anat_brain', 'anat_brain_mask', 'vol_to_remove', 'TR', 'epi_resolution', 'echo_space', 'te_diff', 'pe_dir' ]), name='inputnode') # 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') # workflow for motion correction moco = create_moco_pipeline() # workflow for fieldmap correction and coregistration #have to rename subject for fu def rename_subject_for_fu(input_id): output_id = input_id + "_fu" return output_id #modify subject name so it can be saved in the same folder as other LIFE- freesurfer data rename = Node(util.Function(input_names=['input_id'], output_names=['output_id'], function=rename_subject_for_fu), name="rename") fmap_coreg = create_fmap_coreg_pipeline() # workflow for applying transformations to timeseries transform_ts = create_transform_pipeline() #detrending detrend = Node(afni.Detrend(), name="detrend") detrend.inputs.args = '-polort 2' detrend.inputs.outputtype = "NIFTI" #outputnode outputnode = Node(util.IdentityInterface(fields=[ 'par', 'rms', 'mean_epi', 'tsnr', 'fmap', 'unwarped_mean_epi2fmap', 'coregistered_epi2fmap', 'fmap_fullwarp', 'epi2anat', 'epi2anat_mat', 'epi2anat_dat', 'epi2anat_mincost', 'full_transform_ts', 'full_transform_mean', 'resamp_brain', 'detrended_epi' ]), name='outputnode') # connections func_preproc.connect([ #remove the first volumes (inputnode, remove_vol, [('func', 'in_file')]), (inputnode, remove_vol, [('vol_to_remove', 't_min')]), #align volumes and motion correction (remove_vol, moco, [('out_file', 'inputnode.epi')]), #prepare field map (inputnode, rename, [('subject', 'input_id')]), (rename, fmap_coreg, [('output_id', 'inputnode.fs_subject_id')]), (inputnode, fmap_coreg, [('fmap_phase', 'inputnode.phase'), ('freesurfer_dir', 'inputnode.fs_subjects_dir'), ('echo_space', 'inputnode.echo_space'), ('te_diff', 'inputnode.te_diff'), ('pe_dir', 'inputnode.pe_dir'), ('fmap_mag', 'inputnode.mag'), ('anat_head', 'inputnode.anat_head'), ('anat_brain', 'inputnode.anat_brain')]), (moco, fmap_coreg, [('outputnode.epi_mean', 'inputnode.epi_mean')]), #transform ts (remove_vol, transform_ts, [('out_file', 'inputnode.orig_ts')]), (inputnode, transform_ts, [('anat_head', 'inputnode.anat_head')]), (inputnode, transform_ts, [('anat_brain_mask', 'inputnode.brain_mask') ]), (inputnode, transform_ts, [('epi_resolution', 'inputnode.resolution') ]), (moco, transform_ts, [('outputnode.mat_moco', 'inputnode.mat_moco')]), (fmap_coreg, transform_ts, [('outputnode.fmap_fullwarp', 'inputnode.fullwarp')]), (transform_ts, detrend, [('outputnode.trans_ts', 'in_file')]), ##all the output ( moco, outputnode, [ #('outputnode.epi_moco', 'realign.@realigned_ts'), ('outputnode.par_moco', 'par'), ('outputnode.rms_moco', 'rms'), ('outputnode.epi_mean', 'mean_epi'), ('outputnode.tsnr_file', 'tsnr') ]), (fmap_coreg, outputnode, [('outputnode.fmap', 'fmap'), ('outputnode.unwarped_mean_epi2fmap', 'unwarped_mean_epi2fmap'), ('outputnode.epi2fmap', 'coregistered_epi2fmap'), ('outputnode.fmap_fullwarp', 'fmap_fullwarp'), ('outputnode.epi2anat', 'epi2anat'), ('outputnode.epi2anat_mat', 'epi2anat_mat'), ('outputnode.epi2anat_dat', 'epi2anat_dat'), ('outputnode.epi2anat_mincost', 'epi2anat_mincost')]), (transform_ts, outputnode, [('outputnode.trans_ts', 'full_transform_ts'), ('outputnode.trans_ts_mean', 'full_transform_mean'), ('outputnode.resamp_brain', 'resamp_brain')]), (detrend, outputnode, [('out_file', 'detrended_epi')]) ]) return func_preproc