Skip to content

Commit f77a27a

Browse files
committed
update channel configuration and laser af cache usages everywhere
1 parent 92f505f commit f77a27a

File tree

3 files changed

+128
-111
lines changed

3 files changed

+128
-111
lines changed

software/control/core/core.py

+52-41
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ def __init__(self, multiPointController):
13771377
self.liveController = self.multiPointController.liveController
13781378
self.autofocusController = self.multiPointController.autofocusController
13791379
self.objectiveStore = self.multiPointController.objectiveStore
1380-
self.acquisitionConfigurationManager = self.multiPointController.acquisitionConfigurationManager
1380+
self.channelConfigurationManager = self.multiPointController.channelConfigurationManager
13811381
self.NX = self.multiPointController.NX
13821382
self.NY = self.multiPointController.NY
13831383
self.NZ = self.multiPointController.NZ
@@ -1754,7 +1754,7 @@ def perform_autofocus(self, region_id, fov):
17541754
config_AF = next(
17551755
(
17561756
config
1757-
for config in self.acquisitionConfigurationManager.get_channel_configurations_for_objective(self.objectiveStore.current_objective)
1757+
for config in self.channelConfigurationManager.get_channel_configurations_for_objective(self.objectiveStore.current_objective)
17581758
if config.name == configuration_name_AF
17591759
)
17601760
)
@@ -1776,7 +1776,7 @@ def perform_autofocus(self, region_id, fov):
17761776
config_AF = next(
17771777
(
17781778
config
1779-
for config in self.acquisitionConfigurationManager.get_channel_configurations_for_objective(self.objectiveStore.current_objective)
1779+
for config in self.channelConfigurationManager.get_channel_configurations_for_objective(self.objectiveStore.current_objective)
17801780
if config.name == configuration_name_AF
17811781
)
17821782
)
@@ -1887,7 +1887,7 @@ def acquire_rgb_image(self, config, file_ID, current_path, current_round_images,
18871887
rgb_channels = ["BF LED matrix full_R", "BF LED matrix full_G", "BF LED matrix full_B"]
18881888
images = {}
18891889

1890-
for config_ in self.acquisitionConfigurationManager.get_channel_configurations_for_objective(self.objectiveStore.current_objective):
1890+
for config_ in self.channelConfigurationManager.get_channel_configurations_for_objective(self.objectiveStore.current_objective):
18911891
if config_.name in rgb_channels:
18921892
# update the current configuration
18931893
self.signal_current_configuration.emit(config_)
@@ -2172,7 +2172,7 @@ def __init__(
21722172
liveController,
21732173
autofocusController,
21742174
objectiveStore,
2175-
acquisitionConfigurationManager,
2175+
channelConfigurationManager,
21762176
usb_spectrometer=None,
21772177
scanCoordinates=None,
21782178
parent=None,
@@ -2187,7 +2187,7 @@ def __init__(
21872187
self.liveController = liveController
21882188
self.autofocusController = autofocusController
21892189
self.objectiveStore = objectiveStore,
2190-
self.acquisitionConfigurationManager = acquisitionConfigurationManager
2190+
self.channelConfigurationManager = channelConfigurationManager
21912191
self.multiPointWorker: Optional[MultiPointWorker] = None
21922192
self.thread: Optional[QThread] = None
21932193
self.NX = 1
@@ -2311,7 +2311,7 @@ def start_new_experiment(self, experiment_ID): # @@@ to do: change name to prep
23112311
self.recording_start_time = time.time()
23122312
# create a new folder
23132313
utils.ensure_directory_exists(os.path.join(self.base_path, self.experiment_ID))
2314-
self.acquisitionConfigurationManager.write_configuration_selected(
2314+
self.channelConfigurationManager.write_configuration_selected(
23152315
self.objectiveStore.current_objective, self.selected_configurations, os.path.join(self.base_path, self.experiment_ID) + "/configurations.xml"
23162316
) # save the configuration for the experiment
23172317
# Prepare acquisition parameters
@@ -2356,7 +2356,7 @@ def set_selected_configurations(self, selected_configurations_name):
23562356
self.selected_configurations.append(
23572357
next(
23582358
(config
2359-
for config in self.acquisitionConfigurationManager.get_channel_configurations_for_objective(self.objectiveStore.current_objective)
2359+
for config in self.channelConfigurationManager.get_channel_configurations_for_objective(self.objectiveStore.current_objective)
23602360
if config.name == configuration_name)
23612361
)
23622362
)
@@ -2630,7 +2630,7 @@ def __init__(
26302630
microcontroller: Microcontroller,
26312631
stage: AbstractStage,
26322632
objectiveStore,
2633-
acquisitionConfigurationManager,
2633+
channelConfigurationManager,
26342634
liveController: LiveController,
26352635
autofocusController,
26362636
imageDisplayWindow,
@@ -2640,7 +2640,7 @@ def __init__(
26402640
self.microcontroller = microcontroller
26412641
self.stage = stage
26422642
self.objectiveStore = objectiveStore
2643-
self.acquisitionConfigurationManager = acquisitionConfigurationManager
2643+
self.channelConfigurationManager = channelConfigurationManager
26442644
self.liveController = liveController
26452645
self.autofocusController = autofocusController
26462646
self.imageDisplayWindow = imageDisplayWindow
@@ -2746,7 +2746,8 @@ def start_new_experiment(self, experiment_ID): # @@@ to do: change name to prep
27462746
# create a new folder
27472747
try:
27482748
utils.ensure_directory_exists(os.path.join(self.base_path, self.experiment_ID))
2749-
self.acquisitionConfigurationManager.write_configuration(
2749+
self.channelConfigurationManager._save_xml_config(
2750+
self.objectiveStore.current_objective,
27502751
os.path.join(self.base_path, self.experiment_ID) + "/configurations.xml"
27512752
) # save the configuration for the experiment
27522753
except:
@@ -2759,7 +2760,7 @@ def set_selected_configurations(self, selected_configurations_name):
27592760
self.selected_configurations.append(
27602761
next((
27612762
config
2762-
for config in self.acquisitionConfigurationManager.get_channel_configurations_for_objective(self.objectiveStore.current_objective)
2763+
for config in self.channelConfigurationManager.get_channel_configurations_for_objective(self.objectiveStore.current_objective)
27632764
if config.name == configuration_name)
27642765
)
27652766
)
@@ -2860,7 +2861,7 @@ def __init__(self, trackingController: TrackingController):
28602861
self.microcontroller = self.trackingController.microcontroller
28612862
self.liveController = self.trackingController.liveController
28622863
self.autofocusController = self.trackingController.autofocusController
2863-
self.acquisitionConfigurationManager = self.trackingController.acquisitionConfigurationManager
2864+
self.channelConfigurationManager = self.trackingController.channelConfigurationManager
28642865
self.imageDisplayWindow = self.trackingController.imageDisplayWindow
28652866
self.crop_width = self.trackingController.crop_width
28662867
self.crop_height = self.trackingController.crop_height
@@ -3714,8 +3715,11 @@ def write_configuration_selected(self, objective: str, selected_configurations:
37143715
self._save_xml_config(objective, filename)
37153716
for conf in selected_configurations:
37163717
self.update_configuration(conf.id, "Selected", 0)
3718+
3719+
def get_channel_configurations_for_objective(self, objective: str) -> List[Configuration]:
3720+
return self.active_channel_config.get(objective, [])
37173721

3718-
def toggle_spinning_disk_mode(self, confocal: bool) -> None:
3722+
def toggle_confocal_widefield(self, confocal: bool) -> None:
37193723
"""Toggle between confocal and widefield configurations."""
37203724
if not ENABLE_SPINNING_DISK:
37213725
return
@@ -3731,7 +3735,7 @@ def toggle_spinning_disk_mode(self, confocal: bool) -> None:
37313735
self.active_config_xml_tree_root = self.widefield_config_xml_tree_root
37323736
self.active_config_flag = 2
37333737

3734-
class LaserAFConfigurationManager:
3738+
class LaserAFCacheManager:
37353739
"""Manages JSON-based laser autofocus configurations."""
37363740
def __init__(self):
37373741
self.autofocus_configurations = {} # Dict[str, Dict[str, Any]]
@@ -3758,21 +3762,24 @@ def save_configurations(self, objective: str) -> None:
37583762
with open(config_file, 'w') as f:
37593763
json.dump(self.autofocus_configurations[objective], f, indent=4)
37603764

3761-
def get_configurations(self, objective: str) -> Dict[str, Any]:
3765+
def get_cache_for_objective(self, objective: str) -> Dict[str, Any]:
37623766
return self.autofocus_configurations.get(objective, {})
3763-
3764-
def update_configuration(self, objective: str, updates: Dict[str, Any]) -> None:
3767+
3768+
def get_laser_af_cache(self) -> Dict[str, Any]:
3769+
return self.autofocus_configurations
3770+
3771+
def update_laser_af_cache(self, objective: str, updates: Dict[str, Any]) -> None:
37653772
if objective not in self.autofocus_configurations:
37663773
self.autofocus_configurations[objective] = {}
37673774
self.autofocus_configurations[objective].update(updates)
37683775

37693776
class ConfigurationManager(QObject):
37703777
"""Main configuration manager that coordinates channel and autofocus configurations."""
37713778
def __init__(self,
3772-
base_config_path: Path = Path("acquisition_configurations"),
3773-
profile: str = "default_profile",
37743779
channel_manager: ChannelConfigurationManager,
3775-
af_manager: Optional[LaserAFCacheManager] = None):
3780+
af_manager: Optional[LaserAFCacheManager] = None,
3781+
base_config_path: Path = Path("acquisition_configurations"),
3782+
profile: str = "default_profile"):
37763783
super().__init__()
37773784
self.base_config_path = Path(base_config_path)
37783785
self.current_profile = profile
@@ -4620,13 +4627,14 @@ def __init__(
46204627
camera,
46214628
stage: AbstractStage,
46224629
objectiveStore: Optional[ObjectiveStore] = None,
4623-
cachedLaserAFConfigurations: Optional[Dict[str, Any]] = None
4630+
laserAFCacheManager: Optional[LaserAFCacheManager] = None
46244631
):
46254632
QObject.__init__(self)
46264633
self.microcontroller = microcontroller
46274634
self.camera = camera
46284635
self.stage = stage
46294636
self.objectiveStore = objectiveStore
4637+
self.laserAFCacheManager = laserAFCacheManager
46304638

46314639
self.is_initialized = False
46324640
self.x_reference = 0
@@ -4644,8 +4652,8 @@ def __init__(
46444652
self.image = None # for saving the focus camera image for debugging when centroid cannot be found
46454653

46464654
# Load configurations if provided
4647-
self.laser_af_cache = cachedLaserAFConfigurations
4648-
if self.laser_af_cache is not None:
4655+
if self.laserAFCacheManager:
4656+
self.laser_af_cache = self.laserAFCacheManager.get_laser_af_cache()
46494657
self.load_cached_configuration()
46504658

46514659
def initialize_manual(self, x_offset, y_offset, width, height, pixel_to_um, x_reference,
@@ -4665,12 +4673,8 @@ def initialize_manual(self, x_offset, y_offset, width, height, pixel_to_um, x_re
46654673
self.is_initialized = True
46664674

46674675
# Update cache if objective store and laser_af_cache is available
4668-
if self.objectiveStore and self.laser_af_cache and self.objectiveStore.current_objective:
4669-
current_objective = self.objectiveStore.current_objective
4670-
if current_objective not in self.laser_af_cache:
4671-
self.laser_af_cache[current_objective] = {}
4672-
4673-
self.laser_af_cache[current_objective].update({
4676+
if self.objectiveStore and self.laserAFCacheManager and self.objectiveStore.current_objective:
4677+
self.laserAFCacheManager.update_laser_af_cache(self.objectiveStore.current_objective, {
46744678
'x_offset': x_offset,
46754679
'y_offset': y_offset,
46764680
'width': width,
@@ -4687,7 +4691,7 @@ def load_cached_configuration(self):
46874691
"""Load configuration from the cache if available."""
46884692
current_objective = self.objectiveStore.current_objective if self.objectiveStore else None
46894693
if current_objective and current_objective in self.laser_af_cache:
4690-
config = self.laser_af_cache[current_objective]
4694+
config = self.laserAFCacheManager.get_cache_for_objective(current_objective)
46914695

46924696
self.focus_camera_exposure_time_ms = config.get('focus_camera_exposure_time_ms', 2),
46934697
self.focus_camera_analog_gain = config.get('focus_camera_analog_gain', 0)
@@ -4739,6 +4743,8 @@ def initialize_auto(self):
47394743
# Calibrate pixel to um conversion
47404744
self._calibrate_pixel_to_um()
47414745

4746+
self.laserAFCacheManager.save_configurations(self.objectiveStore.current_objective)
4747+
47424748
def _calibrate_pixel_to_um(self):
47434749
"""Calibrate the pixel to micrometer conversion factor."""
47444750
self.microcontroller.turn_on_AF_laser()
@@ -4774,11 +4780,10 @@ def _calibrate_pixel_to_um(self):
47744780
# set reference
47754781
self.x_reference = x1
47764782

4777-
# Update cache if objective store and laser_af_cache is available
4778-
if self.objectiveStore and self.laser_af_cache and self.objectiveStore.current_objective:
4779-
current_objective = self.objectiveStore.current_objective
4780-
if current_objective in self.laser_af_cache:
4781-
self.laser_af_cache[current_objective]['pixel_to_um'] = self.pixel_to_um
4783+
# Update cache
4784+
self.laserAFCacheManager.update_laser_af_cache(self.objectiveStore.current_objective, {
4785+
'pixel_to_um': self.pixel_to_um
4786+
})
47824787

47834788
def set_laser_af_properties(self, has_two_interfaces, use_glass_top, focus_camera_exposure_time_ms, focus_camera_analog_gain):
47844789
# These properties can be set from gui
@@ -4787,6 +4792,8 @@ def set_laser_af_properties(self, has_two_interfaces, use_glass_top, focus_camer
47874792
self.focus_camera_exposure_time_ms = focus_camera_exposure_time_ms
47884793
self.focus_camera_analog_gain = focus_camera_analog_gain
47894794

4795+
self.is_initialized = False
4796+
47904797
def measure_displacement(self):
47914798
# turn on the laser
47924799
self.microcontroller.turn_on_AF_laser()
@@ -4830,11 +4837,15 @@ def set_reference(self):
48304837
self.x_reference = x
48314838
self.signal_displacement_um.emit(0)
48324839

4833-
# Update cache if objective store and laser_af_cache is available
4834-
if self.objectiveStore and self.laser_af_cache and self.objectiveStore.current_objective:
4835-
current_objective = self.objectiveStore.current_objective
4836-
if current_objective in self.laser_af_cache:
4837-
self.laser_af_cache[current_objective]['x_reference'] = x + self.x_offset
4840+
# Update cache
4841+
self.laserAFCacheManager.update_laser_af_cache(self.objectiveStore.current_objective, {
4842+
'x_reference': x + self.x_offset
4843+
})
4844+
self.laserAFCacheManager.save_configurations(self.objectiveStore.current_objective)
4845+
4846+
def on_objective_changed(self):
4847+
self.is_initialized = False
4848+
self.load_cached_configurations()
48384849

48394850
def _calculate_centroid(self, image):
48404851
"""Calculate the centroid of the laser spot."""

0 commit comments

Comments
 (0)