mirror of
https://github.com/cdtools-developers/cdtools.git
synced 2026-11-16 02:53:56 +01:00
Add a reverse tool to calculate translations from pixel space offsets
This commit is contained in:
1 parent
6035841874
commit
75b257f816
2 files changed
+87
-1
No files matched your search
@@ -5,7 +5,7 @@ import torch as t
|
||||
import numpy as np
|
||||
|
||||
|
||||
__all__ = ['translations_to_pixel', #'pixel_to_translations',
|
||||
__all__ = ['translations_to_pixel', 'pixel_to_translations',
|
||||
'ptycho_2D_round','ptycho_2D_linear','ptycho_2D_sinc']
|
||||
|
||||
#
|
||||
@@ -60,6 +60,53 @@ def translations_to_pixel(basis, translations, surface_normal=t.Tensor([0,0,1]))
|
||||
return pixel_translations
|
||||
|
||||
|
||||
def pixel_to_translations(basis, pixel_translations, surface_normal=t.Tensor([0,0,1])):
|
||||
"""Takes pixel-space translations and outputs them in real space
|
||||
|
||||
This works for any 2D ptychography geometry. It takes in
|
||||
A set of internal pixel unit translations in (i,j) space and
|
||||
outputs the same translations real (x,y) space
|
||||
|
||||
It uses information on the wavefield basis and, if defined, the
|
||||
sample normal, to perform the conversion.
|
||||
|
||||
The assumed geometry is incoming radiation with a wavevector parallel
|
||||
to the +z axis, [0,0,1]. The default sample orientation has a surface
|
||||
normal parallel to this direction. Because of this, the z direction
|
||||
translation is always set to zero in the conversion
|
||||
|
||||
Args:
|
||||
basis (torch.Tensor) : The real space basis the wavefields are defined in
|
||||
translations (torch.Tensor) : A Jx2 stack of pixel-space translations
|
||||
surface_normal (torch.Tensor) : Optional, the sample's surface normal
|
||||
"""
|
||||
projection_1 = t.Tensor([[1,0,0],
|
||||
[0,1,0],
|
||||
[0,0,0]]).to(device=basis.device,dtype=basis.dtype)
|
||||
projection_2 = t.inverse(t.Tensor([[1,0,0],
|
||||
[0,1,0],
|
||||
-surface_normal/
|
||||
surface_normal[2]])).to(device=basis.device,dtype=basis.dtype)
|
||||
basis_vectors_inv = t.pinverse(basis)
|
||||
projection = t.mm(basis_vectors_inv,
|
||||
t.mm(projection_2,projection_1))
|
||||
# Literally just need the pseudoinverse of the projection we used to go
|
||||
# the other way
|
||||
projection = t.pinverse(projection).t()
|
||||
|
||||
single_translation = False
|
||||
if len(pixel_translations.shape) == 1:
|
||||
pixel_translations = pixel_translations[None,:]
|
||||
single_translation = True
|
||||
|
||||
translations = t.mm(pixel_translations, projection)
|
||||
|
||||
if single_translation:
|
||||
return translations[0]
|
||||
else:
|
||||
return translations
|
||||
|
||||
|
||||
def ptycho_2D_round(probe, obj, translations):
|
||||
"""Returns a stack of exit waves without accounting for subpixel shifts
|
||||
|
||||
|
||||
@@ -56,6 +56,45 @@ def test_translations_to_pixel():
|
||||
surface_normal=surface_normal)
|
||||
exp_translations = t.stack((-translations[:,1],translations[:,0]),dim=1)
|
||||
assert t.allclose(output, exp_translations)
|
||||
|
||||
|
||||
|
||||
def test_pixel_to_translations():
|
||||
# First, try the case where everything is ones and simple
|
||||
basis = t.Tensor([[0,-1,0],[-1,0,0]]).t()
|
||||
translations = t.rand((10,3))
|
||||
translations[:,2] = 0
|
||||
output = interactions.translations_to_pixel(basis, translations)
|
||||
roundtrip = interactions.pixel_to_translations(basis, output)
|
||||
assert t.allclose(translations, roundtrip)
|
||||
|
||||
# Next, try a case with a single translation
|
||||
translation = t.rand((3))
|
||||
translation[2] = 0
|
||||
output = interactions.translations_to_pixel(basis, translation)
|
||||
roundtrip = interactions.pixel_to_translations(basis, output)
|
||||
assert t.allclose(translation, roundtrip)
|
||||
|
||||
|
||||
# Then, try a case with no surface normal but with a real conversion
|
||||
basis = t.Tensor([[0,-2,0],[-1,0,0.1]]).t()
|
||||
translations = t.rand((10,3))
|
||||
translations[:,2] = 0 # manually project off z component
|
||||
output = interactions.translations_to_pixel(basis, translations)
|
||||
roundtrip = interactions.pixel_to_translations(basis, output)
|
||||
assert t.allclose(translations, roundtrip)
|
||||
|
||||
# Finally, try a case with a known surface normal (reflection)
|
||||
basis = t.Tensor([[0,-1,0],[0,0,1]]).t()
|
||||
surface_normal = t.Tensor([np.sqrt(2),0,-np.sqrt(2)])
|
||||
translations = t.rand((10,3))
|
||||
translations[:,2] = 0 # manually project off z component
|
||||
output = interactions.translations_to_pixel(basis, translations,
|
||||
surface_normal=surface_normal)
|
||||
roundtrip = interactions.pixel_to_translations(basis, output,
|
||||
surface_normal=surface_normal)
|
||||
assert t.allclose(translations, roundtrip)
|
||||
|
||||
|
||||
|
||||
def test_ptycho_2D_round(random_probe, random_obj):
|
||||
|
||||
Reference in new issue
Block a user