.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02_Analysis/plot_similarity_example.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 .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_02_Analysis_plot_similarity_example.py: Similarity and Distance ======================= This tutorial illustrates how to calculate similarity and distance between images. .. GENERATED FROM PYTHON SOURCE LINES 10-14 Load Data --------- First, let's load the pain data for this example. .. GENERATED FROM PYTHON SOURCE LINES 14-19 .. code-block:: default from nltools.datasets import fetch_pain data = fetch_pain() .. rst-class:: sphx-glr-script-out .. code-block:: none /usr/share/miniconda3/envs/test/lib/python3.8/site-packages/nilearn/maskers/nifti_masker.py:108: UserWarning: imgs are being resampled to the mask_img resolution. This process is memory intensive. You might want to provide a target_affine that is equal to the affine of the imgs or resample the mask beforehand to save memory and computation time. warnings.warn( .. GENERATED FROM PYTHON SOURCE LINES 20-26 Distance -------- We can calculate the pairwise spatial distance between all images in a Brain_Data() instance using any method from sklearn or scipy. This outputs an Adjacency() class object. .. GENERATED FROM PYTHON SOURCE LINES 26-30 .. code-block:: default d = data.distance(metric='correlation') d.plot() .. image-sg:: /auto_examples/02_Analysis/images/sphx_glr_plot_similarity_example_001.png :alt: plot similarity example :srcset: /auto_examples/02_Analysis/images/sphx_glr_plot_similarity_example_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 31-38 Similarity ---------- The similarity of an image to other images can be computed using the similarity() method. Here we calculate the mean image for high pain intensity across all participants and calculate the degree of spatial similarity between this image and all pain intensities for all participants. This is a useful method for calculating pattern responses. .. GENERATED FROM PYTHON SOURCE LINES 38-55 .. code-block:: default import numpy as np import matplotlib.pylab as plt high = data[np.where(data.X['PainLevel']==3)[0]].mean() r = high.similarity(data, method='correlation') f,a = plt.subplots(ncols=2, figsize=(10,4)) a[0].hist(r) a[0].set_ylabel('Spatial Similarity') a[0].set_xlabel('Pain Intensity') a[0].set_title('Histogram of similarity with mean high intensity image') a[1].scatter(data.X['PainLevel'],r) a[1].set_ylabel('Spatial Similarity') a[1].set_xlabel('Pain Intensity') a[1].set_title('Spatial Similarity by Pain Intensity') .. image-sg:: /auto_examples/02_Analysis/images/sphx_glr_plot_similarity_example_002.png :alt: Histogram of similarity with mean high intensity image, Spatial Similarity by Pain Intensity :srcset: /auto_examples/02_Analysis/images/sphx_glr_plot_similarity_example_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Spatial Similarity by Pain Intensity') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 31.062 seconds) .. _sphx_glr_download_auto_examples_02_Analysis_plot_similarity_example.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_similarity_example.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_similarity_example.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_