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/events.py
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import datetime
|
|
2
|
+
import json
|
|
3
|
+
from uuid import UUID, uuid4
|
|
4
|
+
|
|
5
|
+
from h5py import Dataset
|
|
6
|
+
|
|
7
|
+
from .experiments import (
|
|
8
|
+
ConfigGroup,
|
|
9
|
+
DeviceProperty,
|
|
10
|
+
MicroscopePosition,
|
|
11
|
+
PositionBase,
|
|
12
|
+
PositionWithAutoFocus,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
STIM_GROUP = "stim_aq"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def storage_group(channel: str, is_stim: bool) -> str:
|
|
19
|
+
"""HDF5 group for a channel: ``stim_aq`` for stimulation, ``channel_<name>`` otherwise."""
|
|
20
|
+
return STIM_GROUP if is_stim else f"channel_{channel}"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def rel_path(index: dict, is_stim: bool) -> str:
|
|
24
|
+
"""Dataset path within the HDF5 file for a frame index, e.g. ``00012/channel_545/``."""
|
|
25
|
+
return f"{index['t']:05d}/{storage_group(index['c'], is_stim)}/"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class UpdatePatternEvent:
|
|
29
|
+
def __init__(
|
|
30
|
+
self,
|
|
31
|
+
experiment,
|
|
32
|
+
config_groups: list[ConfigGroup] | None = None,
|
|
33
|
+
devices: list[DeviceProperty] | None = None,
|
|
34
|
+
index: dict | None = None,
|
|
35
|
+
skippable_if_blank: bool = False,
|
|
36
|
+
):
|
|
37
|
+
self.id = uuid4()
|
|
38
|
+
|
|
39
|
+
self.experiment_name = experiment
|
|
40
|
+
self.config_groups = config_groups
|
|
41
|
+
self.devices = devices
|
|
42
|
+
self.index = dict(index or {})
|
|
43
|
+
# a blank pattern may skip this position at this timepoint (see PlannedEvent)
|
|
44
|
+
self.skippable_if_blank = skippable_if_blank
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class UpdateStagePositionEvent:
|
|
48
|
+
"""
|
|
49
|
+
Moves the stage
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
def __init__(
|
|
53
|
+
self,
|
|
54
|
+
position: PositionBase,
|
|
55
|
+
experiment_name: str,
|
|
56
|
+
index: dict | None = None,
|
|
57
|
+
skippable_if_blank: bool = False,
|
|
58
|
+
):
|
|
59
|
+
self.id = uuid4()
|
|
60
|
+
self.position = position
|
|
61
|
+
self.experiment_name = experiment_name
|
|
62
|
+
self.index = dict(index or {})
|
|
63
|
+
self.skippable_if_blank = skippable_if_blank
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class UpdatePositionWithAutoFocusEvent(UpdateStagePositionEvent):
|
|
67
|
+
def __init__(self, position: PositionWithAutoFocus, experiment_name):
|
|
68
|
+
super().__init__(position, experiment_name)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class AcquisitionEvent:
|
|
72
|
+
"""
|
|
73
|
+
One image acquisition: identity from the plan (``index``), where and how
|
|
74
|
+
to take it, and whether the frame is saved. Who consumes the frame is
|
|
75
|
+
not the event's business; the Router decides that from its identity.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
def __init__(
|
|
79
|
+
self,
|
|
80
|
+
experiment,
|
|
81
|
+
position: MicroscopePosition,
|
|
82
|
+
channel_id: UUID,
|
|
83
|
+
index: dict | None = None,
|
|
84
|
+
scheduled_time=0,
|
|
85
|
+
scheduled_time_since_start=0,
|
|
86
|
+
exposure_time_ms=10,
|
|
87
|
+
needs_slm=False,
|
|
88
|
+
config_groups: list[ConfigGroup] | None = None,
|
|
89
|
+
devices: list[DeviceProperty] | None = None,
|
|
90
|
+
save_output=True,
|
|
91
|
+
binning: int = 1,
|
|
92
|
+
):
|
|
93
|
+
self.id = uuid4()
|
|
94
|
+
|
|
95
|
+
# experiment (determines the output file / store)
|
|
96
|
+
self.experiment_name = experiment
|
|
97
|
+
|
|
98
|
+
# position
|
|
99
|
+
self.position = position
|
|
100
|
+
|
|
101
|
+
self.scheduled_time = scheduled_time
|
|
102
|
+
self.time_since_start = scheduled_time_since_start
|
|
103
|
+
self.complete = False
|
|
104
|
+
self.completed_time = None
|
|
105
|
+
|
|
106
|
+
# frame identity from the plan: {"t": timepoint, "p": experiment, "c": channel}
|
|
107
|
+
self.index: dict = dict(index) if index else {}
|
|
108
|
+
|
|
109
|
+
# acquisition details
|
|
110
|
+
self.exposure_time_ms = exposure_time_ms
|
|
111
|
+
self.needs_slm = needs_slm
|
|
112
|
+
self.binning = binning
|
|
113
|
+
|
|
114
|
+
# config group config-preset pairs
|
|
115
|
+
self.config_groups = config_groups
|
|
116
|
+
|
|
117
|
+
# device-name, parameter, value, type
|
|
118
|
+
self.devices = devices
|
|
119
|
+
|
|
120
|
+
# whether the writer records the frame
|
|
121
|
+
self.save_output = save_output
|
|
122
|
+
|
|
123
|
+
self.channel_id = channel_id
|
|
124
|
+
|
|
125
|
+
# settings changed at runtime for this channel and in force for this
|
|
126
|
+
# frame, {frames-table column: value} (see core/settings.py)
|
|
127
|
+
self.overrides: dict = {}
|
|
128
|
+
|
|
129
|
+
self.pixel_width_um = None
|
|
130
|
+
|
|
131
|
+
@property
|
|
132
|
+
def t_index(self) -> int:
|
|
133
|
+
return int(self.index.get("t", 0))
|
|
134
|
+
|
|
135
|
+
def get_rel_path(self) -> str:
|
|
136
|
+
"""
|
|
137
|
+
Returns dset path within the hdf5 structure
|
|
138
|
+
"""
|
|
139
|
+
if "t" not in self.index or "c" not in self.index:
|
|
140
|
+
return "UNNAMED_DATA/"
|
|
141
|
+
return rel_path(self.index, self.needs_slm)
|
|
142
|
+
|
|
143
|
+
def _fmt_time(self, timestamp) -> str:
|
|
144
|
+
if timestamp is None:
|
|
145
|
+
return ""
|
|
146
|
+
return datetime.datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
|
|
147
|
+
|
|
148
|
+
def as_attrs(self) -> dict:
|
|
149
|
+
"""
|
|
150
|
+
Flat, HDF5-attribute-compatible description of this event.
|
|
151
|
+
|
|
152
|
+
Used both to annotate datasets (``write_attrs``) and for ``repr``.
|
|
153
|
+
"""
|
|
154
|
+
attrs = {}
|
|
155
|
+
|
|
156
|
+
attrs["id"] = str(self.id)
|
|
157
|
+
attrs["position"] = [(k, str(v)) for k, v in self.position.as_dict().items()]
|
|
158
|
+
|
|
159
|
+
attrs["experiment_name"] = self.experiment_name
|
|
160
|
+
|
|
161
|
+
attrs["time_scheduled"] = self._fmt_time(self.scheduled_time)
|
|
162
|
+
attrs["time_since_start"] = str(
|
|
163
|
+
datetime.timedelta(seconds=self.time_since_start)
|
|
164
|
+
)
|
|
165
|
+
attrs["time_completed"] = self._fmt_time(self.completed_time)
|
|
166
|
+
attrs["complete"] = self.complete
|
|
167
|
+
|
|
168
|
+
attrs["exposure_time_ms"] = self.exposure_time_ms
|
|
169
|
+
attrs["needs_slm"] = self.needs_slm
|
|
170
|
+
attrs["binning"] = self.binning
|
|
171
|
+
|
|
172
|
+
attrs["index"] = json.dumps(self.index)
|
|
173
|
+
|
|
174
|
+
if self.config_groups is not None:
|
|
175
|
+
for cg in self.config_groups:
|
|
176
|
+
attrs[f"config_groups: {cg.group}"] = str(cg.config)
|
|
177
|
+
|
|
178
|
+
if self.devices is not None:
|
|
179
|
+
for dp in self.devices:
|
|
180
|
+
attrs[f"devices: {dp.device}-{dp.property}"] = str(dp.value)
|
|
181
|
+
|
|
182
|
+
attrs["save_output"] = self.save_output
|
|
183
|
+
attrs["channel_id"] = str(self.channel_id)
|
|
184
|
+
|
|
185
|
+
attrs["pixel_width_um"] = str(self.pixel_width_um)
|
|
186
|
+
|
|
187
|
+
return attrs
|
|
188
|
+
|
|
189
|
+
def write_attrs(self, dset: Dataset):
|
|
190
|
+
for key, value in self.as_attrs().items():
|
|
191
|
+
dset.attrs[key] = value
|
|
192
|
+
|
|
193
|
+
def __repr__(self):
|
|
194
|
+
return repr(self.as_attrs())
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the Experiment class and related data structures for managing imaging experiments.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
from collections import namedtuple
|
|
7
|
+
from uuid import uuid4
|
|
8
|
+
|
|
9
|
+
from .kinds import DEFAULT_SEGMENTATION
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
13
|
+
ConfigGroup = namedtuple("ConfigGroup", ["group", "config"])
|
|
14
|
+
DeviceProperty = namedtuple("DeviceProperty", ["device", "property", "value", "type"])
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def make_configgroup_dict(config_groups: list[ConfigGroup] | None):
|
|
18
|
+
if config_groups is None:
|
|
19
|
+
return {}
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
config: cfg
|
|
23
|
+
for (config, _), cfg in zip(config_groups, config_groups, strict=False)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def make_deviceproperty_dict(device_properties: list[DeviceProperty] | None):
|
|
28
|
+
if device_properties is None:
|
|
29
|
+
return {}
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
f"{dev}-{prop}": devprop
|
|
33
|
+
for (dev, prop, _, _), devprop in zip(
|
|
34
|
+
device_properties, device_properties, strict=False
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class ImagingConfig:
|
|
40
|
+
"""
|
|
41
|
+
Contains device properties and config groups which can be updated during experiment initialization or runtime
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
def __init__(
|
|
45
|
+
self,
|
|
46
|
+
experiment_name: str,
|
|
47
|
+
exposure_ms: float = 10,
|
|
48
|
+
every_t: int = 1,
|
|
49
|
+
binning: int = 1,
|
|
50
|
+
config_groups: list[ConfigGroup] | None = None,
|
|
51
|
+
device_properties: list[DeviceProperty] | None = None,
|
|
52
|
+
save=True,
|
|
53
|
+
):
|
|
54
|
+
self.channel_id = uuid4()
|
|
55
|
+
self.experiment_name = experiment_name
|
|
56
|
+
self.exposure = exposure_ms
|
|
57
|
+
self.every_t = every_t
|
|
58
|
+
self.save = save
|
|
59
|
+
self.binning = binning
|
|
60
|
+
|
|
61
|
+
self._config_groups = make_configgroup_dict(config_groups)
|
|
62
|
+
self._device_properties = make_deviceproperty_dict(device_properties)
|
|
63
|
+
|
|
64
|
+
def update_config_groups(self, gps: list[ConfigGroup]):
|
|
65
|
+
gps = make_configgroup_dict(gps)
|
|
66
|
+
self._config_groups.update(gps)
|
|
67
|
+
|
|
68
|
+
def update_device_properties(self, devp: list[DeviceProperty]):
|
|
69
|
+
devp = make_deviceproperty_dict(devp)
|
|
70
|
+
self._device_properties.update(devp)
|
|
71
|
+
|
|
72
|
+
def get_config_groups(self) -> list[ConfigGroup]:
|
|
73
|
+
return list(self._config_groups.values())
|
|
74
|
+
|
|
75
|
+
def get_device_properties(self) -> list[DeviceProperty]:
|
|
76
|
+
return list(self._device_properties.values())
|
|
77
|
+
|
|
78
|
+
def set_id(self):
|
|
79
|
+
self.channel_id = uuid4()
|
|
80
|
+
|
|
81
|
+
def __repr__(self):
|
|
82
|
+
return (
|
|
83
|
+
f"ImagingConfig({self.channel_id}: exposure={self.exposure}ms, image_every={self.every_t}, "
|
|
84
|
+
f"configs={self._config_groups}, device_props={self._device_properties})"
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
def as_dict(self):
|
|
88
|
+
return {
|
|
89
|
+
"channel_id": str(self.channel_id),
|
|
90
|
+
"experiment_name": self.experiment_name,
|
|
91
|
+
"exposure": self.exposure,
|
|
92
|
+
"every_t": self.every_t,
|
|
93
|
+
"save": self.save,
|
|
94
|
+
"binning": self.binning,
|
|
95
|
+
"config_groups": [
|
|
96
|
+
{"group": c.group, "config": c.config} for c in self.get_config_groups()
|
|
97
|
+
],
|
|
98
|
+
"device_properties": [
|
|
99
|
+
{
|
|
100
|
+
"device": d.device,
|
|
101
|
+
"property": d.property,
|
|
102
|
+
"value": d.value,
|
|
103
|
+
"type": d.type,
|
|
104
|
+
}
|
|
105
|
+
for d in self.get_device_properties()
|
|
106
|
+
],
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class MethodBasedConfig:
|
|
111
|
+
def __init__(self, method_name: str, save_output: bool = True, every_t=1, **kwargs):
|
|
112
|
+
self.method_name = method_name
|
|
113
|
+
self.save = save_output
|
|
114
|
+
self.every_t = every_t
|
|
115
|
+
|
|
116
|
+
logger.debug(f"method {method_name} kwargs: {kwargs}")
|
|
117
|
+
|
|
118
|
+
self.kwargs = kwargs
|
|
119
|
+
|
|
120
|
+
def __repr__(self):
|
|
121
|
+
return f"Method({self.method_name}: {', '.join([f'{k}={v}' for k, v in self.kwargs.items()])})"
|
|
122
|
+
|
|
123
|
+
def as_dict(self):
|
|
124
|
+
return {
|
|
125
|
+
"method_name": self.method_name,
|
|
126
|
+
"save": self.save,
|
|
127
|
+
"every_t": self.every_t,
|
|
128
|
+
"kwargs": self.kwargs,
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
SegmentationConfig = MethodBasedConfig
|
|
133
|
+
PatternConfig = MethodBasedConfig
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
class TrackingConfig(MethodBasedConfig):
|
|
137
|
+
"""``[tracking]``: the method and its kwargs, plus which segmentation it links."""
|
|
138
|
+
|
|
139
|
+
def __init__(
|
|
140
|
+
self,
|
|
141
|
+
method_name: str,
|
|
142
|
+
save_output: bool = True,
|
|
143
|
+
every_t=1,
|
|
144
|
+
segmentation: str | None = None,
|
|
145
|
+
**kwargs,
|
|
146
|
+
):
|
|
147
|
+
super().__init__(method_name, save_output, every_t, **kwargs)
|
|
148
|
+
self.segmentation = segmentation or DEFAULT_SEGMENTATION
|
|
149
|
+
|
|
150
|
+
def as_dict(self):
|
|
151
|
+
out = super().as_dict()
|
|
152
|
+
out["segmentation"] = self.segmentation
|
|
153
|
+
return out
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
class Experiment:
|
|
157
|
+
def __init__(
|
|
158
|
+
self,
|
|
159
|
+
experiment_name,
|
|
160
|
+
imaging_configs: dict[str, ImagingConfig],
|
|
161
|
+
stimulation_config: ImagingConfig,
|
|
162
|
+
segmentation: SegmentationConfig | None,
|
|
163
|
+
pattern: PatternConfig,
|
|
164
|
+
t_delay: int = 0,
|
|
165
|
+
t_stop: int = 0,
|
|
166
|
+
tracking: TrackingConfig | None = None,
|
|
167
|
+
segmentations: dict[str, SegmentationConfig] | None = None,
|
|
168
|
+
):
|
|
169
|
+
self.key = uuid4()
|
|
170
|
+
self.experiment_name = experiment_name
|
|
171
|
+
self.channels = imaging_configs
|
|
172
|
+
self.stimulation = stimulation_config
|
|
173
|
+
# every [segmentation] table by name, the default one ("segmentation") first
|
|
174
|
+
named = dict(segmentations or {})
|
|
175
|
+
default = segmentation
|
|
176
|
+
if default is None:
|
|
177
|
+
default = named.pop(DEFAULT_SEGMENTATION, None)
|
|
178
|
+
self.segmentations: dict[str, SegmentationConfig] = {
|
|
179
|
+
DEFAULT_SEGMENTATION: default or SegmentationConfig("none")
|
|
180
|
+
}
|
|
181
|
+
self.segmentations.update(
|
|
182
|
+
(k, v) for k, v in named.items() if k != DEFAULT_SEGMENTATION
|
|
183
|
+
)
|
|
184
|
+
self.pattern = pattern
|
|
185
|
+
self.tracking = tracking if tracking is not None else TrackingConfig("none")
|
|
186
|
+
|
|
187
|
+
self.t_delay = t_delay
|
|
188
|
+
self.t_stop = t_stop
|
|
189
|
+
|
|
190
|
+
@property
|
|
191
|
+
def segmentation(self) -> SegmentationConfig:
|
|
192
|
+
"""The default ``[segmentation]`` table (``method = "none"`` if absent)."""
|
|
193
|
+
return self.segmentations[DEFAULT_SEGMENTATION]
|
|
194
|
+
|
|
195
|
+
@segmentation.setter
|
|
196
|
+
def segmentation(self, cfg: SegmentationConfig):
|
|
197
|
+
self.segmentations[DEFAULT_SEGMENTATION] = cfg
|
|
198
|
+
|
|
199
|
+
@property
|
|
200
|
+
def segmentation_names(self) -> list[str]:
|
|
201
|
+
"""Names of the segmentation tables that configure a method."""
|
|
202
|
+
return [n for n, c in self.segmentations.items() if c.method_name != "none"]
|
|
203
|
+
|
|
204
|
+
def __repr__(self):
|
|
205
|
+
return (
|
|
206
|
+
f"Experiment('{self.experiment_name}': Channels={self.channels}, Stimulation={self.stimulation}, "
|
|
207
|
+
f"Segmentation method={self.segmentation}, Pattern method={self.pattern})"
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
def as_dict(self):
|
|
211
|
+
return {
|
|
212
|
+
"experiment_name": self.experiment_name,
|
|
213
|
+
"channels": {k: v.as_dict() for k, v in self.channels.items()},
|
|
214
|
+
"stimulation": self.stimulation.as_dict(),
|
|
215
|
+
"segmentation": self.segmentation.as_dict(),
|
|
216
|
+
"segmentations": {k: v.as_dict() for k, v in self.segmentations.items()},
|
|
217
|
+
"tracking": self.tracking.as_dict(),
|
|
218
|
+
"pattern": self.pattern.as_dict(),
|
|
219
|
+
"t_delay": self.t_delay,
|
|
220
|
+
"t_stop": self.t_stop,
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
class PositionBase:
|
|
225
|
+
def as_dict(self):
|
|
226
|
+
return {}
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
class MicroscopePosition(PositionBase):
|
|
230
|
+
"""
|
|
231
|
+
Standard microscope position. x, y, and z are required. Any optional
|
|
232
|
+
hardware-specific values (e.g. focus-offset) are stored in ``extras``
|
|
233
|
+
keyed by device name, e.g. ``{"PFSOffset": 11122.0}``.
|
|
234
|
+
|
|
235
|
+
A grid (MicroManager's Create Grid, see :mod:`pyclm.core.grid`) is one
|
|
236
|
+
position at the centre of its tiles: ``tiles`` holds one position per
|
|
237
|
+
tile (each with ``grid_rc = (row, col)``), ``grid`` the rows, columns
|
|
238
|
+
and pitch in µm, and ``geometry`` the :class:`~pyclm.core.grid.GridGeometry`
|
|
239
|
+
once the camera is known. All three are None for a plain position.
|
|
240
|
+
"""
|
|
241
|
+
|
|
242
|
+
def __init__(
|
|
243
|
+
self,
|
|
244
|
+
x: float,
|
|
245
|
+
y: float,
|
|
246
|
+
z: float,
|
|
247
|
+
label: str | None = None,
|
|
248
|
+
extras: dict | None = None,
|
|
249
|
+
):
|
|
250
|
+
self.x = x
|
|
251
|
+
self.y = y
|
|
252
|
+
self.z = z
|
|
253
|
+
self.label = label
|
|
254
|
+
self.extras: dict = extras if extras is not None else {}
|
|
255
|
+
self.tiles: list[MicroscopePosition] | None = None
|
|
256
|
+
self.grid: dict | None = None
|
|
257
|
+
self.grid_rc: tuple[int, int] | None = None
|
|
258
|
+
self.geometry = None
|
|
259
|
+
|
|
260
|
+
@property
|
|
261
|
+
def is_grid(self) -> bool:
|
|
262
|
+
return bool(self.tiles)
|
|
263
|
+
|
|
264
|
+
def as_dict(self):
|
|
265
|
+
d = {"label": self.label, "x": self.x, "y": self.y, "z": self.z}
|
|
266
|
+
d.update(self.extras)
|
|
267
|
+
return d
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
class PositionWithAutoFocus(MicroscopePosition):
|
|
271
|
+
"""
|
|
272
|
+
Legacy position class for MicroManager multipoints.xml files.
|
|
273
|
+
Prefer ``MicroscopePosition`` with ``extras={"PFSOffset": value}`` for new code.
|
|
274
|
+
"""
|
|
275
|
+
|
|
276
|
+
def __init__(self, x=None, y=None, z=None, autofocus_offset=None, label=None):
|
|
277
|
+
extras = {}
|
|
278
|
+
if autofocus_offset is not None:
|
|
279
|
+
extras["PFSOffset"] = autofocus_offset
|
|
280
|
+
super().__init__(x=x, y=y, z=z, label=label, extras=extras)
|
|
281
|
+
self.autofocus_offset = autofocus_offset
|
|
282
|
+
|
|
283
|
+
def get_xy(self):
|
|
284
|
+
if not ((self.x is None) or (self.y is None)):
|
|
285
|
+
return [self.x, self.y]
|
|
286
|
+
return None
|
|
287
|
+
|
|
288
|
+
def get_z(self):
|
|
289
|
+
return self.z
|
|
290
|
+
|
|
291
|
+
def get_autofocus_offset(self):
|
|
292
|
+
return self.autofocus_offset
|
|
293
|
+
|
|
294
|
+
def as_dict(self):
|
|
295
|
+
return {
|
|
296
|
+
"label": self.label,
|
|
297
|
+
"x": self.x,
|
|
298
|
+
"y": self.y,
|
|
299
|
+
"z": self.z,
|
|
300
|
+
"autofocus_offset": self.autofocus_offset,
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
class TimeCourse:
|
|
305
|
+
def __init__(self, count, interval, setup, between):
|
|
306
|
+
self.count = count
|
|
307
|
+
self.interval = interval
|
|
308
|
+
self.setup = setup
|
|
309
|
+
self.between = between
|
|
310
|
+
|
|
311
|
+
def as_dict(self):
|
|
312
|
+
return {
|
|
313
|
+
"count": self.count,
|
|
314
|
+
"interval": self.interval,
|
|
315
|
+
"setup": self.setup,
|
|
316
|
+
"between": self.between,
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
class ExperimentSchedule:
|
|
321
|
+
def __init__(
|
|
322
|
+
self,
|
|
323
|
+
experiments: dict[str, Experiment],
|
|
324
|
+
positions: dict[str, PositionBase],
|
|
325
|
+
t_count: int = 1,
|
|
326
|
+
t_interval: float = 30.0,
|
|
327
|
+
t_setup=2.0,
|
|
328
|
+
t_between=1.0,
|
|
329
|
+
timecourse: TimeCourse | None = None,
|
|
330
|
+
):
|
|
331
|
+
self.experiment_names = [exp for exp in experiments]
|
|
332
|
+
self.experiments = experiments
|
|
333
|
+
self.positions = positions
|
|
334
|
+
|
|
335
|
+
assert set(experiments.keys()) == set(positions.keys())
|
|
336
|
+
|
|
337
|
+
if timecourse:
|
|
338
|
+
self.times = timecourse
|
|
339
|
+
else:
|
|
340
|
+
self.times = TimeCourse(t_count, t_interval, t_setup, t_between)
|
|
341
|
+
|
|
342
|
+
def as_dict(self):
|
|
343
|
+
return {
|
|
344
|
+
"experiment_names": self.experiment_names,
|
|
345
|
+
"experiments": {k: v.as_dict() for k, v in self.experiments.items()},
|
|
346
|
+
"positions": {k: v.as_dict() for k, v in self.positions.items()},
|
|
347
|
+
"times": self.times.as_dict(),
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def check_type(obj):
|
|
352
|
+
if isinstance(obj, float):
|
|
353
|
+
return "float"
|
|
354
|
+
elif isinstance(obj, str):
|
|
355
|
+
return "str"
|
|
356
|
+
elif isinstance(obj, int):
|
|
357
|
+
return "int"
|
|
358
|
+
elif isinstance(obj, bool):
|
|
359
|
+
return "bool"
|
|
360
|
+
else:
|
|
361
|
+
return "unknown"
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
def get_config_groups(toml_dict, key):
|
|
365
|
+
"""
|
|
366
|
+
Returns a list of config groups from the toml_dict
|
|
367
|
+
:param toml_dict: dict
|
|
368
|
+
:param key: str
|
|
369
|
+
:return: list[ConfigGroup]
|
|
370
|
+
"""
|
|
371
|
+
if key not in toml_dict:
|
|
372
|
+
return []
|
|
373
|
+
|
|
374
|
+
config_groups = []
|
|
375
|
+
for group, config in toml_dict[key].items():
|
|
376
|
+
config_groups.append(ConfigGroup(group, config))
|
|
377
|
+
|
|
378
|
+
return config_groups
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def get_device_properties(toml_dict, key):
|
|
382
|
+
"""
|
|
383
|
+
Returns a list of device properties from the toml_dict
|
|
384
|
+
:param toml_dict: dict
|
|
385
|
+
:param key: str
|
|
386
|
+
:return: list[DeviceProperty]
|
|
387
|
+
"""
|
|
388
|
+
if key not in toml_dict:
|
|
389
|
+
return []
|
|
390
|
+
|
|
391
|
+
device_properties = []
|
|
392
|
+
for k, v in toml_dict[key].items():
|
|
393
|
+
try:
|
|
394
|
+
dev, prop = k.split("-")[0], k.split("-")[1]
|
|
395
|
+
except IndexError as e:
|
|
396
|
+
raise IndexError(
|
|
397
|
+
f"error in device property {k}; should be formatted as device-property"
|
|
398
|
+
) from e
|
|
399
|
+
|
|
400
|
+
t = check_type(v)
|
|
401
|
+
device_properties.append(DeviceProperty(dev, prop, v, t))
|
|
402
|
+
|
|
403
|
+
return device_properties
|