This repository provides an interactive annotation tool for 4D STEM (Scanning Transmission Electron Microscopy) data. It is designed to faciliate the navigation, visualization, and annotation of complex 4D datasets. The tool would be useful for researchers and scientists in materials science, electron microscopy, and related fields where diffraction pattern analysis is required.

Screenshots of the Virtual STEM Image Viewer and Diffraction Pattern Annotator
with a single scan point selected

Screenshots of the Virtual STEM Image Viewer and Diffraction Pattern Annotator
with a rectangular region of scan points selected
This code has been tested and works with the following dependencies:
- Python 3.12.4
- NumPy 2.1.0
- OpenCV-Python 4.11.0.86
- PyQt5 5.15.11
- QtPy 2.4.1
Ensure that all required packages are installed before running the code.
It's recommended to use a virtual environment to manage dependencies and avoid conflicts with other Python projects. For example, we can use conda
:
conda create -n test_4dstem python=3.12
Once the environment is created, activate it:
conda activate test_4dstem
Next, use pip
to install the required dependencies:
pip install numpy opencv-python PyQt5 QtPy
To get started, clone the repository:
git clone https://github.com/chenbei102/cb_4dstem_annotator.git
cd cb_4dstem_annotator
Your 4D STEM data should be saved as a NumPy .npz
file. You can use the following Python snippet to save your data:
import numpy as np
# Replace with your actual 4D STEM data
data = ...
# Specify the file path where you want to save the data
file_path = "your_data.npz"
np.savez(file_path, data=data)
Run the following command:
python cb_4dstem_annotator.py
Once the viewer is open, click the "Load Data" button, and select the .npz
file containing your 4D STEM data.
Note: If your dataset is large, it may take some time to load. After the data is successfully loaded, you will see:
- A virtual STEM image in the viewer window.
- A diffraction pattern image in the annotator window.
- Single Scan Point: Click on the virtual STEM image to select a single spatial scan point.
- Rectangular Region: Hold Shift and click a second point to define a rectangular region of interest.
- Add Annotations: Click on the diffraction pattern image to annotate bright spots with circular markers.
- Selection Mode: Hold Shift to activate selection mode. In this mode, if a click occurs close to a nearby spot, the selection state of that spot will toggle.
- Click the "Export Data" button to save the coordinates of the annotated spots to files. A separate output file will be generated for each selected diffraction pattern image.