Bin#
Calibration-aware 4D-STEM binning widget with BF/ADF quality checks.
Usage#
import numpy as np
from quantem.widget import Bin
data = np.random.rand(64, 64, 128, 128).astype(np.float32)
w = Bin(data, pixel_size=2.39, k_pixel_size=0.46, device="cpu")
w
Features#
Torch-only compute with configurable device (
cpu/cuda/mps)Independent scan + detector bin factors with
mean/sumreductionEdge handling modes:
crop,pad,errorCalibration propagation for real-space and detector sampling after binning
BF/ADF QC previews before and after binning with compact stats
Export helpers:
save_image,save_zip,save_gif
Methods#
w = Bin(data)
# Replace data (preserves bin settings)
w.set_data(new_data)
# Access results
w.result # binned tensor (on device)
w.get_binned_data() # copy as tensor
w.get_binned_data(as_numpy=True) # copy as NumPy
State Persistence#
w = Bin(data, pixel_size=2.39, k_pixel_size=0.46)
w.summary() # human-readable summary
w.state_dict() # serializable state dictionary
w.save("bin_preset.json") # save preset JSON
w2 = Bin(data, state="bin_preset.json")
Programmatic Export#
w.save_image("bin_grid.png", view="grid")
w.save_zip("bin_bundle.zip", include_arrays=True)
w.save_gif("bin_bf_compare.gif", channel="bf")
Batch Runner#
Use the same preset over folders (one file at a time):
python -m quantem.widget.bin_batch \
--input-dir /path/to/raw \
--output-dir /path/to/binned \
--preset bin_preset.json \
--pattern '*.npy' \
--recursive \
--device cpu
Examples#
API Reference#
See quantem.widget.Bin for full documentation.