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.
- closed_loop_microscopy-1.1.0.dist-info/METADATA +118 -0
- closed_loop_microscopy-1.1.0.dist-info/RECORD +74 -0
- closed_loop_microscopy-1.1.0.dist-info/WHEEL +4 -0
- closed_loop_microscopy-1.1.0.dist-info/entry_points.txt +5 -0
- closed_loop_microscopy-1.1.0.dist-info/licenses/LICENSE.md +7 -0
- pyclm/__init__.py +26 -0
- pyclm/__main__.py +8 -0
- pyclm/check.py +931 -0
- pyclm/cli.py +256 -0
- pyclm/commands.py +183 -0
- pyclm/controller.py +447 -0
- pyclm/convert_hdf5s.py +273 -0
- pyclm/core/__init__.py +20 -0
- pyclm/core/base_process.py +166 -0
- pyclm/core/core_interface.py +47 -0
- pyclm/core/datatypes.py +142 -0
- pyclm/core/events.py +194 -0
- pyclm/core/experiments.py +403 -0
- pyclm/core/grid.py +272 -0
- pyclm/core/kinds.py +42 -0
- pyclm/core/manager.py +861 -0
- pyclm/core/measure.py +266 -0
- pyclm/core/messages.py +124 -0
- pyclm/core/microscope.py +504 -0
- pyclm/core/pattern_process.py +282 -0
- pyclm/core/patterns/__init__.py +60 -0
- pyclm/core/patterns/bar_patterns.py +259 -0
- pyclm/core/patterns/cell_intensity_patterns.py +181 -0
- pyclm/core/patterns/composite.py +220 -0
- pyclm/core/patterns/embryo_patterns.py +90 -0
- pyclm/core/patterns/fbc_cell_movement.py +295 -0
- pyclm/core/patterns/pattern.py +610 -0
- pyclm/core/patterns/pulse_patterns.py +261 -0
- pyclm/core/patterns/static_patterns.py +58 -0
- pyclm/core/patterns/wave_patterns.py +95 -0
- pyclm/core/patterns/zoo.py +231 -0
- pyclm/core/plan.py +623 -0
- pyclm/core/position_mover.py +185 -0
- pyclm/core/queues.py +41 -0
- pyclm/core/real_core.py +158 -0
- pyclm/core/router.py +422 -0
- pyclm/core/segmentation/__init__.py +2 -0
- pyclm/core/segmentation/cellpose_segmentation.py +113 -0
- pyclm/core/segmentation/segmentation.py +48 -0
- pyclm/core/segmentation_process.py +179 -0
- pyclm/core/settings.py +120 -0
- pyclm/core/storage/__init__.py +37 -0
- pyclm/core/storage/base.py +234 -0
- pyclm/core/storage/events.py +130 -0
- pyclm/core/storage/hdf5_v1.py +337 -0
- pyclm/core/storage/ome_zarr.py +861 -0
- pyclm/core/tracking/__init__.py +28 -0
- pyclm/core/tracking/centroid.py +75 -0
- pyclm/core/tracking/tracking.py +124 -0
- pyclm/core/tracking_process.py +106 -0
- pyclm/core/virtual_microscope/__init__.py +0 -0
- pyclm/core/virtual_microscope/simulated_core.py +262 -0
- pyclm/core/virtual_microscope/simulated_source.py +206 -0
- pyclm/core/writer_process.py +130 -0
- pyclm/demo.py +225 -0
- pyclm/directories.py +563 -0
- pyclm/gui/__init__.py +28 -0
- pyclm/gui/debug_viewer.py +41 -0
- pyclm/gui/gui_controller.py +551 -0
- pyclm/gui/widgets.py +292 -0
- pyclm/io/__init__.py +29 -0
- pyclm/io/export.py +202 -0
- pyclm/io/readers.py +589 -0
- pyclm/methods.py +249 -0
- pyclm/mmconfig.py +143 -0
- pyclm/preview.py +414 -0
- pyclm/run_pyclm.py +298 -0
- pyclm/schema.py +806 -0
- pyclm/templates.py +165 -0
pyclm/core/grid.py
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Grid acquisition: a position made of tiles, laid out by MicroManager's tile
|
|
3
|
+
creator, imaged as one stitched frame.
|
|
4
|
+
|
|
5
|
+
MicroManager's Create Grid writes one position per tile, labelled
|
|
6
|
+
``<prefix>-<n>-<col>_<row>`` with ``GridRow`` / ``GridCol`` set, spaced by
|
|
7
|
+
the camera field (through the pixel-size affine, so tiles placed by row and
|
|
8
|
+
column abut). :func:`group_tiles` folds those entries into one
|
|
9
|
+
:class:`~pyclm.core.experiments.MicroscopePosition` per grid that carries
|
|
10
|
+
the tiles; :func:`geometry_for` turns it, once the camera ROI and pixel size
|
|
11
|
+
are known, into a :class:`GridGeometry`; :func:`stitch` and :func:`cut`
|
|
12
|
+
move frames and patterns between the tiles and the stitched frame.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import re
|
|
18
|
+
from dataclasses import dataclass
|
|
19
|
+
from statistics import median
|
|
20
|
+
|
|
21
|
+
import numpy as np
|
|
22
|
+
|
|
23
|
+
from .experiments import MicroscopePosition
|
|
24
|
+
|
|
25
|
+
# MicroManager's TileCreator: labelPrefix + "-" + "%03d" % col + "_" + "%03d" % row
|
|
26
|
+
TILE_LABEL = re.compile(r"^(?P<prefix>.+)-(?P<col>\d{3})_(?P<row>\d{3})$")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True)
|
|
30
|
+
class GridGeometry:
|
|
31
|
+
"""
|
|
32
|
+
How a grid's tiles sit in the stitched frame, in unbinned camera pixels.
|
|
33
|
+
|
|
34
|
+
``pitch_x_px`` / ``pitch_y_px`` are the distances between column / row
|
|
35
|
+
neighbours; ``tile_w`` / ``tile_h`` the camera ROI; ``tiles`` the
|
|
36
|
+
``(row, col)`` of each tile in acquisition order.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
rows: int
|
|
40
|
+
columns: int
|
|
41
|
+
pitch_x_px: float
|
|
42
|
+
pitch_y_px: float
|
|
43
|
+
tile_h: int
|
|
44
|
+
tile_w: int
|
|
45
|
+
tiles: tuple[tuple[int, int], ...]
|
|
46
|
+
|
|
47
|
+
def pitch(self, binning: int = 1) -> tuple[int, int]:
|
|
48
|
+
"""(row pitch, column pitch) in pixels at ``binning``."""
|
|
49
|
+
return (
|
|
50
|
+
round(self.pitch_y_px / binning),
|
|
51
|
+
round(self.pitch_x_px / binning),
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
def tile_shape(self, binning: int = 1) -> tuple[int, int]:
|
|
55
|
+
return (self.tile_h // binning, self.tile_w // binning)
|
|
56
|
+
|
|
57
|
+
def shape(self, binning: int = 1) -> tuple[int, int]:
|
|
58
|
+
"""The stitched frame's (height, width) at ``binning``."""
|
|
59
|
+
h, w = self.tile_shape(binning)
|
|
60
|
+
py, px = self.pitch(binning)
|
|
61
|
+
return ((self.rows - 1) * py + h, (self.columns - 1) * px + w)
|
|
62
|
+
|
|
63
|
+
def overlap(self, binning: int = 1) -> tuple[int, int]:
|
|
64
|
+
"""(rows, columns) overlap in pixels between neighbours; negative is a gap."""
|
|
65
|
+
h, w = self.tile_shape(binning)
|
|
66
|
+
py, px = self.pitch(binning)
|
|
67
|
+
return (h - py, w - px)
|
|
68
|
+
|
|
69
|
+
def tile_slices(self, row: int, col: int, binning: int = 1) -> tuple[slice, slice]:
|
|
70
|
+
h, w = self.tile_shape(binning)
|
|
71
|
+
py, px = self.pitch(binning)
|
|
72
|
+
y0, x0 = row * py, col * px
|
|
73
|
+
return (slice(y0, y0 + h), slice(x0, x0 + w))
|
|
74
|
+
|
|
75
|
+
def as_dict(self) -> dict:
|
|
76
|
+
return {
|
|
77
|
+
"rows": self.rows,
|
|
78
|
+
"columns": self.columns,
|
|
79
|
+
"pitch_px": [self.pitch_x_px, self.pitch_y_px],
|
|
80
|
+
"tile_shape": [self.tile_h, self.tile_w],
|
|
81
|
+
"tiles": [list(rc) for rc in self.tiles],
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
# ------------------------------------------------------------- from a position list
|
|
86
|
+
def group_tiles(
|
|
87
|
+
positions: list[MicroscopePosition],
|
|
88
|
+
raw_labels: list[str],
|
|
89
|
+
grid_rc: list[tuple[int, int] | None],
|
|
90
|
+
) -> list[MicroscopePosition]:
|
|
91
|
+
"""
|
|
92
|
+
Fold the tiles of MicroManager grids into one position per grid.
|
|
93
|
+
|
|
94
|
+
``raw_labels`` are the labels as written by MicroManager (before ``-``
|
|
95
|
+
became ``.``); ``grid_rc`` the ``(GridRow, GridCol)`` of each entry, or
|
|
96
|
+
None. A grid is every entry whose label matches :data:`TILE_LABEL` with
|
|
97
|
+
the same prefix; the grid position is labelled by the prefix (``-`` →
|
|
98
|
+
``.``), sits at the centre of its tiles, and carries them in
|
|
99
|
+
``tiles`` (each with ``grid_rc``) with the pitch in µm in ``grid``.
|
|
100
|
+
Entries that are not tiles pass through unchanged, in order.
|
|
101
|
+
"""
|
|
102
|
+
out: list[MicroscopePosition] = []
|
|
103
|
+
grids: dict[str, list[tuple[MicroscopePosition, int, int]]] = {}
|
|
104
|
+
order: list[str] = []
|
|
105
|
+
for pos, raw, rc in zip(positions, raw_labels, grid_rc, strict=True):
|
|
106
|
+
m = TILE_LABEL.match(raw or "")
|
|
107
|
+
if m is None:
|
|
108
|
+
out.append(pos)
|
|
109
|
+
continue
|
|
110
|
+
col, row = int(m.group("col")), int(m.group("row"))
|
|
111
|
+
if rc is not None and rc != (0, 0) and rc != (row, col):
|
|
112
|
+
raise ValueError(
|
|
113
|
+
f"position '{raw}': GridRow/GridCol {rc} disagree with the label "
|
|
114
|
+
f"(row {row}, column {col})"
|
|
115
|
+
)
|
|
116
|
+
prefix = m.group("prefix")
|
|
117
|
+
if prefix not in grids:
|
|
118
|
+
grids[prefix] = []
|
|
119
|
+
order.append(prefix)
|
|
120
|
+
out.append(None) # type: ignore[arg-type] (placeholder keeps the order)
|
|
121
|
+
grids[prefix].append((pos, row, col))
|
|
122
|
+
|
|
123
|
+
for prefix in order:
|
|
124
|
+
entries = grids[prefix]
|
|
125
|
+
grid = _fold(prefix, entries)
|
|
126
|
+
out[out.index(None)] = grid
|
|
127
|
+
return out
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _fold(prefix: str, entries: list[tuple[MicroscopePosition, int, int]]):
|
|
131
|
+
label = prefix.replace("-", ".")
|
|
132
|
+
rcs = [(r, c) for _, r, c in entries]
|
|
133
|
+
if len(set(rcs)) != len(rcs):
|
|
134
|
+
raise ValueError(f"grid '{label}': duplicate tiles {rcs}")
|
|
135
|
+
rows = max(r for r, _ in rcs) + 1
|
|
136
|
+
cols = max(c for _, c in rcs) + 1
|
|
137
|
+
if rows * cols != len(rcs) or min(r for r, _ in rcs) or min(c for _, c in rcs):
|
|
138
|
+
raise ValueError(
|
|
139
|
+
f"grid '{label}': {len(rcs)} tiles do not form a complete "
|
|
140
|
+
f"{rows} x {cols} rectangle (tiles must not be deleted from a grid)"
|
|
141
|
+
)
|
|
142
|
+
by_rc = {(r, c): p for p, r, c in entries}
|
|
143
|
+
|
|
144
|
+
def pitch(pairs):
|
|
145
|
+
dists = [float(np.hypot(b.x - a.x, b.y - a.y)) for a, b in pairs]
|
|
146
|
+
if not dists:
|
|
147
|
+
return 0.0
|
|
148
|
+
p = median(dists)
|
|
149
|
+
if p > 0 and max(abs(d - p) for d in dists) > 0.02 * p:
|
|
150
|
+
raise ValueError(
|
|
151
|
+
f"grid '{label}': tile spacing is not uniform ({min(dists):.2f} to "
|
|
152
|
+
f"{max(dists):.2f} um); was the grid edited by hand?"
|
|
153
|
+
)
|
|
154
|
+
return p
|
|
155
|
+
|
|
156
|
+
col_pitch = pitch(
|
|
157
|
+
[
|
|
158
|
+
(by_rc[(r, c)], by_rc[(r, c + 1)])
|
|
159
|
+
for r in range(rows)
|
|
160
|
+
for c in range(cols - 1)
|
|
161
|
+
]
|
|
162
|
+
)
|
|
163
|
+
row_pitch = pitch(
|
|
164
|
+
[
|
|
165
|
+
(by_rc[(r, c)], by_rc[(r + 1, c)])
|
|
166
|
+
for r in range(rows - 1)
|
|
167
|
+
for c in range(cols)
|
|
168
|
+
]
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
tiles = []
|
|
172
|
+
for pos, r, c in entries:
|
|
173
|
+
tile = MicroscopePosition(
|
|
174
|
+
pos.x,
|
|
175
|
+
pos.y,
|
|
176
|
+
pos.z,
|
|
177
|
+
label=f"{label}.{c:03d}_{r:03d}",
|
|
178
|
+
extras=dict(pos.extras),
|
|
179
|
+
)
|
|
180
|
+
tile.grid_rc = (r, c)
|
|
181
|
+
tiles.append(tile)
|
|
182
|
+
|
|
183
|
+
n = len(tiles)
|
|
184
|
+
extras: dict = {}
|
|
185
|
+
for key in tiles[0].extras:
|
|
186
|
+
values = [t.extras.get(key) for t in tiles]
|
|
187
|
+
if all(isinstance(v, int | float) for v in values):
|
|
188
|
+
extras[key] = float(sum(values)) / n
|
|
189
|
+
centre = MicroscopePosition(
|
|
190
|
+
sum(t.x for t in tiles) / n,
|
|
191
|
+
sum(t.y for t in tiles) / n,
|
|
192
|
+
sum(t.z for t in tiles) / n,
|
|
193
|
+
label=label,
|
|
194
|
+
extras=extras,
|
|
195
|
+
)
|
|
196
|
+
centre.tiles = tiles
|
|
197
|
+
centre.grid = {"rows": rows, "columns": cols, "pitch_um": [col_pitch, row_pitch]}
|
|
198
|
+
return centre
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
# ------------------------------------------------------------- at run time
|
|
202
|
+
def geometry_for(
|
|
203
|
+
position: MicroscopePosition, roi, pixel_size_um: float
|
|
204
|
+
) -> GridGeometry | None:
|
|
205
|
+
"""
|
|
206
|
+
The :class:`GridGeometry` of a grid position given the camera ROI
|
|
207
|
+
``(x, y, width, height)`` (unbinned) and the unbinned pixel size; None
|
|
208
|
+
for a plain position.
|
|
209
|
+
"""
|
|
210
|
+
tiles = getattr(position, "tiles", None)
|
|
211
|
+
if not tiles:
|
|
212
|
+
return None
|
|
213
|
+
info = position.grid
|
|
214
|
+
col_um, row_um = info["pitch_um"]
|
|
215
|
+
px = float(pixel_size_um)
|
|
216
|
+
if px <= 0:
|
|
217
|
+
raise ValueError("pixel size must be positive to lay out a grid")
|
|
218
|
+
return GridGeometry(
|
|
219
|
+
rows=int(info["rows"]),
|
|
220
|
+
columns=int(info["columns"]),
|
|
221
|
+
pitch_x_px=col_um / px,
|
|
222
|
+
pitch_y_px=row_um / px,
|
|
223
|
+
tile_h=int(roi[3]),
|
|
224
|
+
tile_w=int(roi[2]),
|
|
225
|
+
tiles=tuple(tuple(t.grid_rc) for t in tiles),
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def stitch(frames, geometry: GridGeometry, binning: int = 1) -> np.ndarray:
|
|
230
|
+
"""
|
|
231
|
+
Place the tiles' frames (in ``geometry.tiles`` order) into one stitched
|
|
232
|
+
frame at ``binning``. Placement only: where tiles overlap the later one
|
|
233
|
+
wins; where the pitch exceeds the tile the gap stays zero. Frames smaller
|
|
234
|
+
than the camera ROI says (the virtual microscope's TIFs stand for a
|
|
235
|
+
larger camera) are laid out at their own scale.
|
|
236
|
+
"""
|
|
237
|
+
frames = [np.asarray(f) for f in frames]
|
|
238
|
+
if len(frames) != len(geometry.tiles):
|
|
239
|
+
raise ValueError(f"{len(frames)} frames for {len(geometry.tiles)} tiles")
|
|
240
|
+
h, w = geometry.tile_shape(binning)
|
|
241
|
+
fh, fw = frames[0].shape[:2]
|
|
242
|
+
scale = float(binning)
|
|
243
|
+
if (fh, fw) != (h, w) and fh > 0 and fw > 0 and abs(h / fh - w / fw) < 1e-6:
|
|
244
|
+
scale = binning * (h / fh)
|
|
245
|
+
h, w = fh, fw
|
|
246
|
+
py = round(geometry.pitch_y_px / scale)
|
|
247
|
+
px = round(geometry.pitch_x_px / scale)
|
|
248
|
+
shape = ((geometry.rows - 1) * py + h, (geometry.columns - 1) * px + w)
|
|
249
|
+
canvas = np.zeros((*shape, *frames[0].shape[2:]), dtype=frames[0].dtype)
|
|
250
|
+
for frame, (row, col) in zip(frames, geometry.tiles, strict=True):
|
|
251
|
+
y0, x0 = row * py, col * px
|
|
252
|
+
th, tw = min(h, frame.shape[0]), min(w, frame.shape[1])
|
|
253
|
+
canvas[y0 : y0 + th, x0 : x0 + tw] = frame[:th, :tw]
|
|
254
|
+
return canvas
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def cut(pattern, geometry: GridGeometry, binning: int = 1) -> list[np.ndarray]:
|
|
258
|
+
"""
|
|
259
|
+
The tiles' share of a stitched pattern at ``binning``, in
|
|
260
|
+
``geometry.tiles`` order, each the full tile extent (zero-padded where the
|
|
261
|
+
pattern is smaller than the stitched frame).
|
|
262
|
+
"""
|
|
263
|
+
pattern = np.asarray(pattern)
|
|
264
|
+
h, w = geometry.tile_shape(binning)
|
|
265
|
+
out = []
|
|
266
|
+
for row, col in geometry.tiles:
|
|
267
|
+
sy, sx = geometry.tile_slices(row, col, binning)
|
|
268
|
+
tile = np.zeros((h, w), dtype=pattern.dtype)
|
|
269
|
+
part = pattern[sy, sx]
|
|
270
|
+
tile[: part.shape[0], : part.shape[1]] = part
|
|
271
|
+
out.append(tile)
|
|
272
|
+
return out
|
pyclm/core/kinds.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The vocabulary of frame-derived data kinds.
|
|
3
|
+
|
|
4
|
+
A kind names what a piece of data *is*: ``"raw"`` (a camera frame),
|
|
5
|
+
``"seg"`` (a label image from the experiment's default ``[segmentation]``
|
|
6
|
+
table), ``"seg:<name>"`` (a label image from a named
|
|
7
|
+
``[segmentation.<name>]`` table) or ``"tracks"`` (a label image relabelled
|
|
8
|
+
with track ids plus a table). The router keys its table on
|
|
9
|
+
``(experiment, channel, kind)``; producers are registered by the *base*
|
|
10
|
+
kind (``"seg"`` for every segmentation), so one segmentation process serves
|
|
11
|
+
every named segmentation.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
DEFAULT_SEGMENTATION = "segmentation"
|
|
17
|
+
BASE_KINDS = ("raw", "seg", "tracks")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def base_kind(kind: str) -> str:
|
|
21
|
+
"""``"seg"`` for ``"seg:nuclei"``; other kinds unchanged."""
|
|
22
|
+
return kind.split(":", 1)[0]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def seg_kind(name: str | None = None) -> str:
|
|
26
|
+
"""Routing kind of a segmentation: ``"seg"`` for the default, ``"seg:<name>"`` otherwise."""
|
|
27
|
+
if not name or name == DEFAULT_SEGMENTATION:
|
|
28
|
+
return "seg"
|
|
29
|
+
return f"seg:{name}"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def seg_name(kind: str) -> str:
|
|
33
|
+
"""Segmentation name of a ``seg`` kind (``"segmentation"`` for the default)."""
|
|
34
|
+
if kind == "seg":
|
|
35
|
+
return DEFAULT_SEGMENTATION
|
|
36
|
+
if kind.startswith("seg:") and len(kind) > 4:
|
|
37
|
+
return kind[4:]
|
|
38
|
+
raise ValueError(f"{kind!r} is not a segmentation kind")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def is_seg_kind(kind: str) -> bool:
|
|
42
|
+
return base_kind(kind) == "seg"
|