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.
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.
- SharedImage:
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_stageandsnap_imagefunctions.
- Connects to the Hypha server to invoke
- Timer-Based Image Updates:
- Uses a
QTimerto update snapped images at regular intervals.
- Uses a
- Built with PyQt5:
Before start, make sure you have installed the required packages:
conda install pyqt
pip install hypha-rpc hypha pydantic-
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
-
Deploy Microscope Control:
- Run
microscope.pyto register the microscope control service. - The service enables controlling the microscope stage and capturing images.
- Run
-
Launch the User Interface:
- Run
ui.pyto 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.
- Run
- The Hypha Server serves as the communication hub between the microscope control component and the UI.
- The Microscope Control Service handles backend operations for stage movement and image capture.
- The User Interface sends commands to the microscope service and displays the results (e.g., snapped images).
-
Starting the Server: The Hypha server is started to enable communication between components.
-
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.
- Registers two schema functions:
-
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.
- 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.