From b4e316a9d0e3bc36d4c5ad4216fb84bd06dd39ee Mon Sep 17 00:00:00 2001 From: Arash Parsa <236871152+arashmparsa@users.noreply.github.com> Date: Sat, 28 Nov 2026 01:19:52 -0800 Subject: [PATCH 1/2] fix(models): scale z padding by the axial upsampling factor in 3D transfer functions calculate_transfer_function in phase_thick_3d and isotropic_fluorescent_thick_3d computes the transfer function on a grid upsampled to axial Nyquist (z_factor) and then crops it in Fourier space to zyx_shape[0] + 2 * z_padding slices. The padding was passed to the upsampled grid unscaled, so that grid spanned (Z + 2 * z_padding / z_factor) * z_pixel_size instead of (Z + 2 * z_padding) * z_pixel_size, and the cropped transfer function was sampled on the wrong axial frequencies whenever z_padding > 0 and z_pixel_size is coarser than axial Nyquist. Pass z_padding * z_factor to the upsampled grid. Add tests that the axial response does not change with z padding when z is upsampled. --- .../test_isotropic_fluorescent_thick_3d.py | 28 ++++++++++++++++++ tests/models/test_phase_thick_3d.py | 29 +++++++++++++++++++ .../models/isotropic_fluorescent_thick_3d.py | 3 +- waveorder/models/phase_thick_3d.py | 3 +- 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/tests/models/test_isotropic_fluorescent_thick_3d.py b/tests/models/test_isotropic_fluorescent_thick_3d.py index cf649a09..860c6d5b 100644 --- a/tests/models/test_isotropic_fluorescent_thick_3d.py +++ b/tests/models/test_isotropic_fluorescent_thick_3d.py @@ -219,3 +219,31 @@ def test_reconstruct_apodization_rolloff_smoke(): nyquist_row_apod = np.abs(np.fft.fftn(density_apod.numpy())[:, 16, :]) nyquist_row_hard = np.abs(np.fft.fftn(density_hard.numpy())[:, 16, :]) assert nyquist_row_apod.max() < 1e-3 * nyquist_row_hard.max() + + +def _central_axial_profile(transfer_function, half_width=6): + kernel = torch.fft.fftshift(torch.real(torch.fft.ifftn(transfer_function, dim=(-3, -2, -1)))) + zc, yc, xc = (s // 2 for s in kernel.shape) + profile = kernel[zc - half_width : zc + half_width + 1, yc, xc] + return profile / profile.abs().max() + + +def test_z_padding_keeps_psf_when_z_is_upsampled(): + """z padding must not change the PSF, also when z is upsampled to Nyquist. + + With z_pixel_size=1.0 the axial Nyquist spacing is ~0.97 um, so the OTF is + computed on a 2x finer z grid. The padding has to be 2x as many slices + there, otherwise the cropped OTF is sampled on the wrong axial frequencies + and the PSF it describes gets wider the more padding is used. + """ + kwargs = dict( + zyx_shape=(48, 64, 64), + yx_pixel_size=0.15, + z_pixel_size=1.0, + wavelength_emission=0.532, + index_of_refraction_media=1.3, + numerical_aperture_detection=0.8, + ) + reference = _central_axial_profile(isotropic_fluorescent_thick_3d.calculate_transfer_function(z_padding=0, **kwargs)) + padded = _central_axial_profile(isotropic_fluorescent_thick_3d.calculate_transfer_function(z_padding=16, **kwargs)) + assert torch.linalg.norm(padded - reference) / torch.linalg.norm(reference) < 1e-2 diff --git a/tests/models/test_phase_thick_3d.py b/tests/models/test_phase_thick_3d.py index 62d9b4db..5c08a916 100644 --- a/tests/models/test_phase_thick_3d.py +++ b/tests/models/test_phase_thick_3d.py @@ -214,3 +214,32 @@ def test_reconstruct_apodization_rolloff_smoke(): nyquist_row_apod = np.abs(np.fft.fftn(phase_apod.numpy())[:, 16, :]) nyquist_row_hard = np.abs(np.fft.fftn(phase_hard.numpy())[:, 16, :]) assert nyquist_row_apod.max() < 1e-3 * nyquist_row_hard.max() + + +def test_z_padding_keeps_axial_response_when_z_is_upsampled(): + """z padding must not change the axial phase response when z is upsampled. + + z_pixel_size=0.5 is coarser than the axial Nyquist spacing (~0.33 um), so + the transfer function is computed on a 2x finer z grid and z padding has + to be 2x as many slices there. + """ + kwargs = dict( + zyx_shape=(32, 64, 64), + yx_pixel_size=0.1, + z_pixel_size=0.5, + wavelength_illumination=0.532, + index_of_refraction_media=1.3, + numerical_aperture_illumination=0.9, + numerical_aperture_detection=1.2, + ) + + def central_axial_profile(real_tf, half_width=6): + kernel = torch.fft.fftshift(torch.real(torch.fft.ifftn(real_tf, dim=(-3, -2, -1)))) + zc, yc, xc = (s // 2 for s in kernel.shape) + profile = kernel[zc - half_width : zc + half_width + 1, yc, xc] + return profile / profile.abs().max() + + reference = central_axial_profile(phase_thick_3d.calculate_transfer_function(z_padding=0, **kwargs)[0]) + padded = central_axial_profile(phase_thick_3d.calculate_transfer_function(z_padding=16, **kwargs)[0]) + # The small residual comes from the axial Hann window, which spans the padded grid + assert torch.linalg.norm(padded - reference) / torch.linalg.norm(reference) < 0.05 diff --git a/waveorder/models/isotropic_fluorescent_thick_3d.py b/waveorder/models/isotropic_fluorescent_thick_3d.py index d1d1d9a3..40a6ae09 100644 --- a/waveorder/models/isotropic_fluorescent_thick_3d.py +++ b/waveorder/models/isotropic_fluorescent_thick_3d.py @@ -96,7 +96,8 @@ def calculate_transfer_function( YXPixelSize(y=yx_pixel_size.y / y_factor, x=yx_pixel_size.x / x_factor), z_pixel_size / z_factor, wavelength_emission, - z_padding, + # z_padding counts output slices; on the upsampled grid each one is z_factor slices + z_padding * z_factor, index_of_refraction_media, numerical_aperture_detection, confocal_pinhole_diameter, diff --git a/waveorder/models/phase_thick_3d.py b/waveorder/models/phase_thick_3d.py index 03bc8d39..8e971175 100644 --- a/waveorder/models/phase_thick_3d.py +++ b/waveorder/models/phase_thick_3d.py @@ -202,7 +202,8 @@ def _to_batch(val): up_yx, up_z, wavelength_illumination, - z_padding, + # z_padding counts output slices; on the upsampled grid each one is z_factor slices + z_padding * z_factor, index_of_refraction_media, na_det[0], invert_phase_contrast, From 10dab129054988c85a384d15f26dc0ca17e6a566 Mon Sep 17 00:00:00 2001 From: Arash Parsa <236871152+arashmparsa@users.noreply.github.com> Date: Sat, 28 Nov 2026 01:23:57 -0800 Subject: [PATCH 2/2] fix(models): zero-pad the object instead of slicing the OTF in apply_transfer_function isotropic_fluorescent_thick_3d.apply_transfer_function (also used by phase_thick_3d.apply_transfer_function) handled z_padding > 0 by slicing optical_transfer_function[z_padding:-z_padding]. The OTF is in unshifted FFT order, so that slice removes the DC term and the lowest axial frequencies: the simulated stack sums to zero and keeps only high-frequency content. Zero-pad the object to the OTF's padded grid, apply the OTF, and crop the simulated data back to the object's shape. --- .../test_isotropic_fluorescent_thick_3d.py | 31 +++++++++++++++++++ .../models/isotropic_fluorescent_thick_3d.py | 6 +++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/tests/models/test_isotropic_fluorescent_thick_3d.py b/tests/models/test_isotropic_fluorescent_thick_3d.py index 860c6d5b..6259d575 100644 --- a/tests/models/test_isotropic_fluorescent_thick_3d.py +++ b/tests/models/test_isotropic_fluorescent_thick_3d.py @@ -247,3 +247,34 @@ def test_z_padding_keeps_psf_when_z_is_upsampled(): reference = _central_axial_profile(isotropic_fluorescent_thick_3d.calculate_transfer_function(z_padding=0, **kwargs)) padded = _central_axial_profile(isotropic_fluorescent_thick_3d.calculate_transfer_function(z_padding=16, **kwargs)) assert torch.linalg.norm(padded - reference) / torch.linalg.norm(reference) < 1e-2 + + +def test_apply_transfer_function_with_z_padding(): + """Simulating with a padded OTF gives the same image as without padding. + + The OTF is in unshifted FFT order, so slicing it along z drops the DC term + and the lowest axial frequencies. Zero-padding the object instead gives the + same image as the unpadded simulation away from the z edges. + """ + kwargs = dict( + yx_pixel_size=0.15, + z_pixel_size=0.25, + wavelength_emission=0.532, + index_of_refraction_media=1.3, + numerical_aperture_detection=0.8, + ) + zyx_shape = (48, 64, 64) + zyx_object = torch.zeros(zyx_shape) + zyx_object[20:28, 24:40, 24:40] = 1.0 + otf = isotropic_fluorescent_thick_3d.calculate_transfer_function(zyx_shape, z_padding=0, **kwargs) + unpadded = isotropic_fluorescent_thick_3d.apply_transfer_function(zyx_object, otf, 0, background=0) + + z_padding = 4 + otf_padded = isotropic_fluorescent_thick_3d.calculate_transfer_function(zyx_shape, z_padding=z_padding, **kwargs) + padded = isotropic_fluorescent_thick_3d.apply_transfer_function(zyx_object, otf_padded, z_padding, background=0) + + assert padded.shape == zyx_shape + # Same image up to an overall brightness factor (the padded grid is longer) + central = slice(16, 32) + correlation = torch.corrcoef(torch.stack([padded[central].flatten(), unpadded[central].flatten()]))[0, 1] + assert correlation > 0.999 diff --git a/waveorder/models/isotropic_fluorescent_thick_3d.py b/waveorder/models/isotropic_fluorescent_thick_3d.py index 40a6ae09..2f00ed6a 100644 --- a/waveorder/models/isotropic_fluorescent_thick_3d.py +++ b/waveorder/models/isotropic_fluorescent_thick_3d.py @@ -252,13 +252,17 @@ def apply_transfer_function( """ if zyx_object.shape[0] + 2 * z_padding != optical_transfer_function.shape[0]: raise ValueError("Please check padding: ZYX_obj.shape[0] + 2 * Z_pad != H_re.shape[0]") + # The OTF is sampled on the padded grid: zero-pad the object to that grid + # and crop the simulated data back, rather than slicing the (unshifted) OTF. if z_padding > 0: - optical_transfer_function = optical_transfer_function[z_padding:-z_padding] + zyx_object = torch.nn.functional.pad(zyx_object, (0, 0, 0, 0, z_padding, z_padding)) # Very simple simulation, consider adding noise and bkg knobs zyx_obj_hat = torch.fft.fftn(zyx_object) zyx_data = zyx_obj_hat * optical_transfer_function data = torch.real(torch.fft.ifftn(zyx_data)) + if z_padding > 0: + data = data[z_padding:-z_padding] data += background # Add a direct background return data