-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathmulti_point_worker.py
More file actions
1738 lines (1517 loc) · 81.8 KB
/
Copy pathmulti_point_worker.py
File metadata and controls
1738 lines (1517 loc) · 81.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import queue
import threading
import time
from typing import Callable, Dict, List, NamedTuple, Optional, Tuple, Type
from datetime import datetime
import imageio as iio
import numpy as np
import pandas as pd
from control._def import *
from control._def import DOWNSAMPLED_VIEW_JOB_TIMEOUT_S, DOWNSAMPLED_VIEW_IDLE_TIMEOUT_S
import control._def
from control import utils
from control.slack_notifier import TimepointStats, AcquisitionStats
from control.core.auto_focus_controller import AutoFocusController
from control.core.laser_auto_focus_controller import LaserAutofocusController
from control.core.live_controller import LiveController
from control.core.multi_point_utils import (
AcquisitionParameters,
MultiPointControllerFunctions,
OverallProgressUpdate,
RegionProgressUpdate,
PlateViewInit,
PlateViewUpdate,
)
from control.core.objective_store import ObjectiveStore
from control.microcontroller import Microcontroller
from control.microscope import Microscope
from control.piezo import PiezoStage
from control.models import AcquisitionChannel
from squid.abc import AbstractCamera, CameraFrame, CameraFrameFormat
import squid.logging
import control.core.job_processing
from control.core.job_processing import ZarrWriteResult
from control.core.job_processing import (
CaptureInfo,
SaveImageJob,
SaveOMETiffJob,
SaveZarrJob,
ZarrWriterInfo,
AcquisitionInfo,
Job,
JobImage,
JobRunner,
JobResult,
DownsampledViewJob,
DownsampledViewResult,
)
from control.core.downsampled_views import (
DownsampledViewManager,
calculate_overlap_pixels,
parse_well_id,
ensure_plate_resolution_in_well_resolutions,
)
from control.core.backpressure import BackpressureController, BackpressureValues
from squid.config import CameraPixelFormat
# Module-level logger for static methods
_log = squid.logging.get_logger(__name__)
class SummarizeResult(NamedTuple):
"""Result from processing job output queues."""
none_failed: bool # True if no jobs failed (or no results to process)
had_results: bool # True if any results were pulled from queue
class MultiPointWorker:
def __init__(
self,
scope: Microscope,
live_controller: LiveController,
auto_focus_controller: Optional[AutoFocusController],
laser_auto_focus_controller: Optional[LaserAutofocusController],
objective_store: ObjectiveStore,
acquisition_parameters: AcquisitionParameters,
callbacks: MultiPointControllerFunctions,
abort_requested_fn: Callable[[], bool],
request_abort_fn: Callable[[], None],
extra_job_classes: list[type[Job]] | None = None,
abort_on_failed_jobs: bool = True,
alignment_widget=None,
slack_notifier=None,
prewarmed_job_runner: Optional[JobRunner] = None,
prewarmed_bp_values: Optional["BackpressureValues"] = None,
):
self._log = squid.logging.get_logger(__class__.__name__)
self._timing = utils.TimingManager("MultiPointWorker Timer Manager")
self._alignment_widget = alignment_widget # Optional AlignmentWidget for coordinate offset
self._slack_notifier = slack_notifier # Optional SlackNotifier for notifications
# Slack notification tracking counters
self._timepoint_image_count = 0
self._timepoint_fov_count = 0
self._timepoint_start_time = 0.0
self._acquisition_error_count = 0
self._laser_af_successes = 0
self._laser_af_failures = 0
self.microscope: Microscope = scope
self.camera: AbstractCamera = scope.camera
self.microcontroller: Microcontroller = scope.low_level_drivers.microcontroller
self.stage: squid.abc.AbstractStage = scope.stage
self.piezo: Optional[PiezoStage] = scope.addons.piezo_stage
self.liveController = live_controller
self.autofocusController: Optional[AutoFocusController] = auto_focus_controller
self.laser_auto_focus_controller: Optional[LaserAutofocusController] = laser_auto_focus_controller
self.objectiveStore: ObjectiveStore = objective_store
self.fluidics = scope.addons.fluidics
self.use_fluidics = acquisition_parameters.use_fluidics
self.callbacks: MultiPointControllerFunctions = callbacks
self.abort_requested_fn: Callable[[], bool] = abort_requested_fn
self.request_abort_fn: Callable[[], None] = request_abort_fn
self.NZ = acquisition_parameters.NZ
self.deltaZ = acquisition_parameters.deltaZ
self.Nt = acquisition_parameters.Nt
self.dt = acquisition_parameters.deltat
self.do_autofocus = acquisition_parameters.do_autofocus
self.do_reflection_af = acquisition_parameters.do_reflection_autofocus
self.use_piezo = acquisition_parameters.use_piezo
self.display_resolution_scaling = acquisition_parameters.display_resolution_scaling
self.experiment_ID = acquisition_parameters.experiment_ID
self.base_path = acquisition_parameters.base_path
self.experiment_path = os.path.join(self.base_path or "", self.experiment_ID or "")
self.selected_configurations = acquisition_parameters.selected_configurations
# Pre-compute acquisition metadata that remains constant throughout the run.
try:
pixel_factor = self.objectiveStore.get_pixel_size_factor()
sensor_pixel_um = self.camera.get_pixel_size_binned_um()
if pixel_factor is not None and sensor_pixel_um is not None:
self._pixel_size_um = float(pixel_factor) * float(sensor_pixel_um)
else:
self._pixel_size_um = None
except Exception:
self._pixel_size_um = None
self._time_increment_s = self.dt if self.Nt > 1 and self.dt > 0 else None
self._physical_size_z_um = self.deltaZ if self.NZ > 1 else None
self.timestamp_acquisition_started = acquisition_parameters.acquisition_start_time
self.acquisition_info = AcquisitionInfo(
total_time_points=self.Nt,
total_z_levels=self.NZ,
total_channels=len(self.selected_configurations),
channel_names=[cfg.name for cfg in self.selected_configurations],
experiment_path=self.experiment_path,
time_increment_s=self._time_increment_s,
physical_size_z_um=self._physical_size_z_um,
physical_size_x_um=self._pixel_size_um,
physical_size_y_um=self._pixel_size_um,
)
self.time_point = 0
self.af_fov_count = 0
self.num_fovs = 0
self.total_scans = 0
self._last_time_point_z_pos = {}
self.scan_region_fov_coords_mm = (
acquisition_parameters.scan_position_information.scan_region_fov_coords_mm.copy()
)
self.scan_region_coords_mm = acquisition_parameters.scan_position_information.scan_region_coords_mm
self.scan_region_names = acquisition_parameters.scan_position_information.scan_region_names
self.z_stacking_config = acquisition_parameters.z_stacking_config # default 'from bottom'
self.z_range = acquisition_parameters.z_range
self.crop = SEGMENTATION_CROP
self.t_dpc = []
self.t_inf = []
self.t_over = []
self.count = 0
self.merged_image = None
self.image_count = 0
# This is for keeping track of whether or not we have the last image we tried to capture.
# NOTE(imo): Once we do overlapping triggering, we'll want to keep a queue of images we are expecting.
# For now, this is an improvement over blocking immediately while waiting for the next image!
self._ready_for_next_trigger = threading.Event()
# Set this to true so that the first frame capture can proceed.
self._ready_for_next_trigger.set()
# This is cleared when the image callback is no longer processing an image. If true, an image is still
# in flux and we need to make sure the object doesn't disappear.
self._image_callback_idle = threading.Event()
self._image_callback_idle.set()
# This is protected by the threading event above (aka set after clear, take copy before set)
self._current_capture_info: Optional[CaptureInfo] = None
# This is only touched via the image callback path. Don't touch it outside of there!
self._current_round_images = {}
self.skip_saving = acquisition_parameters.skip_saving
job_classes = []
use_ome_tiff = FILE_SAVING_OPTION == FileSavingOption.OME_TIFF
use_zarr_v3 = FILE_SAVING_OPTION == FileSavingOption.ZARR_V3
if not self.skip_saving:
if use_ome_tiff:
job_classes.append(SaveOMETiffJob)
elif use_zarr_v3:
job_classes.append(SaveZarrJob)
else:
job_classes.append(SaveImageJob)
if extra_job_classes:
job_classes.extend(extra_job_classes)
# Downsampled view generation setup
# Only generate downsampled views for well-based acquisitions
is_select_wells = acquisition_parameters.xy_mode == "Select Wells"
is_loaded_wells = acquisition_parameters.xy_mode == "Load Coordinates" and self._is_well_based_acquisition()
self._generate_downsampled_views = acquisition_parameters.generate_downsampled_views and (
is_select_wells or is_loaded_wells
)
self._downsampled_view_manager: Optional[DownsampledViewManager] = None
self._downsampled_well_resolutions_um = acquisition_parameters.downsampled_well_resolutions_um or [
5.0,
10.0,
20.0,
]
self._downsampled_plate_resolution_um = acquisition_parameters.downsampled_plate_resolution_um
self._downsampled_z_projection = acquisition_parameters.downsampled_z_projection
self._downsampled_interpolation_method = acquisition_parameters.downsampled_interpolation_method
self._save_downsampled_well_images = acquisition_parameters.save_downsampled_well_images
self._plate_num_rows = acquisition_parameters.plate_num_rows
self._plate_num_cols = acquisition_parameters.plate_num_cols
self._overlap_pixels: Optional[Tuple[int, int, int, int]] = None
self._region_fov_counts: Dict[str, int] = {} # Track total FOVs per region
if self._generate_downsampled_views:
# Ensure plate resolution is in well resolutions
self._downsampled_well_resolutions_um = ensure_plate_resolution_in_well_resolutions(
self._downsampled_well_resolutions_um,
self._downsampled_plate_resolution_um,
)
# Add DownsampledViewJob to job classes
job_classes.append(DownsampledViewJob)
# Pre-calculate FOV counts per region
for region_id, coords in self.scan_region_fov_coords_mm.items():
self._region_fov_counts[region_id] = len(coords)
mode = "Select Wells" if is_select_wells else "Load Coordinates (auto-detected)"
self._log.info(
f"Downsampled view generation enabled ({mode}). Resolutions: {self._downsampled_well_resolutions_um} um"
)
# Initialize backpressure controller for throttling acquisition when queue fills up.
# If pre-warmed values are provided, use them for consistent tracking with the
# pre-warmed job runner. Otherwise, BackpressureController creates its own values.
bp_kwargs = {
"max_jobs": control._def.ACQUISITION_MAX_PENDING_JOBS,
"max_mb": control._def.ACQUISITION_MAX_PENDING_MB,
"timeout_s": control._def.ACQUISITION_THROTTLE_TIMEOUT_S,
"enabled": control._def.ACQUISITION_THROTTLING_ENABLED,
}
if prewarmed_bp_values is not None:
bp_kwargs["bp_values"] = prewarmed_bp_values
self._backpressure = BackpressureController(**bp_kwargs)
# For now, use 1 runner per job class. There's no real reason/rationale behind this, though. The runners
# can all run any job type. But 1 per is a reasonable arbitrary arrangement while we don't have a lot
# of job types. If we have a lot of custom jobs, this could cause problems via resource hogging.
self._job_runners: List[Tuple[Type[Job], JobRunner]] = []
self._log.info(f"Acquisition.USE_MULTIPROCESSING = {Acquisition.USE_MULTIPROCESSING}")
# Get the current log file path to share with subprocess workers
log_file_path = squid.logging.get_current_log_file_path()
# Build ZarrWriterInfo if using ZARR_V3 format
# Output structure depends on acquisition type and settings:
# - HCS (wells): {experiment_path}/plate.ome.zarr/{row}/{col}/{fov}/0 (5D per FOV, OME-NGFF compliant)
# - Non-HCS default: {experiment_path}/zarr/{region}/fov_{n}.ome.zarr (5D per FOV, OME-NGFF compliant)
# - Non-HCS 6D: {experiment_path}/zarr/{region}/acquisition.zarr (6D, non-standard)
zarr_writer_info = None
if use_zarr_v3:
# Detect HCS mode using well-based acquisition state.
# is_loaded_wells already reflects the result of _is_well_based_acquisition(),
# so we only need to combine it with is_select_wells here.
is_hcs = is_select_wells or is_loaded_wells
# Pre-compute FOV counts per region (needed for 6D shape calculation in non-HCS mode)
region_fov_counts = {}
for region_id, coords in self.scan_region_fov_coords_mm.items():
region_fov_counts[str(region_id)] = len(coords)
# Extract channel metadata for zarr output
channel_names = [cfg.name for cfg in self.selected_configurations]
channel_colors = [cfg.display_color for cfg in self.selected_configurations]
# Get wavelengths from illumination config
channel_wavelengths = []
illumination_config = self.microscope.config_repo.get_illumination_config()
for cfg in self.selected_configurations:
wavelength = cfg.get_illumination_wavelength(illumination_config) if illumination_config else None
channel_wavelengths.append(wavelength)
zarr_writer_info = ZarrWriterInfo(
base_path=self.experiment_path,
t_size=self.Nt,
c_size=len(self.selected_configurations),
z_size=self.NZ,
is_hcs=is_hcs,
use_6d_fov=control._def.ZARR_USE_6D_FOV_DIMENSION,
region_fov_counts=region_fov_counts,
pixel_size_um=self._pixel_size_um,
z_step_um=self._physical_size_z_um,
time_increment_s=self._time_increment_s,
channel_names=channel_names,
channel_colors=channel_colors,
channel_wavelengths=channel_wavelengths,
)
if is_hcs:
mode_str = "HCS plate hierarchy"
elif control._def.ZARR_USE_6D_FOV_DIMENSION:
mode_str = "per-region 6D (non-standard)"
else:
mode_str = "per-FOV 5D (OME-NGFF compliant)"
self._log.info(f"ZARR_V3 output: {mode_str}, base path: {self.experiment_path}")
# Use pre-warmed job runner if available, otherwise create new ones.
# IMPORTANT: Only use pre-warmed runner if BOTH runner AND backpressure values
# are available. Using a runner without matching backpressure values would cause
# the BackpressureController to track different counters than the JobRunner.
can_use_prewarmed = prewarmed_job_runner is not None and prewarmed_bp_values is not None
used_prewarmed = False
for job_class in job_classes:
job_runner = None
if Acquisition.USE_MULTIPROCESSING:
# Try to use pre-warmed runner for the first job class
if can_use_prewarmed and not used_prewarmed:
if prewarmed_job_runner.is_ready():
self._log.info(f"Using pre-warmed job runner for {job_class.__name__} jobs")
job_runner = prewarmed_job_runner
# Configure it with current acquisition settings
job_runner.set_acquisition_info(self.acquisition_info)
if zarr_writer_info:
job_runner.set_zarr_writer_info(zarr_writer_info)
used_prewarmed = True
else:
self._log.warning(
f"Pre-warmed job runner not ready (possibly hung during warmup), "
f"shutting it down and creating new one for {job_class.__name__}"
)
# Shutdown the hung pre-warmed runner to avoid resource leak
try:
prewarmed_job_runner.shutdown(timeout_s=1.0)
except Exception as e:
self._log.error(f"Error shutting down hung pre-warmed runner: {e}")
# Don't try to use pre-warmed runner again for subsequent job classes
can_use_prewarmed = False
if job_runner is None:
self._log.info(f"Creating job runner for {job_class.__name__} jobs")
job_runner = control.core.job_processing.JobRunner(
self.acquisition_info,
cleanup_stale_ome_files=use_ome_tiff,
log_file_path=log_file_path,
# Pass backpressure shared values for cross-process tracking
bp_pending_jobs=self._backpressure.pending_jobs_value,
bp_pending_bytes=self._backpressure.pending_bytes_value,
bp_capacity_event=self._backpressure.capacity_event,
# Pass zarr writer info for ZARR_V3 format
zarr_writer_info=zarr_writer_info,
)
job_runner.start()
# Subprocess starts warming up in background - don't block here
self._job_runners.append((job_class, job_runner))
self._abort_on_failed_job = abort_on_failed_jobs
self._first_job_dispatched = False # Track if we've waited for subprocess warmup
def update_use_piezo(self, value):
self.use_piezo = value
self._log.info(f"MultiPointWorker: updated use_piezo to {value}")
def _is_well_based_acquisition(self) -> bool:
"""Check if regions represent a valid well-based acquisition.
Returns True if:
- All region names are valid well IDs (A1, B2, etc.)
- All regions have the same FOV grid pattern (same distinct X and Y counts)
"""
if not self.scan_region_names:
self._log.debug(
"_is_well_based_acquisition: no scan_region_names defined; treating as non well-based acquisition"
)
return False
# Check all region names are valid well IDs using parse_well_id
for region_id in self.scan_region_names:
if not region_id:
self._log.debug(
"_is_well_based_acquisition: encountered empty region_id in scan_region_names; "
"treating as invalid well-based acquisition"
)
return False
try:
parse_well_id(region_id)
except ValueError as exc:
self._log.debug(
"_is_well_based_acquisition: region_id '%s' is not a valid well ID: %s; "
"treating as invalid well-based acquisition",
region_id,
exc,
)
return False
# Check all wells have same grid size
grid_sizes = set()
for region_id, coords in self.scan_region_fov_coords_mm.items():
if not coords:
self._log.debug(
"_is_well_based_acquisition: region '%s' has no FOV coordinates; skipping in grid-size check",
region_id,
)
continue
x_positions = set(round(c[0], 4) for c in coords) # Round to avoid float precision issues
y_positions = set(round(c[1], 4) for c in coords)
grid_sizes.add((len(x_positions), len(y_positions)))
# All wells should have the same grid pattern
if not grid_sizes:
self._log.debug(
"_is_well_based_acquisition: no valid FOV coordinates found for any region; "
"treating as non well-based acquisition"
)
return False
if len(grid_sizes) > 1:
self._log.debug(
"_is_well_based_acquisition: inconsistent FOV grid sizes detected across wells: %s; "
"treating as non well-based acquisition",
grid_sizes,
)
return False
self._log.debug(
"_is_well_based_acquisition: valid well-based acquisition detected with grid size %s",
next(iter(grid_sizes)),
)
return True
def run(self):
this_image_callback_id = None
try:
start_time = time.perf_counter_ns()
self.camera.start_streaming()
this_image_callback_id = self.camera.add_frame_callback(self._image_callback)
sleep_time = min(self.dt / 20.0, 0.5)
# Send Slack acquisition start notification
if self._slack_notifier is not None:
try:
self._slack_notifier.notify_acquisition_start(
experiment_id=self.experiment_ID or "unknown",
num_regions=len(self.scan_region_names) if self.scan_region_names else 0,
num_timepoints=self.Nt,
num_channels=len(self.selected_configurations) if self.selected_configurations else 0,
num_z_levels=self.NZ,
)
except Exception as e:
self._log.warning(f"Failed to send Slack acquisition start notification: {e}")
while self.time_point < self.Nt:
# check if abort acquisition has been requested
if self.abort_requested_fn():
self._log.debug("In run, abort_acquisition_requested=True")
break
if self.fluidics and self.use_fluidics:
self.fluidics.update_port(self.time_point) # use the port in PORT_LIST
# For MERFISH, before imaging, run the first 3 sequences (Add probe, wash buffer, imaging buffer)
self.fluidics.run_before_imaging()
self.fluidics.wait_for_completion()
# Check for abort after fluidics completes (user may have stopped during fluidics)
if self.abort_requested_fn():
self._log.debug("Abort requested after fluidics, skipping imaging")
break
with self._timing.get_timer("run_single_time_point"):
self.run_single_time_point()
if self.fluidics and self.use_fluidics:
# For MERFISH, after imaging, run the following 2 sequences (Cleavage buffer, SSC rinse)
self.fluidics.run_after_imaging()
self.fluidics.wait_for_completion()
self.time_point = self.time_point + 1
if self.dt == 0: # continous acquisition
pass
else: # timed acquisition
# check if the aquisition has taken longer than dt or integer multiples of dt, if so skip the next time point(s)
while time.time() > self.timestamp_acquisition_started + self.time_point * self.dt:
self._log.info("skip time point " + str(self.time_point + 1))
self.time_point = self.time_point + 1
# check if it has reached Nt
if self.time_point == self.Nt:
break # no waiting after taking the last time point
# wait until it's time to do the next acquisition
while time.time() < self.timestamp_acquisition_started + self.time_point * self.dt:
if self.abort_requested_fn():
self._log.debug("In run wait loop, abort_acquisition_requested=True")
break
self._sleep(sleep_time)
elapsed_time = time.perf_counter_ns() - start_time
self._log.info("Time taken for acquisition: " + str(elapsed_time / 10**9))
# Since we use callback based acquisition, make sure to wait for any final images to come in
self._wait_for_outstanding_callback_images()
self._log.info(f"Time taken for acquisition/processing: {(time.perf_counter_ns() - start_time) / 1e9} [s]")
except TimeoutError as te:
self._log.error(f"Operation timed out during acquisition, aborting acquisition!")
self._log.error(te)
self.request_abort_fn()
except Exception as e:
self._log.exception(e)
raise
finally:
# We do this above, but there are some paths that skip the proper end of the acquisition so make
# sure to always wait for final images here before removing our callback.
self._wait_for_outstanding_callback_images()
self._log.debug(self._timing.get_report())
if this_image_callback_id:
self.camera.remove_frame_callback(this_image_callback_id)
self._finish_jobs()
# Send Slack acquisition finished notification via callback (ensures ordering with timepoint notifications)
if self._slack_notifier is not None:
try:
total_duration = time.time() - self.timestamp_acquisition_started
stats = AcquisitionStats(
total_images=self.image_count,
total_timepoints=self.time_point,
total_duration_seconds=total_duration,
errors_encountered=self._acquisition_error_count,
experiment_id=self.experiment_ID or "unknown",
)
self.callbacks.signal_slack_acquisition_finished(stats)
except Exception as e:
self._log.warning(f"Failed to send Slack acquisition finished notification: {e}")
self.callbacks.signal_acquisition_finished()
def _wait_for_outstanding_callback_images(self):
# If there are outstanding frames, wait for them to come in.
self._log.info("Waiting for any outstanding frames.")
if not self._ready_for_next_trigger.wait(self._frame_wait_timeout_s()):
self._log.warning("Timed out waiting for the last outstanding frames at end of acquisition!")
if not self._image_callback_idle.wait(self._frame_wait_timeout_s()):
self._log.warning("Timed out waiting for the last image to process!")
# No matter what, set the flags so things can continue
self._ready_for_next_trigger.set()
self._image_callback_idle.set()
def _finish_jobs(self, timeout_s=10):
# Drain and summarize all currently available job results before waiting for completion
self._summarize_runner_outputs(drain_all=True)
active_runners = [
(job_class, job_runner) for job_class, job_runner in self._job_runners if job_runner is not None
]
self._log.info(f"Waiting for jobs to finish on {len(active_runners)} job runners before shutting them down...")
timeout_time = time.time() + timeout_s
def timed_out():
return time.time() > timeout_time
def time_left():
return max(timeout_time - time.time(), 0)
# Wait for all pending jobs across all runners (round-robin to avoid blocking on one)
while not timed_out():
any_pending = False
for job_class, job_runner in active_runners:
if job_runner.has_pending():
any_pending = True
break
if not any_pending:
break
# Process any available results while waiting
self._summarize_runner_outputs(drain_all=True)
time.sleep(0.1)
else:
# Timed out - kill any runners that still have pending jobs
for job_class, job_runner in active_runners:
if job_runner.has_pending():
self._log.error(
f"Timed out after {timeout_s} [s] waiting for jobs to finish. Pending jobs for {job_class.__name__} abandoned!!!"
)
job_runner.kill()
# Drain results before shutdown
self._summarize_runner_outputs(drain_all=True)
# Shut down all job runners in parallel (in background to avoid blocking on subprocess termination).
# Using daemon threads is safe here because:
# 1. All jobs are complete and results are already drained
# 2. The subprocess termination is best-effort cleanup only
# 3. If app exits before threads complete, OS will terminate subprocesses anyway
# 4. This prevents slow subprocess termination from blocking acquisition completion
log = self._log # Capture for closure
def shutdown_runner(job_runner, timeout):
try:
job_runner.shutdown(timeout)
except Exception as e:
log.error(f"Error shutting down job runner in background: {e}")
self._log.info("Shutting down job runners (non-blocking)...")
remaining_time = time_left()
for job_class, job_runner in active_runners:
t = threading.Thread(target=shutdown_runner, args=(job_runner, remaining_time), daemon=True)
t.start()
# Final drain of all output queues (should be empty, but check anyway)
self._summarize_runner_outputs(drain_all=True)
# Release backpressure resources now that all jobs are complete
try:
self._backpressure.close()
except Exception as e:
self._log.error(f"Error closing backpressure controller: {e}")
def wait_till_operation_is_completed(self):
self.microcontroller.wait_till_operation_is_completed()
def run_single_time_point(self):
try:
start = time.time()
self._timepoint_start_time = start
self._timepoint_image_count = 0
self._timepoint_fov_count = 0
self._laser_af_successes = 0
self._laser_af_failures = 0
self.microcontroller.enable_joystick(False)
self._log.debug("multipoint acquisition - time point " + str(self.time_point + 1))
# for each time point, create a new folder
if self.experiment_path:
utils.ensure_directory_exists(str(self.experiment_path))
current_path = os.path.join(self.experiment_path, f"{self.time_point:0{FILE_ID_PADDING}}")
utils.ensure_directory_exists(str(current_path))
# create a dataframe to save coordinates
self.initialize_coordinates_dataframe()
# init z parameters, z range
self.initialize_z_stack()
with self._timing.get_timer("run_coordinate_acquisition"):
self.run_coordinate_acquisition(current_path)
# Save plate view for this timepoint
if self._generate_downsampled_views and self._downsampled_view_manager is not None:
# Wait for pending downsampled view jobs to complete
self._wait_for_downsampled_view_jobs()
# Save plate view
plate_resolution = int(self._downsampled_plate_resolution_um)
plate_view_path = os.path.join(current_path, "downsampled", f"plate_{plate_resolution}um.tiff")
self.save_plate_view(plate_view_path)
self._log.info(f"Saved plate view for timepoint {self.time_point} to {plate_view_path}")
# Clear plate view for next timepoint
self._downsampled_view_manager.clear()
# finished region scan
self.coordinates_pd.to_csv(os.path.join(current_path, "coordinates.csv"), index=False, header=True)
# Send Slack timepoint notification via callback (allows main thread to capture screenshot)
if self._slack_notifier is not None:
try:
elapsed = time.time() - self.timestamp_acquisition_started
timepoint_duration = time.time() - self._timepoint_start_time
self._slack_notifier.record_timepoint_duration(timepoint_duration)
estimated_remaining = self._slack_notifier.estimate_remaining_time(self.time_point + 1, self.Nt)
stats = TimepointStats(
timepoint=self.time_point + 1,
total_timepoints=self.Nt,
elapsed_seconds=elapsed,
estimated_remaining_seconds=estimated_remaining,
images_captured=self._timepoint_image_count,
fovs_captured=self._timepoint_fov_count,
laser_af_successes=self._laser_af_successes,
laser_af_failures=self._laser_af_failures,
laser_af_failure_reasons=[],
)
# Use callback to allow main thread to capture screenshot before sending
self.callbacks.signal_slack_timepoint_notification(stats)
except Exception as e:
self._log.warning(f"Failed to send Slack timepoint notification: {e}")
utils.create_done_file(current_path)
self._log.debug(f"Single time point took: {time.time() - start} [s]")
finally:
self.microcontroller.enable_joystick(True)
_VALID_Z_STACKING_CONFIGS = set(Z_STACKING_CONFIG_MAP.values())
def initialize_z_stack(self):
if self.z_stacking_config not in self._VALID_Z_STACKING_CONFIGS:
self._log.error(
f"Invalid z_stacking_config: '{self.z_stacking_config}'. "
f"Valid values: {self._VALID_Z_STACKING_CONFIGS}. Defaulting to 'FROM BOTTOM'."
)
self.z_stacking_config = "FROM BOTTOM"
if self.z_stacking_config == "FROM TOP":
self.deltaZ = -abs(self.deltaZ)
self.move_to_z_level(self.z_range[1])
elif self.z_stacking_config == "FROM CENTER":
# Move to center of z-range so autofocus runs at the correct Z level
center_z = (self.z_range[0] + self.z_range[1]) / 2
self.move_to_z_level(center_z)
else:
self.move_to_z_level(self.z_range[0])
self.z_pos = self.stage.get_pos().z_mm # zpos at the beginning of the scan
def initialize_coordinates_dataframe(self):
base_columns = ["z_level", "x (mm)", "y (mm)", "z (um)", "time"]
piezo_column = ["z_piezo (um)"] if self.use_piezo else []
self.coordinates_pd = pd.DataFrame(columns=["region", "fov"] + base_columns + piezo_column)
def update_coordinates_dataframe(self, region_id, z_level, pos: squid.abc.Pos, fov=None):
base_data = {
"z_level": [z_level],
"x (mm)": [pos.x_mm],
"y (mm)": [pos.y_mm],
"z (um)": [pos.z_mm * 1000],
"time": [datetime.now().strftime("%Y-%m-%d_%H-%M-%S.%f")],
}
piezo_data = {"z_piezo (um)": [self.z_piezo_um]} if self.use_piezo else {}
new_row = pd.DataFrame({"region": [region_id], "fov": [fov], **base_data, **piezo_data})
self.coordinates_pd = pd.concat([self.coordinates_pd, new_row], ignore_index=True)
def move_to_coordinate(self, coordinate_mm, region_id, fov):
x_mm = coordinate_mm[0]
y_mm = coordinate_mm[1]
if self._alignment_widget is not None and self._alignment_widget.has_offset:
x_mm, y_mm = self._alignment_widget.apply_offset(x_mm, y_mm)
self._log.info(
f"moving to coordinate ({x_mm:.4f}, {y_mm:.4f}) "
f"[original: ({coordinate_mm[0]:.4f}, {coordinate_mm[1]:.4f}), offset applied]"
)
else:
self._log.info(f"moving to coordinate {coordinate_mm}")
self.stage.move_x_to(x_mm)
self._sleep(SCAN_STABILIZATION_TIME_MS_X / 1000)
self.stage.move_y_to(y_mm)
self._sleep(SCAN_STABILIZATION_TIME_MS_Y / 1000)
# check if z is included in the coordinate
if (self.do_reflection_af or self.do_autofocus) and self.time_point > 0:
if (region_id, fov) in self._last_time_point_z_pos:
last_z_mm = self._last_time_point_z_pos[(region_id, fov)]
self.move_to_z_level(last_z_mm)
self._log.info(f"Moved to last z position {last_z_mm} [mm]")
return
else:
self._log.warning(f"No last z position found for region {region_id}, fov {fov}")
if len(coordinate_mm) == 3:
z_mm = coordinate_mm[2]
self.move_to_z_level(z_mm)
def move_to_z_level(self, z_mm):
self._log.debug("moving z")
self.stage.move_z_to(z_mm)
self._sleep(SCAN_STABILIZATION_TIME_MS_Z / 1000)
def _summarize_runner_outputs(self, drain_all: bool = False) -> SummarizeResult:
"""Process job results from output queues.
Args:
drain_all: If True, process ALL available results. If False, process at most one per queue.
Returns:
SummarizeResult with none_failed and had_results.
"""
none_failed = True
had_results = False
for job_class, job_runner in self._job_runners:
if job_runner is None:
continue
out_queue = job_runner.output_queue()
if out_queue is None:
# Queue was cleared during shutdown
continue
while True:
try:
job_result: JobResult = out_queue.get_nowait()
none_failed = none_failed and self._summarize_job_result(job_result)
had_results = True
if not drain_all:
break # Only process one result per queue if not draining
except queue.Empty:
break
except ValueError:
# Queue was closed during shutdown - nothing more to drain
break
return SummarizeResult(none_failed=none_failed, had_results=had_results)
def _summarize_job_result(self, job_result: JobResult) -> bool:
"""
Prints a summary, then returns True if the result was successful or False otherwise.
"""
if job_result.exception is not None:
self._log.error(f"Error while running job {job_result.job_id}: {job_result.exception}")
self._acquisition_error_count += 1
# Send Slack error notification
if self._slack_notifier is not None:
try:
context = {"job_id": job_result.job_id}
self._slack_notifier.notify_error(
str(job_result.exception),
context,
)
except Exception as e:
self._log.warning(f"Failed to send Slack error notification: {e}")
return False
else:
self._log.info(f"Got result for job {job_result.job_id}, it completed!")
# Handle DownsampledViewResult - update plate view
if isinstance(job_result.result, DownsampledViewResult) and job_result.result.well_images:
self._handle_downsampled_view_result(job_result.result)
# Handle ZarrWriteResult - notify viewer that frame is written
elif isinstance(job_result.result, ZarrWriteResult):
r = job_result.result
self.callbacks.signal_zarr_frame_written(r.fov, r.time_point, r.z_index, r.channel_name, r.region_idx)
return True
def _handle_downsampled_view_result(self, result: DownsampledViewResult) -> None:
"""Update plate view with completed well image."""
t_start = time.perf_counter()
if self._downsampled_view_manager is None:
return
try:
self._downsampled_view_manager.update_well(
result.well_row,
result.well_col,
result.well_images,
)
t_update = time.perf_counter()
self._log.info(
f"Updated plate view for well {result.well_id} at ({result.well_row}, {result.well_col}) "
f"with {len(result.well_images)} channels"
)
# Emit plate view update for each channel
for ch_idx, plate_image in enumerate(self._downsampled_view_manager.plate_view):
channel_name = (
self._downsampled_view_manager.channel_names[ch_idx]
if ch_idx < len(self._downsampled_view_manager.channel_names)
else f"Channel_{ch_idx}"
)
self.callbacks.signal_plate_view_update(
PlateViewUpdate(
channel_idx=ch_idx,
channel_name=channel_name,
plate_image=plate_image.copy(),
)
)
t_signal = time.perf_counter()
self._log.debug(
f"[PERF] _handle_downsampled_view_result {result.well_id}: "
f"update_well={t_update - t_start:.3f}s, signals={t_signal - t_update:.3f}s, "
f"TOTAL={t_signal - t_start:.3f}s"
)
except Exception as e:
self._log.exception(
f"Failed to update plate view for well {result.well_id} "
f"at ({result.well_row}, {result.well_col}): {e}"
)
def _create_job(self, job_class: Type[Job], info: CaptureInfo, image: np.ndarray) -> Optional[Job]:
"""Create a job instance for the given job class.
Returns None if the job should be skipped.
"""
if job_class == DownsampledViewJob:
return self._create_downsampled_view_job(info, image)
else:
return job_class(capture_info=info, capture_image=JobImage(image_array=image))
def _create_downsampled_view_job(self, info: CaptureInfo, image: np.ndarray) -> Optional[DownsampledViewJob]:
"""Create a DownsampledViewJob for the given capture.
Returns None if downsampled views are disabled or not applicable.
"""
if not self._generate_downsampled_views:
return None
# Calculate overlap first (needed for plate view manager initialization)
if self._overlap_pixels is None:
self._calculate_overlap_pixels(image)
# Initialize plate view manager on first image (we need image dimensions)
if self._downsampled_view_manager is None:
self._initialize_downsampled_view_manager(image)
# Get well info from region_id
region_id = str(info.region_id)
try:
well_row, well_col = parse_well_id(region_id)
except (ValueError, IndexError):
# Region ID is not a valid well ID (e.g., "R0", "manual")
# Region ID is not a valid well ID (e.g., "R0", "manual", custom names).
# Use region index as a fallback. This is expected for non-plate acquisitions.
self._log.debug(f"Region {region_id} is not a well ID, using fallback positioning")
if not self._plate_num_rows or not self._plate_num_cols:
self._log.warning(
f"Plate dimensions not set (rows={self._plate_num_rows}, cols={self._plate_num_cols}); "
"using (0, 0) for well position"
)
well_row, well_col = 0, 0
else:
region_idx = self.scan_region_names.index(region_id) if region_id in self.scan_region_names else 0
well_row = region_idx // self._plate_num_cols
well_col = region_idx % self._plate_num_cols
# Warn if region index exceeds plate capacity (data will be overwritten)
max_slots = self._plate_num_rows * self._plate_num_cols
if region_idx >= max_slots:
self._log.warning(
f"Region index {region_idx} exceeds plate capacity ({max_slots} slots); "
f"well position will be clamped and may overwrite existing data"
)
# Clamp to plate bounds
well_row = min(well_row, self._plate_num_rows - 1)
well_col = min(well_col, self._plate_num_cols - 1)
# Get FOV position within well
total_fovs = self._region_fov_counts.get(region_id, 1)
fov_index = info.fov
# Get the first FOV position for this region to calculate relative position
region_coords = self.scan_region_fov_coords_mm.get(region_id, [])
if region_coords and fov_index < len(region_coords):
first_fov = region_coords[0]
current_fov = region_coords[fov_index]
# Relative position in mm from first FOV
fov_position = (current_fov[0] - first_fov[0], current_fov[1] - first_fov[1])
else:
fov_position = (0.0, 0.0)
# Determine output directory
output_dir = os.path.join(self.experiment_path, str(self.time_point), "downsampled")
# Get channel info
channel_idx = info.configuration_idx
total_channels = len(self.selected_configurations)
channel_name = info.configuration.name if info.configuration else f"Channel_{channel_idx}"
channel_names = [cfg.name for cfg in self.selected_configurations]
return DownsampledViewJob(
capture_info=info,
capture_image=JobImage(image_array=image),
well_id=region_id,
well_row=well_row,
well_col=well_col,
fov_index=fov_index,
total_fovs_in_well=total_fovs,
channel_idx=channel_idx,
total_channels=total_channels,
channel_name=channel_name,
fov_position_in_well=fov_position,
overlap_pixels=self._overlap_pixels,
pixel_size_um=self._pixel_size_um or 1.0,
target_resolutions_um=self._downsampled_well_resolutions_um,
plate_resolution_um=self._downsampled_plate_resolution_um,
output_dir=output_dir,
channel_names=channel_names,
z_index=info.z_index,
total_z_levels=self.NZ,
z_projection_mode=self._downsampled_z_projection,
interpolation_method=self._downsampled_interpolation_method,
skip_saving=self.skip_saving
or not self._save_downsampled_well_images
or control._def.SIMULATED_DISK_IO_ENABLED,
)