Improve plotting infrastructure and fix various bugs

- Switch panel plots to use subfigures with constrained_layout for better layout management
- Add plot_loss_history method to CDIModel base class
- Fix matplotlib compatibility for older versions (interactive backend detection)
- Make CUDA usage conditional in examples
- Add panel_plot_mode and plot_level params to FancyPtycho constructor
- Default plot_level filtering to 1 instead of 0

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
allevitanandClaude Sonnet 4.6 committed 2026-03-20 15:33:49 +01:00
1 parent 44fe36d24f
commit dd744a00a4
6 files changed
+386 -265

No files matched your search

+10 -5
View File
@@ -1,4 +1,5 @@
import cdtools
import torch as t
from matplotlib import pyplot as plt
filename = 'example_data/lab_ptycho_data.cxi'
@@ -12,12 +13,15 @@ model = cdtools.models.FancyPtycho.from_dataset(
probe_support_radius=120, # Force the probe to 0 outside a radius of 120 pix
propagation_distance=5e-3, # Propagate the initial probe guess by 5 mm
units='mm', # Set the units for the live plots
obj_view_crop=-50, # Expands the field of view in the object plot by 50 pix
obj_view_crop=-50, # Expands the field of view in the object plot by 50 pix,
exponentiate_obj=False,
panel_plot_mode=True,
plot_level=2,
)
device = 'cuda'
model.to(device=device)
dataset.get_as(device=device)
if t.cuda.is_available():
model.to(device='cuda')
dataset.get_as(device='cuda')
# For this script, we use a slightly different pattern where we explicitly
# create a `Reconstructor` class to orchestrate the reconstruction. The
@@ -26,13 +30,14 @@ dataset.get_as(device=device)
# e.g. estimates of the moments of individual parameters
recon = cdtools.reconstructors.AdamReconstructor(model, dataset)
# The learning rate parameter sets the alpha for Adam.
# The beta parameters are (0.9, 0.999) by default
# The batch size sets the minibatch size
for loss in recon.optimize(50, lr=0.02, batch_size=10):
print(model.report())
# Plotting is expensive, so we only do it every tenth epoch
if model.epoch % 10 == 0:
if model.epoch % 2 == 0:
model.inspect(dataset)
# It's common to chain several different reconstruction loops. Here, we
+6 -6
View File
@@ -8,6 +8,7 @@ more powerful FancyPtycho model and include more information on how to
correct for common sources of error.
"""
import cdtools
import torch as t
from matplotlib import pyplot as plt
# We load an example dataset from a .cxi file
@@ -15,12 +16,12 @@ filename = 'example_data/lab_ptycho_data.cxi'
dataset = cdtools.datasets.Ptycho2DDataset.from_cxi(filename)
# We create a ptychography model from the dataset
model = cdtools.models.SimplePtycho.from_dataset(dataset, panel_plot_mode=True)
model = cdtools.models.SimplePtycho.from_dataset(dataset)
# We move the model to the GPU
device = 'cuda'
model.to(device=device)
dataset.get_as(device=device)
# We move the model to the GPU, if possible
if t.cuda.is_available():
model.to(device='cuda')
dataset.get_as(device='cuda')
model.inspect(dataset)
print('hi')
@@ -32,7 +33,6 @@ for loss in model.Adam_optimize(30, dataset, batch_size=10):
if model.epoch % 10 == 0:
model.inspect(dataset)
# We study the results
model.inspect(dataset, replot_all=True)
model.compare(dataset)