Skip to content

Add DiffractionSim and remove the abTEM dependency - #245

Draft
cophus wants to merge 1 commit into
electronmicroscopy:mainfrom
cophus:diffraction
Draft

cophus wants to merge 1 commit into
electronmicroscopy:mainfrom
cophus:diffraction

Conversation

@cophus

@cophus cophus commented Oct 7, 2026

Copy link
Copy Markdown

What problem does it solve?

This PR adds DiffractionSim, an interactive electron diffraction simulator, and removes abTEM from quantem.widget.

DiffractionSim shows a crystal and its diffraction pattern side by side for any orientation, computed live with Bloch waves: nanobeam disks, CBED and Kikuchi patterns, with thickness, convergence, precession and energy controls. It was developed alongside the orientation and phase mapping work in quantEM (electronmicroscopy/quantem#297), whose Crystal and bloch modules it uses. A framework-free build of the same frontend (npm run build:web) runs on the group website.

abTEM was used only for Lobato scattering factors, one projected potential, cell orthogonalization and the interaction constant, but it brings in a large dependency stack including dask. We replaced these four pieces with quantEM's Lobato factors (Lobato and Van Dyck, Acta Cryst. A 70, 636 (2014), https://doi.org/10.1107/S205327331401643X) and short numpy implementations:

  • ShowCIF potential previews and the scattering factors agree with abTEM to about 1e-5.
  • PlanPtycho cells are identical to abTEM's, except for one case where abTEM's cell was missing an atom.
  • The projected potential is now an exact Fourier sum. It differs from abTEM's by 0.5 to 4.5 % per pixel, because abTEM spreads each atom over four pixels; against an independent real-space reference the new potential is accurate to about 1e-4.
  • The old cells also placed some atoms at -1e-17 A, which abTEM silently dropped, so SrTiO3 [111] and [112] potentials were 16 to 20 % too low. The new cells keep every coordinate in [0, 1).

The crystal extra is now ase>=3.23, spglib>=2.5 and pandas (pandas is used by PlanPtycho.report() and was previously supplied by abTEM). DiffractionSim, ShowCIF and PlanPtycho do not need quantem.gpu at runtime.

What should reviewer(s) do?

This is a draft until quantEM PR #297 is released. DiffractionSim, the ShowCIF potential preview and PlanPtycho now need quantem.diffraction from that PR. The quantem>= pin cannot be raised before the release, so their tests skip with that requirement, and the docs CI will fail on the PlanPtycho tutorial until quantEM is installed from #297.

Please review:

  • DiffractionSim against the widget conventions: naming (diffractionsim), the export bridge, state_dict / save / load_state_dict, save_state with a frontend PNG fallback, validators, and the Title Case UI (src/quantem/widget/diffraction_sim.py, js/diffractionsim/).
  • The abTEM replacements in src/quantem/widget/_lobato.py, _showcif_potential.py and planptycho.py, and the reference values in tests/test_showcif_potential.py and tests/planptycho/.

Verification

  • tests/diffractionsim: 53 passed with quantEM from PR #297 (52 passed and 1 skipped without quantem.gpu installed). All DiffractionSim cases pass in tests/test_html_export_protocol.py and tests/test_save_state.py.
  • ShowCIF and PlanPtycho tests: 81 passed in an environment where import abtem fails.
  • abTEM 1.0.10 comparison (run in a throwaway environment): scattering factors to 1.2e-5, radial tables to 7e-6 of the peak, interaction constant to 6e-9, and identical oriented cells for 8 crystals and 11 zone axes each.
  • npx vitest run js/diffractionsim and js/showcif: pass. tsc reports no new errors.
  • JupyterLab: the widget renders; Export PNG and Export HTML work through the bridge ("Ready ... (1.1 MB, single file)"); the static fallback PNG reaches Python; Compute Reference renders the Kikuchi reference pattern.
  • Not checked: Copy to the clipboard and the JupyterLab dark theme.

Generated with Claude Code

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
  • Keep cophus/quantem.widget:diffraction until this PR is merged.
  • After merge, delete cophus/quantem.widget:diffraction if you own it and no other PR uses it.
  • Then remove its dedicated worktree and local diffraction branch and update local main.

@bobleesj

bobleesj commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

Either we can wait for architecture refactoring or merge now. up to you. probably I can do this during the weekend or so.

This branch has not been deployed

No deployments
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