mirror of
https://github.com/cdtools-developers/cdtools.git
synced 2026-11-17 03:20:04 +01:00
Finally finally finally add an RPI model. Makes it soooo much easier...
This commit is contained in:
1 parent
75011eaccf
commit
4be2af8609
9 files changed
+597
-19
No files matched your search
@@ -9,11 +9,14 @@ import numpy as np
|
||||
import torch as t
|
||||
|
||||
__all__ = ['exit_wave_geometry', 'calc_object_setup', 'gaussian',
|
||||
'gaussian_probe', 'SHARP_style_probe']
|
||||
'gaussian_probe', 'SHARP_style_probe', 'RPI_spectral_init']
|
||||
|
||||
from CDTools.tools import cmath
|
||||
from CDTools.tools.propagators import inverse_far_field, generate_angular_spectrum_propagator, near_field
|
||||
from CDTools.tools.propagators import *
|
||||
from CDTools.tools.analysis import orthogonalize_probes
|
||||
from scipy.fftpack import next_fast_len
|
||||
from scipy.sparse import linalg as spla
|
||||
from torch.nn.functional import pad
|
||||
import numpy as np
|
||||
|
||||
|
||||
@@ -368,4 +371,68 @@ def SHARP_style_probe(dataset, shape, det_slice, propagation_distance=None, over
|
||||
|
||||
return final_probe
|
||||
|
||||
|
||||
def RPI_spectral_init(pattern, probe, obj_shape, n_modes=1, mask=None, background=None):
|
||||
|
||||
# First, check if the probe is a single mode or many modes.
|
||||
# If the probe is many modes, orthogonalize it and use the top mode
|
||||
# for initialization
|
||||
if probe.dim() == 4:
|
||||
probe = orthogonalize_probes(probe)[0]
|
||||
|
||||
pad0 = (probe.shape[-3] - obj_shape[0])//2
|
||||
pad1 = (probe.shape[-2] - obj_shape[1])//2
|
||||
|
||||
def a_dagger(im):
|
||||
im = cmath.complex_to_torch(im.reshape(obj_shape)).to(dtype=t.float32)
|
||||
im = inverse_far_field(pad(far_field(im), (0,0,pad1,pad1,pad0,pad0)))
|
||||
exit_wave = cmath.cmult(probe,im)
|
||||
farfield = cmath.torch_to_complex(far_field(exit_wave))
|
||||
return farfield.ravel()
|
||||
|
||||
def a(measured):
|
||||
measured = cmath.complex_to_torch(measured.reshape(pattern.shape[0],pattern.shape[1])).to(dtype=t.float32)
|
||||
im = inverse_far_field(measured)
|
||||
multiplied = cmath.cmult(cmath.cconj(probe), im)
|
||||
backplane = far_field(multiplied)
|
||||
clipped = backplane[pad0:pad0+obj_shape[0],
|
||||
pad1:pad1+obj_shape[1],:]
|
||||
return cmath.torch_to_complex(inverse_far_field(clipped)).ravel()
|
||||
|
||||
patsize = pattern.shape[0]*pattern.shape[1]
|
||||
imsize = obj_shape[0]*obj_shape[1]
|
||||
probesize = probe.shape[0]*probe.shape[1]
|
||||
A_dagger = spla.LinearOperator((patsize, imsize),matvec=a_dagger)
|
||||
A = spla.LinearOperator((imsize,patsize),matvec=a)
|
||||
|
||||
# Correct the pattern for the background and mask
|
||||
np_pattern = pattern.numpy()
|
||||
if background is not None:
|
||||
np_pattern = np_pattern - background.numpy()
|
||||
if mask is not None:
|
||||
np_pattern = np_pattern * mask.numpy()
|
||||
np_pattern = np.abs(np_pattern.ravel())
|
||||
|
||||
realspace_intensities = A * A_dagger * np.ones(imsize)
|
||||
vec = A_dagger * realspace_intensities
|
||||
def y(measured):
|
||||
#return measured * np_pattern
|
||||
# This normalizes the intensities to account for the fact
|
||||
# that some pixels draw from way more spots on the detector than
|
||||
# others
|
||||
return measured * np_pattern / np.abs(vec)
|
||||
|
||||
Y = spla.LinearOperator((patsize, patsize),matvec=y)
|
||||
eigval, z0 = spla.eigs(A * Y * A_dagger, k=n_modes, which='LM')
|
||||
z0 = z0.transpose().reshape(n_modes,obj_shape[0], obj_shape[1])
|
||||
|
||||
# Now we set the overall scale and relative weights of the guess
|
||||
scale_factor = np.sqrt(np.sum(np_pattern) /
|
||||
t.sum(cmath.cabssq(probe)).numpy())
|
||||
relative_weights = eigval / np.sum(eigval**2)
|
||||
|
||||
z0 = z0 * (scale_factor * relative_weights[:,None,None])
|
||||
# Now we have to normalize the modes by their eigenvalues
|
||||
|
||||
return cmath.complex_to_torch(z0).to(dtype=t.float32)
|
||||
|
||||
@@ -10,11 +10,12 @@ from __future__ import division, print_function, absolute_import
|
||||
from CDTools.tools.cmath import *
|
||||
import torch as t
|
||||
import numpy as np
|
||||
|
||||
from CDTools.tools import propagators
|
||||
|
||||
__all__ = ['translations_to_pixel', 'pixel_to_translations',
|
||||
'project_translations_to_sample',
|
||||
'ptycho_2D_round','ptycho_2D_linear','ptycho_2D_sinc']
|
||||
'ptycho_2D_round','ptycho_2D_linear','ptycho_2D_sinc',
|
||||
'RPI_interaction']
|
||||
|
||||
|
||||
|
||||
@@ -524,6 +525,56 @@ def ptycho_2D_sinc_s_matrix(probe, s_matrix, translations, shift_probe=True, pad
|
||||
return exit_waves[0]
|
||||
else:
|
||||
return t.stack(exit_waves)
|
||||
|
||||
|
||||
def RPI_interaction(probe, obj):
|
||||
"""Returns an exit wave from a high-res probe and a low-res obj
|
||||
|
||||
In this interaction, the probe and object arrays are assumed to cover
|
||||
the same physical region of space, but with the probe array sampling that
|
||||
region of space more finely. Thus, to do the interaction, the object
|
||||
is first upsampled by padding it in Fourier space (equivalent to a sinc
|
||||
interpolation) before being multiplied with the probe. This function is
|
||||
called RPI_interaction because this interaction is central to the RPI
|
||||
method and is not commonly used elsewhere.
|
||||
|
||||
This also works with object functions that have an extra first dimension
|
||||
for an incoherently mixing model.
|
||||
|
||||
|
||||
Parameters
|
||||
----------
|
||||
probe : torch.Tensor
|
||||
An MxL probe function for simulating the exit waves
|
||||
obj : torch.Tensor
|
||||
An M'xL' or NxM'xL' object function for simulating the exit waves
|
||||
|
||||
Returns
|
||||
-------
|
||||
exit_wave : torch.Tensor
|
||||
An MxL tensor of the calculated exit waves
|
||||
"""
|
||||
|
||||
# TODO: The upsampling only works for arrays of even dimension!
|
||||
|
||||
# The far-field propagator is just a 2D FFT but with an fftshift
|
||||
fftobj = propagators.far_field(obj)
|
||||
# We calculate the padding that we need to do the upsampling
|
||||
pad0 = (probe.shape[-3] - obj.shape[-3])//2
|
||||
pad1 = (probe.shape[-2] - obj.shape[-2])//2
|
||||
|
||||
if obj.dim() == 3:
|
||||
fftobj = t.nn.functional.pad(fftobj, (0, 0, pad1, pad1, pad0, pad0))
|
||||
elif obj.dim() == 4:
|
||||
fftobj = t.nn.functional.pad(fftobj,
|
||||
(0, 0, pad1, pad1, pad0, pad0, 0, 0))
|
||||
else:
|
||||
raise NotImplementedError('RPI interaction with obj of dimension higher than 4 (including complex dimension) is not supported.')
|
||||
|
||||
# Again, just an inverse FFT but with an fftshift
|
||||
upsampled_obj = propagators.inverse_far_field(fftobj)
|
||||
|
||||
if obj.dim() == 4:
|
||||
return cmult(probe[None,...], upsampled_obj)
|
||||
else:
|
||||
return cmult(probe, upsampled_obj)
|
||||
Reference in new issue
Block a user