Skip to content

Commit

Permalink
fix: fixes for numpy2 (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 authored Jun 20, 2024
1 parent 11e3874 commit de1fbe2
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ repos:
- id: mypy
files: "^src/"
additional_dependencies:
- numpy
- numpy>=2
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test_thirdparty = [
"colorspacious",
"bokeh",
"colour",
"napari",
"napari>=0.4.19",
"plotly",
"pydantic",
"pydantic-extra-types",
Expand Down
2 changes: 1 addition & 1 deletion src/cmap/_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def __repr__(self) -> str:
if self.name:
arg: str | tuple = self.name
else:
arg = tuple(round(x, 4) for x in tuple(self._rgba))
arg = tuple(round(float(x), 4) for x in tuple(self._rgba))
if self._rgba.a == 1:
arg = arg[:3]
return f"{self.__class__.__name__}({arg!r})"
Expand Down
6 changes: 4 additions & 2 deletions src/cmap/_colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,9 @@ def __call__(

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

def __eq__(self, __o: object) -> bool:
Expand Down
4 changes: 3 additions & 1 deletion tests/test_colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ def test_colormap_apply() -> None:
img = np.zeros((10, 10))
assert cmap1(img).shape == (10, 10, 4)
# non-native byte order
assert cmap1(img.byteswap().newbyteorder()).shape == (10, 10, 4)
new_order = ">" if sys.byteorder == "little" else "<"
swapped = img.view(img.dtype.newbyteorder(new_order))
assert cmap1(swapped).shape == (10, 10, 4)


def test_fill_stops() -> None:
Expand Down

0 comments on commit de1fbe2

Please sign in to comment.