Skip to content

Commit de1fbe2

Browse files
authored
fix: fixes for numpy2 (#60)
1 parent 11e3874 commit de1fbe2

File tree

5 files changed

+10
-6
lines changed

5 files changed

+10
-6
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ repos:
3535
- id: mypy
3636
files: "^src/"
3737
additional_dependencies:
38-
- numpy
38+
- numpy>=2

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ test_thirdparty = [
5151
"colorspacious",
5252
"bokeh",
5353
"colour",
54-
"napari",
54+
"napari>=0.4.19",
5555
"plotly",
5656
"pydantic",
5757
"pydantic-extra-types",

src/cmap/_color.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def __repr__(self) -> str:
476476
if self.name:
477477
arg: str | tuple = self.name
478478
else:
479-
arg = tuple(round(x, 4) for x in tuple(self._rgba))
479+
arg = tuple(round(float(x), 4) for x in tuple(self._rgba))
480480
if self._rgba.a == 1:
481481
arg = arg[:3]
482482
return f"{self.__class__.__name__}({arg!r})"

src/cmap/_colormap.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ def __call__(
354354

355355
xa = np.array(x, copy=True)
356356
if not xa.dtype.isnative:
357-
xa = xa.byteswap().newbyteorder() # Native byteorder is faster.
357+
# Native byteorder is faster.
358+
native: Literal[">", "<"] = ">" if xa.dtype.byteorder in ("<", "=") else "<"
359+
xa = xa.view(xa.dtype.newbyteorder(native))
358360
if xa.dtype.kind == "f":
359361
xa *= N
360362
# xa == 1 (== N after multiplication) is not out of range.
@@ -974,7 +976,7 @@ def __repr__(self) -> str:
974976
rev = " <reversed>"
975977
name = f"{f.__module__}{f.__qualname__}"
976978
return f"ColorStops(lut_func={name!r}{rev})"
977-
m = ",\n ".join(repr((pos, Color(rgba))) for pos, *rgba in self._stops)
979+
m = ",\n ".join(repr((pos.item(), Color(rgba))) for pos, *rgba in self._stops)
978980
return f"ColorStops(\n {m}\n)"
979981

980982
def __eq__(self, __o: object) -> bool:

tests/test_colormap.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ def test_colormap_apply() -> None:
130130
img = np.zeros((10, 10))
131131
assert cmap1(img).shape == (10, 10, 4)
132132
# non-native byte order
133-
assert cmap1(img.byteswap().newbyteorder()).shape == (10, 10, 4)
133+
new_order = ">" if sys.byteorder == "little" else "<"
134+
swapped = img.view(img.dtype.newbyteorder(new_order))
135+
assert cmap1(swapped).shape == (10, 10, 4)
134136

135137

136138
def test_fill_stops() -> None:

0 commit comments

Comments
 (0)