Coverage for src/bioimageio/core/_ops_cellpose.py: 81%

62 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 09:46 +0000

1from dataclasses import dataclass 

2from typing import Any, Literal, cast 

3 

4import numpy as np 

5from numpy.typing import NDArray 

6 

7from bioimageio.spec.model.v0_5 import ( 

8 CellposeFlowDynamicsDescr, 

9 CellposeFlowDynamicsKwargs, 

10) 

11 

12from ._op_base import SamplewiseOperator 

13from .axis import AxisId, PerAxis 

14from .common import MemberId 

15from .sample import Sample 

16from .tensor import Tensor 

17 

18DEFAULT_LABELS_ID = MemberId("labels") 

19 

20 

21@dataclass(frozen=True) 

22class CellposeFlowDynamics(SamplewiseOperator): 

23 """Cellpose flow-dynamics postprocessing operator. 

24 

25 Adds `output` member to the sample, containing instance labels (int32, 0 = background) 

26 decoded from the flow fields and cell probability output of a Cellpose model. 

27 

28 """ 

29 

30 cellprob_threshold: float = 0.0 

31 flow_threshold: float = 0.4 

32 do_3D: bool = False 

33 min_size: int = 15 

34 """Minimum size of objects to keep, in pixels. Default is 15, which is the default in Cellpose. Set to 0 to disable filtering by size.""" 

35 labels_id: MemberId = DEFAULT_LABELS_ID 

36 output_dtype: Literal["uint16", "uint32"] = "uint16" 

37 

38 @classmethod 

39 def from_proc_descr( 

40 cls, proc_descr: CellposeFlowDynamicsDescr, member_id: MemberId 

41 ) -> "CellposeFlowDynamics": 

42 kwargs = proc_descr.kwargs 

43 return cls( 

44 labels_id=member_id, 

45 cellprob_threshold=kwargs.cellprob_threshold, 

46 flow_threshold=kwargs.flow_threshold, 

47 do_3D=kwargs.do_3D, 

48 output_dtype=kwargs.output_dtype, 

49 min_size=kwargs.min_size, 

50 ) 

51 

52 @property 

53 def output_member_ids(self) -> tuple[MemberId, ...]: 

54 return (self.labels_id,) 

55 

56 def get_descr(self): 

57 return CellposeFlowDynamicsDescr( 

58 kwargs=CellposeFlowDynamicsKwargs( 

59 cellprob_threshold=self.cellprob_threshold, 

60 flow_threshold=self.flow_threshold, 

61 do_3D=self.do_3D, 

62 min_size=self.min_size, 

63 output_dtype=self.output_dtype, 

64 ) 

65 ) 

66 

67 def get_output_shape(self, input_shape: PerAxis[int]) -> PerAxis[int]: 

68 output_shape = dict(input_shape) 

69 output_shape[AxisId("channel")] = 1 

70 return output_shape 

71 

72 def __call__(self, sample: Sample) -> None: 

73 input_tensor = sample.members[self.labels_id] 

74 output_tensor = self._apply(input_tensor) 

75 sample.members[self.labels_id] = output_tensor 

76 sample.applied_ops += (self,) 

77 

78 def _apply(self, x: Tensor) -> Tensor: 

79 if x.dims[0] != AxisId("batch"): 79 ↛ 80line 79 didn't jump to line 80 because the condition on line 79 was never true

80 raise ValueError( 

81 "Expected first axis to be 'batch' for cellpose flow dynamics." 

82 ) 

83 

84 if x.dims[1] != AxisId("channel"): 84 ↛ 85line 84 didn't jump to line 85 because the condition on line 84 was never true

85 raise ValueError( 

86 "Expected axis 1 to be 'channel' with 3 channels for cellpose flow dynamics." 

87 ) 

88 if x.shape[1] != 3: 88 ↛ 89line 88 didn't jump to line 89 because the condition on line 88 was never true

89 raise ValueError( 

90 f"Expected 3 stacked tensors along 'channel' axis at {x.shape}[1] for flow_y, flow_x, cellprob for cellpose flow dynamics." 

91 ) 

92 

93 masks = [self._apply_impl(xx) for xx in x] 

94 output = Tensor.from_numpy(np.stack(masks, axis=0), dims=x.dims) 

95 output.set_physical_scale(x.get_physical_scale()) 

96 output.set_physical_scale_unit(x.get_physical_scale_unit()) 

97 return output 

98 

99 def _apply_impl(self, x: Tensor) -> NDArray[Any]: 

100 """apply on a tensor without batch dimension""" 

101 *flows, cellprob = x.to_numpy() 

102 try: 

103 from cellpose.dynamics import ( # pyright: ignore[reportMissingTypeStubs] 

104 compute_masks, # pyright: ignore[reportUnknownVariableType] 

105 ) 

106 from cellpose.utils import ( # pyright: ignore[reportMissingTypeStubs] 

107 fill_holes_and_remove_small_masks, # pyright: ignore[reportUnknownVariableType] 

108 ) 

109 except ImportError as e: 

110 raise ImportError( 

111 "cellpose is required for cellpose_flow_dynamics. Install with: pip install cellpose" 

112 ) from e 

113 

114 flows = np.stack(flows, axis=0) 

115 mask = cast( 

116 NDArray[Any], 

117 compute_masks( 

118 flows, 

119 cellprob, 

120 cellprob_threshold=self.cellprob_threshold, 

121 flow_threshold=self.flow_threshold, 

122 do_3D=self.do_3D, 

123 ), 

124 ) 

125 mask = fill_holes_and_remove_small_masks(mask, min_size=self.min_size) 

126 

127 # add singleton channel axis for output to keep dims consistent with postprocessing input 

128 mask = mask[None] 

129 return mask.astype(np.dtype(self.output_dtype))