Tutorial: Building a Distributed Microscope Control Application with Hypha

This tutorial explains how to create a distributed application for controlling a microscope and snapping images using Hypha. It consists of two components: Microscope Control and a User Interface.


Components Overview

1. Microscope Control (microscope.py)

The microscope control component handles operations for moving the microscope stage and capturing images.

  • Key Features:

    • Move Stage: Moves the microscope stage to specified X and Y offsets.
    • Snap Image: Simulates capturing an image, returning either a shared memory object (SharedImage) or a numpy array.
  • Implementation Highlights:

    • SharedImage:
      • A custom class to transfer large image data efficiently via shared memory.
      • Includes methods to create, encode, decode, and restore images from shared memory.
    • Schema Functions:
      • move_stage: Moves the microscope stage based on user input.
      • snap_image: Captures a simulated image and optionally returns it via shared memory.
    • Hypha Server Integration:
      • Registers schema functions with the Hypha server, enabling access via the Hypha RPC framework.

2. User Interface (ui.py)

The user interface allows users to interact with the microscope system, move the stage, and snap images.

  • Key Features:

    • Move Stage UI: Provides input fields to move the stage to specified X and Y positions.
    • Snap Image UI: Captures and displays images in real-time, with customizable exposure times.
  • Implementation Highlights:

    • Built with PyQt5:
      • Provides a responsive and user-friendly interface.
      • Displays captured images using a QLabel.
    • Integration with Microscope Control:
      • Connects to the Hypha server to invoke move_stage and snap_image functions.
    • Timer-Based Image Updates:
      • Uses a QTimer to update snapped images at regular intervals.

Workflow Overview

Before start, make sure you have installed the required packages:

conda install pyqt
pip install hypha-rpc hypha pydantic
  1. Set Up the Hypha Server: Start the Hypha server on your machine:

    export JWT_SECRET=19b2e4b3 # Set a secret key for JWT token, change it to your own secret key
    python3 -m hypha.server --host=0.0.0.0 --port=9527
  2. Deploy Microscope Control:

    • Run microscope.py to register the microscope control service.
    • The service enables controlling the microscope stage and capturing images.
  3. Launch the User Interface:

    • Run ui.py to open the GUI.
    • The GUI connects to the microscope control service, allowing users to:
      • Move the microscope stage.
      • Snap images and view them in real-time.

Interaction Between Components

  1. The Hypha Server serves as the communication hub between the microscope control component and the UI.
  2. The Microscope Control Service handles backend operations for stage movement and image capture.
  3. The User Interface sends commands to the microscope service and displays the results (e.g., snapped images).

How It Works Together

  1. Starting the Server: The Hypha server is started to enable communication between components.

  2. Microscope Control Service:

    • Registers two schema functions:
      • move_stage: Moves the microscope stage based on user input.
      • snap_image: Captures a simulated image, optionally using shared memory.
  3. User Interface:

    • Allows users to interact with the microscope control service via input fields and buttons.
    • Captured images are displayed in real-time in the UI.

Future Extensions

  • Add more microscope operations, such as focusing or changing magnification.
  • Integrate real microscope hardware into the system.
  • Extend the UI to include advanced visualization and analysis tools.

This streamlined setup demonstrates the power of Hypha in building distributed, modular scientific applications with efficient data handling.