# collect all the con images for each contrast. contrast_ids = list(range(1, len(contrasts) + 1)) l2source = pe.Node(nio.DataGrabber(infields=['fwhm', 'con']), name="l2source") # we use .*i* to capture both .img (SPM8) and .nii (SPM12) l2source.inputs.template = os.path.abspath( 'spm_dartel_tutorial/l1output/*/con*/*/_fwhm_%d/con_%04d.*i*') # iterate over all contrast images l2source.iterables = [('fwhm', fwhmlist), ('con', contrast_ids)] l2source.inputs.sort_filelist = True """Use :class:`nipype.interfaces.spm.OneSampleTTestDesign` to perform a simple statistical analysis of the contrasts from the group of subjects (n=2 in this example). """ # setup a 1-sample t-test node onesamplettestdes = pe.Node(spm.OneSampleTTestDesign(), name="onesampttestdes") l2estimate = pe.Node(spm.EstimateModel(), name="level2estimate") l2estimate.inputs.estimation_method = {'Classical': 1} l2conestimate = pe.Node(spm.EstimateContrast(), name="level2conestimate") cont1 = ('Group', 'T', ['mean'], [1]) l2conestimate.inputs.contrasts = [cont1] l2conestimate.inputs.group_contrast = True """As before, we setup a pipeline to connect these two nodes (l2source -> onesamplettest). """ l2pipeline = pe.Workflow(name="level2") l2pipeline.base_dir = os.path.abspath('spm_dartel_tutorial/l2output') l2pipeline.connect([ (l2source, onesamplettestdes, [('outfiles', 'in_files')]), (onesamplettestdes, l2estimate, [('spm_mat_file', 'spm_mat_file')]),
# collect all the con images for each contrast. contrast_ids = range(1, len(contrasts) + 1) l2source = pe.Node(nio.DataGrabber(infields=['fwhm', 'con']), name="l2source") # we use .*i* to capture both .img (SPM8) and .nii (SPM12) l2source.inputs.template = os.path.abspath( 'spm_tutorial2/l1output/*/con*/*/_fwhm_%d/con_%04d.*i*') # iterate over all contrast images l2source.iterables = [('fwhm', fwhmlist), ('con', contrast_ids)] l2source.inputs.sort_filelist = True """Use :class:`nipype.interfaces.spm.OneSampleTTestDesign` to perform a simple statistical analysis of the contrasts from the group of subjects (n=2 in this example). """ # setup a 1-sample t-test node onesamplettestdes = pe.Node(interface=spm.OneSampleTTestDesign(), name="onesampttestdes") l2estimate = pe.Node(interface=spm.EstimateModel(), name="level2estimate") l2estimate.inputs.estimation_method = {'Classical': 1} l2conestimate = pe.Node(interface=spm.EstimateContrast(), name="level2conestimate") cont1 = ('Group', 'T', ['mean'], [1]) l2conestimate.inputs.contrasts = [cont1] l2conestimate.inputs.group_contrast = True """As before, we setup a pipeline to connect these two nodes (l2source -> onesamplettest). """ l2pipeline = pe.Workflow(name="level2") l2pipeline.base_dir = os.path.abspath('spm_tutorial2/l2output') l2pipeline.connect([
Grab the data """ #Node: DataGrabber - to collect all the con images for each contrast l2volSource = pe.Node(nio.DataGrabber(infields=['con']), name="l2volSource") path2normcons = experiment_dir + '/results/level1_output/subject*/normcons/ants_con_%04d.nii' l2volSource.inputs.template = path2normcons l2volSource.iterables = [('con',contrast_ids)] # iterate over all contrast images """ Define nodes """ #Node: OneSampleTTest - to perform an one sample t-test analysis on the volume oneSampleTTestVolDes = pe.Node(interface=spm.OneSampleTTestDesign(), name="oneSampleTTestVolDes") #Node: EstimateModel - to estimate the model l2estimate = pe.Node(interface=spm.EstimateModel(), name="l2estimate") l2estimate.inputs.estimation_method = {'Classical' : 1} #Node: EstimateContrast - to estimate the contrast (in this example just one) l2conestimate = pe.Node(interface = spm.EstimateContrast(), name="l2conestimate") cont1 = ('Group','T', ['mean'],[1]) l2conestimate.inputs.contrasts = [cont1] l2conestimate.inputs.group_contrast = True #Node: Threshold - to threshold the estimated contrast l2threshold = pe.Node(interface = spm.Threshold(), name="l2threshold") l2threshold.inputs.contrast_index = 1
tImage = glob.glob('/media/Data/work/KPE_SPM/Sink/1stLevel/_subject_id_1263/spmT_00*.nii') for con_image in tImage: nilearn.plotting.plot_glass_brain(con_image, display_mode='lyrz', colorbar=True, plot_abs=False, threshold=2) conImage = glob.glob('/media/Data/work/KPE_SPM/Sink/1stLevel/_subject_id_1263/con_00*.nii') for con_image in tImage: nilearn.plotting.plot_stat_map(nilearn.image.smooth_img(con_image, 6), display_mode='x', threshold=2.3, bg_img=anatimg, dim=1) #, cut_coords=(-5, 0, 5, 10, 15), dim=1) # nilearn.plotting.plot_glass_brain(nilearn.image.smooth_img(con_image, 6), # display_mode='lyrz', colorbar=True, plot_abs=False, threshold=2.3, bg_img=antimg) #%% Gourp analysis - based on SPM - should condifer the fsl Randomize option (other script) # OneSampleTTestDesign - creates one sample T-Test Design onesamplettestdes = Node(spm.OneSampleTTestDesign(), name="onesampttestdes") # EstimateModel - estimates the model level2estimate = Node(spm.EstimateModel(estimation_method={'Classical': 1}), name="level2estimate") # 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']
contrast_ids = list(range(1, len(contrasts) + 1)) l2source = pe.Node(nio.DataGrabber(infields=['fwhm', 'con']), name="l2source") # we use .*i* to capture both .img (SPM8) and .nii (SPM12) l2source.inputs.template = os.path.abspath( 'spm_tutorial/l1output/*/con*/*/_fwhm_%d/con_%04d.*i*') # iterate over all contrast images l2source.iterables = [('fwhm', fwhmlist), ('con', contrast_ids)] l2source.inputs.sort_filelist = True """Use :class:`nipype.interfaces.spm.OneSampleTTestDesign` to perform a simple statistical analysis of the contrasts from the group of subjects (n=2 in this example). """ # setup a 1-sample t-test node onesamplettestdes = pe.Node( interface=spm.OneSampleTTestDesign(), name="onesampttestdes") l2estimate = pe.Node(interface=spm.EstimateModel(), name="level2estimate") l2estimate.inputs.estimation_method = {'Classical': 1} l2conestimate = pe.Node( interface=spm.EstimateContrast(), name="level2conestimate") cont1 = ('Group', 'T', ['mean'], [1]) l2conestimate.inputs.contrasts = [cont1] l2conestimate.inputs.group_contrast = True """As before, we setup a pipeline to connect these two nodes (l2source -> onesamplettest). """ l2pipeline = pe.Workflow(name="level2") l2pipeline.base_dir = os.path.abspath('spm_tutorial/l2output') l2pipeline.connect([ (l2source, onesamplettestdes, [('outfiles', 'in_files')]),
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