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/run_pyclm.py
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import logging
|
|
3
|
+
import os
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
from collections.abc import Sequence
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
from .check import CheckFailed, check_directory, find_pyclm_config
|
|
12
|
+
from .controller import Controller
|
|
13
|
+
from .core import PatternMethod, SegmentationMethod
|
|
14
|
+
from .core.position_mover import PositionMover
|
|
15
|
+
from .core.tracking import TrackingMethod
|
|
16
|
+
from .directories import dry_schedule_from_directory, schedule_from_directory
|
|
17
|
+
from .schema import PyclmConfig
|
|
18
|
+
|
|
19
|
+
logger = logging.getLogger(__name__)
|
|
20
|
+
|
|
21
|
+
# marks the handlers installed by set_logging so a later call can replace them
|
|
22
|
+
_PYCLM_HANDLER_FLAG = "_pyclm_run_handler"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def remove_pyclm_log_handlers():
|
|
26
|
+
"""Detach and close the handlers installed by a previous ``set_logging`` call."""
|
|
27
|
+
root = logging.getLogger()
|
|
28
|
+
for handler in list(root.handlers):
|
|
29
|
+
if getattr(handler, _PYCLM_HANDLER_FLAG, False):
|
|
30
|
+
root.removeHandler(handler)
|
|
31
|
+
handler.close()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def set_logging(experiment_directory: Path):
|
|
35
|
+
"""
|
|
36
|
+
Route logging for one run: WARNING and above to the console, INFO and above
|
|
37
|
+
to ``<experiment_directory>/log.log``.
|
|
38
|
+
|
|
39
|
+
Handlers from a previous call are removed first, so successive runs in the
|
|
40
|
+
same interpreter each log to their own experiment directory.
|
|
41
|
+
"""
|
|
42
|
+
remove_pyclm_log_handlers()
|
|
43
|
+
|
|
44
|
+
console_handler = logging.StreamHandler()
|
|
45
|
+
file_handler = logging.FileHandler(Path(experiment_directory) / "log.log")
|
|
46
|
+
|
|
47
|
+
# Set levels for handlers
|
|
48
|
+
console_handler.setLevel(logging.WARNING)
|
|
49
|
+
file_handler.setLevel(logging.INFO)
|
|
50
|
+
|
|
51
|
+
# Create formatters and add them to handlers
|
|
52
|
+
console_format = logging.Formatter("%(levelname)s - %(message)s")
|
|
53
|
+
file_format = logging.Formatter(
|
|
54
|
+
"%(asctime)s - %(filename)-12s \t - %(levelname)-8s - %(message)s",
|
|
55
|
+
datefmt="%H:%M:%S",
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
console_handler.setFormatter(console_format)
|
|
59
|
+
file_handler.setFormatter(file_format)
|
|
60
|
+
|
|
61
|
+
root = logging.getLogger()
|
|
62
|
+
for handler in (console_handler, file_handler):
|
|
63
|
+
setattr(handler, _PYCLM_HANDLER_FLAG, True)
|
|
64
|
+
root.addHandler(handler)
|
|
65
|
+
|
|
66
|
+
if root.level > logging.INFO:
|
|
67
|
+
root.setLevel(logging.INFO)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def launch_gui_process(
|
|
71
|
+
srcs: Sequence[tuple[str, str]],
|
|
72
|
+
cwd: Path | None = None,
|
|
73
|
+
experiment_dir: Path | None = None,
|
|
74
|
+
) -> subprocess.Popen:
|
|
75
|
+
args = [
|
|
76
|
+
sys.executable,
|
|
77
|
+
"-m",
|
|
78
|
+
"pyclm.gui.gui_controller",
|
|
79
|
+
str(experiment_dir or cwd or "."),
|
|
80
|
+
]
|
|
81
|
+
for fp, ch in srcs:
|
|
82
|
+
args += ["--src", f"{fp}:{ch}"]
|
|
83
|
+
env = os.environ.copy()
|
|
84
|
+
env.setdefault("NAPARI_DISABLE_PLUGIN_AUTOLOAD", "1")
|
|
85
|
+
return subprocess.Popen(
|
|
86
|
+
args,
|
|
87
|
+
cwd=str(cwd) if cwd else None,
|
|
88
|
+
env=env,
|
|
89
|
+
stdout=None,
|
|
90
|
+
stderr=None,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def run_pyclm(
|
|
95
|
+
experiment_directory,
|
|
96
|
+
config_path=None,
|
|
97
|
+
segmentation_methods: dict[str, type[SegmentationMethod]] | None = None,
|
|
98
|
+
pattern_methods: dict[str, type[PatternMethod]] | None = None,
|
|
99
|
+
position_mover: PositionMover | None = None,
|
|
100
|
+
dry_image_source=None,
|
|
101
|
+
tracking_methods: dict[str, type[TrackingMethod]] | None = None,
|
|
102
|
+
dry: bool = False,
|
|
103
|
+
gui: bool = False,
|
|
104
|
+
check: bool = True,
|
|
105
|
+
force: bool = False,
|
|
106
|
+
):
|
|
107
|
+
"""
|
|
108
|
+
Run the experiments of a directory: the same as ``pyclm run``.
|
|
109
|
+
|
|
110
|
+
:param experiment_directory: the directory holding the experiment TOMLs,
|
|
111
|
+
the schedule, the position list and (usually) ``pyclm_config.toml``
|
|
112
|
+
:param config_path: the configuration file; None means the directory's
|
|
113
|
+
``pyclm_config[.<anything>].toml``, then the working directory's
|
|
114
|
+
:param segmentation_methods: ``{name: class}`` of custom segmentation
|
|
115
|
+
methods to register (``name`` is what ``[segmentation] method`` selects)
|
|
116
|
+
:param pattern_methods: ``{name: class}`` of custom pattern methods; the
|
|
117
|
+
configuration's ``methods`` and installed ``pyclm.methods`` entry
|
|
118
|
+
points are loaded as well (see :mod:`pyclm.methods`), these win
|
|
119
|
+
:param position_mover: how the stage reaches a position; None means the
|
|
120
|
+
configuration's ``position_mover`` (``basic`` by default)
|
|
121
|
+
:param dry_image_source: frames for the virtual microscope; None with
|
|
122
|
+
``dry=True`` discovers them from the directory
|
|
123
|
+
:param tracking_methods: ``{name: class}`` of custom tracking methods
|
|
124
|
+
:param dry: run on the virtual microscope
|
|
125
|
+
:param gui: open the live viewer in its own process
|
|
126
|
+
:param check: run ``pyclm check`` first and stop on errors
|
|
127
|
+
:param force: start despite check errors
|
|
128
|
+
"""
|
|
129
|
+
|
|
130
|
+
experiment_directory = Path(experiment_directory)
|
|
131
|
+
print(f"experiment directory: {experiment_directory}")
|
|
132
|
+
|
|
133
|
+
# the config file: given, in the experiment directory, or in the working directory
|
|
134
|
+
config_path = find_pyclm_config(experiment_directory, config_path)
|
|
135
|
+
if config_path is None:
|
|
136
|
+
config_path = experiment_directory / "pyclm_config.toml"
|
|
137
|
+
config_path = Path(config_path)
|
|
138
|
+
|
|
139
|
+
assert experiment_directory.exists(), (
|
|
140
|
+
f"experiment directory {experiment_directory} does not exist"
|
|
141
|
+
)
|
|
142
|
+
assert config_path.exists(), (
|
|
143
|
+
f"config file {config_path} does not exist: pyclm_config.toml (or "
|
|
144
|
+
"pyclm_config.<name>.toml) must be given or be in the experiment directory"
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
if gui:
|
|
148
|
+
# before the hardware is touched
|
|
149
|
+
from .gui import require_gui
|
|
150
|
+
|
|
151
|
+
require_gui()
|
|
152
|
+
|
|
153
|
+
set_logging(experiment_directory)
|
|
154
|
+
|
|
155
|
+
# the same check as `pyclm check`; errors stop the run unless forced
|
|
156
|
+
if check:
|
|
157
|
+
report = check_directory(
|
|
158
|
+
experiment_directory,
|
|
159
|
+
config_path,
|
|
160
|
+
pattern_methods=pattern_methods,
|
|
161
|
+
segmentation_methods=segmentation_methods,
|
|
162
|
+
tracking_methods=tracking_methods,
|
|
163
|
+
)
|
|
164
|
+
print(report.text())
|
|
165
|
+
if report.errors and not force:
|
|
166
|
+
raise CheckFailed(report)
|
|
167
|
+
|
|
168
|
+
config = PyclmConfig.from_file(config_path)
|
|
169
|
+
logger.info(f"loaded config from {config_path}")
|
|
170
|
+
|
|
171
|
+
# custom methods: installed entry points and the configuration's `methods`;
|
|
172
|
+
# the ones passed in code win
|
|
173
|
+
from .methods import discover
|
|
174
|
+
|
|
175
|
+
found, ep_warnings = discover(config, config_path)
|
|
176
|
+
for warning in ep_warnings:
|
|
177
|
+
logger.warning(warning)
|
|
178
|
+
pattern_methods, segmentation_methods, tracking_methods = found.merged_with(
|
|
179
|
+
pattern_methods, segmentation_methods, tracking_methods
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
focus_device = config.focus_device
|
|
183
|
+
if position_mover is None:
|
|
184
|
+
# the command line (and any caller that does not pass one) takes the
|
|
185
|
+
# mover from pyclm_config.toml; a mover passed in code wins
|
|
186
|
+
from .core.position_mover import resolve_mover
|
|
187
|
+
|
|
188
|
+
position_mover = resolve_mover(config.position_mover)()
|
|
189
|
+
logger.info(
|
|
190
|
+
f"position mover: {type(position_mover).__name__} "
|
|
191
|
+
f"(pyclm_config.toml position_mover = {config.position_mover!r})"
|
|
192
|
+
)
|
|
193
|
+
settle_time_s = config.settle_time_seconds
|
|
194
|
+
storage_format = config.output.format
|
|
195
|
+
pattern_policy = config.output.pattern_policy
|
|
196
|
+
export_imagej = config.output.export_imagej
|
|
197
|
+
logger.info(f"output format {storage_format}, pattern policy {pattern_policy}")
|
|
198
|
+
|
|
199
|
+
base_path = experiment_directory
|
|
200
|
+
|
|
201
|
+
# For dry runs without an explicit image source, discover the schedule and
|
|
202
|
+
# image source together from the directory before creating the Controller.
|
|
203
|
+
if dry and dry_image_source is None:
|
|
204
|
+
schedule, dry_image_source = dry_schedule_from_directory(base_path)
|
|
205
|
+
else:
|
|
206
|
+
schedule = schedule_from_directory(base_path)
|
|
207
|
+
|
|
208
|
+
c = Controller(
|
|
209
|
+
config.config_path,
|
|
210
|
+
dry,
|
|
211
|
+
position_mover=position_mover,
|
|
212
|
+
dry_image_source=dry_image_source,
|
|
213
|
+
settle_time_s=settle_time_s,
|
|
214
|
+
storage_format=storage_format,
|
|
215
|
+
pattern_policy=pattern_policy,
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
# register any custom methods
|
|
219
|
+
if segmentation_methods is not None:
|
|
220
|
+
for name, method in segmentation_methods.items():
|
|
221
|
+
c.register_segmentation_method(name, method)
|
|
222
|
+
|
|
223
|
+
if pattern_methods is not None:
|
|
224
|
+
for name, method in pattern_methods.items():
|
|
225
|
+
c.register_pattern_method(name, method)
|
|
226
|
+
|
|
227
|
+
if tracking_methods is not None:
|
|
228
|
+
for name, method in tracking_methods.items():
|
|
229
|
+
c.register_tracking_method(name, method)
|
|
230
|
+
|
|
231
|
+
core = c.core
|
|
232
|
+
core.describe()
|
|
233
|
+
|
|
234
|
+
if focus_device:
|
|
235
|
+
core.setFocusDevice(focus_device)
|
|
236
|
+
logger.info(f"focus device set to '{focus_device}'")
|
|
237
|
+
|
|
238
|
+
print("---listing available config groups---")
|
|
239
|
+
groups = core.getAvailableConfigGroups()
|
|
240
|
+
if not groups:
|
|
241
|
+
# an empty tuple means no configuration is loaded; None has been seen
|
|
242
|
+
# with a pymmcore-plus / pymmcore pair that do not belong together
|
|
243
|
+
logger.warning(
|
|
244
|
+
"no config groups reported by the core "
|
|
245
|
+
f"(getAvailableConfigGroups returned {groups!r}); "
|
|
246
|
+
"is the MicroManager configuration loaded, and do the installed "
|
|
247
|
+
"pymmcore and pymmcore-plus match (see pyproject.toml, [tool.uv])?"
|
|
248
|
+
)
|
|
249
|
+
for group in groups or ():
|
|
250
|
+
cg = core.getConfigGroupObject(group, False)
|
|
251
|
+
print(cg.name, list(cg.items()))
|
|
252
|
+
|
|
253
|
+
slm_shape = config.slm_shape
|
|
254
|
+
at = config.affine
|
|
255
|
+
|
|
256
|
+
c.initialize(schedule, slm_shape, at, base_path, camera_roi=config.camera_roi)
|
|
257
|
+
|
|
258
|
+
all_layers = c.all_layers
|
|
259
|
+
t_gcd = c.t_gcd
|
|
260
|
+
|
|
261
|
+
all_layers_output = {
|
|
262
|
+
"t": t_gcd,
|
|
263
|
+
"all_layers": [f"{filepath}:{channel}" for filepath, channel in all_layers],
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
with open(f"{base_path}/all_layers.txt", "w") as file:
|
|
267
|
+
json.dump(all_layers_output, file, indent=4)
|
|
268
|
+
|
|
269
|
+
gui_proc = None
|
|
270
|
+
if gui:
|
|
271
|
+
print(all_layers)
|
|
272
|
+
gui_proc = launch_gui_process(
|
|
273
|
+
all_layers, cwd=base_path, experiment_dir=base_path
|
|
274
|
+
)
|
|
275
|
+
logger.info(f"Started GUI process (pid={gui_proc.pid})")
|
|
276
|
+
|
|
277
|
+
c.run()
|
|
278
|
+
|
|
279
|
+
if export_imagej:
|
|
280
|
+
export_outputs(c.outbox.writer.output_paths().values())
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def export_outputs(paths) -> list[Path]:
|
|
284
|
+
"""Write ImageJ hyperstacks next to each finished output; failures are logged, not raised."""
|
|
285
|
+
from . import io as pyclm_io
|
|
286
|
+
|
|
287
|
+
written = []
|
|
288
|
+
for path in paths:
|
|
289
|
+
try:
|
|
290
|
+
with pyclm_io.open(path) as exp:
|
|
291
|
+
written += pyclm_io.export_imagej(exp)
|
|
292
|
+
except Exception as e:
|
|
293
|
+
logger.error(f"ImageJ export of {path} failed: {e}", exc_info=True)
|
|
294
|
+
if written:
|
|
295
|
+
print(
|
|
296
|
+
f"exported {len(written)} ImageJ stack(s): {', '.join(p.name for p in written)}"
|
|
297
|
+
)
|
|
298
|
+
return written
|