Skip to content

Improve saving and loading of in-progress reconstructions - #77

Merged
allevitan merged 13 commits into
masterfrom
savenload
Jun 17, 2026
Merged

Improve saving and loading of in-progress reconstructions#77
allevitan merged 13 commits into
masterfrom
savenload

Conversation

@allevitan

@allevitan allevitan commented Apr 13, 2026

Copy link
Copy Markdown
Collaborator

This PR will implement a better system for loading reconstructions from the saved results files as .h5

This touches a lot of outstanding needs:

  • Helping with Better Initialization #51 , by enabling easy and reliable loading of saved .h5 files back into python models
  • Helping with rebuilding the checkpointing system to work off of .h5 files
  • Enabling easy continuation of existing reconstructions after they are saved
  • Making it simpler to view results using the same viewer which is available with an in-progress model.

To-dos:

  • Create a base system for how it will work in a generic model
    • Save the translations in the model, when needed
    • Side benefit: update the plotting and saving functions to no longer require the original dataset
    • Add a default way to save and load the units and the object view crop
  • Update all models
    • SimplePtycho
    • Tutorial SimplePtycho
    • FancyPtycho
    • Bragg2DPtycho
    • Multislice2DPtycho
    • MultislicePtycho
    • RPI
  • Update the example files to include an example of loading an in-progress reconstruction
  • While doing so, update all the saved in-progress results in the examples folder to be the latest style (including the ipython notebooks)
  • Update the documentation to match the new examples
  • Check for test coverage of all models

allevitan and others added 3 commits April 13, 2026 15:47
…iles

Adds two classmethods to CDIModel and its ptychography subclasses that
allow models to be reconstructed from saved .h5 result files without
needing the original dataset.

- CDIModel base class: add model_class and cdtools_version to
  save_results(); add _load_results_dict() helper (restores state_dict
  + training metadata); add from_results_dict() interface method and a
  concrete from_results_h5() that reads a file and delegates to
  from_results_dict() — subclasses inherit this for free.

- SimplePtycho: make save_results(dataset=None) optional (dataset was
  accepted but never used); add from_results_dict() which reconstructs
  from probe, obj, wavelength, probe_basis, and min_translation stored
  in the state_dict.

- FancyPtycho: add optional translations parameter to __init__,
  registered as original_translations buffer; update from_dataset to
  pass translations; make corrected_translations(dataset=None) fall back
  to self.original_translations when no dataset is provided; make
  save_results(dataset=None) use stored translations when no dataset is
  provided; add from_results_dict() that detects all optional features
  (mask, translation_offsets, weights, near-field propagators, etc.)
  from the state_dict and reconstructs the full model. Also fix a latent
  bug where background was passed to t.nn.Parameter() without
  t.as_tensor(), which now fails when given a numpy array.

All existing tests pass; new tests added for both models verifying that
state_dict, training metadata, and forward pass output are all restored
exactly after a round-trip through from_results_dict and from_results_h5.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ults(), and update the examples accordingly. Docs and ipython notebooks not yet updated
…dd the obj_view_crop and units arguments back
@allevitan
allevitan marked this pull request as ready for review June 17, 2026 13:13
@allevitan

Copy link
Copy Markdown
Collaborator Author

I think this is useful enough and close enough to be merged into master - loading obj_view_crop is not critical, Multislice2DPtycho is depricated anyway, and the additional models don't have any test coverage yet. Adding that coverage is it's own, separate project

@allevitan allevitan left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewed all the changes from yesterday. I hate to merge into master so quickly, but the vast majority of this work comes from >1 month ago, self-reviewed yesterday with the final, primarily cosmetic, changes to update all the documentations and scripts. I will merge

@allevitan
allevitan merged commit 6faade0 into master Jun 17, 2026
9 checks passed
@allevitan
allevitan deleted the savenload branch June 17, 2026 13:23
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.

1 participant