closed-loop-microscopy 1.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. closed_loop_microscopy-1.1.0.dist-info/METADATA +118 -0
  2. closed_loop_microscopy-1.1.0.dist-info/RECORD +74 -0
  3. closed_loop_microscopy-1.1.0.dist-info/WHEEL +4 -0
  4. closed_loop_microscopy-1.1.0.dist-info/entry_points.txt +5 -0
  5. closed_loop_microscopy-1.1.0.dist-info/licenses/LICENSE.md +7 -0
  6. pyclm/__init__.py +26 -0
  7. pyclm/__main__.py +8 -0
  8. pyclm/check.py +931 -0
  9. pyclm/cli.py +256 -0
  10. pyclm/commands.py +183 -0
  11. pyclm/controller.py +447 -0
  12. pyclm/convert_hdf5s.py +273 -0
  13. pyclm/core/__init__.py +20 -0
  14. pyclm/core/base_process.py +166 -0
  15. pyclm/core/core_interface.py +47 -0
  16. pyclm/core/datatypes.py +142 -0
  17. pyclm/core/events.py +194 -0
  18. pyclm/core/experiments.py +403 -0
  19. pyclm/core/grid.py +272 -0
  20. pyclm/core/kinds.py +42 -0
  21. pyclm/core/manager.py +861 -0
  22. pyclm/core/measure.py +266 -0
  23. pyclm/core/messages.py +124 -0
  24. pyclm/core/microscope.py +504 -0
  25. pyclm/core/pattern_process.py +282 -0
  26. pyclm/core/patterns/__init__.py +60 -0
  27. pyclm/core/patterns/bar_patterns.py +259 -0
  28. pyclm/core/patterns/cell_intensity_patterns.py +181 -0
  29. pyclm/core/patterns/composite.py +220 -0
  30. pyclm/core/patterns/embryo_patterns.py +90 -0
  31. pyclm/core/patterns/fbc_cell_movement.py +295 -0
  32. pyclm/core/patterns/pattern.py +610 -0
  33. pyclm/core/patterns/pulse_patterns.py +261 -0
  34. pyclm/core/patterns/static_patterns.py +58 -0
  35. pyclm/core/patterns/wave_patterns.py +95 -0
  36. pyclm/core/patterns/zoo.py +231 -0
  37. pyclm/core/plan.py +623 -0
  38. pyclm/core/position_mover.py +185 -0
  39. pyclm/core/queues.py +41 -0
  40. pyclm/core/real_core.py +158 -0
  41. pyclm/core/router.py +422 -0
  42. pyclm/core/segmentation/__init__.py +2 -0
  43. pyclm/core/segmentation/cellpose_segmentation.py +113 -0
  44. pyclm/core/segmentation/segmentation.py +48 -0
  45. pyclm/core/segmentation_process.py +179 -0
  46. pyclm/core/settings.py +120 -0
  47. pyclm/core/storage/__init__.py +37 -0
  48. pyclm/core/storage/base.py +234 -0
  49. pyclm/core/storage/events.py +130 -0
  50. pyclm/core/storage/hdf5_v1.py +337 -0
  51. pyclm/core/storage/ome_zarr.py +861 -0
  52. pyclm/core/tracking/__init__.py +28 -0
  53. pyclm/core/tracking/centroid.py +75 -0
  54. pyclm/core/tracking/tracking.py +124 -0
  55. pyclm/core/tracking_process.py +106 -0
  56. pyclm/core/virtual_microscope/__init__.py +0 -0
  57. pyclm/core/virtual_microscope/simulated_core.py +262 -0
  58. pyclm/core/virtual_microscope/simulated_source.py +206 -0
  59. pyclm/core/writer_process.py +130 -0
  60. pyclm/demo.py +225 -0
  61. pyclm/directories.py +563 -0
  62. pyclm/gui/__init__.py +28 -0
  63. pyclm/gui/debug_viewer.py +41 -0
  64. pyclm/gui/gui_controller.py +551 -0
  65. pyclm/gui/widgets.py +292 -0
  66. pyclm/io/__init__.py +29 -0
  67. pyclm/io/export.py +202 -0
  68. pyclm/io/readers.py +589 -0
  69. pyclm/methods.py +249 -0
  70. pyclm/mmconfig.py +143 -0
  71. pyclm/preview.py +414 -0
  72. pyclm/run_pyclm.py +298 -0
  73. pyclm/schema.py +806 -0
  74. pyclm/templates.py +165 -0
