def create_extract_inplane_workflow( name='extract_inplane', templates={'inplane': 'Raw/Anatomy/Inplane{id}/'}, format_string='inplane'): extract_inplane = pe.Workflow(name=name) inputs = pe.Node( interface=util.IdentityInterface(fields=['session_dir', 'ref_vol']), name='inputs') get_inplane = pe.Node(interface=nio.SelectFiles(templates), name='get_inplane') inplane_to_nii = pe.Node(interface=ds.DcmStack(), name='inplane_to_nii') inplane_to_nii.inputs.embed_meta = True rename_inplane = pe.Node(interface=util.Rename(format_string), name='rename_inplane') rename_inplane.inputs.keep_ext = True outputs = pe.Node(interface=util.IdentityInterface(fields=['out_file']), name='outputs') extract_inplane.connect(inputs, 'session_dir', get_inplane, 'base_directory') extract_inplane.connect(inputs, ('ref_vol', ref_vol_to_inplane_id), get_inplane, 'id') extract_inplane.connect(get_inplane, 'inplane', inplane_to_nii, 'dicom_files') extract_inplane.connect(inplane_to_nii, 'out_file', rename_inplane, 'in_file') extract_inplane.connect(rename_inplane, 'out_file', outputs, 'out_file') return extract_inplane
def create_extract_functional_workflow( name='extract_functional', templates={'functional': 'Raw/Functional/Scan_{scan}/'}, format_string='f%(scan)d'): extract_functional = pe.Workflow(name=name) inputs = pe.Node( interface=util.IdentityInterface(fields=['session_dir', 'scan']), name='inputs') get_functional = pe.Node(interface=nio.SelectFiles(templates), name='get_functional') functional_to_nii = pe.Node(interface=ds.DcmStack(), name='functional_to_nii') functional_to_nii.inputs.embed_meta = True rename_functional = pe.Node(interface=util.Rename(format_string), name='rename_functional') rename_functional.inputs.keep_ext = True outputs = pe.Node(interface=util.IdentityInterface(fields=['out_file']), name='outputs') extract_functional.connect(inputs, 'session_dir', get_functional, 'base_directory') extract_functional.connect(inputs, 'scan', get_functional, 'scan') extract_functional.connect(get_functional, 'functional', functional_to_nii, 'dicom_files') extract_functional.connect(functional_to_nii, 'out_file', rename_functional, 'in_file') extract_functional.connect(inputs, 'scan', rename_functional, 'scan') extract_functional.connect(rename_functional, 'out_file', outputs, 'out_file') return extract_functional
####Nipype script begins below#### #Set up iteration over subjects infosource = Node(IdentityInterface(fields=['subject_id']), name='infosource') infosource.iterables = ('subject_id', subject_list) selectfiles = Node(SelectFiles(template), name='selectfiles') selectfiles.inputs.base_directory = rawdir selectfiles.inputs.sort_files = True #Outputs: anat, epi, flair, mask, wm_noise, csf_noise, mni_template ####EPI preprocessing#### #Convert EPI dicoms to nii (with embeded metadata) epi_stack = Node(dcmstack.DcmStack(), name='epistack') epi_stack.inputs.embed_meta = True epi_stack.inputs.out_format = 'epi' epi_stack.inputs.out_ext = '.nii' #Outputs: out_file #Despiking using afni (position based on Jo et al. (2013)). despike = Node(afni.Despike(), name='despike') despike.inputs.outputtype = 'NIFTI' #Outputs: out_file #Slice timing corrected (gets timing from header) st_corr = Node(spm.SliceTiming(), name='slicetiming_correction') st_corr.inputs.ref_slice = 1 #Outputs: timecorrected_files
def get_wf(): import numpy as np wf = pe.Workflow(name="main_workflow") wf.base_dir = os.path.join(workingdir, "rs_preprocessing/") wf.config['execution']['crashdump_dir'] = wf.base_dir + "crash_files/" ##Infosource## subject_id_infosource = pe.Node( util.IdentityInterface(fields=['subject_id']), name="subject_id_infosource") subject_id_infosource.iterables = ('subject_id', subjects) session_infosource = pe.Node(util.IdentityInterface(fields=['session']), name="session_infosource") session_infosource.iterables = ('session', sessions) hemi_infosource = pe.Node(util.IdentityInterface(fields=['hemi']), name="hemi_infosource") hemi_infosource.iterables = ('hemi', hemispheres) fs_infosource = pe.Node(util.IdentityInterface(fields=['fs']), name="fs_infosource") fs_infosource.iterables = ('fs', fsaverage) ##Datagrabber## datagrabber = pe.Node(nio.DataGrabber( infields=['subject_id'], outfields=['resting_nifti', 't1_nifti', 'dicoms']), name="datagrabber", overwrite=True) datagrabber.inputs.base_directory = '/' datagrabber.inputs.template = '*' datagrabber.inputs.field_template = rs_preprocessing_dg_template datagrabber.inputs.template_args = rs_preprocessing_dg_args datagrabber.inputs.sort_filelist = True wf.connect(subject_id_infosource, 'subject_id', datagrabber, 'subject_id') wf.connect(session_infosource, 'session', datagrabber, 'session') ##DcmStack & MetaData## dicom_filedir = pe.Node(name='dicom_filedir', interface=util.Function( input_names=['subject_id', 'session'], output_names=['filedir'], function=construct_dicomfiledir)) stack = pe.Node(dcm.DcmStack(), name='stack') stack.inputs.embed_meta = True tr_lookup = pe.Node(dcm.LookupMeta(), name='tr_lookup') tr_lookup.inputs.meta_keys = {'RepetitionTime': 'TR'} wf.connect(subject_id_infosource, 'subject_id', dicom_filedir, 'subject_id') wf.connect(session_infosource, 'session', dicom_filedir, 'session') wf.connect(dicom_filedir, 'filedir', stack, 'dicom_files') wf.connect(stack, 'out_file', tr_lookup, 'in_file') ##Preproc from BIPs## preproc = create_rest_prep(name="bips_resting_preproc", fieldmap=False) zscore = preproc.get_node('z_score') preproc.remove_nodes([zscore]) mod_realign = preproc.get_node('mod_realign') mod_realign.plugin_args = {'submit_specs': 'request_memory=4000\n'} #workaround for realignment crashing in multiproc environment mod_realign.run_without_submitting = True # inputs preproc.inputs.inputspec.motion_correct_node = 'nipy' ad = preproc.get_node('artifactdetect') preproc.disconnect(mod_realign, 'parameter_source', ad, 'parameter_source') ad.inputs.parameter_source = 'NiPy' preproc.inputs.inputspec.realign_parameters = { "loops": [5], "speedup": [5] } preproc.inputs.inputspec.do_whitening = False preproc.inputs.inputspec.timepoints_to_remove = 4 preproc.inputs.inputspec.smooth_type = 'susan' preproc.inputs.inputspec.do_despike = False preproc.inputs.inputspec.surface_fwhm = 0.0 preproc.inputs.inputspec.num_noise_components = 6 preproc.inputs.inputspec.regress_before_PCA = False preproc.get_node('fwhm_input').iterables = ('fwhm', [0, 5]) preproc.get_node('take_mean_art').get_node( 'strict_artifact_detect').inputs.save_plot = True preproc.inputs.inputspec.ad_normthresh = 1 preproc.inputs.inputspec.ad_zthresh = 3 preproc.inputs.inputspec.do_slicetime = True preproc.inputs.inputspec.compcor_select = [True, True] preproc.inputs.inputspec.filter_type = 'fsl' preproc.get_node('bandpass_filter').iterables = [('highpass_freq', [0.01]), ('lowpass_freq', [0.1])] preproc.inputs.inputspec.reg_params = [ True, True, True, False, True, False ] preproc.inputs.inputspec.fssubject_dir = freesurferdir #preproc.inputs.inputspec.tr = 1400/1000 #preproc.inputs.inputspec.motion_correct_node = 'afni' #preproc.inputs.inputspec.sliceorder = slicetime_file #preproc.inputs.inputspec.sliceorder = list(np.linspace(0,1.4,64)) def convert_units(tr): mstr = (tr * .001) return tr def get_sliceorder(in_file): import nipype.interfaces.dcmstack as dcm import numpy as np nii_wrp = dcm.NiftiWrapper.from_filename(in_file) sliceorder = np.argsort( np.argsort( nii_wrp.meta_ext.get_values('CsaImage.MosaicRefAcqTimes') [0])).tolist() return sliceorder wf.connect(tr_lookup, ("TR", convert_units), preproc, "inputspec.tr") wf.connect(stack, ('out_file', get_sliceorder), preproc, "inputspec.sliceorder") wf.connect(subject_id_infosource, 'subject_id', preproc, "inputspec.fssubject_id") wf.connect(datagrabber, "resting_nifti", preproc, "inputspec.func") ##Sampler## sampler = pe.Node(fs.SampleToSurface(), name='sampler') sampler.inputs.sampling_method = 'average' sampler.inputs.sampling_range = (0.2, 0.8, 0.1) sampler.inputs.sampling_units = 'frac' sampler.inputs.interp_method = 'nearest' sampler.inputs.out_type = 'nii' wf.connect(preproc, ('bandpass_filter.out_file', list_to_filename), sampler, 'source_file') wf.connect(preproc, ('getmask.register.out_reg_file', list_to_filename), sampler, 'reg_file') wf.connect(hemi_infosource, 'hemi', sampler, 'hemi') ##SXFM## sxfm = pe.Node(fs.SurfaceTransform(), name='sxfm') sxfm.inputs.args = '--cortex --fwhm-src 5 --noreshape' sxfm.inputs.target_type = 'nii' wf.connect(sampler, 'out_file', sxfm, 'source_file') wf.connect(subject_id_infosource, 'subject_id', sxfm, 'source_subject') wf.connect(hemi_infosource, 'hemi', sxfm, 'hemi') wf.connect(fs_infosource, 'fs', sxfm, 'target_subject') ########### #report_wf = create_preproc_report_wf(os.path.join(preprocdir, "reports")) #report_wf.inputs.inputspec.fssubjects_dir = preproc.inputs.inputspec.fssubject_dir def pick_full_brain_ribbon(l): import os for path in l: if os.path.split(path)[1] == "ribbon.mgz": return path #wf.connect(preproc,"artifactdetect.plot_files", report_wf, "inputspec.art_detect_plot") #wf.connect(preproc,"take_mean_art.weighted_mean.mean_image", report_wf, "inputspec.mean_epi") #wf.connect(preproc,("getmask.register.out_reg_file", list_to_filename), report_wf, "inputspec.reg_file") #wf.connect(preproc,("getmask.fssource.ribbon",pick_full_brain_ribbon), report_wf, "inputspec.ribbon") #wf.connect(preproc,("CompCor.tsnr.tsnr_file", list_to_filename), report_wf, "inputspec.tsnr_file") #wf.connect(subject_id_infosource, 'subject_id', report_wf, "inputspec.subject_id") ##Datasink## ds = pe.Node(nio.DataSink(), name="datasink") ds.inputs.base_directory = os.path.join(preprocdir, "aimivolumes") wf.connect(preproc, 'bandpass_filter.out_file', ds, "preprocessed_resting") wf.connect(preproc, 'getmask.register.out_fsl_file', ds, "func2anat_transform") wf.connect(sampler, 'out_file', ds, 'sampledtosurf') wf.connect(sxfm, 'out_file', ds, 'sxfmout') #wf.write_graph() return wf
infosource.iterables = [('subject_id', subject_list)] #Select files template = { 'anat': data_dir + '{subject_id}/T1_MPRAGE_SAG_ISO_0_7*/*.IMA', 'epi_s1': data_dir + '{subject_id}/*PACE_MOCO_P2-1*/*.IMA', 'epi_s2': data_dir + '{subject_id}/*PACE_MOCO_P2-2*/*.IMA', 'template': template_dir + 't1_3mm_brain.nii', 'mask': template_dir + 't1_3mm_mask.nii' } select_files = Node(SelectFiles(template), name='select_files') select_files.inputs.base_directory = h_dir select_files.inputs.sort_files = True anat_stack = Node(dcmstack.DcmStack(), name='anat_stack') anat_stack.inputs.embed_meta = True anat_stack.inputs.out_format = 'anat' anat_stack.inputs.out_ext = '.nii' epi_s1_stack = Node(dcmstack.DcmStack(), name='epi_s1_stack') epi_s1_stack.inputs.embed_meta = True epi_s1_stack.inputs.out_format = 'epi1' epi_s1_stack.inputs.out_ext = '.nii' epi_s2_stack = Node(dcmstack.DcmStack(), name='epi_s2_stack') epi_s2_stack.inputs.embed_meta = True epi_s2_stack.inputs.out_format = 'epi2' epi_s2_stack.inputs.out_ext = '.nii' st_corr = Node(spm.SliceTiming(), name='slicetiming_correction')