From 3a1888ebe69cfec474435cac2e907146dfaacdda Mon Sep 17 00:00:00 2001 From: Ming Du Date: Tue, 17 Nov 2026 15:56:29 -0600 Subject: [PATCH] REFACTOR: move magnitude projection into noise models --- src/ptychi/api/options/pie.py | 3 + src/ptychi/forward_models.py | 116 ++++++++++++++++++-- src/ptychi/reconstructors/base.py | 85 +++----------- src/ptychi/reconstructors/dm.py | 3 +- src/ptychi/reconstructors/lsqml.py | 19 ---- src/ptychi/reconstructors/magpie.py | 3 +- src/ptychi/reconstructors/pie.py | 9 +- src/ptychi/reconstructors/raar.py | 3 +- tests/test_detector_response_updates.py | 17 ++- tests/test_exclude_measured_pixels_below.py | 20 ++-- tests/test_noise_models.py | 61 ++++++++++ tests/test_projection_numerics.py | 15 ++- 12 files changed, 227 insertions(+), 127 deletions(-) create mode 100644 tests/test_noise_models.py diff --git a/src/ptychi/api/options/pie.py b/src/ptychi/api/options/pie.py index 07808d71..663a747b 100644 --- a/src/ptychi/api/options/pie.py +++ b/src/ptychi/api/options/pie.py @@ -13,6 +13,9 @@ @dataclass class PIEReconstructorOptions(base.ReconstructorOptions): + + displayed_loss_function: enums.LossFunctions | None = None + """Displayed metric; None uses the magnitude mean squared noise-model loss.""" def get_reconstructor_type(self) -> enums.Reconstructors: return enums.Reconstructors.PIE diff --git a/src/ptychi/forward_models.py b/src/ptychi/forward_models.py index b7412066..23a75f45 100644 --- a/src/ptychi/forward_models.py +++ b/src/ptychi/forward_models.py @@ -1259,6 +1259,91 @@ def nll(self, y_pred: Tensor, y_true: Tensor, reduction: str = "mean") -> Tensor """ raise NotImplementedError + def project_to_propagated_magnitude_constraint( + self, + psi: Tensor, + actual_pattern_intensity: Tensor, + constrained_pixel_mask: Optional[Tensor] = None, + *, + forward_model: Optional[PlanarPtychographyForwardModel] = None, + ) -> Tensor: + """ + Project the propagated exit wave onto the measured intensity constraint. + + With no blur, the target wave intensity is + max((data - offset) / gain, 0). Phase and relative mode amplitudes + are preserved. Masked and unmeasured pixels remain unchanged. + Data below the offset project to zero intensity. Both grids use internal + FFT ordering; detector pixels are zero-inserted by the crop adjoint. + A fixed identity response on a matching grid retains the historical 1e-7 + amplitude stabilization, including its zero-wave behavior. Other + cases use exact norm replacement, choosing the first mode's phase + as zero when the input wave vanishes. Blur requires LSQML or autodiff. + + Parameters + ---------- + psi : Tensor + Complex detector-plane wave, shaped (batch, incoherent_modes, H, W). + actual_pattern_intensity : Tensor + Measured intensities, shaped (batch, detector_height, detector_width). + constrained_pixel_mask : Tensor, optional + If given, only pixels where this mask is True are constrained. Other pixels + are left unchanged from `psi`. Defaults to the noise model's valid-pixel + mask combined with its measured-intensity threshold. + forward_model : PlanarPtychographyForwardModel, optional + Supplies detector response and crop geometry for the projection. + If omitted, measurements must be on the wave grid and are assumed + to have no blur, gain or offset correction, or crop. + + Returns + ------- + Tensor + Exit wave with the measured total intensity, or the inverse-response + intensity when a response map is present, on constrained detector pixels. + """ + + wave_shape = psi.shape[-2:] + same_grid = wave_shape == actual_pattern_intensity.shape[-2:] + target = actual_pattern_intensity + identity_response = True + if forward_model is not None: + if forward_model.diffraction_pattern_blur_sigma is not None: + raise ValueError( + "Magnitude projection requires diffraction_pattern_blur_sigma=None. " + "Use LSQML or autodiff for blur." + ) + response = forward_model.detector_response + identity_response = response.is_identity + if not identity_response: + target = (target - response.offset) / response.gain + elif not same_grid: + raise ValueError( + "Pass forward_model to magnitude projection when detector and wave grids differ." + ) + target = target.clamp_min(0) + mask = ( + get_constrained_pixel_mask( + self.valid_pixel_mask, self.exclude_measured_pixels_below, + actual_pattern_intensity, + ) + if constrained_pixel_mask is None else constrained_pixel_mask + ) + if forward_model is not None: + target = forward_model.detector_crop_adjoint(target, wave_shape) + mask = forward_model.detector_crop_adjoint(mask, wave_shape) + intensity = psi.abs().square().sum(1) + if identity_response and same_grid: + # Preserve the original same-grid amplitude stabilization. + psi_prime = ( + psi / (intensity.sqrt()[:, None] + 1e-7) * (target + 1e-7).sqrt()[:, None] + ) + else: + scale = (target / torch.where(intensity > 0, intensity, 1)).sqrt() + psi_prime = psi * scale[:, None] + # A zero wave has no phase; choose the first mode to satisfy the norm. + psi_prime[:, 0] = torch.where(intensity > 0, psi_prime[:, 0], target.sqrt()) + return torch.where(mask[:, None] if mask.ndim == 3 else mask, psi_prime, psi) + def backward(self, *args, **kwargs): raise NotImplementedError @@ -1301,23 +1386,36 @@ def backward_to_psi_far( return gradient[:, None] * psi_far -class GaussianNoiseModel(NoiseModel): +class MagnitudeMeanSquaredNoiseModel(NoiseModel): + """Mean squared amplitude residual for intensity inputs. + + The NLL is MSE(sqrt(y_pred + 1e-7), sqrt(y_true + 1e-7)), with the + requested reduction. Callers select constrained pixels before evaluation. + """ + + def __init__(self, *args, **kwargs) -> None: + super().__init__(*args, **kwargs) + self.loss_function = MSELossOfSqrt() + + def nll(self, y_pred: Tensor, y_true: Tensor, reduction: str = "mean") -> Tensor: + validate_predicted_intensity(y_pred) + return torch.nn.functional.mse_loss( + torch.sqrt(y_pred + self.loss_function.eps), + torch.sqrt(y_true + self.loss_function.eps), + reduction=reduction, + ) + + +class GaussianNoiseModel(MagnitudeMeanSquaredNoiseModel): def __init__(self, sigma: float = 0.5, eps: float = 1e-9, *args, **kwargs) -> None: super().__init__(eps=eps, *args, **kwargs) self.noise_statistics = "gaussian" self.sigma = sigma - self.loss_function = MSELossOfSqrt() def nll(self, y_pred: Tensor, y_true: Tensor, reduction: str = "mean") -> Tensor: # This is averaged over all pixels, so it differs from Eq. 11a in Odstrcil (2018) # by a factor of 1 / y_pred.numel(). - validate_predicted_intensity(y_pred) - loss = torch.nn.functional.mse_loss( - torch.sqrt(y_pred + self.loss_function.eps), - torch.sqrt(y_true + self.loss_function.eps), - reduction=reduction, - ) / self.sigma**2 * 0.5 - return loss + return super().nll(y_pred, y_true, reduction=reduction) / self.sigma**2 * 0.5 class PtychographyGaussianNoiseModel(GaussianNoiseModel): diff --git a/src/ptychi/reconstructors/base.py b/src/ptychi/reconstructors/base.py index be94ae7a..2b0ab895 100644 --- a/src/ptychi/reconstructors/base.py +++ b/src/ptychi/reconstructors/base.py @@ -19,7 +19,6 @@ import ptychi.api.enums as enums import ptychi.io_handles as io import ptychi.image_proc as ip -from ptychi.metrics import MSELossOfSqrt from ptychi.parallel import MultiprocessMixin if TYPE_CHECKING: import ptychi.data_structures.parameter_group as pg @@ -733,6 +732,10 @@ def __init__( ) self.forward_model = None self.build_forward_model() + self.noise_model = fm.MagnitudeMeanSquaredNoiseModel( + valid_pixel_mask=self.dataset.valid_pixel_mask, + exclude_measured_pixels_below=self.options.exclude_measured_pixels_below, + ) def build_forward_model(self): self.forward_model = fm.PlanarPtychographyForwardModel( @@ -748,15 +751,23 @@ def build_forward_model(self): diffraction_pattern_blur_sigma=self.options.forward_model_options.diffraction_pattern_blur_sigma, ) + def build_loss_tracker(self): + f = ( + self.noise_model.nll + if self.displayed_loss_function is None + else self.displayed_loss_function + ) + self.loss_tracker = LossTracker(metric_function=f) + def compute_optimizer_loss(self, input_data, y_true): - """Use the masked mean squared amplitude residual for projection algorithms.""" + """Evaluate the masked likelihood of the configured noise model.""" y_pred = self.forward_model(*input_data) mask = get_constrained_pixel_mask( valid_pixel_mask=self.dataset.valid_pixel_mask, exclude_measured_pixels_below=self.options.exclude_measured_pixels_below, y_true=y_true, ) - return MSELossOfSqrt()(y_pred[mask], y_true[mask]) + return self.noise_model.nll(y_pred[mask], y_true[mask]) def stabilize_object_projection_denominator(self, denominator: Tensor) -> Tensor: """Floor full-object projection weights at 5% of their maximum. @@ -823,74 +834,6 @@ def prepare_data(self, *args, **kwargs): self.parameter_group.probe.normalize_eigenmodes() logger.info("Probe eigenmodes normalized.") - def replace_propagated_exit_wave_magnitude( - self, - psi: Tensor, - actual_pattern_intensity: Tensor, - constrained_pixel_mask: Optional[Tensor] = None, - ) -> Tensor: - """ - Project the propagated exit wave onto the measured intensity constraint. - - With no blur, the target wave intensity is - max((data - offset) / gain, 0). Phase and relative mode amplitudes - are preserved. Masked and unmeasured pixels remain unchanged. - Data below the offset project to zero intensity. Both grids use internal - FFT ordering; detector pixels are zero-inserted by the crop adjoint. - A fixed identity response on a matching grid retains the historical 1e-7 - amplitude stabilization, including its zero-wave behavior. Other - cases use exact norm replacement, choosing the first mode's phase - as zero when the input wave vanishes. Blur requires LSQML or autodiff. - - Parameters - ---------- - psi : Tensor - Predicted exit wave propagated to the detector plane. - actual_pattern_intensity : Tensor - The measured diffraction pattern at the detector. - constrained_pixel_mask : Tensor, optional - If given, only pixels where this mask is True are constrained. Other pixels - are left unchanged from `psi`. - - Returns - ------- - Tensor - Exit wave with the measured total intensity, or the inverse-response - intensity when a response map is present, on constrained detector pixels. - """ - - model = self.forward_model - if model.diffraction_pattern_blur_sigma is not None: - raise ValueError( - "Magnitude projection requires diffraction_pattern_blur_sigma=None. " - "Use LSQML or autodiff for blur." - ) - target = actual_pattern_intensity - if not model.detector_response.is_identity: - target = (target - model.detector_response.offset) / model.detector_response.gain - target = target.clamp_min(0) - mask = ( - self.dataset.valid_pixel_mask - if constrained_pixel_mask is None else constrained_pixel_mask - ) - target = model.detector_crop_adjoint(target, psi.shape[-2:]) - mask = model.detector_crop_adjoint(mask, psi.shape[-2:]) - intensity = psi.abs().square().sum(1) - if ( - model.detector_response.is_identity - and psi.shape[-2:] == actual_pattern_intensity.shape[-2:] - ): - # Preserve the original same-grid amplitude stabilization. - psi_prime = ( - psi / (intensity.sqrt()[:, None] + 1e-7) * (target + 1e-7).sqrt()[:, None] - ) - else: - scale = (target / torch.where(intensity > 0, intensity, 1)).sqrt() - psi_prime = psi * scale[:, None] - # A zero wave has no phase; choose the first mode to satisfy the norm. - psi_prime[:, 0] = torch.where(intensity > 0, psi_prime[:, 0], target.sqrt()) - return torch.where(mask[:, None] if mask.ndim == 3 else mask, psi_prime, psi) - @timer() def adjoint_shift_probe_update_direction(self, indices, delta_p, first_mode_only=None): """ diff --git a/src/ptychi/reconstructors/dm.py b/src/ptychi/reconstructors/dm.py index a4d98a09..547d56aa 100644 --- a/src/ptychi/reconstructors/dm.py +++ b/src/ptychi/reconstructors/dm.py @@ -244,7 +244,7 @@ def apply_dm_update_to_exit_wave_chunk( 2 * new_psi - self.psi[start_pt:end_pt] ) # Replace intensities - revised_psi = self.replace_propagated_exit_wave_magnitude( + revised_psi = self.noise_model.project_to_propagated_magnitude_constraint( revised_psi, y_true[start_pt:end_pt], constrained_pixel_mask=get_constrained_pixel_mask( @@ -252,6 +252,7 @@ def apply_dm_update_to_exit_wave_chunk( exclude_measured_pixels_below=self.options.exclude_measured_pixels_below, y_true=y_true[start_pt:end_pt], ), + forward_model=self.forward_model, ) # Propagate back to sample plane revised_psi = self.forward_model.free_space_propagator.propagate_backward(revised_psi) diff --git a/src/ptychi/reconstructors/lsqml.py b/src/ptychi/reconstructors/lsqml.py index 7771276d..2f9819d0 100644 --- a/src/ptychi/reconstructors/lsqml.py +++ b/src/ptychi/reconstructors/lsqml.py @@ -13,7 +13,6 @@ from ptychi.utils import get_constrained_pixel_mask from ptychi.reconstructors.base import ( AnalyticalIterativePtychographyReconstructor, - LossTracker, ) import ptychi.forward_models as fm from ptychi.metrics import validate_predicted_intensity @@ -311,24 +310,6 @@ def build(self) -> None: self.build_cached_variables() self.build_noise_model() - def compute_optimizer_loss(self, input_data, y_true): - """Evaluate the masked likelihood of the configured noise model.""" - y_pred = self.forward_model(*input_data) - mask = get_constrained_pixel_mask( - valid_pixel_mask=self.dataset.valid_pixel_mask, - exclude_measured_pixels_below=self.options.exclude_measured_pixels_below, - y_true=y_true, - ) - return self.noise_model.nll(y_pred[mask], y_true[mask]) - - def build_loss_tracker(self): - f = ( - self.noise_model.nll - if self.displayed_loss_function is None - else self.displayed_loss_function - ) - self.loss_tracker = LossTracker(metric_function=f) - def build_noise_model(self): self.noise_model = self.noise_model.to(torch.get_default_device()) diff --git a/src/ptychi/reconstructors/magpie.py b/src/ptychi/reconstructors/magpie.py index d42225be..90561b42 100644 --- a/src/ptychi/reconstructors/magpie.py +++ b/src/ptychi/reconstructors/magpie.py @@ -341,7 +341,7 @@ def compute_updates(self, indices: Tensor, y_true: Tensor): variables = self.forward_model.intermediate_variables object_old = variables["obj_patches"][:, :1] probe_old = variables.shifted_unique_probes[0] - psi_prime = self.replace_propagated_exit_wave_magnitude( + psi_prime = self.noise_model.project_to_propagated_magnitude_constraint( variables["psi_far"], y_true, constrained_pixel_mask=get_constrained_pixel_mask( @@ -349,6 +349,7 @@ def compute_updates(self, indices: Tensor, y_true: Tensor): exclude_measured_pixels_below=self.options.exclude_measured_pixels_below, y_true=y_true, ), + forward_model=self.forward_model, ) psi_prime = self.forward_model.free_space_propagator.propagate_backward(psi_prime) delta_psi = psi_prime - variables["psi"] diff --git a/src/ptychi/reconstructors/pie.py b/src/ptychi/reconstructors/pie.py index 21ad0ac0..96f9ea46 100644 --- a/src/ptychi/reconstructors/pie.py +++ b/src/ptychi/reconstructors/pie.py @@ -11,7 +11,6 @@ from ptychi.reconstructors.base import ( AnalyticalIterativePtychographyReconstructor, ) -from ptychi.metrics import MSELossOfSqrt from ptychi.timing.timer_utils import timer if TYPE_CHECKING: @@ -51,11 +50,6 @@ def __init__( **kwargs, ) - def build_loss_tracker(self): - if self.displayed_loss_function is None: - self.displayed_loss_function = MSELossOfSqrt() - return super().build_loss_tracker() - def check_inputs(self, *args, **kwargs): for var in self.parameter_group.get_optimizable_parameters(): if "lr" not in var.optimizer_params.keys(): @@ -93,7 +87,7 @@ def compute_updates( psi_far = self.forward_model.intermediate_variables["psi_far"] unique_probes = self.forward_model.intermediate_variables.shifted_unique_probes - psi_prime = self.replace_propagated_exit_wave_magnitude( + psi_prime = self.noise_model.project_to_propagated_magnitude_constraint( psi_far, y_true, constrained_pixel_mask=get_constrained_pixel_mask( @@ -101,6 +95,7 @@ def compute_updates( exclude_measured_pixels_below=self.options.exclude_measured_pixels_below, y_true=y_true, ), + forward_model=self.forward_model, ) psi_prime = self.forward_model.free_space_propagator.propagate_backward(psi_prime) diff --git a/src/ptychi/reconstructors/raar.py b/src/ptychi/reconstructors/raar.py index 160ae7b1..bf9019e0 100644 --- a/src/ptychi/reconstructors/raar.py +++ b/src/ptychi/reconstructors/raar.py @@ -190,7 +190,7 @@ def calculate_exit_wave_chunk(self, start_pt: int, end_pt: int) -> Tensor: @timer() def apply_data_projection(self, exit_wave: Tensor, y_true: Tensor) -> Tensor: propagated_exit_wave = self.forward_model.free_space_propagator.propagate_forward(exit_wave) - propagated_exit_wave = self.replace_propagated_exit_wave_magnitude( + propagated_exit_wave = self.noise_model.project_to_propagated_magnitude_constraint( propagated_exit_wave, y_true, constrained_pixel_mask=get_constrained_pixel_mask( @@ -198,6 +198,7 @@ def apply_data_projection(self, exit_wave: Tensor, y_true: Tensor) -> Tensor: exclude_measured_pixels_below=self.options.exclude_measured_pixels_below, y_true=y_true, ), + forward_model=self.forward_model, ) return self.forward_model.free_space_propagator.propagate_backward(propagated_exit_wave) diff --git a/tests/test_detector_response_updates.py b/tests/test_detector_response_updates.py index a82a9aa1..0e3294c8 100644 --- a/tests/test_detector_response_updates.py +++ b/tests/test_detector_response_updates.py @@ -82,16 +82,22 @@ def test_inverse_response_projection_with_crop_and_mask(response): measured = torch.full((2, 3, 5), 11.0) mask = torch.ones_like(measured, dtype=torch.bool) mask[0, 1, 2] = False - projected = rec.replace_propagated_exit_wave_magnitude(psi, measured, mask) + projected = rec.noise_model.project_to_propagated_magnitude_constraint( + psi, measured, mask, forward_model=model + ) constrained = mask prediction = model.map_intensity_to_detector(projected.abs().square().sum(1)) torch.testing.assert_close(prediction[constrained], measured[constrained]) full_mask = model.detector_crop_adjoint(constrained, psi.shape[-2:])[:, None].expand_as(psi) torch.testing.assert_close(projected[~full_mask], psi[~full_mask], rtol=0, atol=0) - repeated = rec.replace_propagated_exit_wave_magnitude(projected, measured, mask) + repeated = rec.noise_model.project_to_propagated_magnitude_constraint( + projected, measured, mask, forward_model=model + ) torch.testing.assert_close(repeated, projected, rtol=1e-12, atol=1e-12) below_offset = model.detector_response.offset[None].expand_as(measured) - 1 - zeroed = rec.replace_propagated_exit_wave_magnitude(psi, below_offset, mask) + zeroed = rec.noise_model.project_to_propagated_magnitude_constraint( + psi, below_offset, mask, forward_model=model + ) torch.testing.assert_close(zeroed[full_mask], torch.zeros_like(zeroed[full_mask])) @@ -99,8 +105,9 @@ def test_inverse_response_projection_with_crop_and_mask(response): def test_response_projection_rejects_blur(response): task = make_task(api.EPIEOptions, blur=0.4, response=response) with pytest.raises(ValueError, match="Magnitude projection.*requires"): - task.reconstructor.replace_propagated_exit_wave_magnitude( - torch.ones((2, 2, 6, 6), dtype=torch.complex128), task.dataset.patterns + task.reconstructor.noise_model.project_to_propagated_magnitude_constraint( + torch.ones((2, 2, 6, 6), dtype=torch.complex128), task.dataset.patterns, + forward_model=task.reconstructor.forward_model, ) diff --git a/tests/test_exclude_measured_pixels_below.py b/tests/test_exclude_measured_pixels_below.py index 7c5bff72..87f4ec3c 100644 --- a/tests/test_exclude_measured_pixels_below.py +++ b/tests/test_exclude_measured_pixels_below.py @@ -6,9 +6,8 @@ from ptychi.utils import get_constrained_pixel_mask from ptychi.api.options.base import ReconstructorOptions from ptychi.api.options.data import PtychographyDataOptions -from ptychi.forward_models import PtychographyGaussianNoiseModel, PtychographyPoissonNoiseModel -from ptychi.reconstructors.base import ( - AnalyticalIterativePtychographyReconstructor, +from ptychi.forward_models import ( + NoiseModel, PtychographyGaussianNoiseModel, PtychographyPoissonNoiseModel, ) from ptychi.reconstructors.bh import BHReconstructor @@ -19,9 +18,6 @@ class _DummyAnalyticalPtychographyReconstructor: diffraction_pattern_blur_sigma=None, detector_crop_adjoint=lambda value, shape: value, ) - replace_propagated_exit_wave_magnitude = ( - AnalyticalIterativePtychographyReconstructor.replace_propagated_exit_wave_magnitude - ) class _IdentityPropagator: def propagate_backward(self, tensor): @@ -141,14 +137,20 @@ def test_projection_helper_leaves_excluded_pixels_unconstrained(): exclude_measured_pixels_below=reconstructor.options.exclude_measured_pixels_below, y_true=y_true, ) - constrained_projection = reconstructor.replace_propagated_exit_wave_magnitude( + model = NoiseModel( + valid_pixel_mask=reconstructor.dataset.valid_pixel_mask, + exclude_measured_pixels_below=reconstructor.options.exclude_measured_pixels_below, + ) + constrained_projection = model.project_to_propagated_magnitude_constraint( psi_far, y_true, - constrained_pixel_mask=constrained_pixel_mask, + forward_model=reconstructor.forward_model, ) - fully_constrained_projection = reconstructor.replace_propagated_exit_wave_magnitude( + fully_constrained_projection = model.project_to_propagated_magnitude_constraint( psi_far, y_true, + constrained_pixel_mask=torch.ones_like(y_true, dtype=torch.bool), + forward_model=reconstructor.forward_model, ) expanded_mask = constrained_pixel_mask[:, None] diff --git a/tests/test_noise_models.py b/tests/test_noise_models.py new file mode 100644 index 00000000..ec40207f --- /dev/null +++ b/tests/test_noise_models.py @@ -0,0 +1,61 @@ +"""Regression checks for amplitude likelihoods and analytical loss selection.""" + +import pytest +import torch + +import ptychi.api as api +from ptychi.forward_models import GaussianNoiseModel, MagnitudeMeanSquaredNoiseModel +from ptychi.metrics import MSELossOfSqrt +from ptychi.utils import get_constrained_pixel_mask +from test_utils import make_task + +pytestmark = pytest.mark.usefixtures("numerical_cpu") + + +@pytest.mark.parametrize("dtype", [torch.float32, torch.float64]) +@pytest.mark.parametrize("reduction", ["mean", "sum", "none"]) +def test_magnitude_nll_preserves_loss_and_gradient(dtype, reduction): + predicted = torch.tensor([0.0, 1e-12, 0.25, 4.0, 100.0], dtype=dtype, requires_grad=True) + measured = torch.tensor([4.0, 0.0, 1.0, 9.0, 100.0], dtype=dtype) + expected = MSELossOfSqrt(reduction=reduction)(predicted, measured) + actual = MagnitudeMeanSquaredNoiseModel().nll(predicted, measured, reduction=reduction) + torch.testing.assert_close(actual, expected, rtol=0, atol=0) + expected_gradient, = torch.autograd.grad(expected.sum(), predicted) + actual_gradient, = torch.autograd.grad(actual.sum(), predicted) + torch.testing.assert_close(actual_gradient, expected_gradient, rtol=0, atol=0) + + gaussian = GaussianNoiseModel(sigma=0.7).nll(predicted, measured, reduction=reduction) + torch.testing.assert_close(gaussian, expected / 0.7**2 * 0.5, rtol=0, atol=0) + + +@pytest.mark.parametrize( + "options_class", [api.EPIEOptions, api.RPIEOptions, api.MAGPIEOptions, api.DMOptions, api.RAAROptions] +) +def test_analytical_magnitude_losses_preserve_masked_mse(options_class): + options = options_class() + options.reconstructor_options.exclude_measured_pixels_below = 1.0 + recon = make_task(options).reconstructor + recon.dataset.valid_pixel_mask[0, 1] = False + measured = torch.tensor([[[0.0, 4.0], [9.0, 1.0]], [[4.0, 0.0], [1.0, 16.0]]]) + inputs = [torch.arange(2)] + predicted = recon.forward_model(*inputs) + mask = get_constrained_pixel_mask(recon.dataset.valid_pixel_mask, 1.0, measured) + expected = MSELossOfSqrt()(predicted[mask], measured[mask]) + + assert type(recon.noise_model) is MagnitudeMeanSquaredNoiseModel + torch.testing.assert_close(recon.compute_optimizer_loss(inputs, measured), expected, rtol=0, atol=0) + if options_class in (api.DMOptions, api.RAAROptions): + # These algorithms retain their exit-wave update diagnostics. + assert recon.loss_tracker.metric_function is None + else: + assert recon.loss_tracker.metric_function == recon.noise_model.nll + recon.update_batch_loss(predicted, measured) + assert recon.loss_tracker.epoch_loss == expected.detach().numpy() + + +def test_explicit_displayed_loss_overrides_magnitude_nll(): + options = api.EPIEOptions() + options.reconstructor_options.displayed_loss_function = api.LossFunctions.MSE + recon = make_task(options).reconstructor + assert isinstance(recon.loss_tracker.metric_function, torch.nn.MSELoss) + assert recon.loss_tracker.metric_function is recon.displayed_loss_function diff --git a/tests/test_projection_numerics.py b/tests/test_projection_numerics.py index 38968cf2..626c40d1 100644 --- a/tests/test_projection_numerics.py +++ b/tests/test_projection_numerics.py @@ -18,6 +18,7 @@ def projection_reconstructor(): recon = make_task(api.EPIEOptions()).reconstructor recon.dataset = SimpleNamespace(valid_pixel_mask=torch.ones((1, 2), dtype=torch.bool)) + recon.noise_model.valid_pixel_mask = recon.dataset.valid_pixel_mask return recon @@ -27,20 +28,25 @@ def test_reciprocal_magnitude_replacement_preserves_mode_ratio_and_phase(masked, psi = torch.tensor([[[[3j, 4]], [[4, -3j]]]]) target = torch.tensor([[[100.0, 6.25]]]) mask = torch.tensor([[[True, False]]]) if masked else None - projected = projection_reconstructor.replace_propagated_exit_wave_magnitude( - psi, target, mask + projected = projection_reconstructor.noise_model.project_to_propagated_magnitude_constraint( + psi, target, mask, forward_model=projection_reconstructor.forward_model ) expected = torch.tensor([[[[6j, 2]], [[8, -1.5j]]]]) if masked: expected[..., 1] = psi[..., 1] torch.testing.assert_close(projected, expected, atol=2e-7, rtol=1e-7) + implicit = projection_reconstructor.noise_model.project_to_propagated_magnitude_constraint( + psi, target, mask + ) + torch.testing.assert_close(implicit, projected, atol=0, rtol=0) torch.testing.assert_close(psi, torch.tensor([[[[3j, 4]], [[4, -3j]]]])) def test_reciprocal_projection_zero_field_remains_finite(projection_reconstructor): psi = torch.zeros((2, 2, 1, 2), dtype=torch.complex128) - actual = projection_reconstructor.replace_propagated_exit_wave_magnitude( - psi, torch.tensor([[[0.0, 4.0]], [[9.0, 0.0]]]) + actual = projection_reconstructor.noise_model.project_to_propagated_magnitude_constraint( + psi, torch.tensor([[[0.0, 4.0]], [[9.0, 0.0]]]), + forward_model=projection_reconstructor.forward_model, ) torch.testing.assert_close(actual, psi) @@ -113,6 +119,7 @@ def test_dm_exit_wave_reflection_and_error_accumulation(relaxation, projection_r exit_wave_update_relaxation=relaxation, exclude_measured_pixels_below=None ) recon.dataset = projection_reconstructor.dataset + recon.noise_model = projection_reconstructor.noise_model recon.psi = torch.tensor([[[[99 + 0j, 99]]], [[[1 + 0j, 1j]]], [[[77 + 0j, 77]]]]) new_psi = torch.tensor([[[[2 + 0j, 2j]]]]) recon.calculate_exit_wave_chunk = Mock(return_value=(new_psi, torch.tensor([5.0])))