- add colormap from string to rust code

This commit is contained in:
Wim Pomp
2025-08-23 23:13:57 +02:00
parent 067368e06c
commit 7ea0d1c093
5 changed files with 38 additions and 53 deletions

View File

@@ -6,8 +6,6 @@ from pathlib import Path
from typing import Any, Callable, Sequence
from warnings import warn
import colorcet
import matplotlib
import numpy as np
from numpy.typing import ArrayLike, DTypeLike
from tqdm.auto import tqdm
@@ -77,6 +75,9 @@ class IJTiffFile(rs.IJTiffFile):
else:
return codecs.get(int(idx), 50000)
if colors is not None and colormap is not None:
warn("Cannot have colors and colormap simultaneously.", TiffWriteWarning, stacklevel=2)
self.path = Path(path)
self.dtype = np.dtype(dtype)
if compression is not None:
@@ -88,7 +89,7 @@ class IJTiffFile(rs.IJTiffFile):
if colors is not None:
self.colors = [str(color) for color in colors]
if colormap is not None:
self.colormap = get_colormap(colormap)
self.colormap = str(colormap)
if pxsize is not None:
self.px_size = float(pxsize)
if deltaz is not None:
@@ -96,7 +97,7 @@ class IJTiffFile(rs.IJTiffFile):
if timeinterval is not None:
self.time_interval = float(timeinterval)
if comment is not None:
self.comment = comment
self.comment = str(comment)
if extratags is not None:
for extra_tag in extratags:
self.append_extra_tag(extra_tag, None)
@@ -106,8 +107,6 @@ class IJTiffFile(rs.IJTiffFile):
TiffWriteWarning,
stacklevel=2,
)
if colors is not None and colormap is not None:
warn("Cannot have colors and colormap simultaneously.", TiffWriteWarning, stacklevel=2)
def __enter__(self) -> IJTiffFile:
return self
@@ -146,41 +145,6 @@ class IJTiffFile(rs.IJTiffFile):
self.append_extra_tag(extra_tag, (c, z, t))
def get_colormap(colormap: str) -> np.ndarray:
if hasattr(colorcet, colormap.rstrip("_r")):
cm = np.array(
[[int("".join(i), 16) for i in zip(*[iter(s[1:])] * 2)] for s in getattr(colorcet, colormap.rstrip("_r"))]
).astype("uint8")
if colormap.endswith("_r"):
cm = cm[::-1]
if colormap.startswith("glasbey") or colormap.endswith("glasbey"):
cm[0] = 255, 255, 255
cm[-1] = 0, 0, 0
else:
cmap = matplotlib.colormaps.get_cmap(colormap)
if cmap.N < 256:
cm = (
255
* np.vstack(
(
(1, 1, 1),
matplotlib.cm.ScalarMappable(matplotlib.colors.Normalize(1, 254), cmap).to_rgba(
np.arange(1, 254)
)[:, :3],
(0, 0, 0),
)
)
).astype("uint8")
else:
cm = (
255
* matplotlib.cm.ScalarMappable(matplotlib.colors.Normalize(0, 255), cmap).to_rgba(np.arange(256))[
:, :3
]
).astype("uint8")
return cm
def tiffwrite(
file: str | Path,
data: np.ndarray,