def __init__(self, datasink, TR, num_vol): # specify input and output nodes self.datasink = datasink self.TR = TR self.num_vol = num_vol # specify nodes # SpecifyModel - Generates SPM-specific Model self.modelspec = pe.Node(interface=model.SpecifySPMModel(), name='model_specification') self.modelspec.inputs.input_units = 'secs' self.modelspec.inputs.output_units = 'secs' self.modelspec.inputs.time_repetition = self.TR self.modelspec.inputs.high_pass_filter_cutoff = 128 subjectinfo = [ Bunch(conditions=['None'], onsets=[list(range(self.num_vol))], durations=[[0.5]]) ] self.modelspec.inputs.subject_info = subjectinfo # Level1Design - Generates an SPM design matrix self.level1design = pe.Node(interface=spm.Level1Design(), name='first_level_design') self.level1design.inputs.bases = {'hrf': {'derivs': [1, 1]}} self.level1design.inputs.interscan_interval = self.TR self.level1design.inputs.timing_units = 'secs' # EstimateModel - estimate the parameters of the model # method can be 'Classical', 'Bayesian' or 'Bayesian2' self.level1estimate = pe.Node(interface=spm.EstimateModel(), name="first_level_estimate") self.level1estimate.inputs.estimation_method = {'Classical': 1} self.threshold = pe.Node(interface=spm.Threshold(), name="threshold") self.threshold.inputs.contrast_index = 1 # EstimateContrast - estimates contrasts self.contrast_estimate = pe.Node(interface=spm.EstimateContrast(), name="contrast_estimate") cont1 = ('active > rest', 'T', ['None'], [1]) contrasts = [cont1] self.contrast_estimate.inputs.contrasts = contrasts # specify workflow instance self.workflow = pe.Workflow(name='first_level_analysis_workflow') # connect nodes self.workflow.connect([ (self.modelspec, self.level1design, [('session_info', 'session_info')]), (self.level1design, self.level1estimate, [('spm_mat_file', 'spm_mat_file')]), (self.level1estimate, self.contrast_estimate, [('spm_mat_file', 'spm_mat_file'), ('beta_images', 'beta_images'), ('residual_image', 'residual_image')]), # (self.contrast_estimate, self.threshold, [('spm_mat_file', 'spm_mat_file'), ('spmT_images', 'stat_image')]), (self.contrast_estimate, self.datasink, [('con_images', 'contrast_img'), ('spmT_images', 'contrast_T')]) ])
def CreateTopoFDRwithGGMM(name="topo_fdr_with_ggmm", correct=False): inputnode = pe.Node(interface=util.IdentityInterface( fields=['stat_image', "spm_mat_file", "contrast_index", "mask_file"]), name="inputnode") ggmm = pe.MapNode(interface=ThresholdGGMM( no_deactivation_class=False, models=[ 'noise_and_activation', 'noise_activation_and_deactivation', 'no_signal', ]), name="ggmm", iterfield=['stat_image']) topo_fdr = pe.MapNode( interface=spm.Threshold(), name="topo_fdr", iterfield=['stat_image', 'contrast_index', 'height_threshold']) topo_fdr.inputs.use_fwe_correction = False topo_fdr.inputs.force_activation = True topo_fdr.inputs.height_threshold_type = 'stat' topo_fdr_with_ggmm = pe.Workflow(name=name) topo_fdr_with_ggmm.connect([ (inputnode, ggmm, [('stat_image', 'stat_image'), ('mask_file', 'mask_file')]), (inputnode, topo_fdr, [('spm_mat_file', 'spm_mat_file'), ('contrast_index', 'contrast_index')]) ]) if correct: topo_fdr_with_ggmm.connect([ (ggmm, topo_fdr, [('corrected_threshold', 'height_threshold'), ('unthresholded_corrected_map', 'stat_image')]) ]) else: topo_fdr_with_ggmm.connect([ (inputnode, topo_fdr, [('stat_image', 'stat_image')]), (ggmm, topo_fdr, [('threshold', 'height_threshold')]) ]) return topo_fdr_with_ggmm
""" modelspec = pe.Node(interface=model.SpecifySPMModel(), name="modelspec") """Generate a first level SPM.mat file for analysis :class:`nipype.interfaces.spm.Level1Design`. """ level1design = pe.Node(interface=spm.Level1Design(), name="level1design") """Use :class:`nipype.interfaces.spm.EstimateModel` to determine the parameters of the model. """ level1estimate = pe.Node(interface=spm.EstimateModel(), name="level1estimate") level1estimate.inputs.estimation_method = {'Classical': 1} threshold = pe.Node(interface=spm.Threshold(), name="threshold") """Use :class:`nipype.interfaces.spm.EstimateContrast` to estimate the first level contrasts specified in a few steps above. """ contrastestimate = pe.Node(interface=spm.EstimateContrast(), name="contrastestimate") def pickfirst(l): return l[0] l1analysis.connect([ (modelspec, level1design, [('session_info', 'session_info')]), (level1design, level1estimate, [('spm_mat_file', 'spm_mat_file')]),
'con_0013', 'con_0014' ] subject_list = [ 2073, 2550, 2582, 2583, 2584, 2585, 2588, 2592, 2593, 2594, 2596, 2597, 2598, 2599, 2600, 2624, 2650, 2651, 2652, 2653, 2654, 2655, 2656, 2657, 2658, 2659, 2660, 2661, 2662, 2663, 2664, 2665, 2666 ] # Threshold - thresholds contrasts level2thresh = Node( spm.Threshold( contrast_index=1, use_topo_fdr=True, # use_fwe_correction=True, # here we can use fwe or fdr, default is true # extent_threshold=10, # height_threshold= 0.05, # default is 0.05 extent_fdr_p_threshold=0.05, height_threshold_type='p-value', # default is p-value ), name="level2thresh") #Infosource - a function free node to iterate over the list of subject names infosource = Node(util.IdentityInterface(fields=['contrast_id', 'subject_id']), name="infosource") infosource.iterables = [('contrast_id', contrast_list)] infosource.inputs.subject_id = subject_list # SelectFiles - to grab the data (alternative to DataGrabber) templates = {
'con_0007', 'con_0008', 'con_0009', 'con_0010', 'con_0011', 'con_0012', 'con_0013', 'con_0014', 'con_0015' ] subject_list = [ 2073, 2550, 2582, 2583, 2584, 2585, 2588, 2592, 2593, 2594, 2596, 2597, 2598, 2599, 2600, 2624, 2650, 2651, 2652, 2653, 2654, 2655, 2656, 2657, 2658, 2659, 2660, 2661, 2662, 2663, 2664, 2665, 2666 ] # Threshold - thresholds contrasts level2thresh = Node( spm.Threshold( contrast_index=1, use_topo_fdr=True, use_fwe_correction=True, # here we can use fwe or fdr extent_threshold=10, height_threshold=0.05, extent_fdr_p_threshold=0.05, height_threshold_type='p-value'), name="level2thresh") #Infosource - a function free node to iterate over the list of subject names infosource = Node(util.IdentityInterface(fields=['contrast_id', 'subject_id']), name="infosource") infosource.iterables = [('contrast_id', contrast_list)] infosource.inputs.subject_id = subject_list # SelectFiles - to grab the data (alternative to DataGrabber) templates = { 'cons':
# EstimateContrast - estimates group contrast level2conestimate = Node(spm.EstimateContrast(group_contrast=True), name="level2conestimate") cont1 = ['Group', 'T', ['mean'], [1]] level2conestimate.inputs.contrasts = [cont1] # Which contrasts to use for the 2nd-level analysis contrast_list = [ 'con_0001', 'con_0002', 'con_0003', 'con_0004', 'con_0005', 'con_0006', 'ess_0007' ] # Threshold - thresholds contrasts level2thresh = Node(spm.Threshold(contrast_index=1, use_topo_fdr=True, use_fwe_correction=False, extent_threshold=0, height_threshold=0.005, extent_fdr_p_threshold=0.1, height_threshold_type='p-value'), name="level2thresh") #Infosource - a function free node to iterate over the list of subject names infosource = Node(util.IdentityInterface(fields=['contrast_id']), name="infosource") infosource.iterables = [('contrast_id', contrast_list)] # SelectFiles - to grab the data (alternative to DataGrabber) templates = { 'cons': opj('/media/Data/work/datasink/1stLevel/_sub*/', '{contrast_id}.nii') } selectfiles = Node(SelectFiles(templates,
def create_model_fit_pipeline(high_pass_filter_cutoff=128, nipy=False, ar1=True, name="model", save_residuals=False): inputnode = pe.Node(interface=util.IdentityInterface(fields=[ 'outlier_files', "realignment_parameters", "functional_runs", "mask", 'conditions', 'onsets', 'durations', 'TR', 'contrasts', 'units', 'sparse' ]), name="inputnode") modelspec = pe.Node(interface=model.SpecifySPMModel(), name="modelspec") if high_pass_filter_cutoff: modelspec.inputs.high_pass_filter_cutoff = high_pass_filter_cutoff create_subject_info = pe.Node(interface=util.Function( input_names=['conditions', 'onsets', 'durations'], output_names=['subject_info'], function=create_subject_inf), name="create_subject_info") modelspec.inputs.concatenate_runs = True #modelspec.inputs.input_units = units modelspec.inputs.output_units = "secs" #modelspec.inputs.time_repetition = tr #modelspec.inputs.subject_info = subjectinfo model_pipeline = pe.Workflow(name=name) model_pipeline.connect([ (inputnode, create_subject_info, [('conditions', 'conditions'), ('onsets', 'onsets'), ('durations', 'durations')]), (inputnode, modelspec, [('realignment_parameters', 'realignment_parameters'), ('functional_runs', 'functional_runs'), ('outlier_files', 'outlier_files'), ('units', 'input_units'), ('TR', 'time_repetition')]), (create_subject_info, modelspec, [('subject_info', 'subject_info')]), ]) if nipy: model_estimate = pe.Node(interface=FitGLM(), name="level1estimate") model_estimate.inputs.TR = tr model_estimate.inputs.normalize_design_matrix = True model_estimate.inputs.save_residuals = save_residuals if ar1: model_estimate.inputs.model = "ar1" model_estimate.inputs.method = "kalman" else: model_estimate.inputs.model = "spherical" model_estimate.inputs.method = "ols" model_pipeline.connect([ (modelspec, model_estimate, [('session_info', 'session_info')]), (inputnode, model_estimate, [('mask', 'mask')]) ]) if contrasts: contrast_estimate = pe.Node(interface=EstimateContrast(), name="contrastestimate") contrast_estimate.inputs.contrasts = contrasts model_pipeline.connect([ (model_estimate, contrast_estimate, [("beta", "beta"), ("nvbeta", "nvbeta"), ("s2", "s2"), ("dof", "dof"), ("axis", "axis"), ("constants", "constants"), ("reg_names", "reg_names")]), (inputnode, contrast_estimate, [('mask', 'mask')]), ]) else: level1design = pe.Node(interface=spm.Level1Design(), name="level1design") level1design.inputs.bases = {'hrf': {'derivs': [0, 0]}} if ar1: level1design.inputs.model_serial_correlations = "AR(1)" else: level1design.inputs.model_serial_correlations = "none" level1design.inputs.timing_units = modelspec.inputs.output_units #level1design.inputs.interscan_interval = modelspec.inputs.time_repetition # if sparse: # level1design.inputs.microtime_resolution = n_slices*2 # else: # level1design.inputs.microtime_resolution = n_slices #level1design.inputs.microtime_onset = ref_slice microtime_resolution = pe.Node(interface=util.Function( input_names=['volume', 'sparse'], output_names=['microtime_resolution'], function=_get_microtime_resolution), name="microtime_resolution") level1estimate = pe.Node(interface=spm.EstimateModel(), name="level1estimate") level1estimate.inputs.estimation_method = {'Classical': 1} contrastestimate = pe.Node(interface=spm.EstimateContrast(), name="contrastestimate") #contrastestimate.inputs.contrasts = contrasts threshold = pe.MapNode(interface=spm.Threshold(), name="threshold", iterfield=['contrast_index', 'stat_image']) #threshold.inputs.contrast_index = range(1,len(contrasts)+1) threshold_topo_ggmm = neuroutils.CreateTopoFDRwithGGMM( "threshold_topo_ggmm") #threshold_topo_ggmm.inputs.inputnode.contrast_index = range(1,len(contrasts)+1) model_pipeline.connect([ (modelspec, level1design, [('session_info', 'session_info')]), (inputnode, level1design, [('mask', 'mask_image'), ('TR', 'interscan_interval'), (("functional_runs", get_ref_slice), "microtime_onset")]), (inputnode, microtime_resolution, [("functional_runs", "volume"), ("sparse", "sparse")]), (microtime_resolution, level1design, [("microtime_resolution", "microtime_resolution")]), (level1design, level1estimate, [('spm_mat_file', 'spm_mat_file')]), (inputnode, contrastestimate, [('contrasts', 'contrasts')]), (level1estimate, contrastestimate, [('spm_mat_file', 'spm_mat_file'), ('beta_images', 'beta_images'), ('residual_image', 'residual_image')]), (contrastestimate, threshold, [('spm_mat_file', 'spm_mat_file'), ('spmT_images', 'stat_image')]), (inputnode, threshold, [(('contrasts', _get_contrast_index), 'contrast_index')]), (level1estimate, threshold_topo_ggmm, [('mask_image', 'inputnode.mask_file')]), (contrastestimate, threshold_topo_ggmm, [('spm_mat_file', 'inputnode.spm_mat_file'), ('spmT_images', 'inputnode.stat_image')]), (inputnode, threshold_topo_ggmm, [(('contrasts', _get_contrast_index), 'inputnode.contrast_index') ]), ]) return model_pipeline
def create_2lvl(do_one_sample, name="group", mask=None): import nipype.interfaces.fsl as fsl import nipype.interfaces.spm as spm import nipype.pipeline.engine as pe import nipype.interfaces.utility as niu wk = pe.Workflow(name=name) inputspec = pe.Node(niu.IdentityInterface(fields=[ 'copes', 'estimation_method', 'template', "contrasts", "include_intercept", "regressors", "p_thresh", "height_thresh", 'min_cluster_size' ]), name='inputspec') if do_one_sample: model = pe.Node(spm.OneSampleTTestDesign(), name='onesample') else: model = pe.Node(spm.MultipleRegressionDesign(), name='l2model') wk.connect(inputspec, 'regressors', model, "user_covariates") wk.connect(inputspec, 'include_intercept', model, 'include_intercept') est_model = pe.Node(spm.EstimateModel(), name='estimate_model') wk.connect(inputspec, 'copes', model, 'in_files') wk.connect(inputspec, 'estimation_method', est_model, 'estimation_method') wk.connect(model, 'spm_mat_file', est_model, 'spm_mat_file') if mask == None: bet = pe.Node(fsl.BET(mask=True, frac=0.3, output_type='NIFTI'), name="template_brainmask") wk.connect(inputspec, 'template', bet, 'in_file') wk.connect(bet, 'mask_file', model, 'explicit_mask_file') else: wk.connect(inputspec, 'template', model, 'explicit_mask_file') est_cont = pe.Node(spm.EstimateContrast(group_contrast=True), name='estimate_contrast') wk.connect(inputspec, 'contrasts', est_cont, "contrasts") wk.connect(est_model, 'spm_mat_file', est_cont, "spm_mat_file") wk.connect(est_model, 'residual_image', est_cont, "residual_image") wk.connect(est_model, 'beta_images', est_cont, "beta_images") thresh = pe.MapNode(spm.Threshold(use_fwe_correction=False, use_topo_fdr=True, height_threshold_type='p-value'), name='fdr', iterfield=['stat_image', 'contrast_index']) wk.connect(est_cont, 'spm_mat_file', thresh, 'spm_mat_file') wk.connect(est_cont, 'spmT_images', thresh, 'stat_image') wk.connect(inputspec, 'min_cluster_size', thresh, 'extent_threshold') count = lambda x: range(1, len(x) + 1) wk.connect(inputspec, ('contrasts', count), thresh, 'contrast_index') wk.connect(inputspec, 'p_thresh', thresh, 'extent_fdr_p_threshold') wk.connect(inputspec, 'height_thresh', thresh, 'height_threshold') outputspec = pe.Node(niu.IdentityInterface(fields=[ 'RPVimage', 'beta_images', 'mask_image', 'residual_image', 'con_images', 'ess_images', 'spmF_images', 'spmT_images', 'spm_mat_file', 'pre_topo_fdr_map', 'thresholded_map' ]), name='outputspec') wk.connect(est_model, 'RPVimage', outputspec, 'RPVimage') wk.connect(est_model, 'beta_images', outputspec, 'beta_images') wk.connect(est_model, 'mask_image', outputspec, 'mask_image') wk.connect(est_model, 'residual_image', outputspec, 'residual_image') wk.connect(est_cont, 'con_images', outputspec, 'con_images') wk.connect(est_cont, 'ess_images', outputspec, 'ess_images') wk.connect(est_cont, 'spmF_images', outputspec, 'spmF_images') wk.connect(est_cont, 'spmT_images', outputspec, 'spmT_images') wk.connect(est_cont, 'spm_mat_file', outputspec, 'spm_mat_file') wk.connect(thresh, 'pre_topo_fdr_map', outputspec, 'pre_topo_fdr_map') wk.connect(thresh, 'thresholded_map', outputspec, 'thresholded_map') return wk