Skip to content

Commit 1ce7e3d

Browse files
committed
Add function to create lut from many sources
1 parent d77cea2 commit 1ce7e3d

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

surfer/tests/test_utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from os.path import join as pjoin
22

33
import numpy as np
4+
import nose.tools as nt
45
from numpy.testing import assert_array_almost_equal, assert_array_equal
56

67
from surfer import utils
@@ -64,3 +65,29 @@ def test_huge_cross():
6465
z = np.cross(x, y)
6566
zz = utils._fast_cross_3d(x, y)
6667
assert_array_equal(z, zz)
68+
69+
70+
def test_create_color_lut():
71+
"""Test various ways of making a colormap."""
72+
# Test valid lut
73+
cmap_in = (np.random.rand(256, 4) * 255).astype(np.int)
74+
cmap_out = utils.create_color_lut(cmap_in)
75+
assert_array_equal(cmap_in, cmap_out)
76+
77+
# Test mostly valid lut
78+
cmap_in = cmap_in[:, :3]
79+
cmap_out = utils.create_color_lut(cmap_in)
80+
assert_array_equal(cmap_in, cmap_out[:, :3])
81+
assert_array_equal(cmap_out[:, 3], np.ones(256, np.int) * 255)
82+
83+
# Test matplotlib lut
84+
cmap_out = utils.create_color_lut("BuGn_r")
85+
nt.assert_equal(cmap_out.shape, (256, 4))
86+
87+
# Test list of colors lut
88+
cmap_out = utils.create_color_lut(["purple", "pink", "white"])
89+
nt.assert_equal(cmap_out.shape, (256, 4))
90+
91+
# Test that we can ask for a specific number of colors
92+
cmap_out = utils.create_color_lut("Reds", 12)
93+
nt.assert_equal(cmap_out.shape, (12, 4))

surfer/utils.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import nibabel as nib
1111
from scipy import sparse
1212
from scipy.spatial.distance import cdist
13+
import matplotlib as mpl
14+
from matplotlib import cm
1315

1416
from .config import config
1517

@@ -413,6 +415,60 @@ def mesh_edges(faces):
413415
return edges
414416

415417

418+
def create_color_lut(cmap, n_colors=256):
419+
"""Return a colormap suitable for setting as a Mayavi LUT.
420+
421+
Parameters
422+
----------
423+
cmap : string, list of colors, n x 3 or n x 4 array
424+
Input colormap definition. This can be the name of a matplotlib
425+
colormap, a list of valid matplotlib colors, or a suitable
426+
mayavi LUT (possibly missing the alpha channel).
427+
n_colors : int, optional
428+
Number of colors in the resulting LUT. This is ignored if cmap
429+
is a 2d array.
430+
Returns
431+
-------
432+
lut : n_colors x 4 integer array
433+
Color LUT suitable for passing to mayavi
434+
"""
435+
if isinstance(cmap, np.ndarray):
436+
if np.ndim(cmap) == 2:
437+
if cmap.shape[1] == 4:
438+
# This looks likes a LUT that's ready to go
439+
lut = cmap.astype(np.int)
440+
elif cmap.shape[1] == 3:
441+
# This looks like a LUT, but it's missing the alpha channel
442+
alpha = np.ones(len(cmap), np.int) * 255
443+
lut = np.c_[cmap, alpha]
444+
445+
return lut
446+
447+
# Otherwise, we're going to try and use matplotlib to create it
448+
449+
if cmap in dir(cm):
450+
# This is probably a matplotlib colormap, so build from that
451+
# The matplotlib colormaps are a superset of the mayavi colormaps
452+
# except for one or two cases (i.e. blue-red, which is a crappy
453+
# rainbow colormap and shouldn't be used for anything, although in
454+
# its defense it's better than "Jet")
455+
cmap = getattr(cm, cmap)
456+
457+
elif np.iterable(cmap):
458+
# This looks like a list of colors? Let's try that.
459+
colors = list(map(mpl.colors.colorConverter.to_rgb, cmap))
460+
cmap = mpl.colors.LinearSegmentedColormap.from_list("_", colors)
461+
462+
else:
463+
# If we get here, it's a bad input
464+
raise ValueError("Input %s was not valid for making a lut" % cmap)
465+
466+
# Convert from a matplotlib colormap to a lut array
467+
lut = (cmap(np.linspace(0, 1, n_colors)) * 255).astype(np.int)
468+
469+
return lut
470+
471+
416472
@verbose
417473
def smoothing_matrix(vertices, adj_mat, smoothing_steps=20, verbose=None):
418474
"""Create a smoothing matrix which can be used to interpolate data defined

0 commit comments

Comments
 (0)