.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/05_glm_second_level/plot_thresholding.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_05_glm_second_level_plot_thresholding.py: Statistical testing of a second-level analysis ============================================== Perform a one-sample t-test on a bunch of images (a.k.a. second-level analysis in fMRI) and threshold the resulting statistical map. This example is based on the so-called localizer dataset. It shows activation related to a mental computation task, as opposed to narrative sentence reading/listening. .. GENERATED FROM PYTHON SOURCE LINES 15-18 Prepare some images for a simple t test --------------------------------------- This is a simple manually performed second level analysis. .. GENERATED FROM PYTHON SOURCE LINES 18-25 .. code-block:: Python from nilearn import datasets n_samples = 20 localizer_dataset = datasets.fetch_localizer_calculation_task( n_subjects=n_samples, legacy_format=False ) .. GENERATED FROM PYTHON SOURCE LINES 26-27 Get the set of individual statstical maps (contrast estimates) .. GENERATED FROM PYTHON SOURCE LINES 27-29 .. code-block:: Python cmap_filenames = localizer_dataset.cmaps .. GENERATED FROM PYTHON SOURCE LINES 30-35 Perform the second level analysis --------------------------------- First, we define a design matrix for the model. As the model is trivial (one-sample test), the design matrix is just one column with ones. .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python import pandas as pd design_matrix = pd.DataFrame([1] * n_samples, columns=["intercept"]) .. GENERATED FROM PYTHON SOURCE LINES 40-41 Next, we specify and estimate the model. .. GENERATED FROM PYTHON SOURCE LINES 41-47 .. code-block:: Python from nilearn.glm.second_level import SecondLevelModel second_level_model = SecondLevelModel(n_jobs=2).fit( cmap_filenames, design_matrix=design_matrix ) .. GENERATED FROM PYTHON SOURCE LINES 48-50 Compute the only possible contrast: the one-sample test. Since there is only one possible contrast, we don't need to specify it in detail. .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python z_map = second_level_model.compute_contrast(output_type="z_score") .. GENERATED FROM PYTHON SOURCE LINES 53-55 Threshold the resulting map without multiple comparisons correction, abs(z) > 3.29 (equivalent to p < 0.001), cluster size > 10 voxels. .. GENERATED FROM PYTHON SOURCE LINES 55-64 .. code-block:: Python from nilearn.image import threshold_img threshold_img( z_map, threshold=3.29, cluster_threshold=10, two_sided=True, ) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 65-67 This is equivalent to thresholding a z-statistic image with a false positive rate < .001, cluster size > 10 voxels. .. GENERATED FROM PYTHON SOURCE LINES 67-77 .. code-block:: Python from nilearn.glm import threshold_stats_img thresholded_map1, threshold1 = threshold_stats_img( z_map, alpha=0.001, height_control="fpr", cluster_threshold=10, two_sided=True, ) .. GENERATED FROM PYTHON SOURCE LINES 78-79 Now use FDR <.05 (False Discovery Rate) and no cluster-level threshold. .. GENERATED FROM PYTHON SOURCE LINES 79-84 .. code-block:: Python thresholded_map2, threshold2 = threshold_stats_img( z_map, alpha=0.05, height_control="fdr" ) print(f"The FDR=.05 threshold is {threshold2:.3g}") .. rst-class:: sphx-glr-script-out .. code-block:: none The FDR=.05 threshold is 2.37 .. GENERATED FROM PYTHON SOURCE LINES 85-88 Now use FWER <.05 (Family-Wise Error Rate) and no cluster-level threshold. As the data has not been intensively smoothed, we can use a simple Bonferroni correction. .. GENERATED FROM PYTHON SOURCE LINES 88-93 .. code-block:: Python thresholded_map3, threshold3 = threshold_stats_img( z_map, alpha=0.05, height_control="bonferroni" ) print(f"The p<.05 Bonferroni-corrected threshold is {threshold3:.3g}") .. rst-class:: sphx-glr-script-out .. code-block:: none The p<.05 Bonferroni-corrected threshold is 4.88 .. GENERATED FROM PYTHON SOURCE LINES 94-98 Visualize the results --------------------- First, the unthresholded map. .. GENERATED FROM PYTHON SOURCE LINES 98-102 .. code-block:: Python from nilearn import plotting display = plotting.plot_stat_map(z_map, title="Raw z map") .. image-sg:: /auto_examples/05_glm_second_level/images/sphx_glr_plot_thresholding_001.png :alt: plot thresholding :srcset: /auto_examples/05_glm_second_level/images/sphx_glr_plot_thresholding_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 103-105 Second, the p<.001 uncorrected-thresholded map (with only clusters > 10 voxels). .. GENERATED FROM PYTHON SOURCE LINES 105-112 .. code-block:: Python plotting.plot_stat_map( thresholded_map1, cut_coords=display.cut_coords, threshold=threshold1, title="Thresholded z map, fpr <.001, clusters > 10 voxels", ) .. image-sg:: /auto_examples/05_glm_second_level/images/sphx_glr_plot_thresholding_002.png :alt: plot thresholding :srcset: /auto_examples/05_glm_second_level/images/sphx_glr_plot_thresholding_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 113-114 Third, the fdr-thresholded map. .. GENERATED FROM PYTHON SOURCE LINES 114-121 .. code-block:: Python plotting.plot_stat_map( thresholded_map2, cut_coords=display.cut_coords, title="Thresholded z map, expected fdr = .05", threshold=threshold2, ) .. image-sg:: /auto_examples/05_glm_second_level/images/sphx_glr_plot_thresholding_003.png :alt: plot thresholding :srcset: /auto_examples/05_glm_second_level/images/sphx_glr_plot_thresholding_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 122-123 Fourth, the Bonferroni-thresholded map. .. GENERATED FROM PYTHON SOURCE LINES 123-130 .. code-block:: Python plotting.plot_stat_map( thresholded_map3, cut_coords=display.cut_coords, title="Thresholded z map, expected fwer < .05", threshold=threshold3, ) .. image-sg:: /auto_examples/05_glm_second_level/images/sphx_glr_plot_thresholding_004.png :alt: plot thresholding :srcset: /auto_examples/05_glm_second_level/images/sphx_glr_plot_thresholding_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 131-137 These different thresholds correspond to different statistical guarantees: in the FWER-corrected image there is only a probability smaller than .05 of observing any false positive voxel. In the FDR-corrected image, 5% of the voxels found are likely to be false positive. In the uncorrected image, one expects a few tens of false positive voxels. .. GENERATED FROM PYTHON SOURCE LINES 137-139 .. code-block:: Python plotting.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 17.354 seconds) **Estimated memory usage:** 9 MB .. _sphx_glr_download_auto_examples_05_glm_second_level_plot_thresholding.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/nilearn/nilearn/0.10.3?urlpath=lab/tree/notebooks/auto_examples/05_glm_second_level/plot_thresholding.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_thresholding.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_thresholding.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_