@@ -0,0 +1,295 @@
1
+ from typing import ClassVar, Literal
2
+
3
+ import numpy as np
4
+ import tifffile
5
+ from scipy.ndimage import distance_transform_edt
6
+ from scipy.spatial import KDTree
7
+ from scipy.stats import gaussian_kde
8
+ from skimage.measure import label, regionprops, regionprops_table
9
+
10
+ from .pattern import AcquiredImageRequest, DataDock, PatternMethod
11
+ from .zoo import ZooMeta
12
+
13
+
14
+ class PerCellPatternMethod(PatternMethod):
15
+ """
16
+ Base class for the per-cell pattern methods. This class provides the basic structure for generating patterns
17
+ based on the properties of segmented cells. Subclasses should implement the `process_prop` method to define
18
+ how each cell's properties are used to generate the pattern, and include any additional parameters needed for the
19
+ specific pattern in their `__init__` method.
20
+
21
+ With ``tracks = true`` in the TOML the per-cell loop runs on the tracked
22
+ labels instead of the segmentation, so ``prop.label`` is a track id that
23
+ persists across timepoints (a ``[tracking]`` table is then required).
24
+ """
25
+
26
+ name = "per_cell_base"
27
+
28
+ def __init__(
29
+ self,
30
+ channel=None,
31
+ voronoi=False,
32
+ gradient=False,
33
+ direction=1,
34
+ tracks=False,
35
+ **kwargs,
36
+ ):
37
+ super().__init__(**kwargs)
38
+
39
+ if channel is None:
40
+ raise AttributeError(
41
+ "PerCellPatternModels must be provided a "
42
+ 'segmentation channel in the .toml: e.g. channel = "638"'
43
+ )
44
+
45
+ self.gradient = gradient
46
+ self.voronoi = voronoi
47
+ self.direction = direction
48
+
49
+ self.channel = channel
50
+ self.use_tracks = bool(tracks)
51
+
52
+ # request the segmentation (or the tracked labels) of the provided channel,
53
+ # to be used by self.generate. This ensures the data is available when
54
+ # generate is called.
55
+ self.add_requirement(
56
+ channel_name=channel, seg=not self.use_tracks, tracks=self.use_tracks
57
+ )
58
+
59
+ def cell_labels(self, context) -> np.ndarray:
60
+ """The label image the per-cell loop runs on: tracked labels with ``tracks = true``, else the segmentation."""
61
+ if self.use_tracks:
62
+ return context.tracks(self.channel).labels
63
+ return context.segmentation(self.channel)
64
+
65
+ def prop_vector(self, prop, vec):
66
+ """
67
+ :param prop: regionprops prop
68
+ :param vec: (y, x) vector
69
+ """
70
+ bin_img = prop.image
71
+ y = np.arange(bin_img.shape[0])
72
+ x = np.arange(bin_img.shape[1])
73
+ yy, xx = np.meshgrid(y, x, indexing="ij")
74
+
75
+ y_center, x_center = prop.centroid_local
76
+
77
+ dot_prod = (yy - y_center) * vec[0] + (xx - x_center) * vec[1]
78
+
79
+ if self.gradient:
80
+ framed = dot_prod * bin_img * (dot_prod > 0)
81
+ out = framed / np.max(framed)
82
+
83
+ else:
84
+ out = (dot_prod > 0) * bin_img
85
+
86
+ return out
87
+
88
+ def process_prop(self, prop) -> np.ndarray:
89
+ return prop.image
90
+
91
+ def voronoi_rebuild(self, img):
92
+ props_table = regionprops_table(img, properties=["centroid"])
93
+
94
+ pts = np.stack([props_table["centroid-0"], props_table["centroid-1"]], axis=-1)
95
+
96
+ kdtree = KDTree(pts)
97
+
98
+ h, w = self.pattern_shape
99
+ y = np.arange(h)
100
+ x = np.arange(w)
101
+ yy, xx = np.meshgrid(y, x, indexing="ij")
102
+
103
+ query_pts = np.stack([yy.flatten(), xx.flatten()], axis=-1)
104
+
105
+ _, nn = kdtree.query(query_pts, 1)
106
+
107
+ out = np.reshape(nn, (int(h), int(w)))
108
+
109
+ return out
110
+
111
+ def generate(self, context) -> np.ndarray:
112
+ seg = self.cell_labels(context)
113
+
114
+ if self.voronoi:
115
+ px_dis = distance_transform_edt(seg == 0)
116
+ seg = self.voronoi_rebuild(seg)
117
+
118
+ seg = seg * (px_dis < 50)
119
+
120
+ h, w = self.pattern_shape
121
+
122
+ new_img = np.zeros((int(h), int(w)), dtype=np.float16)
123
+
124
+ for prop in regionprops(seg):
125
+ cell_stim = self.process_prop(prop)
126
+
127
+ new_img[prop.bbox[0] : prop.bbox[2], prop.bbox[1] : prop.bbox[3]] += (
128
+ cell_stim
129
+ )
130
+
131
+ new_img_clamped = np.clip(new_img, 0, 1).astype(np.float16)
132
+
133
+ return new_img_clamped
134
+
135
+
136
+ class RotateCcwModel(PerCellPatternMethod):
137
+ name = "rotate_ccw"
138
+ zoo_meta = ZooMeta(
139
+ source="mdck",
140
+ kwargs={"channel": "seg_channel"},
141
+ title="Rotate CCW",
142
+ description="Illuminates the half of each cell pointing counter-clockwise around the field center.",
143
+ )
144
+
145
+ def __init__(self, channel=None, **kwargs):
146
+ super().__init__(channel=channel, **kwargs)
147
+
148
+ def process_prop(self, prop) -> np.ndarray:
149
+ center_y, center_x = self.pattern_shape[0] / 2, self.pattern_shape[1] / 2
150
+ prop_centroid = prop.centroid
151
+
152
+ vec = -(prop_centroid[1] - center_x), prop_centroid[0] - center_y
153
+
154
+ return self.prop_vector(prop, vec)
155
+
156
+
157
+ class MoveOutModel(PerCellPatternMethod):
158
+ name = "move_out"
159
+ zoo_meta = ZooMeta(
160
+ source="mdck",
161
+ kwargs={"channel": "seg_channel"},
162
+ title="Move Out",
163
+ description="Illuminates the outward-facing half of each cell.",
164
+ )
165
+
166
+ def __init__(self, channel=None, **kwargs):
167
+ super().__init__(channel=channel, **kwargs)
168
+
169
+ def process_prop(self, prop) -> np.ndarray:
170
+ center_y, center_x = self.pattern_shape[0] / 2, self.pattern_shape[1] / 2
171
+ prop_centroid = prop.centroid
172
+
173
+ vec = prop_centroid[0] - center_y, (prop_centroid[1] - center_x)
174
+
175
+ return self.prop_vector(prop, vec)
176
+
177
+
178
+ class MoveInModel(PerCellPatternMethod):
179
+ name = "move_in"
180
+ zoo_meta = ZooMeta(
181
+ source="mdck",
182
+ kwargs={"channel": "seg_channel"},
183
+ title="Move In",
184
+ description="Illuminates the inward-facing half of each cell.",
185
+ )
186
+
187
+ def __init__(self, channel=None, **kwargs):
188
+ super().__init__(channel=channel, **kwargs)
189
+
190
+ def process_prop(self, prop) -> np.ndarray:
191
+ center_y, center_x = self.pattern_shape[0] / 2, self.pattern_shape[1] / 2
192
+ prop_centroid = prop.centroid
193
+
194
+ vec = -(prop_centroid[0] - center_y), -(prop_centroid[1] - center_x)
195
+
196
+ return self.prop_vector(prop, vec)
197
+
198
+
199
+ class PeriodDirectedPattern(PerCellPatternMethod):
200
+ name = "direction"
201
+
202
+ direction_vec: ClassVar[dict[str, tuple[int, int]]] = {
203
+ "u": (1, 0),
204
+ "d": (-1, 0),
205
+ "r": (0, 1),
206
+ "l": (0, -1),
207
+ }
208
+
209
+ def __init__(
210
+ self,
211
+ sequence: list[Literal["u", "d", "l", "r", "s"]] | str | None = None,
212
+ period_h=2,
213
+ channel="545",
214
+ voronoi=True,
215
+ gradient=True,
216
+ **kwargs,
217
+ ):
218
+ super().__init__(channel=channel, voronoi=voronoi, gradient=gradient, **kwargs)
219
+
220
+ assert sequence, "sequence kwarg required but not provided"
221
+ assert all([val in ["u", "d", "l", "r", "s"] for val in sequence]), (
222
+ f"sequence {sequence} contains unrecognized characters"
223
+ )
224
+
225
+ self.sequence = "".join([val for val in sequence])
226
+ self.period_h = period_h
227
+
228
+ self.current_direction = self.sequence[0]
229
+
230
+ def process_prop(self, prop) -> np.ndarray:
231
+ if self.current_direction == "s":
232
+ return 0 * prop.image
233
+
234
+ vec = self.direction_vec.get(self.current_direction, None)
235
+
236
+ assert vec is not None, (
237
+ f"direction {self.current_direction} produced an invalid vector"
238
+ )
239
+
240
+ return self.prop_vector(prop, vec)
241
+
242
+ def generate(self, context) -> np.ndarray:
243
+ t = context.time
244
+ t_h = t / 3600
245
+
246
+ step = t_h // self.period_h
247
+ step = int(step % len(self.sequence))
248
+
249
+ self.current_direction = self.sequence[step]
250
+
251
+ return super().generate(context)
252
+
253
+
254
+ class MoveDownModel(PerCellPatternMethod):
255
+ name = "move_down"
256
+
257
+ def __init__(self, channel=None, **kwargs):
258
+ super().__init__(channel=channel, **kwargs)
259
+
260
+ def process_prop(self, prop) -> np.ndarray:
261
+ center_y, center_x = self.pattern_shape[0] / 2, self.pattern_shape[1] / 2
262
+ prop_centroid = prop.centroid
263
+
264
+ vec = (1, 0)
265
+
266
+ return self.prop_vector(prop, vec)
267
+
268
+
269
+ class BounceModel(PerCellPatternMethod):
270
+ name = "fb_bounce"
271
+
272
+ def __init__(self, channel=None, t_loop=60, **kwargs):
273
+ self.t_loop_s = t_loop * 60
274
+ self.down = True
275
+
276
+ super().__init__(channel=channel, **kwargs)
277
+
278
+ def process_prop(self, prop) -> np.ndarray:
279
+ center_y, center_x = self.pattern_shape[0] / 2, self.pattern_shape[1] / 2
280
+ prop_centroid = prop.centroid
281
+
282
+ vec = (1, 0) if self.down else (-1, 0)
283
+
284
+ return self.prop_vector(prop, vec)
285
+
286
+ def generate(self, context) -> np.ndarray:
287
+ t = context.time
288
+ t = t % self.t_loop_s
289
+
290
+ halfway = self.t_loop_s / 2
291
+
292
+ if t > halfway:
293
+ self.down = False
294
+
295
+ return super().generate(context)