1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
"""
=================================
Plot RGBA values on brain surface
=================================
In this example, each vertex on a 3D brain is plotted with a different
RGBA value. Hue varies along the x-axis (right/left direction) and
alpha varies along the z-axis (up/down direction). However, this can be
easily generalised to other use cases.
"""
import os
import numpy as np
import matplotlib.pyplot as plt
from mayavi import mlab
from tvtk.api import tvtk
from tvtk.common import configure_input_data
from surfer import Brain
print(__doc__)
# 1) define helper functions
def norm(x):
'''Normalise array betweeen 0-1'''
return (x - np.min(x)) / (np.max(x) - np.min(x))
# 2) init brain and get spatial co-ordinates
# params
subjects_dir = os.environ['SUBJECTS_DIR']
hemi = 'lh'
surf = 'white'
# init figure
fig = mlab.figure()
b = Brain('fsaverage', hemi, surf, subjects_dir=subjects_dir,
background='white', figure=fig)
# co-ordinates
x, y, z = b.geo[hemi].coords.T
tris = b.geo[hemi].faces
# 3) generate an rgba matrix, of shape n_vertices x 4
# define color map
cmap = plt.cm.viridis
# change colour based on position on the x axis
hue = norm(x)
colors = cmap(hue)[:, :3]
# change alpha based on position on the z axis
alpha = norm(z)
# combine hue and alpha into a Nx4 matrix
rgba_vals = np.concatenate((colors, alpha[:, None]), axis=1)
# 4) add data to plot
# plot points in x,y,z
mesh = mlab.pipeline.triangular_mesh_source(
x, y, z, tris, figure=fig)
mesh.data.point_data.scalars.number_of_components = 4 # r, g, b, a
mesh.data.point_data.scalars = (rgba_vals * 255).astype('ubyte')
# tvtk for vis
mapper = tvtk.PolyDataMapper()
configure_input_data(mapper, mesh.data)
actor = tvtk.Actor()
actor.mapper = mapper
fig.scene.add_actor(actor)
|