From 34cf555a3c94c6475c394df0d032df4a235b08c3 Mon Sep 17 00:00:00 2001 From: Abe Levitan Date: Wed, 24 Apr 2019 11:13:13 -0400 Subject: [PATCH] Fix an inconsistency in the incoherent sum measurement and update the test --- CDTools/tools/measurements.py | 12 ++++++------ tests/tools/test_measurements.py | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/CDTools/tools/measurements.py b/CDTools/tools/measurements.py index 1c83ab0..23d1a75 100644 --- a/CDTools/tools/measurements.py +++ b/CDTools/tools/measurements.py @@ -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 diff --git a/tests/tools/test_measurements.py b/tests/tools/test_measurements.py index fe94f50..4b75c82 100644 --- a/tests/tools/test_measurements.py +++ b/tests/tools/test_measurements.py @@ -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]))