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>
CDTools
CDTools is a python library for ptychography and CDI reconstructions, using an Automatic Differentiation based approach.
from matplotlib import pyplot as plt
from cdtools.datasets import Ptycho2DDataset
from cdtools.models import FancyPtycho
# Load a data file
dataset = Ptycho2DDataset.from_cxi('ptycho_data.cxi')
# Initialize a model from the data
model = FancyPtycho.from_dataset(dataset)
# Run a reconstruction
for loss in model.Adam_optimize(10, dataset):
print(model.report())
# Save the results
model.save_to_h5('ptycho_results.h5', dataset)
# And look at them!
model.inspect(dataset) # See the reconstructed object, probe, etc.
model.compare(dataset) # See how the simulated and measured patterns compare
plt.show()
Further documentation is found here.
Instructions for building custom documentation based on a specific version or commit can be found here.
Installation
CDTools can be installed in several ways depending on your needs. For most users, installation from pypi is recommended. For developers or those who want the latest features, installation from source is available.
Installation from pypi
CDTools can be installed via pip as the cdtools-py package on PyPI:
$ pip install cdtools-py
or using uv:
$ uv pip install cdtools-py
Installation from Source
For development or to access the latest features, CDTools can be installed directly from source:
$ git clone https://github.com/cdtools-developers/cdtools.git
$ cd cdtools
$ pip install -e .
or using uv:
$ git clone https://github.com/cdtools-developers/cdtools.git
$ cd cdtools
$ uv pip install -e .
Installing for Contributors (with tests and docs dependencies)
If you want to run the test suite or build the documentation, install with the extra dependencies:
$ pip install -e ."[tests,docs]"
or with uv:
$ uv pip install -e ."[tests,docs]"
CDTools was developed in the photon scattering lab at MIT, and further development took place within the computational x-ray imaging group at PSI. The code is distributed under an MIT (a.k.a. Expat) license. If you would like to publish any work that uses CDTools, please contact Abe Levitan.
Have a wonderful day!