.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_connectivity/plot_seed_to_voxel_correlation.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_03_connectivity_plot_seed_to_voxel_correlation.py: Producing single subject maps of seed-to-voxel correlation ========================================================== This example shows how to produce seed-to-:term:`voxel` correlation maps for a single subject based on movie-watching :term:`fMRI` scans. These maps depict the temporal correlation of a **seed region** with the **rest of the brain**. This example is an advanced one that requires manipulating the data with numpy. Note the difference between images, that lie in brain space, and the numpy array, corresponding to the data inside the mask. See also :ref:`for a similar example using cortical surface input data `. Author: Franz Liem .. include:: ../../../examples/masker_note.rst .. GENERATED FROM PYTHON SOURCE LINES 24-31 Getting the data ---------------- We will work with the first subject of the brain development :term:`fMRI` data set. dataset.func is a list of filenames. We select the 1st (0-based) subject by indexing with [0]). .. GENERATED FROM PYTHON SOURCE LINES 31-37 .. code-block:: Python from nilearn import datasets, plotting dataset = datasets.fetch_development_fmri(n_subjects=1) func_filename = dataset.func[0] confound_filename = dataset.confounds[0] .. GENERATED FROM PYTHON SOURCE LINES 38-40 Note that func_filename and confound_filename are strings pointing to files on your hard drive. .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: Python print(func_filename) print(confound_filename) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/himanshu/nilearn_data/development_fmri/development_fmri/sub-pixar123_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz /home/himanshu/nilearn_data/development_fmri/development_fmri/sub-pixar123_task-pixar_desc-reducedConfounds_regressors.tsv .. GENERATED FROM PYTHON SOURCE LINES 44-54 Time series extraction ---------------------- We are going to extract signals from the functional time series in two steps. First we will extract the mean signal within the **seed region of interest**. Second, we will extract the **brain-wide voxel-wise time series**. We will be working with one seed sphere in the Posterior Cingulate Cortex (PCC), considered part of the Default Mode Network. .. GENERATED FROM PYTHON SOURCE LINES 54-56 .. code-block:: Python pcc_coords = [(0, -52, 18)] .. GENERATED FROM PYTHON SOURCE LINES 57-64 We use :class:`nilearn.maskers.NiftiSpheresMasker` to extract the **time series from the functional imaging within the sphere**. The sphere is centered at pcc_coords and will have the radius we pass the NiftiSpheresMasker function (here 8 mm). The extraction will also detrend, standardize, and bandpass filter the data. This will create a NiftiSpheresMasker object. .. GENERATED FROM PYTHON SOURCE LINES 64-80 .. code-block:: Python from nilearn.maskers import NiftiSpheresMasker seed_masker = NiftiSpheresMasker( pcc_coords, radius=8, detrend=True, standardize="zscore_sample", standardize_confounds="zscore_sample", low_pass=0.1, high_pass=0.01, t_r=2, memory="nilearn_cache", memory_level=1, verbose=0, ) .. GENERATED FROM PYTHON SOURCE LINES 81-84 Then we extract the mean time series within the seed region while regressing out the confounds that can be found in the dataset's csv file .. GENERATED FROM PYTHON SOURCE LINES 84-88 .. code-block:: Python seed_time_series = seed_masker.fit_transform( func_filename, confounds=[confound_filename] ) .. GENERATED FROM PYTHON SOURCE LINES 89-92 Next, we can proceed similarly for the **brain-wide voxel-wise time series**, using :class:`nilearn.maskers.NiftiMasker` with the same input arguments as in the seed_masker in addition to smoothing with a 6 mm kernel .. GENERATED FROM PYTHON SOURCE LINES 92-107 .. code-block:: Python from nilearn.maskers import NiftiMasker brain_masker = NiftiMasker( smoothing_fwhm=6, detrend=True, standardize="zscore_sample", standardize_confounds="zscore_sample", low_pass=0.1, high_pass=0.01, t_r=2, memory="nilearn_cache", memory_level=1, verbose=0, ) .. GENERATED FROM PYTHON SOURCE LINES 108-110 Then we extract the brain-wide voxel-wise time series while regressing out the confounds as before .. GENERATED FROM PYTHON SOURCE LINES 110-114 .. code-block:: Python brain_time_series = brain_masker.fit_transform( func_filename, confounds=[confound_filename] ) .. GENERATED FROM PYTHON SOURCE LINES 115-118 We can now inspect the extracted time series. Note that the **seed time series** is an array with shape n_volumes, 1), while the **brain time series** is an array with shape (n_volumes, n_voxels). .. GENERATED FROM PYTHON SOURCE LINES 118-122 .. code-block:: Python print(f"Seed time series shape: ({seed_time_series.shape})") print(f"Brain time series shape: ({brain_time_series.shape})") .. rst-class:: sphx-glr-script-out .. code-block:: none Seed time series shape: ((168, 1)) Brain time series shape: ((168, 32504)) .. GENERATED FROM PYTHON SOURCE LINES 123-124 We can plot the **seed time series**. .. GENERATED FROM PYTHON SOURCE LINES 124-133 .. code-block:: Python import matplotlib.pyplot as plt plt.plot(seed_time_series) plt.title("Seed time series (Posterior cingulate cortex)") plt.xlabel("Scan number") plt.ylabel("Normalized signal") plt.tight_layout() .. image-sg:: /auto_examples/03_connectivity/images/sphx_glr_plot_seed_to_voxel_correlation_001.png :alt: Seed time series (Posterior cingulate cortex) :srcset: /auto_examples/03_connectivity/images/sphx_glr_plot_seed_to_voxel_correlation_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 134-136 Exemplarily, we can also select 5 random voxels from the **brain-wide data** and plot the time series from. .. GENERATED FROM PYTHON SOURCE LINES 136-143 .. code-block:: Python plt.plot(brain_time_series[:, [10, 45, 100, 5000, 10000]]) plt.title("Time series from 5 random voxels") plt.xlabel("Scan number") plt.ylabel("Normalized signal") plt.tight_layout() .. image-sg:: /auto_examples/03_connectivity/images/sphx_glr_plot_seed_to_voxel_correlation_002.png :alt: Time series from 5 random voxels :srcset: /auto_examples/03_connectivity/images/sphx_glr_plot_seed_to_voxel_correlation_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 144-154 Performing the seed-to-voxel correlation analysis ------------------------------------------------- Now that we have two arrays (**sphere signal**: (n_volumes, 1), **brain-wide voxel-wise signal** (n_volumes, n_voxels)), we can correlate the **seed signal** with the **signal of each voxel**. The dot product of the two arrays will give us this correlation. Note that the signals have been variance-standardized during extraction. To have them standardized to norm unit, we further have to divide the result by the length of the time series. .. GENERATED FROM PYTHON SOURCE LINES 154-160 .. code-block:: Python import numpy as np seed_to_voxel_correlations = ( np.dot(brain_time_series.T, seed_time_series) / seed_time_series.shape[0] ) .. GENERATED FROM PYTHON SOURCE LINES 161-165 The resulting array will contain a value representing the correlation values between the signal in the **seed region** of interest and **each voxel's signal**, and will be of shape (n_voxels, 1). The correlation values can potentially range between -1 and 1. .. GENERATED FROM PYTHON SOURCE LINES 165-174 .. code-block:: Python print( "Seed-to-voxel correlation shape: (%s, %s)" % seed_to_voxel_correlations.shape ) print( "Seed-to-voxel correlation: min = %.3f; max = %.3f" % (seed_to_voxel_correlations.min(), seed_to_voxel_correlations.max()) ) .. rst-class:: sphx-glr-script-out .. code-block:: none Seed-to-voxel correlation shape: (32504, 1) Seed-to-voxel correlation: min = -0.642; max = 0.954 .. GENERATED FROM PYTHON SOURCE LINES 175-182 Plotting the seed-to-voxel correlation map ------------------------------------------ We can now plot the seed-to-voxel correlation map and perform thresholding to only show values more extreme than +/- 0.5. Before displaying, we need to create an in memory Nifti image object. Furthermore, we can display the location of the seed with a sphere and set the cross to the center of the seed region of interest. .. GENERATED FROM PYTHON SOURCE LINES 182-204 .. code-block:: Python seed_to_voxel_correlations_img = brain_masker.inverse_transform( seed_to_voxel_correlations.T ) display = plotting.plot_stat_map( seed_to_voxel_correlations_img, threshold=0.5, vmax=1, cut_coords=pcc_coords[0], title="Seed-to-voxel correlation (PCC seed)", ) display.add_markers( marker_coords=pcc_coords, marker_color="g", marker_size=300 ) # At last, we save the plot as pdf. from pathlib import Path output_dir = Path.cwd() / "results" / "plot_seed_to_voxel_correlation" output_dir.mkdir(exist_ok=True, parents=True) print(f"Output will be saved to: {output_dir}") display.savefig(output_dir / "pcc_seed_correlation.pdf") .. image-sg:: /auto_examples/03_connectivity/images/sphx_glr_plot_seed_to_voxel_correlation_003.png :alt: plot seed to voxel correlation :srcset: /auto_examples/03_connectivity/images/sphx_glr_plot_seed_to_voxel_correlation_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Output will be saved to: /home/himanshu/Desktop/nilearn_work/nilearn/examples/03_connectivity/results/plot_seed_to_voxel_correlation .. GENERATED FROM PYTHON SOURCE LINES 205-209 Fisher-z transformation and save nifti -------------------------------------- Finally, we can Fisher-z transform the data to achieve a normal distribution. The transformed array can now have values more extreme than +/- 1. .. GENERATED FROM PYTHON SOURCE LINES 209-216 .. code-block:: Python seed_to_voxel_correlations_fisher_z = np.arctanh(seed_to_voxel_correlations) print( "Seed-to-voxel correlation Fisher-z transformed: " f"min = {seed_to_voxel_correlations_fisher_z.min():.3f}; " f"max = {seed_to_voxel_correlations_fisher_z.max():.3f}f" ) .. rst-class:: sphx-glr-script-out .. code-block:: none Seed-to-voxel correlation Fisher-z transformed: min = -0.762; max = 1.874f .. GENERATED FROM PYTHON SOURCE LINES 217-219 Eventually, we can transform the correlation array back to a Nifti image object, that we can save. .. GENERATED FROM PYTHON SOURCE LINES 219-225 .. code-block:: Python seed_to_voxel_correlations_fisher_z_img = brain_masker.inverse_transform( seed_to_voxel_correlations_fisher_z.T ) seed_to_voxel_correlations_fisher_z_img.to_filename( output_dir / "pcc_seed_correlation_z.nii.gz" ) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 19.910 seconds) **Estimated memory usage:** 1140 MB .. _sphx_glr_download_auto_examples_03_connectivity_plot_seed_to_voxel_correlation.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/03_connectivity/plot_seed_to_voxel_correlation.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_seed_to_voxel_correlation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_seed_to_voxel_correlation.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_