From d5958298ad35b58d1d84b24f744c1e3380bd1d1c Mon Sep 17 00:00:00 2001 From: Garrett Wright Date: Fri, 13 Nov 2020 15:33:02 -0500 Subject: [PATCH 1/2] tighten up types in polar2d adjoint test and fix random seed --- tests/test_PolarBasis2D.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/tests/test_PolarBasis2D.py b/tests/test_PolarBasis2D.py index 5b4017763b..3466da36a6 100644 --- a/tests/test_PolarBasis2D.py +++ b/tests/test_PolarBasis2D.py @@ -1,3 +1,4 @@ +import logging from unittest import TestCase import numpy as np @@ -6,12 +7,16 @@ from aspire.image import Image from aspire.utils import complex_type, utest_tolerance from aspire.utils.matlab_compat import m_reshape +from aspire.utils.random import randn + +logger = logging.getLogger(__name__) class PolarBasis2DTestCase(TestCase): def setUp(self): self.dtype = np.float32 self.basis = PolarBasis2D((8, 8), 4, 32, dtype=self.dtype) + self.seed = 8675309 def tearDown(self): pass @@ -469,7 +474,7 @@ def testPolarBasis2DAdjoint(self): # The evaluate function should be the adjoint operator of evaluate_t. # Namely, if A = evaluate, B = evaluate_t, and B=A^t, we will have # (y, A*x) = (A^t*y, x) = (B*y, x) - x = np.random.randn(self.basis.count).astype(self.dtype) + x = randn(self.basis.count, seed=self.seed).astype(self.dtype) x = m_reshape(x, (self.basis.nrad, self.basis.ntheta)) @@ -483,14 +488,15 @@ def testPolarBasis2DAdjoint(self): x = m_reshape(x, (self.basis.nrad * self.basis.ntheta,)) x_t = self.basis.evaluate(x).asnumpy() - y = np.random.randn(np.prod(self.basis.sz)).astype(self.dtype) + y = randn(np.prod(self.basis.sz), seed=self.seed).astype(self.dtype) y_t = self.basis.evaluate_t( Image(m_reshape(y, self.basis.sz)[np.newaxis, :]) ) # RCOPT - self.assertTrue( - np.isclose( - np.dot(y, m_reshape(x_t, (np.prod(self.basis.sz),))), - np.dot(y_t, x), - atol=utest_tolerance(self.dtype), - ) + + lhs = np.dot(y, m_reshape(x_t, (np.prod(self.basis.sz),))) + rhs = np.real(np.dot(y_t, x)) + logging.debug( + f"lhs: {lhs} rhs: {rhs} absdiff: {np.abs(lhs-rhs)} atol: {utest_tolerance(self.dtype)}" ) + + self.assertTrue(np.isclose(lhs, rhs, atol=utest_tolerance(self.dtype))) From ee8980d5647fbab1e6229e0790a3aab770a68d22 Mon Sep 17 00:00:00 2001 From: Garrett Wright Date: Fri, 13 Nov 2020 15:37:52 -0500 Subject: [PATCH 2/2] add a little note about what we're doing --- tests/test_PolarBasis2D.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_PolarBasis2D.py b/tests/test_PolarBasis2D.py index 3466da36a6..e76f550e63 100644 --- a/tests/test_PolarBasis2D.py +++ b/tests/test_PolarBasis2D.py @@ -16,6 +16,8 @@ class PolarBasis2DTestCase(TestCase): def setUp(self): self.dtype = np.float32 self.basis = PolarBasis2D((8, 8), 4, 32, dtype=self.dtype) + # Note, in practice we got a degenerate random array around 1% + # of the time, so we fix a seed for the randn calls. self.seed = 8675309 def tearDown(self):