Skip to content

Commit 967ce9f

Browse files
committed
Add demo script for running riCOM in CLI with python
1 parent bd961f5 commit 967ce9f

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

ricom_demo.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Demo script for running riCOM from command line interface through python.
4+
This script demonstrates how to execute the riCOM executable with various arguments.
5+
"""
6+
7+
import subprocess
8+
import os
9+
import matplotlib.pyplot as plt
10+
import numpy as np
11+
12+
# Path to the riCOM executable (adjust as needed)
13+
RICOM_EXECUTABLE = "./build/RICOM"
14+
15+
def demo_timepix_camera():
16+
"""
17+
Demo: Process data from Timepix camera with dwell time setting.
18+
Available arguments are:
19+
20+
Input/Connection:
21+
-filename <path> : Read from .mib or .t3p file
22+
23+
Dimensions:
24+
-nx <number> : Width of scan image
25+
-ny <number> : Height of scan image
26+
-cam_nx <number> : Width of camera sensor
27+
-cam_ny <number> : Height of camera sensor
28+
29+
Processing:
30+
-skipr <number> : Skip rows (skip n frames at the end of each line for flyback correction)
31+
-skipi <number> : Skip images (skip n frames at the end of each image for flyback correction)
32+
-k <number> : Kernel size for riCOM
33+
-r <float> : CBED rotation angle in degrees
34+
-offset <x> <y> : CBED center offset (two values)
35+
-update_offset <0|1> : Auto-update offset during processing
36+
-radius <inner> <outer>: Virtual STEM detector radii (enables vSTEM)
37+
-f <low> <high> : Kernel filter frequency range
38+
-threads <number> : Number of processing threads
39+
-queue_size <number> : Size of processing queue
40+
-rep <number> : Number of repetitions
41+
42+
Camera-specific:
43+
-depth <number> : Bit depth per pixel (Merlin camera)
44+
-dwell_time <float> : Dwell time in seconds (Timepix camera)
45+
46+
Output:
47+
-save_img_path <path> : Save reconstruction as image file
48+
-save_data_path <path> : Save reconstruction as numpy array (.npy)
49+
50+
Visualization:
51+
-redraw_interval <ms> : Redraw interval in milliseconds (enables SDL display)
52+
-plot_e_field <0|1> : Plot electric field magnitude
53+
"""
54+
55+
args = [
56+
RICOM_EXECUTABLE,
57+
# "-filename", "/home/thomas/Documents/Git/4D-STEM-Data/data/cryst_1 mono_120_CL 115 Mag_2.55Mx defocus_-198.67 CA_21.47 Tue_Jul_16_11_50_09_2024_STEM_300kV_2048x2048_4_us_1_scans.t3p",
58+
"-filename", "/home/thomas/Documents/Git/4D-STEM-Data/data/cryst_5.5 mono_140_CL 115 Mag_2.55Mx defocus_0 CA_24.70 Tue_Oct__1_17_48_58_2024_STEM_300kV_2048x2048_4_us_1_scans.t3p",
59+
60+
# Timepix-specific setting
61+
"-dwell_time", "4000", # Dwell time in nanoseconds (Timepix camera)
62+
63+
# Scan dimensions
64+
"-nx", "2049",
65+
"-ny", "2048",
66+
67+
# Camera dimensions
68+
"-cam_nx", "256",
69+
"-cam_ny", "256",
70+
71+
# Kernel
72+
"-k", "12", # Kernel half-size for riCOM
73+
"-r", "260", # CBED rotation angle in degrees
74+
"-offset", "135.12", "116.694", # CBED center (x, y)
75+
76+
# Processing
77+
"-threads", "1",
78+
"-redraw_interval", "200", # Redraw every 200 ms
79+
80+
# Output
81+
"-save_data_path", "timepix_output.npy",
82+
]
83+
84+
print("Running riCOM with Timepix camera data...")
85+
print(" ".join(args))
86+
subprocess.run(args, capture_output=False, text=True)
87+
88+
89+
if __name__ == "__main__":
90+
91+
# Check if executable exists
92+
if not os.path.exists(RICOM_EXECUTABLE):
93+
print(f"Warning: Executable not found at {RICOM_EXECUTABLE}")
94+
print("Please adjust RICOM_EXECUTABLE path in this script.")
95+
print()
96+
97+
# Run reconstruction, and get COM_AVG from the output, which can be used for correction in the second run if needed
98+
demo_timepix_camera()
99+
100+
# When given a data path, the script will save the reconstruction as a numpy array. We can load and visualize it here.
101+
img_data = np.load("timepix_output.npy")
102+
fig, ax = plt.subplots(1, 1, figsize=(8, 8))
103+
im = ax.imshow(img_data, cmap='viridis')
104+
105+
ax.axis('off')
106+
plt.colorbar(im, ax=ax, shrink=0.8)
107+
plt.tight_layout()
108+
plt.show()
109+

src/RunCLI.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ int run_cli(int argc, char *argv[], Ricom *ricom, CAMERA::Default_configurations
263263
{
264264
RICOM::run_ricom(ricom, ricom->mode);
265265
}
266+
std::cout << "COM_AVG " << ricom->com_public[0] << " " << ricom->com_public[1] << std::endl;
266267
if (save_dat != "")
267268
{
268269
save_numpy(&save_dat, ricom->nx, ricom->ny, &ricom->ricom_data);

0 commit comments

Comments
 (0)