Skip to content
68 changes: 18 additions & 50 deletions fastplotlib/utils/functions.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
from collections import OrderedDict
from typing import *
from pathlib import Path

import numpy as np
from cmap import Colormap

from pygfx import Texture, Color

# some funcs adapted from mesmerize


QUALITATIVE_CMAPS = [
"Pastel1",
"Pastel2",
"Paired",
"Accent",
"Dark2",
"Set1",
"Set2",
"Set3",
"tab10",
"tab20",
"tab20b",
"tab20c",
]


def get_cmap(name: str, alpha: float = 1.0) -> np.ndarray:
def get_cmap(name: str, alpha: float = 1.0, gamma: float = 1.0) -> np.ndarray:
Comment thread
tlambert03 marked this conversation as resolved.
"""
Get a colormap as numpy array

Expand All @@ -42,24 +24,8 @@ def get_cmap(name: str, alpha: float = 1.0) -> np.ndarray:
[n_colors, 4], i.e. [n_colors, RGBA]

"""

cmap_path = Path(__file__).absolute().parent.joinpath("colormaps", name)
if cmap_path.is_file():
cmap = np.loadtxt(cmap_path)

else:
try:
from .generate_colormaps import make_cmap

cmap = make_cmap(name, alpha)
except (ImportError, ModuleNotFoundError):
raise ModuleNotFoundError(
"Couldn't find colormap files, matplotlib is required to generate them "
"if they aren't found. Please install `matplotlib`"
)

cmap = Colormap(name).lut(256, gamma=gamma)
cmap[:, -1] = alpha

return cmap.astype(np.float32)


Expand All @@ -85,34 +51,35 @@ def make_colors(n_colors: int, cmap: str, alpha: float = 1.0) -> np.ndarray:
shape is [n_colors, 4], where the last dimension is RGBA

"""
name = cmap
cmap = get_cmap(name, alpha)

if name in QUALITATIVE_CMAPS:
max_colors = cmap.shape[0]
if n_colors > cmap.shape[0]:
cm = Colormap(cmap)

# can also use cm.category == "qualitative", but checking for non-interpolated
# colormaps is a bit more general. (and not all "custom" colormaps will be
# assigned a category)
if cm.interpolation == "nearest":
max_colors = len(cm.color_stops)
if n_colors > max_colors:
raise ValueError(
f"You have requested <{n_colors}> but only <{max_colors} existing for the "
f"chosen cmap: <{cmap}>"
)
return cmap[:n_colors]
return np.asarray(cm.color_stops, dtype=np.float32)[:n_colors, 1:]

cm_ixs = np.linspace(0, 255, n_colors, dtype=int)
return np.take(cmap, cm_ixs, axis=0).astype(np.float32)
return cm(cm_ixs).astype(np.float32)


def get_cmap_texture(name: str, alpha: float = 1.0) -> Texture:
cmap = get_cmap(name)
return Texture(cmap, dim=1)
return Colormap(name).to_pygfx()


def make_colors_dict(labels: iter, cmap: str, **kwargs) -> OrderedDict:
def make_colors_dict(labels: Sequence, cmap: str, **kwargs) -> OrderedDict:
"""
Get a dict for mapping labels onto colors.

Parameters
----------
labels: Iterable[Any]
labels: Sequence[Any]
labels for creating a colormap. Order is maintained if it is a list of unique elements.

cmap: str
Expand Down Expand Up @@ -280,7 +247,8 @@ def parse_cmap_values(

n_colors = colormap.shape[0] - 1

if cmap_name in QUALITATIVE_CMAPS:
# can also use cm.category == "qualitative"
if Colormap(cmap_name).interpolation == "nearest":
# check that cmap_values are <int> and within the number of colors `n_colors`
# do not scale, use directly
if not np.issubdtype(cmap_values.dtype, np.integer):
Expand Down
126 changes: 0 additions & 126 deletions fastplotlib/utils/generate_colormaps.py

This file was deleted.

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
install_requires = [
"numpy>=1.23.0",
"pygfx>=0.1.14",
"cmap>=0.1.3",
]


Expand Down