Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_L1Proj(self):
shape = [6]
epsilon = 1.0
P = prox.L1Proj(shape, epsilon)
x = util.randn(shape)
y = P(1.0, x)
z = 1.0 if np.linalg.norm(x, 1) > 1.0 else np.linalg.norm(x, 1)
npt.assert_allclose(np.linalg.norm(y, 1), z)
x = util.randn(shape) * 0.0001
y = P(1.0, x)
z = 1.0 if np.linalg.norm(x, 1) > 1.0 else np.linalg.norm(x, 1)
npt.assert_allclose(np.linalg.norm(y, 1), z)
def test_Compose(self):
shape = [5]
I = linop.Identity(shape)
A = linop.Compose([I, I])
x = util.randn(shape)
npt.assert_allclose(A(x), x)
self.check_linop_linear(A)
self.check_linop_adjoint(A)
self.check_linop_pickleable(A)
def test_Conj(self):
shape = [5]
I = linop.Identity(shape)
A = linop.Conj(I)
x = util.randn(shape)
npt.assert_allclose(A(x), x)
self.check_linop_linear(A)
self.check_linop_adjoint(A)
self.check_linop_pickleable(A)
def test_RightMatMul(self):
ishape = (5, 4, 2)
mshape = (5, 2, 3)
A = linop.RightMatMul(ishape, util.randn(mshape))
self.check_linop_adjoint(A)
self.check_linop_linear(A)
self.check_linop_pickleable(A)
def test_monte_carlo_sure(self):
x = np.ones([100000], dtype=np.float)
sigma = 0.1
noise = 0.1 * util.randn([100000], dtype=np.float)
y = x + noise
def f(y):
return y
npt.assert_allclose(
sigma**2, util.monte_carlo_sure(f, y, sigma), atol=1e-3)
def test_Compose(self):
shape = [5]
I = linop.Identity(shape)
A = linop.Compose([I, I])
x = util.randn(shape)
npt.assert_allclose(A(x), x)
self.check_linop_linear(A)
self.check_linop_adjoint(A)
self.check_linop_pickleable(A)
def test_L1Proj(self):
shape = [6]
epsilon = 1.0
P = prox.L1Proj(shape, epsilon)
x = util.randn(shape)
y = P(1.0, x)
z = 1.0 if np.linalg.norm(x, 1) > 1.0 else np.linalg.norm(x, 1)
npt.assert_allclose(np.linalg.norm(y, 1), z)
x = util.randn(shape) * 0.0001
y = P(1.0, x)
z = 1.0 if np.linalg.norm(x, 1) > 1.0 else np.linalg.norm(x, 1)
npt.assert_allclose(np.linalg.norm(y, 1), z)
def test_Identity(self):
shape = [5]
A = linop.Identity(shape)
x = util.randn(shape)
npt.assert_allclose(A(x), x)
self.check_linop_linear(A)
self.check_linop_adjoint(A)
self.check_linop_unitary(A)
self.check_linop_pickleable(A)
def test_dual_precond_LinearLeastSquares(self):
n = 5
_A = np.eye(n) + 0.1 * util.randn([n, n])
A = linop.MatMul([n, 1], _A)
x = util.randn([n, 1])
y = A(x)
x_lstsq = np.linalg.lstsq(_A, y, rcond=-1)[0]
d = 1 / np.sum(abs(_A)**2, axis=1, keepdims=True).reshape([n, 1])
x_rec = app.LinearLeastSquares(
A,
y,
solver='PrimalDualHybridGradient',
max_iter=1000,
sigma=d, show_pbar=False).run()
npt.assert_allclose(x_rec, x_lstsq, atol=1e-3)
def test_Identity(self):
shape = [5]
A = linop.Identity(shape)
x = util.randn(shape)
npt.assert_allclose(A(x), x)
self.check_linop_linear(A)
self.check_linop_adjoint(A)
self.check_linop_unitary(A)
self.check_linop_pickleable(A)