Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions tests/models/test_isotropic_fluorescent_thick_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,62 @@ 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


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
29 changes: 29 additions & 0 deletions tests/models/test_phase_thick_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 7 additions & 2 deletions waveorder/models/isotropic_fluorescent_thick_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -251,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
Expand Down
3 changes: 2 additions & 1 deletion waveorder/models/phase_thick_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down