From 415dc5256b92222f9af5615a70ff8ba0d729e676 Mon Sep 17 00:00:00 2001 From: Jacopo Abramo Date: Wed, 13 Nov 2024 18:24:00 +0100 Subject: [PATCH] Fix examples --- .../examples/implicit_vs_explicit_excutor.py | 39 +++++++++++-------- src/exengine/examples/micromanager_example.py | 18 +++++---- src/exengine/examples/using_devices.py | 7 ++-- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/exengine/examples/implicit_vs_explicit_excutor.py b/src/exengine/examples/implicit_vs_explicit_excutor.py index 4fafe2e..74564ac 100644 --- a/src/exengine/examples/implicit_vs_explicit_excutor.py +++ b/src/exengine/examples/implicit_vs_explicit_excutor.py @@ -1,27 +1,34 @@ -from mmpycorex import create_core_instance, download_and_install_mm, terminate_core_instances +from mmpycorex import create_core_instance, terminate_core_instances from exengine.kernel.executor import ExecutionEngine -from exengine.backends.micromanager.mm_device_implementations import MicroManagerCamera, MicroManagerSingleAxisStage +from exengine.kernel.notification_base import Notification +from exengine.backends.micromanager.mm_device_implementations import MicroManagerSingleAxisStage from exengine.events.positioner_events import SetPosition1DEvent +def event_complete(notification: Notification) -> None: + print(f"Event complete, notification: {notification.category} - {notification.description} - {notification.payload}") # download_and_install_mm() # If needed # Start Micro-Manager core instance with Demo config -create_core_instance() +try: + create_core_instance() -executor = ExecutionEngine() -z_stage = MicroManagerSingleAxisStage() + executor = ExecutionEngine() + z_stage = MicroManagerSingleAxisStage() -# This occurs on the executor thread. The event is submitted to the executor and its result is awaited, -# meaning the call will block until the method is executed. -z_stage.set_position(100, thread='device_setting_thread') -# it is equivalent to: -executor.submit(SetPosition1DEvent(position=100, device=z_stage)).await_execution() + executor.subscribe_to_notifications(event_complete) + # explicit + z_stage.set_position(100) + # it is equivalent to: + # executor.submit(SetPosition1DEvent(position=100, device=z_stage), thread_name='device_setting_thread').await_execution() + # but the execution thread is the main thread + # implicit + # start capture first; we use await execution in order to make sure that the camera has finished acquisition + executor.submit(SetPosition1DEvent(position=100, device=z_stage), thread_name='device_setting_thread') -executor.submit(SetPosition1DEvent(position=100, device=z_stage), thread='device_setting_thread') -executor.submit(ReadoutImages(), thread='readout_thread') - - - -executor.shutdown() \ No newline at end of file + executor.shutdown() + terminate_core_instances() +except Exception as e: + print(f"An error occurred: {e}") + terminate_core_instances() \ No newline at end of file diff --git a/src/exengine/examples/micromanager_example.py b/src/exengine/examples/micromanager_example.py index 1a65e96..c1f8420 100644 --- a/src/exengine/examples/micromanager_example.py +++ b/src/exengine/examples/micromanager_example.py @@ -1,11 +1,14 @@ -from mmpycorex import create_core_instance, download_and_install_mm, terminate_core_instances +from mmpycorex import create_core_instance, terminate_core_instances from exengine.kernel.executor import ExecutionEngine from exengine.kernel.data_coords import DataCoordinates -from exengine.kernel.ex_event_base import DataHandler +from exengine.kernel.notification_base import Notification, NotificationCategory +from exengine.data import DataHandler from exengine.backends.micromanager.mm_device_implementations import MicroManagerCamera, MicroManagerSingleAxisStage -from storage_backends.ndtiff_and_ndram.NDTiffandRAM import NDRAMStorage +from exengine.storage_backends.ndtiff_and_ndram import NDRAMStorage from exengine.events.detector_events import StartCapture, ReadoutData +def callback(notification: Notification) -> None: + print(f"Notification received: {notification.category} - {notification.payload}") # download_and_install_mm() # If needed # Start Micro-Manager core instance with Demo config @@ -13,20 +16,21 @@ executor = ExecutionEngine() - # Create Micro-Manager Devices camera = MicroManagerCamera() z_stage = MicroManagerSingleAxisStage() - # Capture 100 images on the camera -num_images = 100 +num_images = 5 data_handler = DataHandler(storage=NDRAMStorage()) start_capture_event = StartCapture(num_blocks=num_images, detector=camera) readout_images_event = ReadoutData(num_blocks=num_images, detector=camera, data_coordinates_iterator=[DataCoordinates(time=t) for t in range(num_images)], data_handler=data_handler) + +executor.subscribe_to_notifications(callback, NotificationCategory.Data) + executor.submit(start_capture_event) future = executor.submit(readout_images_event) @@ -35,8 +39,6 @@ data_handler.finish() - - executor.shutdown() terminate_core_instances() diff --git a/src/exengine/examples/using_devices.py b/src/exengine/examples/using_devices.py index 1a76323..5987563 100644 --- a/src/exengine/examples/using_devices.py +++ b/src/exengine/examples/using_devices.py @@ -1,4 +1,4 @@ -from mmpycorex import create_core_instance, download_and_install_mm, terminate_core_instances +from mmpycorex import create_core_instance, terminate_core_instances from exengine.kernel.executor import ExecutionEngine from exengine.backends.micromanager.mm_device_implementations import MicroManagerCamera, MicroManagerSingleAxisStage @@ -9,12 +9,10 @@ executor = ExecutionEngine() - # Create Micro-Manager Devices camera = MicroManagerCamera() z_stage = MicroManagerSingleAxisStage() - # By default, setting/getting attributes and calling methods occure on the main executor thread # This sets the property of the Micro-Manager camera object camera.Exposure = 100 @@ -26,4 +24,5 @@ print(image) -executor.shutdown() \ No newline at end of file +executor.shutdown() +terminate_core_instances() \ No newline at end of file