py4DSTEM
What this library is for
py4DSTEM is a toolkit for reading, calibrating, visualizing, and analyzing
4D-STEM diffraction datacubes. It provides domain objects and workflows for
virtual imaging, Bragg disk detection, center-of-mass/DPC analysis, strain and
orientation mapping, phase retrieval, ptychography, and calibration.
When to use this vs. alternatives
- Use py4DSTEM for 4D-STEM datacubes, diffraction-pattern stacks, scan/detector
axis handling, virtual images/diffraction, Bragg vectors, COM/DPC, strain,
orientation mapping, and ptychographic phase retrieval.
- Use HyperSpy when the task is broader multidimensional microscopy/spectroscopy
signal handling, EELS/EDX workflows, lazy loading, or interactive signal
decomposition outside py4DSTEM-specific diffraction workflows.
- Use NumPy/scikit-image only for narrow image-processing steps after py4DSTEM
has handled datacube loading, calibration, slicing, and diffraction semantics.
- Do not treat a 4D-STEM file as an arbitrary 4D array without documenting scan
axes, detector axes, reciprocal-space calibration, beam center, and masks.
Canonical workflow
Start by loading a datacube, inspecting dimensions, setting or checking
calibration, building virtual detectors/images, then moving to Bragg/COM/strain
workflows only after the raw data and masks make sense.
import py4DSTEM
py4DSTEM.print_h5_tree("scan_4dstem.h5")
datacube = py4DSTEM.read("scan_4dstem.h5", datapath="root/datacube")
# For non-native microscope formats, use py4DSTEM.import_file(...) instead.
print(datacube.data.shape) # typically (scan_y, scan_x, q_y, q_x)
datacube.calibration
# Average diffraction pattern and virtual bright-field image.
dp_mean = datacube.get_dp_mean()
bf = datacube.get_virtual_image(
mode="circle",
geometry=((0, 0), 20),
centered=True,
)
# Inspect before quantitative analysis.
py4DSTEM.show(dp_mean)
py4DSTEM.show(bf)
# Bragg disk workflows need a probe/kernel and detection parameters chosen from
# the actual diffraction pattern, not copied blindly from an example.
probe = datacube.get_vacuum_probe()
bragg_peaks = datacube.find_Bragg_disks(template=probe, corrPower=1.0, sigma=2)
For deeper examples, read:
Key conventions and gotchas
- py4DSTEM v0.14 is a major workflow/API boundary. Older pre-0.14 examples can
be structurally misleading; check the docs version before copying code.
- Phase-retrieval APIs were reorganized in v0.14.9 with shortened class names.
If ptychography examples fail at import time, verify the installed py4DSTEM
version and current phase-retrieval class names.
- Datacube axes are domain-significant. Confirm scan axes and diffraction axes
before indexing, reshaping, summing, or exporting arrays.
- Virtual detector geometry is in detector/reciprocal-space pixel coordinates
unless calibration-specific code says otherwise. Do not use a copied radius or
center without checking the beam center and diffraction pattern scale.
- Quantitative Bragg/strain/orientation workflows depend on probe/kernel choice,
thresholds, masks, calibration, elliptical distortion correction, and scan
distortions. Do not report quantitative strain from raw peaks without these
checks.
- py4DSTEM data can be large. Prefer the package's readers, tree objects, and
tutorial patterns over loading entire HDF5 datasets into ad hoc arrays.
Anti-patterns
- Do not write generic NumPy code that assumes shape order without printing and
naming
(R_y, R_x, Q_y, Q_x) or the corresponding py4DSTEM dimensions.
- Do not run Bragg disk detection with tutorial thresholds on a new dataset.
Inspect the mean/max diffraction pattern, mask saturated/hot pixels, choose a
probe/template, and validate peaks visually.
- Do not average or crop diffraction patterns before recording calibration and
beam-center assumptions.
- Do not mix py4DSTEM v0.13 notebooks with v0.14+ code unless deliberately
porting the workflow.
- Do not use phase-retrieval examples without checking whether the class names
match the installed version.
Diagnostic checks
Before trusting outputs, the agent should:
- Log py4DSTEM version, file path, tree keys, datacube shape, dtype, and whether
data are loaded eagerly or lazily.
- Show the mean or max diffraction pattern and at least one scan-position
diffraction pattern before quantitative processing.
- Verify scan/detector axis order, beam center, calibration units, detector
mask, and virtual detector geometry.
- For Bragg workflows, overlay detected peaks on representative diffraction
patterns and record probe/template, thresholds, and masks.
- For strain/orientation outputs, record calibration, reference lattice/peaks,
distortion corrections, and uncertainty or residual checks.
- Save intermediate py4DSTEM objects or output files with enough metadata to
reproduce detector geometry and calibration choices.
Pointers to deeper material
1---2name: py4dstem3description: Use when the user is working with 4D-STEM, scanning nanobeam diffraction, diffraction datacubes, Bragg disk detection, virtual bright/dark field imaging, center-of-mass/DPC, strain/orientation mapping, ptychography, phase retrieval, or microscope calibration from STEM diffraction data. Prefer py4DSTEM over generic NumPy/scikit-image code when diffraction datacube conventions, detector axes, scan axes, calibration, and 4D-STEM workflows matter.4---56# py4DSTEM78## What this library is for910py4DSTEM is a toolkit for reading, calibrating, visualizing, and analyzing114D-STEM diffraction datacubes. It provides domain objects and workflows for12virtual imaging, Bragg disk detection, center-of-mass/DPC analysis, strain and13orientation mapping, phase retrieval, ptychography, and calibration.1415## When to use this vs. alternatives1617- Use py4DSTEM for 4D-STEM datacubes, diffraction-pattern stacks, scan/detector18 axis handling, virtual images/diffraction, Bragg vectors, COM/DPC, strain,19 orientation mapping, and ptychographic phase retrieval.20- Use HyperSpy when the task is broader multidimensional microscopy/spectroscopy21 signal handling, EELS/EDX workflows, lazy loading, or interactive signal22 decomposition outside py4DSTEM-specific diffraction workflows.23- Use NumPy/scikit-image only for narrow image-processing steps after py4DSTEM24 has handled datacube loading, calibration, slicing, and diffraction semantics.25- Do not treat a 4D-STEM file as an arbitrary 4D array without documenting scan26 axes, detector axes, reciprocal-space calibration, beam center, and masks.2728## Canonical workflow2930Start by loading a datacube, inspecting dimensions, setting or checking31calibration, building virtual detectors/images, then moving to Bragg/COM/strain32workflows only after the raw data and masks make sense.3334```python35import py4DSTEM3637py4DSTEM.print_h5_tree("scan_4dstem.h5")38datacube = py4DSTEM.read("scan_4dstem.h5", datapath="root/datacube")39# For non-native microscope formats, use py4DSTEM.import_file(...) instead.4041print(datacube.data.shape) # typically (scan_y, scan_x, q_y, q_x)42datacube.calibration4344# Average diffraction pattern and virtual bright-field image.45dp_mean = datacube.get_dp_mean()46bf = datacube.get_virtual_image(47 mode="circle",48 geometry=((0, 0), 20),49 centered=True,50)5152# Inspect before quantitative analysis.53py4DSTEM.show(dp_mean)54py4DSTEM.show(bf)5556# Bragg disk workflows need a probe/kernel and detection parameters chosen from57# the actual diffraction pattern, not copied blindly from an example.58probe = datacube.get_vacuum_probe()59bragg_peaks = datacube.find_Bragg_disks(template=probe, corrPower=1.0, sigma=2)60```6162For deeper examples, read:6364- Tutorial notebooks: https://github.com/py4dstem/py4DSTEM_tutorials65- First steps: https://py4dstem.readthedocs.io/en/latest/examples/first_steps.html66- Virtual imaging: https://py4dstem.readthedocs.io/en/latest/examples/virtual_imaging.html67- Bragg disk detection: https://py4dstem.readthedocs.io/en/latest/examples/bragg_disk_detection.html6869## Key conventions and gotchas7071- py4DSTEM v0.14 is a major workflow/API boundary. Older pre-0.14 examples can72 be structurally misleading; check the docs version before copying code.73- Phase-retrieval APIs were reorganized in v0.14.9 with shortened class names.74 If ptychography examples fail at import time, verify the installed py4DSTEM75 version and current phase-retrieval class names.76- Datacube axes are domain-significant. Confirm scan axes and diffraction axes77 before indexing, reshaping, summing, or exporting arrays.78- Virtual detector geometry is in detector/reciprocal-space pixel coordinates79 unless calibration-specific code says otherwise. Do not use a copied radius or80 center without checking the beam center and diffraction pattern scale.81- Quantitative Bragg/strain/orientation workflows depend on probe/kernel choice,82 thresholds, masks, calibration, elliptical distortion correction, and scan83 distortions. Do not report quantitative strain from raw peaks without these84 checks.85- py4DSTEM data can be large. Prefer the package's readers, tree objects, and86 tutorial patterns over loading entire HDF5 datasets into ad hoc arrays.8788## Anti-patterns8990- Do not write generic NumPy code that assumes shape order without printing and91 naming `(R_y, R_x, Q_y, Q_x)` or the corresponding py4DSTEM dimensions.92- Do not run Bragg disk detection with tutorial thresholds on a new dataset.93 Inspect the mean/max diffraction pattern, mask saturated/hot pixels, choose a94 probe/template, and validate peaks visually.95- Do not average or crop diffraction patterns before recording calibration and96 beam-center assumptions.97- Do not mix py4DSTEM v0.13 notebooks with v0.14+ code unless deliberately98 porting the workflow.99- Do not use phase-retrieval examples without checking whether the class names100 match the installed version.101102## Diagnostic checks103104Before trusting outputs, the agent should:105106- Log py4DSTEM version, file path, tree keys, datacube shape, dtype, and whether107 data are loaded eagerly or lazily.108- Show the mean or max diffraction pattern and at least one scan-position109 diffraction pattern before quantitative processing.110- Verify scan/detector axis order, beam center, calibration units, detector111 mask, and virtual detector geometry.112- For Bragg workflows, overlay detected peaks on representative diffraction113 patterns and record probe/template, thresholds, and masks.114- For strain/orientation outputs, record calibration, reference lattice/peaks,115 distortion corrections, and uncertainty or residual checks.116- Save intermediate py4DSTEM objects or output files with enough metadata to117 reproduce detector geometry and calibration choices.118119## Pointers to deeper material120121- Documentation: https://py4dstem.readthedocs.io/122- Tutorial repository: https://github.com/py4dstem/py4DSTEM_tutorials123- Source repository: https://github.com/py4dstem/py4DSTEM124- Paper: Savitzky et al. (2021), "py4DSTEM: A Software Package for Four-125 Dimensional Scanning Transmission Electron Microscopy Data Analysis",126 Microscopy and Microanalysis 27, 712-743. https://doi.org/10.1017/S1431927621000477