Data obtained from: https://neuroimaging-core-docs.readthedocs.io/en/latest/pages/atlases.html
A direct link: https://bitbucket.org/dpat/tools/raw/master/REF/ATLASES/HCP-MMP1_UniqueRegionList.csv
3D plot example source: https://jakevdp.github.io/PythonDataScienceHandbook/04.12-three-dimensional-plotting.html
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from mpl_toolkits import mplot3d
from scipy.spatial.distance import squareform, pdist
data = pd.read_csv('HCP-MMP1_UniqueRegionList.csv', index_col=0)
data
Simply plot all the coordinates first
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.scatter3D(data['x-cog'],
data['y-cog'],
data['z-cog']);
Create masks for the left and the right hemispheres
data[data.index.str.contains('_L')]
left_mask, right_mask = [data.index.str.contains(hemisphere) for hemisphere in ('_L', '_R')]
print('Left mask')
data[left_mask][:3]
print('Right mask')
data[right_mask][:3]
fig = plt.figure()
ax = plt.axes(projection='3d')
plt.title('Left hemisphere: red\nRight hemisphere: green')
ax.scatter3D(data['x-cog'][left_mask],
data['y-cog'][left_mask],
data['z-cog'][left_mask],
c=data['z-cog'][left_mask],
cmap="Reds");
ax.scatter3D(data['x-cog'][right_mask],
data['y-cog'][right_mask],
data['z-cog'][right_mask],
c=data['z-cog'][right_mask],
cmap="Greens");
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');
Preview one of the labels:
data.loc['V1_L']
For text annotation examples see: https://matplotlib.org/stable/gallery/mplot3d/text3d.html
labels = ['V1_L', 'V1_R', '1_L', '1_R', 'TGv_L', 'TGv_R', '10pp_L', '10pp_R']
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.view_init(elev=20., azim=-45)
plt.title('Left hemisphere: red\nRight hemisphere: green')
ax.scatter3D(data['x-cog'][left_mask],
data['y-cog'][left_mask],
data['z-cog'][left_mask],
c=data['z-cog'][left_mask],
cmap="Reds");
ax.scatter3D(data['x-cog'][right_mask],
data['y-cog'][right_mask],
data['z-cog'][right_mask],
c=data['z-cog'][right_mask],
cmap="Greens");
# Display labels (texts) to see which ROI is where.
# None is direction of the text. None is en face.
for label in labels:
region = data.loc[label]
ax.text(region['x-cog'],
region['y-cog'],
region['z-cog']*1.2,
label,
None,
color='blue')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');
It seems that this is presented in radiology notation, i.e., where left is right and right is left. It doesn't really matter here, as we are primarily interested in the distances between the regions.
Preview all the columns with coordinates.
data[['x-cog', 'y-cog', 'z-cog']]
distances_array = squareform(
pdist(X=data[['x-cog', 'y-cog', 'z-cog']],
metric='euclidean')
)
distances_df = pd.DataFrame(distances_array, index=data.index, columns=data.index)
distances_df
# Prepare a bigger figure.
# Axes are unequal because colorbar will be smaller and
# seaborn doesn't compensate for that automatically.
plt.figure(figsize=(16, 13))
# Color bar keyworded arguments are passed so that the colorbar
# will be smaller -- this way the visualization looks better.
ax = sns.heatmap(distances_df,
cbar_kws={"shrink": 0.5})
ax.set_xlabel('Region name')
ax.set_ylabel('Region name');
distances_df.to_csv('MMP_distances.csv')
!ls -1