orig = genfromtxt(orig, delimiter=' ', dtype=None, skip_header=0) new = 'func_roi_ts.txt' savetxt(new, orig, delimiter=' ') new_file = abspath(new) return (new_file) converthex = Node(name='converthex', interface=Function(input_names=['orig'], output_names=['new_file'], function=converthex)) # model ROI connectivity glm = Node(GLM(out_file='betas.nii', out_cope='cope.nii'), name='glm', iterfield='design') # In[4]: sbc1_workflow = Workflow(name='sbc1_workflow') sbc1_workflow.connect([ (infosource, selectfiles, [('subject_id', 'subject_id')]), (selectfiles, ROI_timeseries, [('orig_func', 'in_file')]), (infosource, ROI_timeseries, [('ROIs', 'mask')]), (ROI_timeseries, converthex, [('out_file', 'orig')]), (converthex, glm, [('new_file', 'design')]), (selectfiles, glm, [('orig_func', 'in_file')]), (converthex, datasink, [('new_file', 'roi_ts')]), (glm, datasink, [('out_cope', 'glm_seed_copes')]),
def rs_preprocess(in_file, fwhm, work_dir, output_dir): from nipype.workflows.fmri.fsl.preprocess import create_susan_smooth from nipype.interfaces.fsl.model import GLM from nipype.interfaces import fsl as fsl # define nodes and workflows rs_preproc_workflow = pe.Workflow(name="rs_preproc_workflow") rs_preproc_workflow.base_dir = work_dir inputnode = pe.Node(interface=util.IdentityInterface(fields=['func', 'fwhm']), name='inputspec') inputnode.inputs.func = in_file inputnode.inputs.fwhm = fwhm #make a brain mask immask = pe.Node(interface=fsl.ImageMaths(op_string = '-abs -bin -Tmin'), name='immask') rs_preproc_workflow.connect(inputnode, 'func', immask, 'in_file') #calculate mean image from smoothed data, for adding back after GSR immean = pe.Node(interface=fsl.ImageMaths(op_string = '-Tmean'), name='immean') rs_preproc_workflow.connect(inputnode, 'func', immean, 'in_file') #get time-series for GSR meants = pe.Node(interface=fsl.utils.ImageMeants(), name='meants') rs_preproc_workflow.connect(inputnode, 'func', meants, 'in_file') rs_preproc_workflow.connect(immask, 'out_file', meants, 'mask') #removing global signal glm = pe.Node(interface=GLM(), name='glm') glm.inputs.out_res_name = op.join(work_dir, 'res4d.nii.gz') rs_preproc_workflow.connect(inputnode, 'func', glm, 'in_file') rs_preproc_workflow.connect(immask, 'out_file', glm, 'mask') rs_preproc_workflow.connect(meants, 'out_file', glm, 'design') #add mean back to GSR'ed image maths = pe.Node(interface=fsl.maths.BinaryMaths(operation = 'add'), name='maths') rs_preproc_workflow.connect(glm, 'out_res', maths, 'in_file') rs_preproc_workflow.connect(immean, 'out_file', maths, 'operand_file') #do the smoothing smooth = create_susan_smooth() rs_preproc_workflow.connect(maths, 'out_file', smooth, 'inputnode.in_files') rs_preproc_workflow.connect(inputnode, 'fwhm', smooth, 'inputnode.fwhm') rs_preproc_workflow.connect(immask, 'out_file', smooth, 'inputnode.mask_file') datasink = pe.Node(nio.DataSink(), name='sinker') datasink.inputs.base_directory = work_dir rs_preproc_workflow.connect(maths, 'out_file', datasink, 'gsr') rs_preproc_workflow.connect(immask, 'out_file', datasink, 'mask') rs_preproc_workflow.connect(smooth.get_node('smooth'), ('smoothed_file', pickfirst), datasink, 'gsr_smooth') rs_preproc_workflow.run() #copy data to directory gsr_fn = glob(op.join(work_dir, 'gsr', '*.nii.gz'))[0] mask_fn = glob(op.join(work_dir, 'mask', '*.nii.gz'))[0] gsr_smooth_fn = glob(op.join(work_dir, 'gsr_smooth', '*', '*.nii.gz'))[0] gsr_fn2 = op.join(output_dir, '{0}.nii.gz'.format(op.basename(in_file).split('.')[0])) mask_fn2 = op.join(output_dir, '{0}_mask.nii.gz'.format(op.basename(in_file).split('.')[0])) gsr_smooth_fn2 = op.join(output_dir, '{0}_smooth.nii.gz'.format(op.basename(in_file).split('.')[0])) shutil.copyfile(gsr_fn, gsr_fn2) shutil.copyfile(mask_fn, mask_fn2) shutil.copyfile(gsr_smooth_fn, gsr_smooth_fn2) shutil.rmtree(work_dir)
noise_file = 'noise_matrix.txt' savetxt(noise_file, noise_matrix, delimiter='\t') noise_filepath = path.abspath(noise_file) return (noise_filepath) noise_mat = Node(name='noise_mat', interface=Function(input_names=[ 'vols_to_censor', 'motion_params', 'comp_noise' ], output_names=['noise_filepath'], function=create_noise_matrix)) denoise = Node(GLM(out_res_name='denoised_residuals.nii', out_data_name='denoised_func.nii'), name='denoise') # band pass filtering- all rates are in Hz (1/TR or samples/second) def bandpass_filter(in_file, lowpass, highpass, TR): import numpy as np import nibabel as nb from os import path from nipype.interfaces.afni.preprocess import Bandpass from nipype import config, logging config.enable_debug_mode() logging.update_logging(config) out_file = 'func_filtered.nii' bp = Bandpass()
from os.path import abspath orig = genfromtxt(orig, delimiter=' ', dtype=None, skip_header=0) new = 'func_roi_ts.txt' savetxt(new, orig, delimiter=' ') new_file = abspath(new) return(new_file) converthex = Node(name='converthex', interface=Function(input_names=['orig'], output_names=['new_file'], function=converthex)) # model ROI connectivity glm = Node(GLM(out_file='betas.nii',out_cope='cope.nii'), name='glm', iterfield='design') # In[4]: sbc1_workflow2 = Workflow(name='sbc1_workflow2') sbc1_workflow2.connect([(infosource,selectfiles,[('subject_id','subject_id')]), (selectfiles,ROI_timeseries,[('orig_func','in_file')]), (infosource,ROI_timeseries,[('ROIs','mask')]), (ROI_timeseries,converthex,[('out_file','orig')]), (converthex,glm,[('new_file','design')]), (selectfiles,glm,[('orig_func','in_file')]), (converthex, datasink, [('new_file','roi_ts')]), (glm,datasink,[('out_cope','glm_seed_copes')]), (glm,datasink,[('out_file','glm_betas')])