|
10 | 10 | import nibabel as nib
|
11 | 11 | from scipy import sparse
|
12 | 12 | from scipy.spatial.distance import cdist
|
| 13 | +import matplotlib as mpl |
| 14 | +from matplotlib import cm |
13 | 15 |
|
14 | 16 | from .config import config
|
15 | 17 |
|
@@ -413,6 +415,60 @@ def mesh_edges(faces):
|
413 | 415 | return edges
|
414 | 416 |
|
415 | 417 |
|
| 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 | + |
416 | 472 | @verbose
|
417 | 473 | def smoothing_matrix(vertices, adj_mat, smoothing_steps=20, verbose=None):
|
418 | 474 | """Create a smoothing matrix which can be used to interpolate data defined
|
|
0 commit comments