diff --git a/ptypy/core/geometry.py b/ptypy/core/geometry.py index 05a2b630e..131474591 100644 --- a/ptypy/core/geometry.py +++ b/ptypy/core/geometry.py @@ -394,7 +394,7 @@ def downsample(self, a): """ if self.resample == 1: return a - return u.rebin_2d(a, self.resample)[0] + return u.rebin_2d(a, self.resample)[0] * (self.resample**2) def __str__(self): keys = list(self.p.keys()) diff --git a/test/core_tests/geometry_test.py b/test/core_tests/geometry_test.py index 84e4c868d..67d26638c 100644 --- a/test/core_tests/geometry_test.py +++ b/test/core_tests/geometry_test.py @@ -58,6 +58,38 @@ def test_geometry_nearfield_resolution(self): G = self.set_up_nearfield() assert (np.round(G.resolution*1e7) == [1.00, 1.00]).all(), "geometry resolution incorrect for the nearfield" + def test_downsample(self): + G = self.set_up_farfield() + G.resample = 2 + + B = np.indices((4,4), dtype=np.complex64)[0] + 1j * np.indices((4,4), dtype=np.complex64)[1] + A = G.downsample(B) + exp_A = np.array([[ 2. +2.j, 2.+10.j], + [10. +2.j, 10.+10.j]], dtype=np.complex64) + np.testing.assert_almost_equal(A.sum(), B.sum()) + np.testing.assert_array_almost_equal(A, exp_A) + + def test_upsample(self): + G = self.set_up_farfield() + G.resample = 2 + + B = np.indices((2,2), dtype=np.complex64)[0] + 1j * np.indices((2,2), dtype=np.complex64)[1] + A = G.upsample(B) + exp_A = np.array([[0. +0.j , 0. +0.j , 0. +0.25j, 0. +0.25j], + [0. +0.j , 0. +0.j , 0. +0.25j, 0. +0.25j], + [0.25+0.j , 0.25+0.j , 0.25+0.25j, 0.25+0.25j], + [0.25+0.j , 0.25+0.j , 0.25+0.25j, 0.25+0.25j]], dtype=np.complex64) + np.testing.assert_almost_equal(A.sum(), B.sum()) + np.testing.assert_array_almost_equal(A, exp_A) + + def test_downsample_upsample(self): + G = self.set_up_farfield() + G.resample = 2 + + A = np.random.random((4,4)) + B = G.downsample(G.upsample(A)) + np.testing.assert_almost_equal(A.sum(), B.sum()) + np.testing.assert_array_almost_equal(A, B) if __name__ == '__main__': unittest.main()