Skip to content

Docstring for API documentation + purity PIE for z and wavelength calibration - #120

Merged
ShantanuKodgirwar merged 42 commits into
PtyLab:mainfrom
cliulucien:API_doc_Engines
Oct 15, 2026
Merged

ShantanuKodgirwar merged 42 commits into
PtyLab:mainfrom
cliulucien:API_doc_Engines

Conversation

@cliulucien

@cliulucien cliulucien commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR improves the documentation and organization of the PtyLab reconstruction engines.

The main goal is to make the engine API easier to understand by documenting the underlying reconstruction strategies, clarifying engine-specific parameters and update rules, and reducing duplicated legacy implementations.

Main changes

Engine documentation

Expanded and revised docstrings for the reconstruction engines, including:

  • purityPIE

    • documented purity-based axial-distance and wavelength calibration
    • documented fixed-grid and adaptive search strategies
    • clarified mixed-state probe purity and calibration workflow
    • added relevant literature references
  • aPIE

    • documented detector-angle calibration and momentum-assisted angle search
    • clarified the detector-coordinate transformation and reconstruction flow
    • added the original aPIE reference
  • e3PIE

    • documented the multislice reconstruction model
    • described inter-slice angular-spectrum propagation
    • clarified forward propagation and backward slice updates
  • OPR

    • documented Orthogonal Probe Relaxation and position-dependent probe reconstruction
    • described the low-rank probe subspace formulation
    • clarified the distinction between mixed-state probe modes and position-dependent probe variations
    • added relevant OPR and mixed-state OPR references
    • documented the current GPU/CuPy requirement
  • qNewton

    • documented the locally weighted and regularized update rules
    • marked the current implementation as experimental
  • mqNewton

    • documented the qNewton-style updates combined with momentum acceleration
    • documented momentum, ADAM-inspired, and NADAM-inspired update schemes

Legacy engine cleanup

multiPIE has been consolidated into mPIE.

Mixed-state reconstruction is already supported through the general
reconstruction dimensions such as:

  • reconstruction.npsm
  • reconstruction.nosm
  • reconstruction.nlambda

The former multiPIE implementation largely duplicated the regularized
object/probe updates and momentum scheme already available in mPIE.

A backward-compatible multiPIE wrapper is retained so that existing code
using Engines.multiPIE continues to work while emitting a deprecation
warning.
Legacy parameter aliases are preserved:

  • betaM -> feedbackM
  • stepM -> frictionM

Tests

Added tests for the deprecated multiPIE wrapper, including:

  • inheritance from mPIE
  • deprecation warning
  • legacy parameter aliases
  • preservation of momentum-acceleration behavior
  • public exposure through Engines.multiPIE

The purityPIE adaptive and fixed calibration logic is also covered by
tests using synthetic objective functions without running full
ptychographic reconstructions.

Notes / follow-up work

During the documentation pass, several implementation details were identified
that should be handled separately and are now mentioned in different issues.

% EDIT (Shantanu): Moved those specific implementation questions and such to github issues to better track and follow-up.

Chang Liu and others added 23 commits September 30, 2026 17:54
…arison

- add fixed and adaptive wavelength calibration
- add local quadratic fitting of purity scan maxima
- store and plot reconstruction error alongside probe purity
- unify scan plotting for z and wavelength calibration
- update the example script for selectable z or wavelength calibration
minor change in reconstruction.py:  the old style np.int.
1. OPR engine documentation
2. remove redundant multiPIE engine, added a wrapper in mPIE.
3. add test for the wrapper.
4. minor change of pytest name.
Comment thread PtyLab/Params/Params.py
# map a change in positions to a change in z. Experimental, do not use
self.map_position_to_z_change = False

# purity based Zscan

@ShantanuKodgirwar ShantanuKodgirwar Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't the purityPIE part of the other PR? @cliulucien

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, this one only for the documentation. I merged that purityPIE to this branch earlier

@ShantanuKodgirwar ShantanuKodgirwar Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Generally, it is not a good idea to add things that logically belong to another PR. This PR should focus only on exactly what it is doing here. If you absolutely need things from that another branch/PR then you could either add only those things here or cherry-pick commit (a bit annoying to work with though) or simply put a specific PR in draft and let the other one merge first and then you sync this particular branch/PR with main.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah this file (3K lines) does not belong in this PR for instance. I will merge the other PR first.

@ShantanuKodgirwar

ShantanuKodgirwar commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

@cliulucien

I updated this PR with quite many things including changing the example script. It now uses synthetic data if a stored dataset is not provided. But mainly the purityPIE engine was changed in these regards:

  • No changes as z or wavelength varies. mPIE in the end gets an No which would be different than at the start. Now the best candidate's object grid No is restored with its object and used for a standard mPIE run. Earlier saving after a calibration used to break or give mismatched sizes.
  • We now specify the same seed for every candidate or else re-run could change the answer.
  • A bug fix that compared each new point with itself, so it never saw an improvement. That is fixed now for the adaptive refinement.
  • There is no more params.plotPurityScan, now the purity plot gets updated with iterations and the final figure can be stored anyway with reconstruction.saveResults(snapshot=monitor) along with the hdf5 files recording purity with z. Also I feel params should not really be mixed with monitor-like utilities preferably.
  • Most importantly there is no fitted curve. Fitting a parabola doesn't make sense if the plot is not one and if there is no distinct peak, it can totally throw off with wrong answer. See the below image. After your runs, you could potentially fit whatever, but allowing this as a user option is a bad idea in my opinion.
image

@ShantanuKodgirwar

ShantanuKodgirwar commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

A gif to show the updates happen live with iterations and in the last one, highlights the result and also get stored as a png if you pass snapshot=monitor (earlier PR that was merged to main): Ex: reconstruction.saveResults("filename.hdf5", snapshot=monitor).

purity_vs_z

@cliulucien could you please validate once with your dataset and tell me how it is. Then we can merge this PR.

@ShantanuKodgirwar ShantanuKodgirwar changed the title API documentation for engines and cleanup legacy PIE implementations Adding purityPIE and cleanup legacy implementations Oct 12, 2026
@ShantanuKodgirwar ShantanuKodgirwar changed the title Adding purityPIE and cleanup legacy implementations API changes + purity PIE Oct 12, 2026
@ShantanuKodgirwar

ShantanuKodgirwar commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

@cliulucien I will close the other PR #118 and just merge this one after you validate with your dataset. The two have drifted and doesn't make sense to spend time and fix the other one. But in general please keep separate PRs for separate things and store one in draft if the other one is waiting to merge

@ShantanuKodgirwar ShantanuKodgirwar changed the title API changes + purity PIE Docstring for API documentation + purity PIE for z and wavelength calibration Oct 12, 2026
@cliulucien

Copy link
Copy Markdown
Contributor Author
Recons Z_scan_spore

The latest functions of adaptive purityPIE have been tested with an experimental dataset of biological specimens and work normally, except the probe window shows some random things, but the purity value is correct. I will check the settings a bit, but I think it can be merged already.

@ShantanuKodgirwar

Copy link
Copy Markdown
Contributor

Nice. LGTM!

@ShantanuKodgirwar
ShantanuKodgirwar merged commit 55b356c into PtyLab:main Oct 15, 2026
6 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