Skip to content

Instantly share code, notes, and snippets.

@margulies
Created February 28, 2019 17:40
Show Gist options
  • Save margulies/acf994936639d075ff0e9b8e65a80369 to your computer and use it in GitHub Desktop.
Save margulies/acf994936639d075ff0e9b8e65a80369 to your computer and use it in GitHub Desktop.
def plot_surf_stat_multidim(coords, faces, face_colors=None, stat_map=None,
elev=0, azim=0,
cmap=None,
threshold=None, bg_map=None,
mask=None,
bg_on_stat=False,
alpha='auto',
vmax=None, symmetric_cbar="auto",
figsize=(14,11), label=None, lenient=None,
**kwargs):
''' Visualize results on cortical surface using matplotlib'''
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri
from mpl_toolkits.mplot3d import Axes3D
# load mesh and derive axes limits
faces = np.array(faces, dtype=int)
limits = [coords.min(), coords.max()]
alpha = 1
fig = plt.figure(figsize=figsize)
fig.patch.set_facecolor('white')
ax1 = fig.add_subplot(111, projection='3d', xlim=limits, ylim=limits)
ax1.grid(False)
ax1.set_axis_off()
ax1.w_zaxis.line.set_lw(0.)
ax1.set_zticks([])
ax1.view_init(elev=elev, azim=azim)
#ax1.set_axis_off()
# plot mesh without data
p3dcollec = ax1.plot_trisurf(coords[:, 0], coords[:, 1], coords[:, 2],
triangles=faces, linewidth=0.,
antialiased=False,
color='white')
#face_colors = np.mean(multidim(faces), axis=0)
p3dcollec.set_facecolors(face_colors)
return fig
# Load data:
df = pd.DataFrame()
df['e0'] = np.array(emb[range(cortLen),0])
df['e1'] = np.array(emb[range(cortLen),1]) * -1 # to reorient plot
# Create rgba colors for each vertex:
import matplotlib.cm as cm
norm_1 = mpl.colors.Normalize(vmin=df['e0'].min(), vmax=df['e0'].max())
cmap_1 = cm.Reds
map_1 = cm.ScalarMappable(norm=norm_1, cmap=cmap_1)
norm_2 = mpl.colors.Normalize(vmin=df['e1'].min(), vmax=df['e1'].max())
cmap_2 = cm.Greens
map_2 = cm.ScalarMappable(norm=norm_2, cmap=cmap_2)
norm_3 = mpl.colors.Normalize(vmin=df['e1'].max() * -1, vmax=df['e1'].min() * -1)
cmap_3 = cm.Blues
map_3= cm.ScalarMappable(norm=norm_3, cmap=cmap_3)
c = np.ones(map_1.to_rgba(df['e0']).shape)
a = df['e1'].copy()
a = a - a.min()
a = a / a.max()
c[:,1] = a
a = df['e1'].copy() * -1
a = a - a.min()
a = a / a.max()
c[:,2] = a
a = df['e0'].copy()
a = a - a.min()
a = a / a.max()
c[:,0] = a
c[:,1] = c[:,1] * ((a - 1) * -1)
c[:,2] = c[:,2] * ((a - 1) * -1)
# Project to faces and visualize:
c = np.array(c)
d = np.ones((32492*2,4))
d[cortL,:] = c[range(len(cortL)),:]
bg_map=sulcL
bg_faces = np.mean(bg_map[surfL[1]], axis=1)
bg_faces = bg_faces - bg_faces.min()
bg_faces = bg_faces / bg_faces.max() / 2.
face_colors = plt.cm.gray_r(bg_faces)
face_colors = np.mean(d[surfL[1]], axis=1) * face_colors
f = plot_surf_stat_multidim(surfL[0], surfL[1], bg_map=sulcL, face_colors=face_colors, azim=0, alpha=1)
f.savefig('gradient_data/figures/hcp.emb.0.multidim.lat.png')
f = plot_surf_stat_multidim(surfL[0], surfL[1], bg_map=sulcL, face_colors=face_colors, azim=180, alpha=1)
f.savefig('gradient_data/figures/hcp.emb.0.multidim.med.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment