Skip to content

Commit c6c3132

Browse files
committed
Revert "Fix color rendering when more than ~250 colors are present (celeritas-project#8)"
This reverts commit b867d24.
1 parent 70af993 commit c6c3132

File tree

4 files changed

+77
-131
lines changed

4 files changed

+77
-131
lines changed

celerpy/visualize.py

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
from pathlib import Path
1313
from subprocess import TimeoutExpired
1414
from tempfile import NamedTemporaryFile
15-
from typing import Any, NamedTuple, Optional, Union
15+
from typing import Any, Optional, Union
1616

1717
import matplotlib.pyplot as plt
1818
import numpy as np
1919
from matplotlib import colormaps
20-
from matplotlib.axes import Axes as mpl_Axes
2120
from matplotlib.colors import BoundaryNorm, ListedColormap
2221

2322
from . import model, process
@@ -28,39 +27,12 @@
2827
_re_ptr = re.compile(r"0x[0-9a-f]+")
2928

3029

31-
class WrappingListedColormap(ListedColormap):
32-
"""A ListedColormap that wraps around when the number of colors is exceeded.
33-
34-
When more colors are requested than available, this colormap will cycle
35-
through the available colors and emit a warning.
36-
"""
37-
38-
def __init__(self, *args, **kwargs):
39-
super().__init__(*args, **kwargs)
40-
self._warned: bool = False
41-
42-
def __call__(self, X, *args, **kwargs):
43-
X = np.asarray(X)
44-
if not self._warned and (max_val := np.max(X)) >= self.N:
45-
warnings.warn(
46-
f"Color index {max_val} exceeds colormap size {self.N}. "
47-
"Colors will be reused cyclically.",
48-
stacklevel=1,
49-
)
50-
self._warned = True
51-
52-
# Wrap indices using modulo
53-
X_wrapped = np.mod(X, self.N)
54-
return super().__call__(X_wrapped, *args, **kwargs)
55-
56-
5730
def _register_cmaps():
5831
resources = files("celerpy._resources")
59-
with resources.joinpath("glasbey-light.txt").open("r") as f:
60-
cmap = WrappingListedColormap(
61-
np.loadtxt(f),
62-
name="glasbey_light",
63-
)
32+
cmap = ListedColormap(
33+
np.loadtxt(resources.joinpath("glasbey-light.txt")),
34+
name="glasbey_light",
35+
)
6436
try:
6537
colormaps.register(cmap)
6638
except ValueError as e:
@@ -125,15 +97,10 @@ class CelerGeo:
12597
image: Optional[model.ImageParams]
12698
volumes: dict[model.GeometryEngine, list[str]]
12799

128-
@classmethod
129-
def with_setup(cls, *args, **kwargs):
130-
"""Construct, forwarding args to ModelSetup."""
131-
return cls(setup=model.ModelSetup(*args, **kwargs))
132-
133100
@classmethod
134101
def from_filename(cls, path: Path):
135102
"""Construct from a geometry filename and default other setup."""
136-
return cls.with_setup(geometry_file=path)
103+
return cls(model.ModelSetup(geometry_file=path))
137104

138105
def __init__(self, setup: model.ModelSetup):
139106
# Create the process and attach stdin/stdout pipes
@@ -243,15 +210,8 @@ def __missing__(self, key: str):
243210
return result
244211

245212

246-
class LabeledAxis(NamedTuple):
247-
label: str
248-
lo: float
249-
hi: float
250-
251-
252-
class LabeledAxes(NamedTuple):
253-
x: LabeledAxis
254-
y: LabeledAxis
213+
LabeledAxis = collections.namedtuple("LabeledAxis", ["label", "lo", "hi"])
214+
LabeledAxes = collections.namedtuple("LabeledAxes", ["x", "y"])
255215

256216

257217
def calc_image_axes(image: model.ImageParams) -> LabeledAxes:
@@ -294,10 +254,10 @@ def __init__(self, celer_geo, image: model.ImageInput):
294254

