Skip to content

Local FFT pseudo-4DSTEM for HRTEM images, with phase and in-plane orientation mapping - #314

Merged
ehrhardtkm merged 6 commits into
electronmicroscopy:devfrom
cophus:acom-fft
Nov 24, 2026
Merged

ehrhardtkm merged 6 commits into
electronmicroscopy:devfrom
cophus:acom-fft

Conversation

@cophus

@cophus cophus commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

What problem this PR addresses

Lattice fringes in HRTEM images carry local phase and orientation information, but quantEM had no way to map them. This PR treats an HRTEM image as a pseudo-4DSTEM dataset: a tapered window moves across the image, and the local Fourier transform at each window position becomes a diffraction pattern. These patterns then go through the existing 4DSTEM workflow (Bragg disk detection and pixel size calibration), followed by phase and in-plane orientation mapping.

acom_fft_pipeline

Left to right: HRTEM image with four positions of the moving window, the four tapered windows, their local Fourier transforms with the detected Bragg disks on the front pattern, and the phase map (LiF yellow, Li2O cyan) and LiF in-plane orientation map.

What's here

  • imaging/local_fft.py: LocalFFT.from_image cuts a window of window_size pixels every step pixels, applies a disk mask with a sin² taper over the full radius (window_mask), and stores the cropped power spectra as a Dataset4dstem in Å and 1/Å. Each pattern is normalized by the azimuthal median of the scan-mean pattern as I / background - 1, so the background is 0 and the noise is close to 1 at every frequency, and the center disk is set to 0. The window centers are known exactly, so LocalFFT.origins goes directly to BraggVectors.correct_peak_origins and we skip origin fitting and ellipse calibration. print(lfft) reports the window diameter and FWHM, which sets the spatial resolution.
  • diffraction/systematic_row.py: SystematicRowMap. Most HRTEM windows show a single set of lattice fringes, which is a single systematic row: it determines the phase and the in-plane direction of g, but not the rotation of the crystal about g. Instead of full ACOM, we pair Friedel spots (+g and -g), assign |g| to the nearest reflection family of the candidate crystals, and map the phase and the in-plane angle of g (fiber texture). Plots: plot_rings, plot_phase, plot_orientation and plot_angle_distribution. Orientation colors use the same perceptually uniform hue as the rest of ACOM (array_to_rgba), with brightness from the row intensity, and angles follow the quantEM convention atan2(g_col, g_row).
  • Crystal drops the ase Spacegroup object that the ase CIF reader and ase.spacegroup.crystal leave in atoms.info, whether it is built with from_cif or from_ase; the caller's structure is left as it was. Crystal finds the space group itself (Crystal.spacegroup), so it holds only native data, and saving a crystal or crystal map no longer warns "Not saving non-JSON-serializable entries of atoms.info".
  • One in-plane angle convention for ACOM: OrientationMap.in_plane_angle_deg measured from the column axis toward the row axis, so a crystal rotated by t about the beam (quat_from_zone_axis(..., in_plane_deg=t)) read 90 - t. It now returns atan2(a_col, a_row), from the row axis toward the column axis, the sense of a right-handed rotation about the beam and the convention of the calibration, the orientation matching and SystematicRowMap. A numeric direction in ipf_color and plot_orientation_map follows the same convention (0 is "r", 90 is "c"), as does fold_in_plane. plot_cluster_pole_figure was drawn with the row axis horizontal and the column axis upward, 90 degrees from plot_pole_figure; both now use the image frame (column rightward, row downward) with the same scan-axes glyph. Saved in-plane angle maps and cluster pole figures from earlier versions will change.
  • One test each for LocalFFT and SystematicRowMap, and a rotation-sense check in the existing in-plane fold test.

Validation (notebook, not in the repo)

HRTEM image of LiF and Li2O recorded by Guoliang Hu, 300 kV, 4096 x 4096 pixels of 0.355 Å. We used a resolution of 20 Å (112 pixel window, 40 Å diameter) and a step of 28 pixels, giving 143 x 143 windows.

  • The pixel size calibration against LiF (200) and Li2O (111) gives a correction of +2.08% relative to the microscope calibration, with a residual of 0.47%.
  • LiF is found in 95.7% and Li2O in 4.3% of the windows with indexed fringes.
  • The in-plane g angles agree with Guoliang's independent fringe angle maps to a median of 1.0° for LiF and 0.8° for Li2O.
  • Only 39 of the 11,200 windows with indexed fringes show two fringe directions from one crystal, so we do not attempt full 3D orientations.

What should the reviewer(s) do

Review the new LocalFFT and SystematicRowMap classes and merge.

Written by Colin Ophus and Claude (Claude Code).

cophus and others added 3 commits October 31, 2026 15:51
Resolve the diffraction/__init__.py export conflict by keeping the new
StrainMapAutocorrelation and ModelDiffraction exports from dev and the
SystematicRowMap export from this branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Crystal.from_cif dropped the Spacegroup object that ase's CIF reader puts in
atoms.info, but Crystal.from_ase kept it, for example from
ase.spacegroup.crystal. It cannot be saved, so saving a crystal or crystal
map printed "Not saving non-JSON-serializable entries of atoms.info".
The constructor now stores a copy of the atoms without it, so both paths
drop it and the caller's structure is left as it was. Crystal finds the
space group itself (Crystal.spacegroup).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ehrhardtkm
ehrhardtkm self-requested a review November 19, 2026 01:15

@ehrhardtkm ehrhardtkm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a first pass and think it mostly looks good! I left some comments on clarifying docstrings and asking a few questions about implementation details in the systematic row mapping

Comment thread src/quantem/imaging/local_fft.py Resolved
Comment thread src/quantem/imaging/local_fft.py Resolved
cophus and others added 3 commits November 23, 2026 15:41
- LocalFFT: drop the amplitude option (the normalized noise of 1 holds
  only for the power spectrum) and use one variable name per meaning in
  _compute.
- SystematicRowMap: document k_min, k_max and min_ring_intensity on the
  class; say that a row is one Friedel pair; an integer ring now indexes
  the rings of the given phase (it indexed the whole ring table); and
  num_directions counts only indexed, unambiguous rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
OrientationMap.in_plane_angle_deg measured from the column axis toward the
row axis, so a crystal rotated by t about the beam (quat_from_zone_axis
in_plane_deg=t) read 90 - t. It now returns atan2(a_col, a_row), from the
row axis toward the column axis, the sense of a right-handed rotation
about the beam, as in the calibration, the orientation matching and
SystematicRowMap.

- ipf_color and plot_orientation_map: a numeric direction is an angle from
  the row axis toward the column axis (0 is "r", 90 is "c"); the title
  says so.
- fold_in_plane uses the same angle, with the correction rotation sign
  flipped to match.
- plot_cluster_pole_figure drew row horizontally and column upward, 90
  degrees from plot_pole_figure. Both now use the image frame (column
  rightward, row downward) with the same scan-axes glyph.
- test_in_plane_angle_auto_fold checks that the folded angle turns with
  the rotation about the beam.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The old assert required an unfolded angle above 60 degrees, which held
only for the previous column-to-row convention and failed on CI after the
switch (max 42.3). Check instead that mod_deg=None returns an angle in
(-180, 180] that folds to the "auto" value modulo 60.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@ehrhardtkm ehrhardtkm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Thanks

@cophus
cophus requested a review from ehrhardtkm November 24, 2026 20:10
@ehrhardtkm
ehrhardtkm merged commit 3f0f6ba into electronmicroscopy:dev Nov 24, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants