diff --git a/CDTools/tools/initializers.py b/CDTools/tools/initializers.py index fbb51f0..6434f12 100644 --- a/CDTools/tools/initializers.py +++ b/CDTools/tools/initializers.py @@ -2,11 +2,73 @@ from __future__ import division, print_function, absolute_import import numpy as np import torch as t -all = ['gaussian'] +__all__ = ['exit_wave_geometry', 'gaussian'] from CDTools.tools import cmath +from scipy.fftpack import next_fast_len +import numpy as np +def exit_wave_geometry(det_basis, det_shape, wavelength, distance, center=None, opt_for_fft=True): + """Returns an exit wave basis and a detector slice for the given detector geometry + + It takes in the parameters for a given detector - the basis defining + the pixel pitch and the shape, as well as the wavelength and propagation + distance. Optionally, it accepts a defined "center", the zero-frequency + pixel's location. It then will automatically define a larger detector + if necessary, define the exit wave basis associated with a far-field + diffraction experiment, and return that basis, shape, and detector slice + + Args: + det_basis (torch.Tensor) : The detector basis, as defined elsewhere + det_shape (torch.Size) : The (i,j) shape of the detector + wavelength (float) : The wavelength of light for the experiment, in m + distance (float) : The sample-detector distance, in m + center (torch.Tensor) : If defined, the location of the zero frequency pixel + opt_for_fft (bool) : Default is true, whether to increase detector size to improve fft performance + + Returns: + torch.Tensor : The exit wave basis + torch.Tensor : The exit wave's shape + tuple(slice) : The slice corresponding to the physical detector + """ + det_shape = t.Tensor(tuple(det_shape)) + # First, set the center if it's not already specified + # This definition matches the center pixel of an fftshifted array + if center is None: + center = det_shape // 2 + + # Then, calculate the required detector size from the centering + # This is a bit opaque but was worth doing accurately + min_left = center * 2 + min_right = (det_shape - center) * 2 - 1 + full_shape = t.max(min_left,min_right).to(t.int32) + if opt_for_fft: + full_shape = t.Tensor([next_fast_len(dim) for dim in full_shape]).to(t.int32) + # Then, generate a slice that pops the actual detector from the full + # detector shape + full_center = full_shape // 2 + det_slice = np.s_[int(full_center[0]-center[0]): + int(full_center[0]-center[0]+det_shape[0]), + int(full_center[1]-center[1]): + int(full_center[1]-center[1]+det_shape[1])] + + # Finally, generate the basis for the exit wave in real space + # I believe this calculation is incorrect for non-rectangular + # detectors, because the real space basis shoud be related to the + # dual of the original basis. Leaving this for now since + # non-rectangular detectors are not a pressing concern. + basis_dirs = det_basis / t.norm(det_basis, dim=0) + real_space_basis = basis_dirs * wavelength * distance / \ + (full_shape.to(t.float32) * t.norm(det_basis,dim=0)) + + # Finally, convert the shape back to a torch.Size + full_shape = t.Size([dim for dim in full_shape]) + + return real_space_basis, full_shape, det_slice + + + def gaussian(shape, amplitude, sigma, center = None): """Returns an array with a centered gaussian diff --git a/tests/tools/test_initializers.py b/tests/tools/test_initializers.py index 2f91fac..214f270 100644 --- a/tests/tools/test_initializers.py +++ b/tests/tools/test_initializers.py @@ -5,6 +5,40 @@ from CDTools.tools import cmath import numpy as np import torch as t +def test_exit_wave_geometry(): + # First test a simple case where nothing need change + basis = t.Tensor([[0,-30e-6,0], + [-20e-6,0,0]]).transpose(0,1) + shape = t.Size([73,56]) + wavelength = 1e-9 + distance = 1. + rs_basis, full_shape, det_slice = \ + initializers.exit_wave_geometry(basis, shape, wavelength, + distance, opt_for_fft=False) + assert full_shape == shape + assert t.ones(full_shape)[det_slice].shape == shape + assert t.allclose(rs_basis[0,1],t.Tensor([-8.928571428571428e-07])) + assert t.allclose(rs_basis[1,0],t.Tensor([-4.5662100456621004e-07])) + + # Then test it's expanding functionality for a non-optimal array + rs_basis, full_shape, det_slice = \ + initializers.exit_wave_geometry(basis, shape, wavelength, + distance, opt_for_fft=True) + exp_shape = t.Size([75,60]) + assert full_shape == exp_shape + assert t.ones(full_shape)[det_slice].shape == shape + assert t.allclose(rs_basis[0,1],t.Tensor([-8.333333333333333e-07])) + assert t.allclose(rs_basis[1,0],t.Tensor([-4.444444444444444e-07])) + + # Finally test it off-center, without expanding + center = t.Tensor([20,42]) + rs_basis, full_shape, det_slice = \ + initializers.exit_wave_geometry(basis, shape, wavelength, + distance, center=center, + opt_for_fft=False) + exp_shape = t.Size([105,84]) + assert full_shape == exp_shape + assert t.ones(full_shape)[det_slice].shape == shape def test_gaussian():