.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/07_advanced/plot_localizer_mass_univariate_methods.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_07_advanced_plot_localizer_mass_univariate_methods.py: Massively univariate analysis of a motor task from the Localizer dataset ======================================================================== This example shows the results obtained in a massively univariate analysis performed at the inter-subject level with various methods. We use the [left button press (auditory cue)] task from the Localizer dataset and seek association between the contrast values and a variate that measures the speed of pseudo-word reading. No confounding variate is included in the model. 1. A standard :term:`ANOVA` is performed. Data smoothed at 5 :term:`voxels` :term:`FWHM` are used. 2. A permuted Ordinary Least Squares algorithm is run at each :term:`voxel`. Data smoothed at 5 :term:`voxels` :term:`FWHM` are used. .. include:: ../../../examples/masker_note.rst .. Original authors: - Virgile Fritsch, May. 2014 .. GENERATED FROM PYTHON SOURCE LINES 26-32 .. code-block:: Python try: import matplotlib.pyplot as plt except ImportError: raise RuntimeError("This script needs the matplotlib library") .. GENERATED FROM PYTHON SOURCE LINES 33-39 .. code-block:: Python import numpy as np from nilearn import datasets from nilearn.maskers import NiftiMasker from nilearn.mass_univariate import permuted_ols .. GENERATED FROM PYTHON SOURCE LINES 40-41 Load Localizer contrast .. GENERATED FROM PYTHON SOURCE LINES 41-65 .. code-block:: Python n_samples = 94 localizer_dataset = datasets.fetch_localizer_contrasts( ["left button press (auditory cue)"], n_subjects=n_samples, legacy_format=False, ) # print basic information on the dataset print( "First contrast nifti image (3D) is located " f"at: {localizer_dataset.cmaps[0]}" ) tested_var = localizer_dataset.ext_vars["pseudo"] # Quality check / Remove subjects with bad tested variate mask_quality_check = np.where(np.logical_not(np.isnan(tested_var)))[0] n_samples = mask_quality_check.size contrast_map_filenames = [ localizer_dataset.cmaps[i] for i in mask_quality_check ] tested_var = tested_var[mask_quality_check].values.reshape((-1, 1)) print(f"Actual number of subjects after quality check: {int(n_samples)}") .. rst-class:: sphx-glr-script-out .. code-block:: none First contrast nifti image (3D) is located at: /home/himanshu/nilearn_data/brainomics_localizer/brainomics_data/S01/cmaps_LeftAuditoryClick.nii.gz Actual number of subjects after quality check: 89 .. GENERATED FROM PYTHON SOURCE LINES 66-67 Mask data .. GENERATED FROM PYTHON SOURCE LINES 67-73 .. code-block:: Python nifti_masker = NiftiMasker( smoothing_fwhm=5, memory="nilearn_cache", memory_level=1 ) fmri_masked = nifti_masker.fit_transform(contrast_map_filenames) .. GENERATED FROM PYTHON SOURCE LINES 74-75 Anova (parametric F-scores) .. GENERATED FROM PYTHON SOURCE LINES 75-86 .. code-block:: Python from sklearn.feature_selection import f_regression _, pvals_anova = f_regression(fmri_masked, tested_var, center=True) pvals_anova *= fmri_masked.shape[1] pvals_anova[np.isnan(pvals_anova)] = 1 pvals_anova[pvals_anova > 1] = 1 neg_log_pvals_anova = -np.log10(pvals_anova) neg_log_pvals_anova_unmasked = nifti_masker.inverse_transform( neg_log_pvals_anova ) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/himanshu/.local/miniconda3/envs/nilearnpy/lib/python3.12/site-packages/sklearn/utils/validation.py:1300: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). y = column_or_1d(y, warn=True) .. GENERATED FROM PYTHON SOURCE LINES 87-97 Perform massively univariate analysis with permuted OLS This method will produce both voxel-level FWE-corrected -log10 p-values and :term:`TFCE`-based FWE-corrected -log10 p-values. .. note:: :func:`~nilearn.mass_univariate.permuted_ols` can support a wide range of analysis designs, depending on the ``tested_var``. For example, if you wished to perform a one-sample test, you could simply provide an array of ones (e.g., ``np.ones(n_samples)``). .. GENERATED FROM PYTHON SOURCE LINES 97-116 .. code-block:: Python ols_outputs = permuted_ols( tested_var, # this is equivalent to the design matrix, in array form fmri_masked, model_intercept=True, masker=nifti_masker, tfce=True, n_perm=200, # 200 for the sake of time. Ideally, this should be 10000. verbose=1, # display progress bar n_jobs=2, # can be changed to use more CPUs output_type="dict", ) neg_log_pvals_permuted_ols_unmasked = nifti_masker.inverse_transform( ols_outputs["logp_max_t"][0, :] # select first regressor ) neg_log_pvals_tfce_unmasked = nifti_masker.inverse_transform( ols_outputs["logp_max_tfce"][0, :] # select first regressor ) .. rst-class:: sphx-glr-script-out .. code-block:: none [Parallel(n_jobs=2)]: Using backend LokyBackend with 2 concurrent workers. [Parallel(n_jobs=2)]: Done 2 out of 2 | elapsed: 1.4min finished .. GENERATED FROM PYTHON SOURCE LINES 117-118 Visualization .. GENERATED FROM PYTHON SOURCE LINES 118-163 .. code-block:: Python from nilearn import plotting from nilearn.image import get_data # Various plotting parameters z_slice = 12 # plotted slice threshold = -np.log10(0.1) # 10% corrected vmax = max( np.amax(ols_outputs["logp_max_t"]), np.amax(neg_log_pvals_anova), np.amax(ols_outputs["logp_max_tfce"]), ) images_to_plot = { "Parametric Test\n(Bonferroni FWE)": neg_log_pvals_anova_unmasked, "Permutation Test\n(Max t-statistic FWE)": ( neg_log_pvals_permuted_ols_unmasked ), "Permutation Test\n(Max TFCE FWE)": neg_log_pvals_tfce_unmasked, } fig, axes = plt.subplots(figsize=(12, 3), ncols=3) for i_col, (title, img) in enumerate(images_to_plot.items()): ax = axes[i_col] n_detections = (get_data(img) > threshold).sum() new_title = f"{title}\n{n_detections} sig. voxels" plotting.plot_glass_brain( img, colorbar=True, vmax=vmax, display_mode="z", plot_abs=False, cut_coords=[12], threshold=threshold, figure=fig, axes=ax, ) ax.set_title(new_title) fig.suptitle( "Group left button press ($-\\log_{10}$ p-values)", y=1.3, fontsize=16, ) .. image-sg:: /auto_examples/07_advanced/images/sphx_glr_plot_localizer_mass_univariate_methods_001.png :alt: Group left button press ($-\log_{10}$ p-values), Parametric Test (Bonferroni FWE) 3 sig. voxels, Permutation Test (Max t-statistic FWE) 11 sig. voxels, Permutation Test (Max TFCE FWE) 1448 sig. voxels :srcset: /auto_examples/07_advanced/images/sphx_glr_plot_localizer_mass_univariate_methods_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.3, 'Group left button press ($-\\log_{10}$ p-values)') .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 31.502 seconds) **Estimated memory usage:** 165 MB .. _sphx_glr_download_auto_examples_07_advanced_plot_localizer_mass_univariate_methods.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.4?urlpath=lab/tree/notebooks/auto_examples/07_advanced/plot_localizer_mass_univariate_methods.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_localizer_mass_univariate_methods.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_localizer_mass_univariate_methods.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_