# additional ``kwargs`` for more informative report paradigm=paradigm.__dict__, TR=tr, n_scans=n_scans, hfcut=hfcut, frametimes=frametimes, drift_model=drift_model, hrf_model=hrf_model, ) ProgressReport().finish_dir(subject_session_output_dir) print "Statistic report written to %s\r\n" % stats_report_filename # compute a population-level mask as the intersection of individual masks print "Inter-session GLM" grp_mask = nibabel.Nifti1Image(intersect_masks(mask_images).astype(np.int8), nibabel.load(mask_images[0]).get_affine()) second_level_z_maps = {} design_matrix = np.ones(len(first_level_effects_maps) )[:, np.newaxis] # only the intercept for contrast_id in contrasts: print "\tcontrast id: %s" % contrast_id # effects maps will be the input to the second level GLM first_level_image = nibabel.concat_images( [x[contrast_id] for x in first_level_effects_maps]) # fit 2nd level GLM for given contrast grp_model = FMRILinearModel(first_level_image, design_matrix, grp_mask) grp_model.fit(do_scaling=False, model='ols')
def group_one_sample_t_test(masks, effects_maps, contrasts, output_dir, start_time=base_reporter.pretty_time(), **kwargs): """ Runs a one-sample t-test procedure for group analysis. Here, we are for each experimental condition, only interested refuting the null hypothesis H0: "The average effect accross the subjects is zero!" Parameters ---------- masks: list of strings or nibabel image objects subject masks, one per subject effects_maps: list of dicts of lists effects maps from subject-level GLM; each entry is a dictionary; each entry (indexed by condition id) of this dictionary is the filename (or correspinding nibabel image object) for the effects maps for that condition (aka contrast),for that subject contrasts: dictionary of array_likes contrasts vectors, indexed by condition id kwargs: dict_like parameters can be regular `nipy.labs.viz.plot_map` parameters (e.g slicer="y") or any parameter we want be reported (e.g fwhm=[5, 5, 5]) """ # make output directory if not os.path.exists(output_dir): os.makedirs(output_dir) assert len(masks) == len(effects_maps), (len(masks), len(effects_maps)) # compute group mask group_mask = nibabel.Nifti1Image( intersect_masks(masks).astype(np.int8), (nibabel.load(masks[0]) if isinstance( masks[0], basestring) else masks[0]).get_affine()) # construct design matrix (only one covariate, namely the "mean effect") design_matrix = np.ones(len(effects_maps) )[:, np.newaxis] # only the intercept group_level_z_maps = {} group_level_t_maps = {} for contrast_id in contrasts: print "\tcontrast id: %s" % contrast_id # effects maps will be the input to the second level GLM first_level_image = nibabel.concat_images( [x[contrast_id] for x in effects_maps]) # fit 2nd level GLM for given contrast group_model = FMRILinearModel(first_level_image, design_matrix, group_mask) group_model.fit(do_scaling=False, model='ols') # specify and estimate the contrast contrast_val = np.array(([[1.]]) ) # the only possible contrast ! z_map, t_map = group_model.contrast(contrast_val, con_id='one_sample %s' % contrast_id, output_z=True, output_stat=True) # save map for map_type, map_img in zip(["z", "t"], [z_map, t_map]): map_dir = os.path.join(output_dir, '%s_maps' % map_type) if not os.path.exists(map_dir): os.makedirs(map_dir) map_path = os.path.join(map_dir, 'group_level_%s.nii.gz' % ( contrast_id)) print "\t\tWriting %s ..." % map_path nibabel.save(map_img, map_path) if map_type == "z": group_level_z_maps[contrast_id] = map_path elif map_type == "t": group_level_z_maps[contrast_id] = map_path # do stats report stats_report_filename = os.path.join( output_dir, "report_stats.html") generate_subject_stats_report(stats_report_filename, contrasts, group_level_z_maps, group_mask, start_time=start_time, **kwargs) print "\r\nStatistic report written to %s\r\n" % ( stats_report_filename) return group_level_z_maps
-1))) if n_jobs > 1: subjects = Parallel(n_jobs=n_jobs, verbose=100)( delayed(run_suject_level1_glm)(subject_data, **kwargs) for subject_data in subjects) else: subjects = [ run_suject_level1_glm(subject_data, **kwargs) for subject_data in subjects ] # level 2 stats_start_time = pretty_time() mask_images = [subject_data.mask for subject_data in subjects] group_mask = nibabel.Nifti1Image( intersect_masks(mask_images).astype(np.int8), nibabel.load(mask_images[0]).get_affine()) nibabel.save(group_mask, os.path.join(task_output_dir, "mask.nii.gz")) print "... done.\r\n" print "Group GLM" contrasts = subjects[0].contrasts subjects_effects_maps = [ subject_data.effects_maps for subject_data in subjects ] group_one_sample_t_test( mask_images, subjects_effects_maps, contrasts, task_output_dir,
subjects = Parallel( n_jobs=n_jobs, verbose=100)(delayed( run_suject_level1_glm)( subject_data, **kwargs) for subject_data in subjects) else: subjects = [run_suject_level1_glm(subject_data, **kwargs) for subject_data in subjects] subjects = [subject for subject in subjects if subject] # level 2 stats_start_time = pretty_time() mask_images = [subject_data.mask for subject_data in subjects] group_mask = nibabel.Nifti1Image( intersect_masks(mask_images).astype(np.int8), nibabel.load(mask_images[0]).get_affine()) nibabel.save(group_mask, os.path.join( task_output_dir, "mask.nii.gz")) print "... done.\r\n" print "Group GLM" contrasts = subjects[0].contrasts subjects_effects_maps = [subject_data.effects_maps for subject_data in subjects] group_one_sample_t_test( mask_images, subjects_effects_maps, contrasts, task_output_dir,
def group_one_sample_t_test(masks, effects_maps, contrasts, output_dir, start_time=base_reporter.pretty_time(), **kwargs): """ Runs a one-sample t-test procedure for group analysis. Here, we are for each experimental condition, only interested refuting the null hypothesis H0: "The average effect accross the subjects is zero!" Parameters ---------- masks: list of strings or nibabel image objects subject masks, one per subject effects_maps: list of dicts of lists effects maps from subject-level GLM; each entry is a dictionary; each entry (indexed by condition id) of this dictionary is the filename (or correspinding nibabel image object) for the effects maps for that condition (aka contrast),for that subject contrasts: dictionary of array_likes contrasts vectors, indexed by condition id kwargs: dict_like parameters can be regular `nipy.labs.viz.plot_map` parameters (e.g slicer="y") or any parameter we want be reported (e.g fwhm=[5, 5, 5]) """ # make output directory if not os.path.exists(output_dir): os.makedirs(output_dir) assert len(masks) == len(effects_maps), (len(masks), len(effects_maps)) # compute group mask group_mask = nibabel.Nifti1Image( intersect_masks(masks).astype(np.int8), (nibabel.load(masks[0]) if isinstance(masks[0], basestring) else masks[0]).get_affine()) # construct design matrix (only one covariate, namely the "mean effect") design_matrix = np.ones( len(effects_maps))[:, np.newaxis] # only the intercept group_level_z_maps = {} group_level_t_maps = {} for contrast_id in contrasts: print "\tcontrast id: %s" % contrast_id # effects maps will be the input to the second level GLM first_level_image = nibabel.concat_images( [x[contrast_id] for x in effects_maps]) # fit 2nd level GLM for given contrast group_model = FMRILinearModel(first_level_image, design_matrix, group_mask) group_model.fit(do_scaling=False, model='ols') # specify and estimate the contrast contrast_val = np.array(([[1.]])) # the only possible contrast ! z_map, t_map = group_model.contrast(contrast_val, con_id='one_sample %s' % contrast_id, output_z=True, output_stat=True) # save map for map_type, map_img in zip(["z", "t"], [z_map, t_map]): map_dir = os.path.join(output_dir, '%s_maps' % map_type) if not os.path.exists(map_dir): os.makedirs(map_dir) map_path = os.path.join(map_dir, 'group_level_%s.nii.gz' % (contrast_id)) print "\t\tWriting %s ..." % map_path nibabel.save(map_img, map_path) if map_type == "z": group_level_z_maps[contrast_id] = map_path elif map_type == "t": group_level_z_maps[contrast_id] = map_path # do stats report stats_report_filename = os.path.join(output_dir, "report_stats.html") generate_subject_stats_report(stats_report_filename, contrasts, group_level_z_maps, group_mask, start_time=start_time, **kwargs) print "\r\nStatistic report written to %s\r\n" % (stats_report_filename) return group_level_z_maps