Skip to content

Commit 3f6406e

Browse files
authored
feat: select unique names by category (#25)
1 parent cac8512 commit 3f6406e

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

src/cmap/_catalog.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from typing import (
1515
TYPE_CHECKING,
1616
Any,
17+
Container,
1718
Iterable,
1819
Iterator,
1920
Literal,
@@ -94,7 +95,7 @@ class CatalogItem:
9495
interpolation: bool | Interpolation
9596
The interpolation method to use when sampling the colormap. One of
9697
{False, True, "linear", "nearest"}, where False is equivalent to "nearest"
97-
and True is equivalent to "linear".
98+
and True is equivalent to "linear". If not provided, defaults to "linear".
9899
tags: list[str]
99100
A list of tags for the colormap. These are displayed in the documentation.
100101
aliases: list[str]
@@ -235,7 +236,11 @@ def __init__(
235236
)
236237

237238
def unique_keys(
238-
self, prefer_short_names: bool = True, normalized_names: bool = False
239+
self,
240+
prefer_short_names: bool = True,
241+
normalized_names: bool = False,
242+
categories: Container[Category] = (),
243+
interpolation: Interpolation | None = None,
239244
) -> set[str]:
240245
"""Return names that refer to unique colormap data.
241246
@@ -250,6 +255,10 @@ def unique_keys(
250255
If True, return the normalized names of the colormaps. If False (default),
251256
return the original names of the colormaps (which may include spaces and/or
252257
capital letters).
258+
categories : Container[Category], optional
259+
If provided, only return colormaps in the given categories.
260+
interpolation : Interpolation, optional
261+
If provided, only return colormaps with the given interpolation method.
253262
254263
Returns
255264
-------
@@ -258,8 +267,18 @@ def unique_keys(
258267
"""
259268
keys: set[str] = set()
260269
for original_name, normed_name in self._original_names.items():
261-
if "alias" in self._data[normed_name]:
270+
data = self._data[normed_name]
271+
if "alias" in data:
262272
continue
273+
if categories and data["category"] not in categories:
274+
continue
275+
if interpolation is not None:
276+
interp = data.get("interpolation", "linear")
277+
if isinstance(interp, bool):
278+
interp = "linear" if interp else "nearest"
279+
if interp != interpolation:
280+
continue
281+
263282
if prefer_short_names:
264283
short_name = normed_name.split(NAMESPACE_DELIMITER, 1)[-1]
265284
data2 = self._data[short_name]

src/cmap/_colormap.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,17 @@
44
import warnings
55
from functools import partial
66
from numbers import Number
7-
from typing import (
8-
TYPE_CHECKING,
9-
Any,
10-
Callable,
11-
Iterable,
12-
Iterator,
13-
Mapping,
14-
NamedTuple,
15-
Sequence,
16-
Union,
17-
cast,
18-
overload,
19-
)
7+
from typing import TYPE_CHECKING, NamedTuple, Sequence, cast, overload
208

219
import numpy as np
2210
import numpy.typing as npt
2311

2412
from . import _external
13+
from ._catalog import Catalog
2514
from ._color import Color
2615

2716
if TYPE_CHECKING:
28-
from typing import Literal
17+
from typing import Any, Callable, Iterable, Iterator, Literal, Union
2918

3019
import bokeh.models
3120
import matplotlib.colors
@@ -130,14 +119,12 @@ class Colormap:
130119
interpolation: Interpolation
131120
info: CatalogItem | None
132121

133-
_catalog_instance: Mapping[str, CatalogItem] | None = None
122+
_catalog_instance: Catalog | None = None
134123

135124
@classmethod
136-
def catalog(cls) -> Mapping[str, CatalogItem]:
125+
def catalog(cls) -> Catalog:
137126
"""Return the global colormaps catalog."""
138127
if cls._catalog_instance is None:
139-
from ._catalog import Catalog
140-
141128
cls._catalog_instance = Catalog()
142129
return cls._catalog_instance
143130

tests/test_catalog.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,8 @@ def test_catalog_names() -> None:
6464
assert "viridis" not in unique
6565
assert "bids:viridis" in unique
6666
assert "matplotlib:viridis" not in unique
67+
68+
assert "viridis" not in catalog.unique_keys(categories=("cyclic",))
69+
assert "viridis" in catalog.unique_keys(categories=("sequential",))
70+
assert "viridis" not in catalog.unique_keys(interpolation="nearest")
71+
assert "viridis" in catalog.unique_keys(interpolation="linear")

0 commit comments

Comments
 (0)