Skip to content

Add polar transform and pair distribution function analysis#177

Draft
ehrhardtkm wants to merge 10 commits intoelectronmicroscopy:nanobeamfrom
ehrhardtkm:polar4dstem
Draft

Add polar transform and pair distribution function analysis#177
ehrhardtkm wants to merge 10 commits intoelectronmicroscopy:nanobeamfrom
ehrhardtkm:polar4dstem

Conversation

@ehrhardtkm
Copy link

What does this PR do?

Implementation of polar coordinate transforms and computing pair distribution functions (PDFs) for 4DSTEM diffraction data. The PDF relies on a polar transformed data structure, which has been implemented in Polar4dstem. The code will be updated in the future with other useful tools, like beam stop masking and elliptical corrections.

The workflow takes a 4DSTEM dataset through the following steps:

  1. Origin finding — Automatically locate the center of each diffraction pattern by minimizing angular intensity variation (auto_origin_id)
  2. Polar transform — Convert diffraction patterns from Cartesian to polar coordinates using torch grid_sample for GPU acceleration (dataset4dstem_polar_transform)
  3. PDF calculation — Compute the reduced PDF G(r) from the azimuthally averaged intensity, including background fitting, structure factor extraction, and a windowed sine transform (calculate_Gr)
  4. Density estimation (optional) — Iteratively estimate atomic number density and dampen unphysical low-r oscillations using the Yoshimoto-Omote method (estimate_density, calculate_gr)

New files

  • polar4dstem.pyPolar4dstem dataset class for polar-coordinate 4D-STEM data, plus the origin finding and polar transform functions
  • polar.py — Contains PairDistributionFunction class. Handles the full PDF pipeline: radial averaging, background fitting, structure factor calculation, windowed sine transform to G(r), iterative density estimation, and conversion to g(r)
  • tests/diffraction/test_polar.py — Tests covering construction, transforms, background fitting, PDF calculation, density estimation, and end-to-end workflows

Modified files

  • dataset4dstem.py — Added polar_transform() convenience method on Dataset4dstem
  • __init__.py files — Exposed new classes and modules

A companion tutorial notebook (PDFanalysis_simulatedTa.ipynb) demonstrating an example workflow on simulated amorphous Ta data is available.


What should the reviewer(s) do?

Provide feedback on the API design and implementation. Key areas for review:

  • Polar4dstem class design and its relationship to Dataset4dstem
  • PDF pipeline correctness (background subtraction, structure factor normalization, Fourier transform)
  • Ensure the example notebook runs correctly
  • Note any design patterns that were not followed or potential bugs

Checklist items:

  • This PR introduces a public-facing change (e.g., API, CLI input/output).
    • For functional and algorithmic changes, tests are written or updated.
    • Documentation (e.g., tutorials, examples, README) has been updated.
    • A tracking issue or plan to update documentation exists.
  • This PR affects internal functionality only (no user-facing change).

Reviewer checklist

  • Tests pass and are appropriate for the changes
  • Documentation and examples are sufficient and clear
  • Imported or adapted code has a compatible license
  • The implementation matches the stated intent of the contribution

@bobleesj
Copy link
Collaborator

I'm happy to review this PR since this would naturally extend along Show4D widget that you could extend and I was a PDF group for x-ray.

If possible, if you could copy/paste API design here as a comment and attach screenshots (plots, before and after) - these can be helpful as well. API design, as a public facing component, is becoming more important since those are what humans mostly these days.

The attached notebook is always good for the reviewers, but PRs contain global URLs so it's a great place to market/demo to the whole group and the world.

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.

3 participants