From 6607cd0c250080260c30a180b6c50e19b29557e1 Mon Sep 17 00:00:00 2001 From: Henry Pinkard <7969470+henrypinkard@users.noreply.github.com> Date: Fri, 14 Jun 2024 11:04:16 +0200 Subject: [PATCH] fix python image processor test --- .../acq_eng_py/main/AcqEngPy_Acquisition.py | 2 +- .../python_backend_acquisitions.py | 12 +++++++++-- pycromanager/install.py | 20 ++++++++++++++++++- pycromanager/test/conftest.py | 5 ++++- pycromanager/test/test_callback_functions.py | 4 ++-- 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/pycromanager/acquisition/acq_eng_py/main/AcqEngPy_Acquisition.py b/pycromanager/acquisition/acq_eng_py/main/AcqEngPy_Acquisition.py index 9f83b916..6bc6fa90 100644 --- a/pycromanager/acquisition/acq_eng_py/main/AcqEngPy_Acquisition.py +++ b/pycromanager/acquisition/acq_eng_py/main/AcqEngPy_Acquisition.py @@ -127,7 +127,7 @@ def saving_thread(acq): if acq.data_sink_: if acq.debug_mode_: acq.core_.log_message("Saving image") - if not img.pix and not img.tags: + if img.tags is None and img.pix is None: break acq.save_image(img) if acq.debug_mode_: diff --git a/pycromanager/acquisition/python_backend_acquisitions.py b/pycromanager/acquisition/python_backend_acquisitions.py index 690cf2f2..3be9b154 100644 --- a/pycromanager/acquisition/python_backend_acquisitions.py +++ b/pycromanager/acquisition/python_backend_acquisitions.py @@ -194,9 +194,17 @@ def _process(self): # this is a signal to stop self.output_queue.put(tagged_image) break - process_fn_result = self._pycromanager_acq._call_image_process_fn(tagged_image.tags, tagged_image.pix) + process_fn_result = self._pycromanager_acq._call_image_process_fn(tagged_image.pix, tagged_image.tags) + try: + self._pycromanager_acq._check_for_exceptions() + except Exception as e: + # unclear if this is functioning properly, check later + self._acq.abort() if process_fn_result is not None: - self.output_queue.put(process_fn_result) + # turn it into the expected tagged_image + # TODO: change this on later unification of acq engines + tagged_image.pix, tagged_image.tags = process_fn_result + self.output_queue.put(tagged_image) # otherwise the image processor intercepted the image and nothing to do here class AcquisitionHook: diff --git a/pycromanager/install.py b/pycromanager/install.py index e11e3d79..d860c4f0 100644 --- a/pycromanager/install.py +++ b/pycromanager/install.py @@ -44,6 +44,24 @@ def _find_versions(): raise ValueError(f"Unsupported OS: {platform}") return re.findall(r'class="rowDefault" href="([^"]+)', webpage.text) +def find_existing_mm_install(): + """ + Check if Micro-Manager is installed in the default auto-download paths + + Returns + ------- + str + The path to the installed Micro-Manager directory, or None if not found + """ + platform = _get_platform() + if platform == 'Windows': + if os.path.isdir(r'C:\Program Files\Micro-Manager'): + return r'C:\Program Files\Micro-Manager' + elif platform == 'Mac': + if os.path.isdir(str(os.path.expanduser('~')) + '/Micro-Manager'): + return str(os.path.expanduser('~')) + '/Micro-Manager' + else: + raise ValueError(f"Unsupported OS: {platform}") def download_and_install(destination='auto', mm_install_log_path=None): """ @@ -84,7 +102,7 @@ def bar(curr, total, width): return destination else: if destination == 'auto': - destination = os.path.expanduser('~') + '/Micro-Manager' + destination = str(os.path.expanduser('~')) + '/Micro-Manager' try: # unmount if already mounted subprocess.run(['hdiutil', 'detach', '/Volumes/Micro-Manager']) diff --git a/pycromanager/test/conftest.py b/pycromanager/test/conftest.py index 22657d46..716040eb 100644 --- a/pycromanager/test/conftest.py +++ b/pycromanager/test/conftest.py @@ -15,7 +15,7 @@ from pycromanager import start_headless from pycromanager.headless import stop_headless import socket -from pycromanager.install import download_and_install +from pycromanager.install import download_and_install, find_existing_mm_install def is_port_in_use(port): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: @@ -61,6 +61,9 @@ def install_mm(): if is_port_in_use(4827): print('Using Micro-manager running on port 4827 for testing') yield + elif find_existing_mm_install(): + print('Micro-Manager is already installed, skipping installation') + yield find_existing_mm_install() else: # Download an install latest nightly build mm_install_dir = download_and_install(destination='auto') diff --git a/pycromanager/test/test_callback_functions.py b/pycromanager/test/test_callback_functions.py index 32d0828e..e6da2ab0 100644 --- a/pycromanager/test/test_callback_functions.py +++ b/pycromanager/test/test_callback_functions.py @@ -8,7 +8,7 @@ def test_img_process_fn(launch_mm_headless, setup_data_folder): events = multi_d_acquisition_events(num_time_points=3) - def hook_fn(image, metadata): + def image_proc_fn(image, metadata): assert np.sum(image) > 0 assert isinstance(metadata, dict) @@ -18,7 +18,7 @@ def hook_fn(image, metadata): return image, metadata with Acquisition(setup_data_folder, 'acq', show_display=False, - image_process_fn=hook_fn) as acq: + image_process_fn=image_proc_fn) as acq: acq.acquire(events) dataset = acq.get_dataset()