Fix an inconsistency in the incoherent sum measurement and update the test

This commit is contained in:
Abe Levitan
2019-04-24 11:13:13 -04:00
parent bf8dbe086a
commit 34cf555a3c
2 changed files with 9 additions and 9 deletions
+6 -6
View File
@@ -47,22 +47,22 @@ def incoherent_sum(wavefields, detector_slice=None, epsilon=1e-7, saturation=Non
the wavefields. If a detector slice is given, the returned array
will only include that slice from the simulated wavefronts.
The first index is the index of the diffraction pattern to measure,
the second index is the set of incoherently adding patterns, and
the next two indices index the wavefield. The final index is the complex
The first index is the set of incoherently adding patterns, and
the second index is the index of the diffraction pattern to measure.
The next two indices index the wavefield. The final index is the complex
index.
Args:
wavefields (torch.Tensor) : A JxLxMxNx2 stack of complex wavefields
wavefields (torch.Tensor) : An LxJxMxNx2 stack of complex wavefields
detector_slice (slice) : Optional, a slice or tuple of slices defining a section of the simulation to return
saturation (float) : Optional, a maximum saturation value to clamp the resulting intensities to
Returns:
torch.Tensor : A real MxN array storing the incoherently summed intensities
torch.Tensor : A real JXMxN array storing the incoherently summed intensities
"""
# This syntax just adds an axis to the slice to preserve the J direction
if detector_slice is None:
output = t.sum(cmath.cabssq(wavefields),dim=-3) + epsilon
output = t.sum(cmath.cabssq(wavefields),dim=0) + epsilon
else:
if wavefields.dim() == 4:
output = t.sum(cmath.cabssq(wavefields[(np.s_[:],)+detector_slice]),dim=0) + epsilon
+3 -3
View File
@@ -31,11 +31,11 @@ def test_intensity():
def test_incoherent_sum():
wavefields = t.rand((5,4,10,10,2))
epsilon=1e-6
np_result = np.sum(np.abs(cmath.torch_to_complex(wavefields))**2,axis=1) + epsilon
np_result = np.sum(np.abs(cmath.torch_to_complex(wavefields))**2,axis=0) + epsilon
assert t.allclose(measurements.incoherent_sum(wavefields,epsilon=epsilon),
t.tensor(np_result))
# Test single field case
assert t.allclose(measurements.incoherent_sum(wavefields[0],epsilon=epsilon),
assert t.allclose(measurements.incoherent_sum(wavefields[:,0],epsilon=epsilon),
t.tensor(np_result[0]))
@@ -43,7 +43,7 @@ def test_incoherent_sum():
assert t.allclose(measurements.incoherent_sum(wavefields,det_slice,epsilon=epsilon),
t.tensor(np_result[(np.s_[:],)+det_slice]))
# Test single field case
assert t.allclose(measurements.incoherent_sum(wavefields[0],det_slice,epsilon=epsilon),
assert t.allclose(measurements.incoherent_sum(wavefields[:,0],det_slice,epsilon=epsilon),
t.tensor(np_result[0][det_slice]))