Coverage for src/bioimageio/core/_ops_stardist.py: 72%
95 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 09:46 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 09:46 +0000
1from __future__ import annotations
3from abc import ABC, abstractmethod
4from dataclasses import dataclass
5from typing import (
6 Any,
7 Generic,
8 cast,
9)
11import numpy as np
12from numpy.typing import NDArray
13from typing_extensions import Self, TypeVar
15from bioimageio.spec.model import v0_5
17from ._op_base import SamplewiseOperator
18from .axis import AxisId
19from .common import MemberId
20from .sample import Sample
21from .tensor import Tensor
23NdTuple = TypeVar("NdTuple", tuple[int, int], tuple[int, int, int])
24NdBorder = TypeVar(
25 "NdBorder",
26 tuple[tuple[int, int], tuple[int, int]],
27 tuple[tuple[int, int], tuple[int, int], tuple[int, int]],
28)
31@dataclass(frozen=True)
32class _StardistPostprocessingBase(SamplewiseOperator, ABC, Generic[NdTuple, NdBorder]):
33 prob_dist_input_id: MemberId
34 instance_labels_output_id: MemberId
36 grid: NdTuple
37 """Grid size of network predictions."""
39 prob_threshold: float
40 """Object probability threshold for non-maximum suppression."""
42 nms_threshold: float
43 """The IoU threshold for non-maximum suppression."""
45 b: int | NdBorder
46 """Border region in which object probability is set to zero."""
48 n_rays: int
49 """Number of radial lines (rays) cast from the center of an object to its boundary."""
51 @property
52 def output_member_ids(self) -> tuple[MemberId, ...]:
53 return (self.instance_labels_output_id,)
55 def __call__(self, sample: Sample) -> None:
56 prob_dist = sample.members[self.prob_dist_input_id]
58 assert AxisId("channel") in prob_dist.dims, (
59 "expected 'channel' axis in stardist probability/distance input"
60 )
61 allowed_spatial = tuple(
62 map(AxisId, ("y", "x") if len(self.grid) == 2 else ("z", "y", "x"))
63 )
64 assert all(
65 a in allowed_spatial or a in (AxisId("batch"), AxisId("channel"))
66 for a in prob_dist.dims
67 ), (
68 f"expected prob_dist to have only 'batch', 'channel', and spatial axes {allowed_spatial}, but got {prob_dist.dims}"
69 )
71 spatial_shape = tuple(
72 prob_dist.tagged_shape[a] * g for a, g in zip(allowed_spatial, self.grid)
73 )
74 if len(spatial_shape) != len(self.grid): 74 ↛ 75line 74 didn't jump to line 75 because the condition on line 74 was never true
75 raise ValueError(
76 f"expected {len(self.grid)} spatial dimensions in prob_dist tensor, but got {len(spatial_shape)}"
77 )
78 else:
79 spatial_shape = cast(NdTuple, spatial_shape)
81 prob_dist = prob_dist.transpose(
82 (AxisId("batch"), *allowed_spatial, AxisId("channel"))
83 )
84 labels: list[NDArray[Any]] = []
85 for batch_idx in range(prob_dist.sizes[AxisId("batch")]):
86 prob = prob_dist[
87 {AxisId("batch"): batch_idx, AxisId("channel"): 0}
88 ].to_numpy()
89 dist = prob_dist[
90 {AxisId("batch"): batch_idx, AxisId("channel"): slice(1, None)}
91 ].to_numpy()
93 labels_i = self._impl(prob, dist, spatial_shape)
94 assert labels_i.shape == spatial_shape, (
95 f"expected label image shape {spatial_shape}, but got {labels_i.shape}"
96 )
97 labels.append(labels_i)
99 instance_labels = Tensor(
100 np.stack(labels)[..., None],
101 dims=(AxisId("batch"), *allowed_spatial, AxisId("channel")),
102 )
103 grid = dict(zip(allowed_spatial, self.grid))
104 instance_labels.set_physical_scale(
105 {a: s / grid.get(a, 1) for a, s in prob_dist.get_physical_scale().items()}
106 )
107 instance_labels.set_physical_scale_unit(prob_dist.get_physical_scale_unit())
108 sample.members[self.instance_labels_output_id] = instance_labels
109 sample.applied_ops += (self,)
111 @abstractmethod
112 def _impl(
113 self, prob: NDArray[Any], dist: NDArray[Any], spatial_shape: NdTuple
114 ) -> NDArray[np.int32]:
115 raise NotImplementedError
118@dataclass(frozen=True)
119class StardistPostprocessing2D(
120 _StardistPostprocessingBase[
121 tuple[int, int], tuple[tuple[int, int], tuple[int, int]]
122 ]
123):
124 def get_descr(self):
125 return v0_5.StardistPostprocessingDescr(
126 kwargs=v0_5.StardistPostprocessingKwargs2D(
127 grid=self.grid,
128 prob_threshold=self.prob_threshold,
129 nms_threshold=self.nms_threshold,
130 b=self.b,
131 n_rays=self.n_rays,
132 )
133 )
135 def _impl(
136 self, prob: NDArray[Any], dist: NDArray[Any], spatial_shape: tuple[int, int]
137 ) -> NDArray[np.int32]:
138 from stardist import ( # pyright: ignore[reportMissingTypeStubs]
139 non_maximum_suppression, # pyright: ignore[reportUnknownVariableType]
140 polygons_to_label, # pyright: ignore[reportUnknownVariableType]
141 )
143 points, probi, disti = non_maximum_suppression(
144 dist,
145 prob,
146 grid=self.grid,
147 prob_thresh=self.prob_threshold,
148 nms_thresh=self.nms_threshold,
149 b=self.b, # pyright: ignore[reportArgumentType]
150 )
152 return polygons_to_label(disti, points, prob=probi, shape=spatial_shape)
154 @classmethod
155 def from_proc_descr(
156 cls, descr: v0_5.StardistPostprocessingDescr, member_id: MemberId
157 ) -> Self:
158 if not isinstance(descr.kwargs, v0_5.StardistPostprocessingKwargs2D):
159 raise TypeError(
160 f"expected v0_5.StardistPostprocessingKwargs2D for 2D stardist post-processing, but got {type(descr.kwargs)}"
161 )
163 kwargs = descr.kwargs
164 return cls(
165 prob_dist_input_id=member_id,
166 instance_labels_output_id=member_id,
167 grid=kwargs.grid,
168 prob_threshold=kwargs.prob_threshold,
169 nms_threshold=kwargs.nms_threshold,
170 b=kwargs.b,
171 n_rays=kwargs.n_rays,
172 )
175@dataclass(frozen=True)
176class StardistPostprocessing3D(
177 _StardistPostprocessingBase[
178 tuple[int, int, int], tuple[tuple[int, int], tuple[int, int], tuple[int, int]]
179 ]
180):
181 anisotropy: tuple[float, float, float]
182 """Anisotropy factors for 3D star-convex polyhedra, i.e. the physical pixel size along each spatial axis."""
184 overlap_label: int | None = None
185 """Optional label to apply to any area of overlapping predicted objects."""
187 def get_descr(self):
188 return v0_5.StardistPostprocessingDescr(
189 kwargs=v0_5.StardistPostprocessingKwargs3D(
190 grid=self.grid,
191 prob_threshold=self.prob_threshold,
192 nms_threshold=self.nms_threshold,
193 b=self.b,
194 n_rays=self.n_rays,
195 anisotropy=self.anisotropy,
196 overlap_label=self.overlap_label,
197 )
198 )
200 def _impl(
201 self,
202 prob: NDArray[Any],
203 dist: NDArray[Any],
204 spatial_shape: tuple[int, int, int],
205 ) -> NDArray[np.int32]:
206 from stardist import ( # pyright: ignore[reportMissingTypeStubs]
207 Rays_GoldenSpiral,
208 non_maximum_suppression_3d, # pyright: ignore[reportUnknownVariableType]
209 polyhedron_to_label, # pyright: ignore[reportUnknownVariableType]
210 )
211 from stardist.matching import ( # pyright: ignore[reportMissingTypeStubs]
212 relabel_sequential, # pyright: ignore[reportUnknownVariableType]
213 )
215 rays = Rays_GoldenSpiral(self.n_rays, anisotropy=self.anisotropy)
217 points, probi, disti = non_maximum_suppression_3d( # pyright: ignore[reportUnknownVariableType]
218 dist,
219 prob,
220 rays,
221 grid=self.grid,
222 prob_thresh=self.prob_threshold,
223 nms_thresh=self.nms_threshold,
224 b=self.b, # pyright: ignore[reportArgumentType]
225 )
227 labels = polyhedron_to_label( # pyright: ignore[reportUnknownVariableType]
228 disti,
229 points,
230 rays=rays,
231 prob=probi,
232 shape=spatial_shape,
233 overlap_label=self.overlap_label,
234 )
236 labels, _, _ = relabel_sequential(labels)
237 assert isinstance(labels, np.ndarray) and labels.dtype == np.int32
238 return labels
240 @classmethod
241 def from_proc_descr(
242 cls, descr: v0_5.StardistPostprocessingDescr, member_id: MemberId
243 ) -> Self:
244 if not isinstance(descr.kwargs, v0_5.StardistPostprocessingKwargs3D):
245 raise TypeError(
246 f"expected v0_5.StardistPostprocessingKwargs3D for 3D stardist post-processing, but got {type(descr.kwargs)}"
247 )
249 kwargs = descr.kwargs
250 return cls(
251 prob_dist_input_id=member_id,
252 instance_labels_output_id=member_id,
253 grid=kwargs.grid,
254 prob_threshold=kwargs.prob_threshold,
255 nms_threshold=kwargs.nms_threshold,
256 n_rays=kwargs.n_rays,
257 anisotropy=kwargs.anisotropy,
258 b=kwargs.b,
259 overlap_label=kwargs.overlap_label,
260 )