Skip to content

Commit baca853

Browse files
LauraGwilliamsagramfort
authored andcommitted
ADD: first draft of plot_custom_colours script (#239)
* ADD: first draft of plot_custom_colours script * FIX: unpack function into linear code * FIX: add surf object to example_data and work from that rather than MNE * FIX: import correct packages * FIX: path to example_data and change colormap choice * FIX: load surface from freesurfer using os.environ rather than adding file to example_data * AES: simplify the loading of coords * FIX: remove #### code blocks * AES: filename consistency. remove curv alpha for nicer rendering * FIX: stupid mistake w/ commenting * FIX: flake complaints * AES: add brief description
1 parent 6eef8dc commit baca853

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

examples/plot_custom_colors.py

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

0 commit comments

Comments
 (0)