mirror of
https://github.com/cdtools-developers/cdtools.git
synced 2026-12-01 01:07:46 +01:00
.
This commit is contained in:
1 parent
0bfbc98922
commit
070622a9e1
3 files changed
+71
-54
No files matched your search
@@ -62,18 +62,16 @@ class PolarizedFancyPtycho(FancyPtycho):
|
||||
x = 1j
|
||||
else:
|
||||
x = -1j
|
||||
# model.probe.data = t.stack((model.probe.data.to(dtype=t.cfloat), x * model.probe.data.to(dtype=t.cfloat)), dim=-3)
|
||||
# obj = t.stack((model.obj.data, model.obj.data), dim=-3)
|
||||
# model.obj.data = t.stack((obj, obj), dim=-4)
|
||||
print('probe guess shape:', model.probe.shape)
|
||||
print('object guess shape:', model.obj.shape)
|
||||
|
||||
model.probe.data = t.stack((model.probe.data.to(dtype=t.cfloat), x * model.probe.data.to(dtype=t.cfloat)), dim=-3)
|
||||
obj = t.stack((model.obj.data, model.obj.data), dim=-3)
|
||||
model.obj.data = t.stack((obj, obj), dim=-4)
|
||||
|
||||
# tensor vs tensor.data
|
||||
return model
|
||||
|
||||
|
||||
def interaction(self, index, translations, polarizer, analyzer):
|
||||
# WHAT IS INDEX?
|
||||
def interaction(self, index, translations, polarizer, analyzer, test=False):
|
||||
|
||||
# Step 1 is to convert the translations for each position into a
|
||||
# value in pixels
|
||||
@@ -99,18 +97,15 @@ class PolarizedFancyPtycho(FancyPtycho):
|
||||
else:
|
||||
raise NotImplementedError('Unstable Modes not Implemented for polarized light')
|
||||
|
||||
print('probes', prs.shape)
|
||||
pol_probes = polarization.apply_linear_polarizer(prs, polarizer)
|
||||
|
||||
print('pol_probes',pol_probes.shape)
|
||||
exit_waves = self.probe_norm * tools.interactions.ptycho_2D_sinc(
|
||||
prs, self.obj_support * self.obj,pix_trans,
|
||||
shift_probe=True, multiple_modes=True, polarized=True)
|
||||
|
||||
analyzed_exit_waves = polarization.apply_linear_polarizer(exit_waves, analyzer)
|
||||
|
||||
#exit_waves = self.probe_norm * tools.interactions.ptycho_2D_round(
|
||||
# prs, self.obj_support * self.obj,pix_trans,
|
||||
# multiple_modes=True)
|
||||
|
||||
return analyzed_exit_waves
|
||||
|
||||
|
||||
@@ -123,10 +118,10 @@ class PolarizedFancyPtycho(FancyPtycho):
|
||||
return out[..., None, :, :]
|
||||
|
||||
def forward_propagator(self, wavefields):
|
||||
return vectorial_wavefields(wavefields, tools.propagators.far_field)
|
||||
return tools.propagators.far_field
|
||||
|
||||
def backward_propagator(self, wavefields):
|
||||
return vectorial_wavefields(wavefields, tools.propagators.inverse_far_field)
|
||||
return tools.propagators.inverse_far_field
|
||||
|
||||
|
||||
def measurement(self, wavefields):
|
||||
|
||||
@@ -454,19 +454,19 @@ def ptycho_2D_sinc(probe, obj, translations, shift_probe=True, padding=10, multi
|
||||
J = 2 * np.pi * J / probe.shape[-1]
|
||||
phase_masks = t.exp(1j*(-subpixel_translations[:,0,None,None]*I
|
||||
-subpixel_translations[:,1,None,None]*J))
|
||||
print('phase masks', phase_masks.shape)
|
||||
|
||||
if polarized:
|
||||
phase_masks = t.stack((phase_masks, phase_masks), dim=-4)
|
||||
phase_masks = t.unsqueeze(phase_masks, dim=-3)
|
||||
phase_masks = phase_masks[..., None, :, :]
|
||||
# Nx2x1xMxL tensor
|
||||
# probe is (P)x2x1xMxL tensor
|
||||
# probe is (N)(P)x2xMxL tensor
|
||||
|
||||
fft_probe = t.fft.fftshift(t.fft.fft2(probe),dim=(-1,-2))
|
||||
|
||||
|
||||
if multiple_modes: # Multi-mode probe
|
||||
if polarized:
|
||||
shifted_fft_probe = fft_probe * phase_masks[...,None,:,:,:,:]
|
||||
shifted_fft_probe = fft_probe * phase_masks[...,None,:,:,:]
|
||||
else:
|
||||
shifted_fft_probe = fft_probe * phase_masks[...,None,:,:]
|
||||
else:
|
||||
@@ -475,6 +475,7 @@ def ptycho_2D_sinc(probe, obj, translations, shift_probe=True, padding=10, multi
|
||||
|
||||
shifted_probe = t.fft.ifft2(t.fft.ifftshift(shifted_fft_probe,
|
||||
dim=(-1,-2)))
|
||||
print('shifted_probe', shifted_probe.shape)
|
||||
|
||||
if not polarized:
|
||||
if multiple_modes: # Multi-mode probe
|
||||
@@ -484,11 +485,10 @@ def ptycho_2D_sinc(probe, obj, translations, shift_probe=True, padding=10, multi
|
||||
# selections: Nx2x2xMxL
|
||||
# probe: Nx(P)x2x1xMxL
|
||||
else:
|
||||
if multiple_modes:
|
||||
selections = selections[..., None, :,:,:,:]
|
||||
shift_probe = shifted_probe.transpose(-1, -3).transpose(-2, -4)
|
||||
selections = selections.transpose(-1, -3).transpose(-2, -4)
|
||||
output = t.matmul(selections, shift_probe).transpose(-1, -3).transpose(-2, -4)
|
||||
# print('F', shift_probe)
|
||||
output = polarization.apply_jones_matrix(shifted_probe, selections)
|
||||
print('selections', selections.shape)
|
||||
print('output', output.shape)
|
||||
|
||||
else:
|
||||
raise NotImplementedError('Object shift not yet implemented')
|
||||
|
||||
@@ -11,24 +11,24 @@ __all__ = ['apply_linear_polarizer',
|
||||
'apply_circular_polarizer',
|
||||
'apply_jones_matrix']
|
||||
|
||||
def apply_linear_polarizer(probe, polarizer, transpose=True):
|
||||
def apply_linear_polarizer(probe, polarizer, multiple_modes=True, transpose=True):
|
||||
"""
|
||||
Applies a linear polarizer to the probe
|
||||
|
||||
Parameters:
|
||||
----------
|
||||
probe: t.Tensor
|
||||
A (...)x2x1xMxL tensor representing the probe, MxL - the size of the probe
|
||||
A (N)(P)x2xMxL tensor representing the probe, MxL - the size of the probe
|
||||
The angle between the fast-axis of the linear polarizer and the horizontal axis
|
||||
polarizer: t.Tensor
|
||||
A 1D tensor representing the polarizer angles for each of the patterns (or a single tensor of shape (1))
|
||||
A 1D tensor (N) representing the polarizer angles for each of the patterns (or a single tensor of shape (1))
|
||||
|
||||
Returns:
|
||||
--------
|
||||
linearly polarized probe: t.Tensor
|
||||
(...)x2x1xMxL
|
||||
(N)(P)x2x1xMxL
|
||||
"""
|
||||
probe = probe.to(dtype=t.cfloat)
|
||||
|
||||
if len(polarizer) == 1:
|
||||
theta = math.radians(polarizer)
|
||||
jones_matrices = t.tensor([[(cos(theta)) ** 2, sin(2 * theta) / 2], [sin(2 * theta) / 2, sin(theta) ** 2]]).to(dtype=t.cfloat)
|
||||
@@ -37,44 +37,61 @@ def apply_linear_polarizer(probe, polarizer, transpose=True):
|
||||
pol_sin = lambda idx: sin(math.radians(polarizer[idx]))
|
||||
jones_matrices = t.stack(([t.tensor([[(pol_cos(idx)) ** 2, pol_sin(idx) * pol_cos(idx)], [pol_sin(idx) * pol_cos(idx), (pol_sin(idx)) ** 2]]).to(dtype=t.cfloat) for idx in range(len(polarizer))]))
|
||||
|
||||
return apply_jones_matrix(probe, jones_matrices, transpose=transpose)
|
||||
return apply_jones_matrix(probe, jones_matrices, transpose=transpose, multiple_modes=multiple_modes)
|
||||
|
||||
|
||||
def apply_jones_matrix(probe, jones_matrix, transpose=True):
|
||||
def apply_jones_matrix(probe, jones_matrix, transpose=True, multiple_modes=True):
|
||||
"""
|
||||
Applies a given Jones matrix to the probe
|
||||
|
||||
Parameters:
|
||||
----------
|
||||
probe: t.Tensor
|
||||
A (...N)x2xMxL tensor representing the probe
|
||||
A (N)(P)x2xMxL tensor representing the probe
|
||||
jones_matrix: t.tensor
|
||||
(N)x2x2
|
||||
(N)x2x2
|
||||
|
||||
Returns:
|
||||
--------
|
||||
a probe with the jones matrix applied: t.Tensor
|
||||
(...N)x2xMxL
|
||||
(N)(P)x2xMxL
|
||||
"""
|
||||
if transpose:
|
||||
jones_matrix = jones_matrix[..., None, None, :, :]
|
||||
# make it (N)x1x1x2x2
|
||||
probe = probe[..., None, :, :]
|
||||
probe = probe.transpose(-1, -3).transpose(-2, -4)
|
||||
# (...N)xMxLx2x1
|
||||
output = t.matmul(jones_matrix, probe).transpose(-2, -4).transpose(-1, -3)
|
||||
# (...N)x2x1xMxL
|
||||
if multiple_modes:
|
||||
if transpose:
|
||||
if len(jones_matrix.shape) >= 4:
|
||||
jones_matrix = jones_matrix[..., None, :, :, :, :]
|
||||
else:
|
||||
jones_matrix = jones_matrix[..., None, :, :, None, None]
|
||||
probe = probe[..., None, :, :]
|
||||
jones_matrix = jones_matrix.transpose(-1, -3).transpose(-2, -4)
|
||||
# (N)1xMxLx2x2 or (N)1x1x1x2x2
|
||||
probe = probe.transpose(-1, -3).transpose(-2, -4)
|
||||
output = t.matmul(jones_matrix, probe).transpose(-2, -4).transpose(-1, -3).squeeze(-3)
|
||||
# (N)Px2xMxL
|
||||
|
||||
else:
|
||||
if len(jones_matrix.shape) < 4:
|
||||
jones_matrix = jones_matrix[..., None, :, :, :, :]
|
||||
probe = t.stack((probe, probe), dim=-4)
|
||||
output = t.sum(jones_matrix * probe, dim=-3)
|
||||
#(N)x2xMxL
|
||||
|
||||
else:
|
||||
# use element-wise multiplicaation and summation to contract a coordinate
|
||||
jones_matrix = jones_matrix[..., None, None]
|
||||
probe = t.stack((probe, probe), dim=-4)
|
||||
output = t.sum(jones_matrix * probe, dim=-3)
|
||||
#(...N)x2x1xMxL
|
||||
if transpose:
|
||||
if len(jones_matrix.shape) < 4:
|
||||
jones_matrix = jones_matrix[..., None, None]
|
||||
probe = probe[..., None, :, :]
|
||||
probe = probe.transpose(-1, -3).transpose(-2, -4)
|
||||
jones_matrix = jones_matrix.transpose(-1, -3).transpose(-2, -4)
|
||||
output = t.matmul(jones_matrix, probe).transpose(-2, -4).transpose(-1, -3).squeeze(-3)
|
||||
|
||||
else:
|
||||
if len(jones_matrix.shape) < 4:
|
||||
jones_matrix = jones_matrix[..., None, None]
|
||||
probe = t.stack((probe, probe), dim=-4)
|
||||
output = t.sum(jones_matrix * probe, dim=-3)
|
||||
|
||||
return output
|
||||
|
||||
return output.squeeze(-3)
|
||||
#(...N)x2x1xMxL
|
||||
|
||||
def apply_phase_retardance(probe, phase_shift):
|
||||
"""
|
||||
@@ -185,10 +202,15 @@ def apply_half_wave_plate(probe, fast_axis_angle):
|
||||
# a = t.ones(17, 8, 2, 3, 4)
|
||||
# b = t.ones(2, 1, 1)
|
||||
|
||||
probe = t.ones(5, 2, 3, 3)
|
||||
|
||||
polarizer = t.tensor([0])
|
||||
|
||||
exitw = apply_linear_polarizer(probe, polarizer)
|
||||
print(exitw[:, 0, :, :])
|
||||
print('y', exitw[:, 1, :, :])
|
||||
# probe = t.ones(5, 2, 3, 3)
|
||||
|
||||
# polarizer = t.tensor([45])
|
||||
|
||||
# exitw = apply_linear_polarizer(probe, polarizer)
|
||||
# print(exitw[:, 0, :, :])
|
||||
# print('y', exitw[:, 1, :, :])
|
||||
|
||||
a = t.ones(2, 4)
|
||||
print(t.sum(a, dim=1).shape)
|
||||
Reference in new issue
Block a user