def test_get_clusters_table(): shape = (9, 10, 11) data = np.zeros(shape) data[2:4, 5:7, 6:8] = 5. stat_img = nib.Nifti1Image(data, np.eye(4)) # test one cluster extracted cluster_table = get_clusters_table(stat_img, 4, 0) assert_true(len(cluster_table) == 1) # test empty table on high stat threshold cluster_table = get_clusters_table(stat_img, 6, 0) assert_true(len(cluster_table) == 0) # test empty table on high cluster threshold cluster_table = get_clusters_table(stat_img, 4, 9) assert_true(len(cluster_table) == 0)
def _make_stat_maps_contrast_clusters(stat_img, contrasts_plots, threshold, alpha, cluster_threshold, height_control, min_distance, bg_img, display_mode, plot_type): """ Populates a smaller HTML sub-template with the proper values, make a list containing one or more of such components & returns the list to be inserted into the HTML Report Template. Each component contains the HTML code for a contrast & its corresponding statistical maps & cluster table; Parameters ---------- stat_img : Niimg-like object or None statistical image (presumably in z scale) whenever height_control is 'fpr' or None, stat_img=None is acceptable. If it is 'fdr' or 'bonferroni', an error is raised if stat_img is None. contrasts_plots: Dict[str, str] Contains the contrast names & the HTML code of the contrast's SVG plot. threshold: float desired threshold in z-scale. This is used only if height_control is None alpha: float number controlling the thresholding (either a p-value or q-value). Its actual meaning depends on the height_control parameter. This function translates alpha to a z-scale threshold. cluster_threshold : float cluster size threshold. In the returned thresholded map, sets of connected voxels (`clusters`) with size smaller than this number will be removed. height_control: string false positive control meaning of cluster forming threshold: 'fpr' or 'fdr' or 'bonferroni' or None min_distance: `float` For display purposes only. Minimum distance between subpeaks in mm. Default is 8 mm. bg_img : Niimg-like object Only used when plot_type is 'slice'. See http://nilearn.github.io/manipulating_images/input_output.html The background image for stat maps to be plotted on upon. If nothing is specified, the MNI152 template will be used. To turn off background image, just pass "bg_img=False". display_mode: string Choose the direction of the cuts: 'x' - sagittal, 'y' - coronal, 'z' - axial, 'l' - sagittal left hemisphere only, 'r' - sagittal right hemisphere only, 'ortho' - three cuts are performed in orthogonal directions. Possible values are: 'ortho', 'x', 'y', 'z', 'xz', 'yx', 'yz', 'l', 'r', 'lr', 'lzr', 'lyr', 'lzry', 'lyrz'. plot_type: string ['slice', 'glass'] The type of plot to be drawn. Returns ------- all_components: List[String] Each element is a set of HTML code for contrast name, contrast plot, statistical map, cluster table. """ all_components = [] components_template_path = os.path.join( HTML_TEMPLATE_ROOT_PATH, 'stat_maps_contrast_clusters_template.html') with open(components_template_path) as html_template_obj: components_template_text = html_template_obj.read() for contrast_name, stat_map_img in stat_img.items(): component_text_ = string.Template(components_template_text) thresholded_stat_map, threshold = map_threshold( stat_img=stat_map_img, threshold=threshold, alpha=alpha, cluster_threshold=cluster_threshold, height_control=height_control, ) table_details = _clustering_params_to_dataframe( threshold, cluster_threshold, min_distance, height_control, alpha, ) stat_map_svg = _stat_map_to_svg( stat_img=thresholded_stat_map, bg_img=bg_img, display_mode=display_mode, plot_type=plot_type, table_details=table_details, ) cluster_table = get_clusters_table( stat_map_img, stat_threshold=threshold, cluster_threshold=cluster_threshold, min_distance=min_distance, ) cluster_table_html = _dataframe_to_html( cluster_table, precision=2, index=False, classes='cluster-table', ) table_details_html = _dataframe_to_html( table_details, precision=2, header=False, classes='cluster-details-table', ) components_values = { 'contrast_name': escape(contrast_name), 'contrast_plot': contrasts_plots[contrast_name], 'stat_map_img': stat_map_svg, 'cluster_table_details': table_details_html, 'cluster_table': cluster_table_html, } component_text_ = component_text_.safe_substitute(**components_values) all_components.append(component_text_) return all_components
display_mode='z', cut_coords=3, black_bg=True, title='Active minus Rest (fdr=0.05), clusters > 10 voxels') plt.show() ############################################################################### # We can save the effect and zscore maps to the disk z_map.to_filename(join(outdir, 'active_vs_rest_z_map.nii.gz')) eff_map.to_filename(join(outdir, 'active_vs_rest_eff_map.nii.gz')) ############################################################################### # Report the found positions in a table from nistats.reporting import get_clusters_table table = get_clusters_table(z_map, stat_threshold=threshold, cluster_threshold=20) print(table) ############################################################################### # the table can be saved for future use table.to_csv(join(outdir, 'table.csv')) ############################################################################### # Performing an F-test # # "active vs rest" is a typical t test: condition versus # baseline. Another popular type of test is an F test in which one # seeks whether a certain combination of conditions (possibly two-, # three- or higher-dimensional) explains a significant proportion of # the signal. Here one might for instance test which voxels are well
cut_coords=3, black_bg=True, title='Active minus Rest (fdr=0.05), clusters > 10 voxels') plt.show() ############################################################################### # We can save the effect and zscore maps to the disk z_map.to_filename(join(outdir, 'active_vs_rest_z_map.nii.gz')) eff_map.to_filename(join(outdir, 'active_vs_rest_eff_map.nii.gz')) ############################################################################### # Report the found positions in a table from nistats.reporting import get_clusters_table table = get_clusters_table(z_map, stat_threshold=threshold, cluster_threshold=20) print(table) ############################################################################### # the table can be saved for future use table.to_csv(join(outdir, 'table.csv')) ############################################################################### # Performing an F-test # # "active vs rest" is a typical t test: condition versus # baseline. Another popular type of test is an F test in which one # seeks whether a certain combination of conditions (possibly two-, # three- or higher-dimensional) explains a significant proportion of
group_dir = os.path.join(data_dir, task, 'derivatives', 'pypreprocess', 'group') if not os.path.exists(group_dir): os.mkdir(group_dir) from nistats.second_level_model import SecondLevelModel for contrast_id in contrasts.keys(): cmap_filenames = [ os.path.join(data_dir, task, 'derivatives', 'spmpreproc_%s' % session, subject, 'glm', '%s_effects.nii.gz' % contrast_id) for subject in subjects ] second_level_model = SecondLevelModel(smoothing_fwhm=5).fit( cmap_filenames, design_matrix=group_design_matrix) z_map = second_level_model.compute_contrast(output_type='z_score') thresholded_map, threshold = map_threshold(z_map, level=.1, height_control='fdr', cluster_threshold=10) z_map.to_filename(os.path.join(group_dir, '%s_z_map.nii.gz' % contrast_id)) output_file = os.path.join(group_dir, '%s_z_map.png' % contrast_id) plotting.plot_stat_map(thresholded_map, title='%s, fdr = .1' % contrast_id, threshold=threshold, output_file=output_file) clusters_table = get_clusters_table(z_map, 3., 10) clusters_table.to_csv( os.path.join(group_dir, '%s_clusters.csv' % contrast_id)) # plotting.show()
# ----------------------------------------------------- # We display the contrast plot and table with cluster information. from nistats.reporting import plot_contrast_matrix plot_contrast_matrix('StopSuccess - Go', design_matrix) plotting.plot_glass_brain(z_map, colorbar=True, threshold=norm.isf(0.001), plot_abs=False, display_mode='z', figure=plt.figure(figsize=(4, 4))) plt.show() ############################################################################### # We can get a latex table from a Pandas Dataframe for display and publication purposes. from nistats.reporting import get_clusters_table print(get_clusters_table(z_map, norm.isf(0.001), 10).to_latex()) ######################################################################### # Generating a report # ------------------- # Using the computed FirstLevelModel and contrast information, # we can quickly create a summary report. from nistats.reporting import make_glm_report report = make_glm_report( model=model, contrasts='StopSuccess - Go', ) #########################################################################
# Calculate and plot contrast # --------------------------- from nilearn import plotting z_map = fmri_glm.compute_contrast('active - rest') plotting.plot_stat_map(z_map, bg_img=mean_img, threshold=3.1) ######################################################################### # Extract the largest clusters # ---------------------------- from nistats.reporting import get_clusters_table from nilearn import input_data table = get_clusters_table(z_map, stat_threshold=3.1, cluster_threshold=20).set_index('Cluster ID', drop=True) table.head() # get the 6 largest clusters' max x, y, and z coordinates coords = table.loc[range(1, 7), ['X', 'Y', 'Z']].values # extract time series from each coordinate masker = input_data.NiftiSpheresMasker(coords) real_timeseries = masker.fit_transform(fmri_img) predicted_timeseries = masker.fit_transform(fmri_glm.predicted[0]) ######################################################################### # Plot predicted and actual time series for 6 most significant clusters # --------------------------------------------------------------------- import matplotlib.pyplot as plt
import matplotlib.pyplot as plt from scipy.stats import norm plotting.plot_glass_brain(z_map, colorbar=True, threshold=norm.isf(0.001), title='Nistats Z map of "StopSuccess - Go" (unc p<0.001)', plot_abs=False, display_mode='ortho') plotting.plot_glass_brain(fsl_z_map, colorbar=True, threshold=norm.isf(0.001), title='FSL Z map of "StopSuccess - Go" (unc p<0.001)', plot_abs=False, display_mode='ortho') plt.show() from nistats.reporting import compare_niimgs compare_niimgs([z_map], [fsl_z_map], model.masker_, ref_label='Nistats', src_label='FSL') plt.show() ############################################################################# # Simple statistical report of thresholded contrast # ----------------------------------------------------- # We display the contrast plot and table with cluster information from nistats.reporting import plot_contrast_matrix plot_contrast_matrix('StopSuccess - Go', design_matrix) plotting.plot_glass_brain(z_map, colorbar=True, threshold=norm.isf(0.001), plot_abs=False, display_mode='z', figure=plt.figure(figsize=(4, 4))) plt.show() ############################################################################### # We can get a latex table from a Pandas Dataframe for display and publication from nistats.reporting import get_clusters_table print(get_clusters_table(z_map, norm.isf(0.001), 10).to_latex())