295255
def __call__(
296256
self,
297-
ax: mpl_Axes,
257+
ax,
298258
geometry: Optional[model.GeometryEngine] = None,
299259
memspace: Optional[model.MemSpace] = None,
300-
colorbar: Union[bool, None, mpl_Axes] = None,
260+
colorbar=None,
301261
) -> dict[str, Any]:
302262
(trace_output, img) = self.celer_geo.trace(
303263
self.image, geometry=geometry, memspace=memspace
@@ -308,9 +268,9 @@ def __call__(
308268
(x, y) = self.axes
309269

310270
ax.set_xlabel(x.label)
311-
ax.set_xlim((x.lo, x.hi))
271+
ax.set_xlim([x.lo, x.hi])
312272
ax.set_ylabel(y.label)
313-
ax.set_ylim((y.lo, y.hi))
273+
ax.set_ylim([y.lo, y.hi])
314274
tr = trace_output.trace
315275
ax.set_title(f"{tr.geometry.name} ({tr.memspace.name})")
316276

@@ -319,7 +279,7 @@ def __call__(
319279
norm = BoundaryNorm(np.arange(len(volumes) + 1), len(volumes) + 1)
320280
im = ax.imshow(
321281
img,
322-
extent=(x.lo, x.hi, y.lo, y.hi),
282+
extent=[x.lo, x.hi, y.lo, y.hi],
323283
interpolation="none",
324284
norm=norm,
325285
cmap="glasbey_light",
@@ -332,14 +292,11 @@ def __call__(
332292
if colorbar:
333293
# Create colorbar
334294
bounds = norm.boundaries
335-
kwargs: dict[str, Any] = {
336-
"ticks": bounds[:-1] + np.diff(bounds) / 2
337-
}
295+
kwargs = {"ticks": bounds[:-1] + np.diff(bounds) / 2}
338296
if not isinstance(colorbar, bool):
339297
# User can specify a new axis to place the colorbar
340298
kwargs["cax"] = colorbar
341299
fig = ax.get_figure()
342-
assert fig is not None
343300
cbar = fig.colorbar(im, **kwargs)
344301
result["colorbar"] = cbar
345302

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ celerpy = "celerpy.cli:app"
3838

3939
[tool.mypy]
4040
plugins = [
41+
"numpy.typing.mypy_plugin",
4142
"pydantic.mypy"
4243
]
4344

requirements-dev.txt

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,140 +8,130 @@ boltons==25.0.0
88
# via
99
# face
1010
# glom
11-
build==1.3.0
11+
build==1.2.2.post1
1212
# via celerpy (pyproject.toml)
1313
cfgv==3.4.0
1414
# via pre-commit
15-
click==8.3.0
15+
click==8.1.8
1616
# via typer
17-
contourpy==1.3.3
17+
contourpy==1.3.1
1818
# via matplotlib
19-
coverage==7.10.7
19+
coverage==7.8.0
2020
# via pytest-cov
2121
cycler==0.12.1
2222
# via matplotlib
2323
dapperdata==0.4.0
2424
# via celerpy (pyproject.toml)
25-
distlib==0.4.0
25+
distlib==0.3.9
2626
# via virtualenv
2727
face==24.0.0
2828
# via glom
29-
filelock==3.19.1
29+
filelock==3.18.0
3030
# via virtualenv
31-
fonttools==4.60.1
31+
fonttools==4.57.0
3232
# via matplotlib
3333
glom==24.11.0
3434
# via celerpy (pyproject.toml)
35-
identify==2.6.15
35+
identify==2.6.9
3636
# via pre-commit
3737
iniconfig==2.1.0
3838
# via pytest
39-
kiwisolver==1.4.9
39+
kiwisolver==1.4.8
4040
# via matplotlib
41-
markdown-it-py==4.0.0
41+
markdown-it-py==3.0.0
4242
# via rich
43-
matplotlib==3.10.6
43+
matplotlib==3.10.1
4444
# via celerpy (pyproject.toml)
4545
mdurl==0.1.2
4646
# via markdown-it-py
47-
mypy==1.18.2
47+
mypy==1.15.0
4848
# via celerpy (pyproject.toml)
49-
mypy-extensions==1.1.0
49+
mypy-extensions==1.0.0
5050
# via mypy
5151
nodeenv==1.9.1
5252
# via pre-commit
53-
numpy==2.3.3
53+
numpy==2.2.4
5454
# via
5555
# celerpy (pyproject.toml)
5656
# contourpy
5757
# matplotlib
58-
packaging==25.0
58+
packaging==24.2
5959
# via
6060
# build
6161
# matplotlib
6262
# pytest
63-
pathspec==0.12.1
64-
# via mypy
65-
pillow==11.3.0
63+
pillow==11.1.0
6664
# via matplotlib
67-
platformdirs==4.4.0
65+
platformdirs==4.3.7
6866
# via virtualenv
69-
pluggy==1.6.0
70-
# via
71-
# pytest
72-
# pytest-cov
73-
pre-commit==4.3.0
67+
pluggy==1.5.0
68+
# via pytest
69+
pre-commit==4.2.0
7470
# via celerpy (pyproject.toml)
75-
pydantic==2.11.10
71+
pydantic==2.11.2
7672
# via
7773
# celerpy (pyproject.toml)
7874
# dapperdata
7975
# pydantic-settings
80-
pydantic-core==2.33.2
76+
pydantic-core==2.33.1
8177
# via pydantic
82-
pydantic-settings==2.11.0
78+
pydantic-settings==2.8.1
8379
# via
8480
# celerpy (pyproject.toml)
8581
# dapperdata
86-
pygments==2.19.2
87-
# via
88-
# pytest
89-
# rich
90-
pyparsing==3.2.5
82+
pygments==2.19.1
83+
# via rich
84+
pyparsing==3.2.3
9185
# via matplotlib
9286
pyproject-hooks==1.2.0
9387
# via build
94-
pytest==8.4.2
88+
pytest==8.3.5
9589
# via
9690
# celerpy (pyproject.toml)
9791
# pytest-cov
9892
# pytest-pretty
99-
pytest-cov==7.0.0
93+
pytest-cov==6.1.0
10094
# via celerpy (pyproject.toml)
101-
pytest-pretty==1.3.0
95+
pytest-pretty==1.2.0
10296
# via celerpy (pyproject.toml)
10397
python-dateutil==2.9.0.post0
10498
# via matplotlib
105-
python-dotenv==1.1.1
99+
python-dotenv==1.1.0
106100
# via pydantic-settings
107-
pyyaml==6.0.3
101+
pyyaml==6.0.2
108102
# via pre-commit
109-
rich==14.1.0
103+
rich==14.0.0
110104
# via
111105
# pytest-pretty
112106
# typer
113-
ruamel-yaml==0.18.15
107+
ruamel-yaml==0.18.10
114108
# via
115109
# celerpy (pyproject.toml)
116110
# dapperdata
117-
ruamel-yaml-clib==0.2.14
118-
# via ruamel-yaml
119-
ruff==0.13.3
111+
ruff==0.11.4
120112
# via celerpy (pyproject.toml)
121113
shellingham==1.5.4
122114
# via typer
123115
six==1.17.0
124116
# via python-dateutil
125-
toml-sort==0.24.3
117+
toml-sort==0.24.2
126118
# via celerpy (pyproject.toml)
127-
tomlkit==0.13.3
119+
tomlkit==0.13.2
128120
# via toml-sort
129-
typer==0.19.2
121+
typer==0.15.2
130122
# via
131123
# celerpy (pyproject.toml)
132124
# dapperdata
133-
typing-extensions==4.15.0
125+
typing-extensions==4.13.1
134126
# via
135127
# mypy
136128
# pydantic
137129
# pydantic-core
138130
# typer
139131
# typing-inspection
140-
typing-inspection==0.4.2
141-
# via
142-
# pydantic
143-
# pydantic-settings
144-
uv==0.8.23
132+
typing-inspection==0.4.0
133+
# via pydantic
134+
uv==0.6.12
145135
# via celerpy (pyproject.toml)
146-
virtualenv==20.34.0
136+
virtualenv==20.30.0
147137
# via pre-commit

0 commit comments

Comments
 (0)