Make big changes to docs to update the tutorial section, and also to finally autoload the documentation of the models and datasets

This commit is contained in:
Abe Levitan committed 2024-09-01 13:04:28 +02:00
1 parent 30a989fe3e
commit 13a2ba86df
22 files changed
+381 -1532

No files matched your search

@@ -1,4 +1,3 @@
import numpy as np
import torch as t
from matplotlib import pyplot as plt
from cdtools.datasets import CDataset
@@ -14,7 +13,7 @@ class BasicPtychoDataset(CDataset):
def __init__(self, translations, patterns, *args, **kwargs):
"""Initialize the dataset from python objects"""
super(BasicPtychoDataset,self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self.translations = t.Tensor(translations).clone()
self.patterns = t.Tensor(patterns).clone()
@@ -27,7 +26,7 @@ class BasicPtychoDataset(CDataset):
def to(self, *args, **kwargs):
"""Sends the relevant data to the given device and dtype"""
super(BasicPtychoDataset,self).to(*args,**kwargs)
super().to(*args,**kwargs)
self.translations = self.translations.to(*args, **kwargs)
self.patterns = self.patterns.to(*args, **kwargs)
@@ -55,7 +54,7 @@ class BasicPtychoDataset(CDataset):
def to_cxi(self, cxi_file):
"""Saves out a BasicPtychoDataset as a .cxi file"""
super(BasicPtychoDataset,self).to_cxi(cxi_file)
super().to_cxi(cxi_file)
cdtdata.add_data(cxi_file, self.patterns, axes=self.axes)
cdtdata.add_ptycho_translations(cxi_file, self.translations)
@@ -63,6 +62,6 @@ class BasicPtychoDataset(CDataset):
def inspect(self):
"""Plots a random diffraction pattern"""
index = np.random.randint(len(self))
index = t.randint(len(self), (1,))[0]
plt.figure()
plt.imshow(self.patterns[index,:,:].cpu().numpy())
+23
View File
@@ -0,0 +1,23 @@
from tutorial_basic_ptycho_dataset import BasicPtychoDataset
from tutorial_simple_ptycho import SimplePtycho
from h5py import File
from matplotlib import pyplot as plt
filename = 'example_data/lab_ptycho_data.cxi'
with File(filename, 'r') as f:
dataset = BasicPtychoDataset.from_cxi(f)
dataset.inspect()
model = SimplePtycho.from_dataset(dataset)
model.to(device='cuda')
dataset.get_as(device='cuda')
for loss in model.Adam_optimize(10, dataset):
model.inspect(dataset)
print(model.report())
model.inspect(dataset)
model.compare(dataset)
plt.show()
+140
View File
@@ -0,0 +1,140 @@
import torch as t
from cdtools.models import CDIModel
from cdtools import tools
from cdtools.tools import plotting as p
__all__ = ['SimplePtycho']
class SimplePtycho(CDIModel):
"""A simple ptychography model to demonstrate the structure of a model
"""
def __init__(
self,
wavelength,
probe_basis,
probe_guess,
obj_guess,
min_translation = [0,0],
):
# We initialize the superclass
super().__init__()
# We register all the constants, like wavelength, as buffers. This
# lets the model hook into some nice pytorch features, like using
# model.to, and broadcasting the model state across multiple GPUs
self.register_buffer('wavelength', t.as_tensor(wavelength))
self.register_buffer('min_translation', t.as_tensor(min_translation))
self.register_buffer('probe_basis', t.as_tensor(probe_basis))
# We cast the probe and object to 64-bit complex tensors
probe_guess = t.as_tensor(probe_guess, dtype=t.complex64)
obj_guess = t.as_tensor(obj_guess, dtype=t.complex64)
# We rescale the probe here so it learns at the same rate as the
# object when using optimizers, like Adam, which set the stepsize
# to a fixed maximum
self.register_buffer('probe_norm', t.max(t.abs(probe_guess)))
# And we store the probe and object guesses as parameters, so
# they can get optimized by pytorch
self.probe = t.nn.Parameter(probe_guess / self.probe_norm)
self.obj = t.nn.Parameter(obj_guess)
@classmethod
def from_dataset(cls, dataset):
# We get the key geometry information from the dataset
wavelength = dataset.wavelength
det_basis = dataset.detector_geometry['basis']
det_shape = dataset[0][1].shape
distance = dataset.detector_geometry['distance']
# Then, we generate the probe geometry
ewg = tools.initializers.exit_wave_geometry
probe_basis = ewg(det_basis, det_shape, wavelength, distance)
# Next generate the object geometry from the probe geometry and
# the translations
(indices, translations), patterns = dataset[:]
pix_translations = tools.interactions.translations_to_pixel(
probe_basis,
translations,
)
obj_size, min_translation = tools.initializers.calc_object_setup(
det_shape,
pix_translations,
)
# Finally, initialize the probe and object using this information
probe = tools.initializers.SHARP_style_probe(dataset)
obj = t.ones(obj_size).to(dtype=t.complex64)
return cls(
wavelength,
probe_basis,
probe,
obj,
min_translation=min_translation
)
def interaction(self, index, translations):
# We map from real-space to pixel-space units
pix_trans = tools.interactions.translations_to_pixel(
self.probe_basis,
translations)
pix_trans -= self.min_translation
# This function extracts the appropriate window from the object and
# multiplies the object and probe functions
return tools.interactions.ptycho_2D_round(
self.probe_norm * self.probe,
self.obj,
pix_trans)
def forward_propagator(self, wavefields):
return tools.propagators.far_field(wavefields)
def measurement(self, wavefields):
return tools.measurements.intensity(wavefields)
def loss(self, real_data, sim_data):
return tools.losses.amplitude_mse(real_data, sim_data)
# This lists all the plots to display on a call to model.inspect()
plot_list = [
('Probe Amplitude',
lambda self, fig: p.plot_amplitude(self.probe, fig=fig, basis=self.probe_basis)),
('Probe Phase',
lambda self, fig: p.plot_phase(self.probe, fig=fig, basis=self.probe_basis)),
('Object Amplitude',
lambda self, fig: p.plot_amplitude(self.obj, fig=fig, basis=self.probe_basis)),
('Object Phase',
lambda self, fig: p.plot_phase(self.obj, fig=fig, basis=self.probe_basis))
]
def save_results(self, dataset):
# This will save out everything needed to recreate the object
# in the same state, but it's not the best formatted.
base_results = super().save_results()
# So we also save out the main results in a more useable format
probe_basis = self.probe_basis.detach().cpu().numpy()
probe = self.probe.detach().cpu().numpy()
probe = probe * self.probe_norm.detach().cpu().numpy()
obj = self.obj.detach().cpu().numpy()
wavelength = self.wavelength.cpu().numpy()
results = {
'probe_basis': probe_basis,
'probe': probe,
'obj': obj,
'wavelength': wavelength,
}
return {**base_results, **results}