Note

Click here to download the full example code

# Adjacency Class¶

Nltools has an additional data structure class for working with two-dimensional square matrices. This can be helpful when working with similarity/distance matrices or directed or undirected graphs. Similar to the Brain_Data class, matrices are vectorized and can store multiple matrices in the same object. This might reflect different brain regions, subjects, or time. Most of the methods on the Adjacency class are consistent with those in the Brain_Data class.

## Load Data¶

Similar to the Brain_Data class, Adjacency instances can be initialized by passing in a numpy array or pandas data frame, or a path to a csv file or list of files. Here we will generate some fake data to demonstrate how to use this class. In addition to data, you must indicate the type of matrix. Currently, you can specify [‘similarity’,’distance’,’directed’]. Similarity matrices are symmetrical with typically ones along diagonal, Distance matrices are symmetrical with zeros along diagonal, and Directed graph matrices are not symmetrical. Symmetrical matrices only store the upper triangle. The Adjacency class can also accommodate labels, but does not require them.

```
from nltools.data import Adjacency
from scipy.linalg import block_diag
import numpy as np
m1 = block_diag(np.ones((4, 4)), np.zeros((4, 4)), np.zeros((4, 4)))
m2 = block_diag(np.zeros((4, 4)), np.ones((4, 4)), np.zeros((4, 4)))
m3 = block_diag(np.zeros((4, 4)), np.zeros((4, 4)), np.ones((4, 4)))
noisy = (m1 * 1 + m2 * 2 + m3 * 3) + np.random.randn(12, 12) * 0.1
dat = Adjacency(
noisy, matrix_type="similarity", labels=["C1"] * 4 + ["C2"] * 4 + ["C3"] * 4
)
```

Basic information about the object can be viewed by simply calling it.

```
print(dat)
```

Out:

```
nltools.data.adjacency.Adjacency(shape=(66,), square_shape=(12, 12), Y=0, is_symmetric=True,matrix_type=similarity)
```

Adjacency objects can easily be converted back into two-dimensional matrices with the .squareform() method.

```
dat.squareform()
```

Out:

```
array([[ 0.00000000e+00, 1.18495788e+00, 1.06731218e+00,
9.20130654e-01, 4.16289884e-02, 1.08741065e-01,
2.91931196e-02, 8.66549435e-02, -7.52809832e-02,
2.13382359e-01, -1.03894910e-01, 1.04023135e-01],
[ 1.18495788e+00, 0.00000000e+00, 1.07705144e+00,
1.03585836e+00, 2.04449601e-01, -8.46648813e-02,
4.86475103e-02, 1.69552961e-02, 2.18421593e-02,
1.98099254e-01, -2.22186078e-01, -1.72493417e-02],
[ 1.06731218e+00, 1.07705144e+00, 0.00000000e+00,
9.02740611e-01, -9.51155298e-02, 3.32489843e-03,
1.68471971e-01, 1.91729072e-01, -7.15635162e-03,
2.37465609e-03, -7.06869825e-02, -6.84703363e-03],
[ 9.20130654e-01, 1.03585836e+00, 9.02740611e-01,
0.00000000e+00, -1.56302153e-01, -1.20254753e-01,
-2.79629061e-02, -1.57212132e-02, -5.41186084e-02,
-1.21914284e-02, 4.62389026e-02, -8.54343505e-03],
[ 4.16289884e-02, 2.04449601e-01, -9.51155298e-02,
-1.56302153e-01, 0.00000000e+00, 1.87137676e+00,
2.00111349e+00, 1.91165433e+00, 1.80877028e-01,
1.17490298e-01, 2.25753328e-01, -2.52302753e-02],
[ 1.08741065e-01, -8.46648813e-02, 3.32489843e-03,
-1.20254753e-01, 1.87137676e+00, 0.00000000e+00,
2.01236770e+00, 1.96794725e+00, -3.77211573e-02,
1.26128336e-01, 6.42961644e-02, 6.40410834e-02],
[ 2.91931196e-02, 4.86475103e-02, 1.68471971e-01,
-2.79629061e-02, 2.00111349e+00, 2.01236770e+00,
0.00000000e+00, 2.01895259e+00, 2.62452112e-01,
1.37562627e-01, -4.36464360e-02, 1.21870251e-01],
[ 8.66549435e-02, 1.69552961e-02, 1.91729072e-01,
-1.57212132e-02, 1.91165433e+00, 1.96794725e+00,
2.01895259e+00, 0.00000000e+00, -5.51193151e-02,
7.65285983e-02, 3.52713980e-02, 3.52896236e-02],
[-7.52809832e-02, 2.18421593e-02, -7.15635162e-03,
-5.41186084e-02, 1.80877028e-01, -3.77211573e-02,
2.62452112e-01, -5.51193151e-02, 0.00000000e+00,
3.07213367e+00, 2.94960006e+00, 3.13010929e+00],
[ 2.13382359e-01, 1.98099254e-01, 2.37465609e-03,
-1.21914284e-02, 1.17490298e-01, 1.26128336e-01,
1.37562627e-01, 7.65285983e-02, 3.07213367e+00,
0.00000000e+00, 3.19844151e+00, 2.93242034e+00],
[-1.03894910e-01, -2.22186078e-01, -7.06869825e-02,
4.62389026e-02, 2.25753328e-01, 6.42961644e-02,
-4.36464360e-02, 3.52713980e-02, 2.94960006e+00,
3.19844151e+00, 0.00000000e+00, 3.07064632e+00],
[ 1.04023135e-01, -1.72493417e-02, -6.84703363e-03,
-8.54343505e-03, -2.52302753e-02, 6.40410834e-02,
1.21870251e-01, 3.52896236e-02, 3.13010929e+00,
2.93242034e+00, 3.07064632e+00, 0.00000000e+00]])
```

