# Similarity and Distance¶

This tutorial illustrates how to calculate similarity and distance between images.

First, let’s load the pain data for this example.

```from nltools.datasets import fetch_pain

data = fetch_pain()
```

## 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.

```d = data.distance(metric='correlation')
d.plot()
``` ## 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.

```import numpy as np
import matplotlib.pylab as plt

high = data[np.where(data.X['PainLevel']==3)].mean()
r = high.similarity(data, method='correlation')

f,a = plt.subplots(ncols=2, figsize=(10,4))
a.hist(r)
a.set_ylabel('Spatial Similarity')
a.set_xlabel('Pain Intensity')
a.set_title('Histogram of similarity with mean high intensity image')
a.scatter(data.X['PainLevel'],r)
a.set_ylabel('Spatial Similarity')
a.set_xlabel('Pain Intensity')
a.set_title('Spatial Similarity by Pain Intensity')
``` ```Text(0.5, 1.0, 'Spatial Similarity by Pain Intensity')
```

Total running time of the script: ( 0 minutes 30.350 seconds)

Gallery generated by Sphinx-Gallery