From c84533148a9cfe7f622dbe689695f8ec99cead42 Mon Sep 17 00:00:00 2001 From: Hongquan Li Date: Sat, 22 Feb 2025 22:54:25 -0800 Subject: [PATCH] move functions to avoid circular import --- software/control/core/core.py | 7 ++++--- software/control/utils.py | 17 ----------------- software/control/utils_channel.py | 18 ++++++++++++++++++ software/control/utils_config.py | 5 ++--- 4 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 software/control/utils_channel.py diff --git a/software/control/core/core.py b/software/control/core/core.py index b1ae4b9d..b1857a4e 100644 --- a/software/control/core/core.py +++ b/software/control/core/core.py @@ -24,6 +24,7 @@ from control.multipoint_built_in_functionalities import malaria_rtp import control.utils as utils +import control.utils_channel as utils_channel import control.utils_config as utils_config import control.tracking as tracking import control.serial_peripherals as serial_peripherals @@ -499,7 +500,7 @@ def __init__( def turn_on_illumination(self): if self.illuminationController is not None and not "LED matrix" in self.currentConfiguration.name: self.illuminationController.turn_on_illumination( - int(utils.extract_wavelength_from_config_name(self.currentConfiguration.name)) + int(utils_channel.extract_wavelength_from_config_name(self.currentConfiguration.name)) ) elif SUPPORT_SCIMICROSCOPY_LED_ARRAY and "LED matrix" in self.currentConfiguration.name: self.led_array.turn_on_illumination() @@ -510,7 +511,7 @@ def turn_on_illumination(self): def turn_off_illumination(self): if self.illuminationController is not None and not "LED matrix" in self.currentConfiguration.name: self.illuminationController.turn_off_illumination( - int(utils.extract_wavelength_from_config_name(self.currentConfiguration.name)) + int(utils_channel.extract_wavelength_from_config_name(self.currentConfiguration.name)) ) elif SUPPORT_SCIMICROSCOPY_LED_ARRAY and "LED matrix" in self.currentConfiguration.name: self.led_array.turn_off_illumination() @@ -563,7 +564,7 @@ def set_illumination(self, illumination_source, intensity, update_channel_settin # update illumination if self.illuminationController is not None: self.illuminationController.set_intensity( - int(utils.extract_wavelength_from_config_name(self.currentConfiguration.name)), intensity + int(utils_channel.extract_wavelength_from_config_name(self.currentConfiguration.name)), intensity ) elif ENABLE_NL5 and NL5_USE_DOUT and "Fluorescence" in self.currentConfiguration.name: wavelength = int(self.currentConfiguration.name[13:16]) diff --git a/software/control/utils.py b/software/control/utils.py index e7a6ff57..087c7350 100644 --- a/software/control/utils.py +++ b/software/control/utils.py @@ -11,7 +11,6 @@ from scipy.ndimage import label from scipy import signal import os -from control._def import CHANNEL_COLORS_MAP from typing import Optional, Tuple from enum import Enum, auto import squid.logging @@ -175,22 +174,6 @@ def ensure_directory_exists(raw_string_path: str): path.mkdir(parents=True, exist_ok=True) -def extract_wavelength_from_config_name(name): - # Split the string and find the wavelength number immediately after "Fluorescence" - parts = name.split() - if "Fluorescence" in parts: - index = parts.index("Fluorescence") + 1 - if index < len(parts): - return parts[index].split()[0] # Assuming 'Fluorescence 488 nm Ex' and taking '488' - for color in ["R", "G", "B"]: - if color in parts or "full_" + color in parts: - return color - return None - - -def get_channel_color(channel): - channel_info = CHANNEL_COLORS_MAP.get(extract_wavelength_from_config_name(channel), {"hex": 0xFFFFFF, "name": "gray"}) - return channel_info["hex"] class SpotDetectionMode(Enum): """Specifies which spot to detect when multiple spots are present. diff --git a/software/control/utils_channel.py b/software/control/utils_channel.py new file mode 100644 index 00000000..f2cf350a --- /dev/null +++ b/software/control/utils_channel.py @@ -0,0 +1,18 @@ +from control._def import CHANNEL_COLORS_MAP + +def extract_wavelength_from_config_name(name): + # Split the string and find the wavelength number immediately after "Fluorescence" + parts = name.split() + if "Fluorescence" in parts: + index = parts.index("Fluorescence") + 1 + if index < len(parts): + return parts[index].split()[0] # Assuming 'Fluorescence 488 nm Ex' and taking '488' + for color in ["R", "G", "B"]: + if color in parts or "full_" + color in parts: + return color + return None + + +def get_channel_color(channel): + channel_info = CHANNEL_COLORS_MAP.get(extract_wavelength_from_config_name(channel), {"hex": 0xFFFFFF, "name": "gray"}) + return channel_info["hex"] \ No newline at end of file diff --git a/software/control/utils_config.py b/software/control/utils_config.py index 5cb0f161..91a88150 100644 --- a/software/control/utils_config.py +++ b/software/control/utils_config.py @@ -2,8 +2,7 @@ from pydantic_xml import BaseXmlModel, element, attr from typing import List, Optional from pathlib import Path -import control.utils as utils - +import control.utils_channel as utils_channel class LaserAFConfig(BaseModel): """Pydantic model for laser autofocus configuration""" @@ -34,7 +33,7 @@ class ChannelMode(BaseXmlModel, tag='mode'): def __init__(self, **data): super().__init__(**data) - self.color = utils.get_channel_color(self.name) + self.color = utils_channel.get_channel_color(self.name) class ChannelConfig(BaseXmlModel, tag='modes'): """Root configuration file model"""