Matrices can viewed as a heatmap using the .plot() method.

```
dat.plot()
```

The mean within a a grouping label can be calculated using the .cluster_summary() method. You must specify a group variable to group the data. Here we use the labels.

```
print(dat.cluster_summary(clusters=dat.labels, summary="within", metric="mean"))
```

Out:

```
{'C3': 3.058891864185117, 'C2': 1.963902020733865, 'C1': 1.0313418521375866}
```

## Regression¶

Adjacency objects can currently accommodate two different types of regression. Sometimes we might want to decompose an Adjacency matrix from a linear combination of other Adjacency matrices. Other times we might want to perform a regression at each pixel in a stack of Adjacency matrices. Here we provide an example of each method. We use the same data we generated above, but attempt to decompose it by each block of data. We create the design matrix by simply concatenating the matrices we used to create the data object. The regress method returns a dictionary containing all of the relevant information from the regression. Here we show that the model recovers the average weight in each block.

```
X = Adjacency([m1, m2, m3], matrix_type="similarity")
stats = dat.regress(X)
print(stats["beta"])
```

Out:

```
[1.03134185 1.96390202 3.05889186]
```

In addition to decomposing a single adjacency matrix, we can also estimate a model that predicts the variance over each voxel. This is equivalent to a univariate regression in imaging analyses. Remember that just like in imaging these tests are non-independent and may require correcting for multiple comparisons. Here we create some data that varies over matrices and identify pixels that follow a particular on-off-on pattern. We plot the t-values that exceed 2.

```
from nltools.data import Design_Matrix
import matplotlib.pyplot as plt
data = Adjacency(
[m1 + np.random.randn(12, 12) * 0.5 for x in range(5)]
+ [np.zeros((12, 12)) + np.random.randn(12, 12) * 0.5 for x in range(5)]
+ [m1 + np.random.randn(12, 12) * 0.5 for x in range(5)]
)
X = Design_Matrix([1] * 5 + [0] * 5 + [1] * 5)
f = X.plot()
f.set_title("Model", fontsize=18)
stats = data.regress(X)
t = stats["t"].plot(vmin=2)
plt.title("Significant Pixels", fontsize=18)
```

Out:

```
Text(0.5, 1.0, 'Significant Pixels')
```

## Similarity/Distance¶

We can calculate similarity between two Adjacency matrices using .similiarity().

```
stats = dat.similarity(m1)
print(stats)
```

Out:

```
{'correlation': 0.29879205721671714, 'p': 0.010597880423915218}
```

We can also calculate the distance between multiple matrices contained within a single Adjacency object. Any distance metric is available from the sci-kit learn by specifying the method flag. This outputs an Adjacency matrix. In the example below we see that several matrices are more similar to each other (i.e., when the signal is on). Remember that the nodes here now represent each matrix from the original distance matrix.

```
dist = data.distance(metric="correlation")
dist.plot()
```

Similarity matrices can be converted to and from Distance matrices using .similarity_to_distance() and .distance_to_similarity().

```
dist.distance_to_similarity(metric="correlation").plot()
```

## Multidimensional Scaling¶

We can perform additional analyses on distance matrices such as multidimensional scaling. Here we provide an example to create a 3D multidimensional scaling plot of our data to see if the on and off matrices might naturally group together.

```
dist = data.distance(metric="correlation")
dist.labels = ["On"] * 5 + ["Off"] * 5 + ["On"] * 5
dist.plot_mds(n_components=3)
```

## Graphs¶

Adjacency matrices can be cast to networkx objects using .to_graph() if the optional dependency is installed. This allows any graph theoretic metrics or plots to be easily calculated from Adjacency objects.

```
import networkx as nx
dat = Adjacency(m1 + m2 + m3, matrix_type="similarity")
g = dat.to_graph()
print("Degree of each node: %s" % g.degree())
nx.draw_circular(g)
```

Out:

```
Degree of each node: [(0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (8, 3), (9, 3), (10, 3), (11, 3)]
```

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