Skip to content

Commit 6767228

Browse files
authored
fixes for napari 0.6.1 (#103)
1 parent 3e78298 commit 6767228

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ include = ["/src", "/tests", "CHANGELOG.md"]
101101
line-length = 88
102102
target-version = "py39"
103103
src = ["src"]
104+
fix = true
105+
unsafe-fixes = true
104106

105107
[tool.ruff.lint]
106108
pydocstyle = { convention = "numpy" }

src/cmap/_colormap.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,13 @@ def __init__(
239239
bad = info.bad if bad is None else bad
240240
self.info = info
241241
if isinstance(info.data, list):
242+
if not info.data: # pragma: no cover
243+
raise ValueError(f"Catalog colormap {info.name!r} has no data")
242244
ld = len(info.data[0])
243245
if ld == 2:
244246
# if it's a list of tuples, it's a list of color stops
245247
stops = ColorStops._from_uniform_stops(info.data)
246-
elif ld == 3:
248+
elif ld in (3, 4):
247249
stops = ColorStops._from_colorarray_like(info.data)
248250
else: # pragma: no cover
249251
raise ValueError(

src/cmap/_external.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import contextlib
6+
from functools import cache
67
from typing import TYPE_CHECKING
78

89
import numpy as np
@@ -82,16 +83,39 @@ def to_plotly(cm: Colormap) -> list[list[float | str]]:
8283
return [[pos, color.rgba_string] for pos, color in cm.color_stops]
8384

8485

86+
@cache
87+
def _napari_colormap_param_names() -> set[str]:
88+
from napari.utils.colormaps import Colormap
89+
90+
if hasattr(Colormap, "__fields__"):
91+
return set(Colormap.__fields__)
92+
elif hasattr(Colormap, "model_fields"):
93+
return set(Colormap.model_fields)
94+
return set()
95+
96+
8597
def to_napari(cm: Colormap) -> NapariColormap:
8698
"""Return a napari colormap."""
8799
from napari.utils.colormaps import Colormap
88100

89-
return Colormap(
90-
colors=cm.color_stops.color_array,
91-
controls=cm.color_stops.stops,
92-
name=cm.identifier or "custom colormap",
93-
display_name=cm.name,
94-
)
101+
kwargs = {
102+
"colors": cm.color_stops.color_array,
103+
"controls": cm.color_stops.stops,
104+
"name": cm.identifier or "custom colormap",
105+
"display_name": cm.name,
106+
}
107+
if param_names := _napari_colormap_param_names():
108+
if "interpolation" in param_names:
109+
kwargs["interpolation"] = (
110+
"zero" if cm.interpolation == "nearest" else "linear"
111+
)
112+
if "nan_color" in param_names and cm.bad_color is not None:
113+
kwargs["nan_color"] = cm.bad_color.rgba
114+
if "high_color" in param_names and cm.over_color is not None:
115+
kwargs["nan_color"] = cm.over_color.rgba
116+
if "low_color" in param_names and cm.under_color is not None:
117+
kwargs["low_color"] = cm.under_color.rgba
118+
return Colormap(**kwargs)
95119

96120

97121
def to_bokeh(cm: Colormap, N: int = 256) -> BokehLinearColorMapper:

src/cmap/data/napari/record.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"colormaps": {
3+
"HiLo": {
4+
"category": "miscellaneous",
5+
"data": [
6+
[
7+
0.0,
8+
0.0,
9+
0.0,
10+
1.0
11+
],
12+
[
13+
1.0,
14+
1.0,
15+
1.0,
16+
1.0
17+
]
18+
],
19+
"over": [
20+
1.0,
21+
0.0,
22+
0.0,
23+
1.0
24+
],
25+
"under": [
26+
0.0,
27+
0.0,
28+
1.0,
29+
1.0
30+
]
31+
},
32+
"nan": {
33+
"bad": [
34+
1.0,
35+
0.0,
36+
0.0,
37+
1.0
38+
],
39+
"category": "miscellaneous",
40+
"data": [
41+
[
42+
0.0,
43+
0.0,
44+
0.0,
45+
1.0
46+
],
47+
[
48+
1.0,
49+
1.0,
50+
1.0,
51+
1.0
52+
]
53+
]
54+
}
55+
},
56+
"license": "BSD-3-Clause",
57+
"namespace": "napari",
58+
"source": "https://github.com/napari/napari"
59+
}

0 commit comments

Comments
 (0)