Skip to content

Commit

Permalink
Fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jacopoabramo committed Nov 13, 2024
1 parent 7a98d03 commit 415dc52
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
39 changes: 23 additions & 16 deletions src/exengine/examples/implicit_vs_explicit_excutor.py
Original file line number Diff line number Diff line change
@@ -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()
executor.shutdown()
terminate_core_instances()
except Exception as e:
print(f"An error occurred: {e}")
terminate_core_instances()
18 changes: 10 additions & 8 deletions src/exengine/examples/micromanager_example.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
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
create_core_instance()

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)

Expand All @@ -35,8 +39,6 @@

data_handler.finish()



executor.shutdown()
terminate_core_instances()

7 changes: 3 additions & 4 deletions src/exengine/examples/using_devices.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -26,4 +24,5 @@

print(image)

executor.shutdown()
executor.shutdown()
terminate_core_instances()

0 comments on commit 415dc52

Please sign in to comment.