pytemca
Preview State
This state is is the entry point to the system. It is the default state at start up and will also be the state that the system will revert to upon detecting an error that does not crash the system.
Preview State Logic
Upon entering this state the system will continuously acquire images from the XIMEA camera and each image frame will be minimally processed. The images are then JPEG encoded and sent to python which pushes them to a websocket to display on a browser based UI.
ROI Initialization State
The ROI Initialization State represents a state for moving to an aperture on a grid tape or stick16.
ROI Initialization State Logic
Upon entering this state it handles the entry logic for the state and initializes tape_ranges if it is None. It tries to fetch barcode values from TEM_db (if using tape); otherwise, it sets default values.
Next the code performs the following:
-
Initialization and Logging: Clears lists related to aperture critical and error events. Logs the action of changing the aperture.
-
State Check for Pause: Checks if the current montage state is PAUSE. If yes, it schedules the _change_aperture method to run again after 0.2 seconds and exits the current execution.
-
Preparatory Actions: Updates beam center results and informs the system that a new montage is starting.
- Abort Conditions Check:
Checks several conditions to decide whether to abort the operation:
- If required media information is missing.
- If environmental conditions are not suitable.
- If there’s a pending request to abort at the end of the montage.
- If there is insufficient disk space.
- If too many errors have accumulated (default is 3).
- Handling of Tape and Aperture:
Retrieves the next task object (TAO) from the montage controller.
If a valid TAO is found:
- Checks if the destination directory exists or needs to be created.
- Checks for existing data that might require an abort.
- Performs adjustments based on the first Region of Interest (ROI) from tao.
-
Aperture Movement and Correction: Manages movement to the correct grid or aperture based on the tape controller status. Deals with potential barcode errors and adjusts based on confidence levels in the barcode readings.
-
Lens and Beam Adjustments: Adjusts the beam’s brightness and attempts to find a centroid for the aperture if needed. Conducts a quality check on the beam’s brightness and might abort if the beam mean value is too low. Manages auto-centering of the beam if necessary.
-
Brightfield Imaging: Handles brightfield imaging conditions and might abort based on the quality of consecutive brightfield images.
-
Lens Correction Montage: Decides whether a new lens correction montage is necessary based on time intervals and changes in lens conditions.
- Finalizing and Error Handling: If no ROIs are found in tao, logs the event but continues processing. If there are still tasks (taos) left in the montage queue, it triggers the start of another attempt. Sets the montage state to NONE if no tasks are left.
Acquisition State
The Acquisition State represents the overall state of the acquisition loop in the system for imaging and processing data. It manages tasks related to moving the stage, capturing images, processing metadata, and handling various states during the acquisition process.
Acquisition State Logic
Main Acquisition Loop Activation
The acquisition starts if both the main_acquisition_loop_active_event is set and _start_new_aperture() returns True. Initialize variables and prepare for the acquisition sequence within a specific aperture.
ROI Acquisition Process
Iterate over each ROI in roi_list.
For each ROI:
- Initialize the ROI and calculate the starting position.
- Move the stage to the initial position and wait for it to stabilize.
- Enter a nested loop to handle image capture for each tile in the ROI until there are no more positions to move to.
- Set metadata for the image.
- Start camera exposure.
- If there was a previous image, wait for asynchronous processes to complete and possibly write metadata.
- Wait for the current exposure to complete.
- Get the next position and move the stage.
- Handle synchronization and stabilization of the stage.
- After completing all tiles in the ROI, perform final processing such as writing metadata and resetting the stage to the starting position.
Lens Correction Process
If lens correction is part of the current montage, attempt to correct lens distortions using template matching and solving algorithms. Multiple attempts are made, and if unsuccessful, a critical error event is logged.
Completion and Metadata Handling
Finalize the metadata file and write results to a database. Create a robocopy file if not aborting.
Abort or Switch Event
If an abort signal is received or it’s time to switch to another aperture, the loop is terminated or adjusted accordingly.
Event Scheduling
Schedule callbacks for events like “BACK_TO_SETUP” or “NEXT_APERTURE” based on the conditions met during the loop.