def test_Hstack(self): shape = [5] I = linop.Identity(shape) x1 = util.randn(shape) x2 = util.randn(shape) x = util.vec([x1, x2]) A = linop.Hstack([I, I]) npt.assert_allclose(A(x), x1 + x2) self.check_linop_linear(A) self.check_linop_adjoint(A) self.check_linop_normal(A) self.check_linop_pickleable(A) shape = [5, 3] I = linop.Identity(shape) x1 = util.randn(shape) x2 = util.randn(shape) x = np.concatenate([x1, x2], axis=1) A = linop.Hstack([I, I], axis=1) npt.assert_allclose(A(x), x1 + x2) self.check_linop_linear(A) self.check_linop_adjoint(A) self.check_linop_normal(A) self.check_linop_pickleable(A)
def test_precond_LinearLeastSquares(self): n = 5 _A = np.eye(n) + 0.01 * 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] p = 1 / (np.sum(abs(_A)**2, axis=0).reshape([n, 1])) P = linop.Multiply([n, 1], p) x_rec = app.LinearLeastSquares(A, y, show_pbar=False).run() npt.assert_allclose(x_rec, x_lstsq, atol=1e-3) alpha = 1 / app.MaxEig(P * A.H * A, show_pbar=False).run() x_rec = app.LinearLeastSquares(A, y, solver='GradientMethod', alpha=alpha, max_power_iter=100, max_iter=1000, show_pbar=False).run() npt.assert_allclose(x_rec, x_lstsq, atol=1e-3) tau = p x_rec = app.LinearLeastSquares(A, y, solver='PrimalDualHybridGradient', max_iter=1000, tau=tau, show_pbar=False).run() npt.assert_allclose(x_rec, x_lstsq, atol=1e-3)
def test_ConvolveFilter(self): for device in devices: for dtype in dtypes: for mode in ['full', 'valid']: with self.subTest(mode=mode, dtype=dtype, device=device): filt_shape = [2, 3] data = util.randn([3, 4], dtype=dtype) A = linop.ConvolveFilter(filt_shape, data, mode=mode) self.check_linop_linear(A, dtype=dtype, device=device) self.check_linop_adjoint(A, dtype=dtype, device=device) self.check_linop_normal(A, dtype=dtype, device=device) self.check_linop_pickleable(A) filt_shape = [1, 4, 2, 3] data = util.randn([4, 3, 4], dtype=dtype) A = linop.ConvolveFilter(filt_shape, data, mode=mode, multi_channel=True) self.check_linop_linear(A, dtype=dtype, device=device) self.check_linop_adjoint(A, dtype=dtype, device=device) self.check_linop_normal(A, dtype=dtype, device=device) self.check_linop_pickleable(A) filt_shape = [4, 1, 2, 3] data = util.randn([1, 3, 4], dtype=dtype) A = linop.ConvolveFilter(filt_shape, data, mode=mode, multi_channel=True) self.check_linop_linear(A, dtype=dtype, device=device) self.check_linop_adjoint(A, dtype=dtype, device=device) self.check_linop_normal(A, dtype=dtype, device=device) self.check_linop_pickleable(A) filt_shape = [4, 2, 2, 3] data = util.randn([2, 3, 4], dtype=dtype) A = linop.ConvolveFilter(filt_shape, data, mode=mode, multi_channel=True) self.check_linop_linear(A, dtype=dtype, device=device) self.check_linop_adjoint(A, dtype=dtype, device=device) self.check_linop_normal(A, dtype=dtype, device=device) self.check_linop_pickleable(A) filt_shape = [4, 2, 2, 3] strides = [2, 2] data = util.randn([2, 3, 4], dtype=dtype) A = linop.ConvolveFilter(filt_shape, data, mode=mode, strides=strides, multi_channel=True) self.check_linop_linear(A, dtype=dtype, device=device) self.check_linop_adjoint(A, dtype=dtype, device=device) self.check_linop_normal(A, dtype=dtype, device=device) self.check_linop_pickleable(A)
def check_linop_adjoint(A, dtype=np.float, device=backend.cpu_device): device = backend.Device(device) x = util.randn(A.ishape, dtype=dtype, device=device) y = util.randn(A.oshape, dtype=dtype, device=device) xp = device.xp with device: lhs = xp.vdot(A * x, y) rhs = xp.vdot(x, A.H * y) xp.testing.assert_allclose(lhs, rhs, atol=1e-5, rtol=1e-5)
def test_ConvolveData(self): for device in devices: for dtype in dtypes: for mode in ['full', 'valid']: with self.subTest(mode=mode, dtype=dtype, device=device): data_shape = [3, 4] filt = util.randn([2, 3], dtype=dtype) A = linop.ConvolveData(data_shape, filt, mode=mode) self.check_linop_linear(A, dtype=dtype, device=device) self.check_linop_adjoint(A, dtype=dtype, device=device) self.check_linop_pickleable(A) data_shape = [4, 3, 4] filt = util.randn([1, 4, 2, 3], dtype=dtype) A = linop.ConvolveData(data_shape, filt, mode=mode, multi_channel=True) self.check_linop_linear(A, dtype=dtype, device=device) self.check_linop_adjoint(A, dtype=dtype, device=device) self.check_linop_pickleable(A) data_shape = [1, 3, 4] filt = util.randn([4, 1, 2, 3], dtype=dtype) A = linop.ConvolveData(data_shape, filt, mode=mode, multi_channel=True) self.check_linop_linear(A, dtype=dtype, device=device) self.check_linop_adjoint(A, dtype=dtype, device=device) self.check_linop_pickleable(A) data_shape = [2, 3, 4] filt = util.randn([4, 2, 2, 3], dtype=dtype) A = linop.ConvolveData(data_shape, filt, mode=mode, multi_channel=True) self.check_linop_linear(A, dtype=dtype, device=device) self.check_linop_adjoint(A, dtype=dtype, device=device) self.check_linop_pickleable(A) data_shape = [2, 3, 4] filt = util.randn([4, 2, 2, 3], dtype=dtype) strides = [2, 2] A = linop.ConvolveData(data_shape, filt, mode=mode, strides=strides, multi_channel=True) self.check_linop_linear(A, dtype=dtype, device=device) self.check_linop_adjoint(A, dtype=dtype, device=device) self.check_linop_pickleable(A)
def check_linop_linear(self, A, device=backend.cpu_device, dtype=np.float): device = backend.Device(device) a = util.randn([1], dtype=dtype, device=device) x = util.randn(A.ishape, dtype=dtype, device=device) y = util.randn(A.ishape, dtype=dtype, device=device) xp = device.xp with device: xp.testing.assert_allclose(A(a * x + y), a * A(x) + A(y), atol=1e-5, rtol=1e-5)
def test_dual_precond_LinearLeastSquares(self): n = 5 mat = np.eye(n) + 0.1 * util.randn([n, n]) A = linop.MatMul([n, 1], mat) x = util.randn([n, 1]) y = A(x) x_lstsq = np.linalg.lstsq(mat, y, rcond=-1)[0] d = 1 / np.sum(abs(mat)**2, axis=1, keepdims=True).reshape([n, 1]) x_rec = app.LinearLeastSquares(A, y, alg_name='PrimalDualHybridGradient', max_iter=1000, sigma=d).run() npt.assert_allclose(x_rec, x_lstsq)
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_L2ConstrainedMinimization(self): n = 5 mat = np.eye(n) + 0.1 * util.randn([n, n]) A = linop.MatMul([n, 1], mat) x = util.randn([n, 1]) y = A(x) eps = 0 def proxg(lamda, x): return x / (1 + lamda) x_rec = app.L2ConstrainedMinimization(A, y, proxg, eps).run() npt.assert_allclose(x_rec, x)
def check_linop_linear(self, A, devices=devices, dtypes=dtypes): for device in devices: for dtype in dtypes: with self.subTest(A=A, dtype=dtype, device=device): device = backend.Device(device) a = util.randn([1], dtype=dtype, device=device) x = util.randn(A.ishape, dtype=dtype, device=device) y = util.randn(A.ishape, dtype=dtype, device=device) xp = device.xp with device: xp.testing.assert_allclose(A(a * x + y), a * A(x) + A(y), atol=1e-5, rtol=1e-5)
def test_MatMul(self): mshape = (5, 4, 2) ishape = (5, 2, 3) A = linop.MatMul(ishape, util.randn(mshape)) check_linop_adjoint(A) check_linop_linear(A) 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_MaxEig(self): n = 5 mat = util.randn([n, n]) A = linop.MatMul([n, 1], mat) s = np.linalg.svd(mat, compute_uv=False) npt.assert_allclose(app.MaxEig(A.H * A, max_iter=100).run(), s[0]**2, atol=1e-2)
def test_L2Reg(self): shape = [6] lamda = 1.0 P = prox.L2Reg(shape, lamda) x = util.randn(shape) y = P(0.1, x) npt.assert_allclose(y, x / (1 + lamda * 0.1))
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_L2Proj(self): shape = [6] epsilon = 1.0 P = prox.L2Proj(shape, epsilon) x = util.randn(shape) y = P(1.0, x) npt.assert_allclose(y, x / np.linalg.norm(x.ravel()))
def check_linop_adjoint(self, A, devices=devices, dtypes=dtypes): for device in devices: for dtype in dtypes: with self.subTest(A=A, dtype=dtype, device=device): device = backend.Device(device) x = util.randn(A.ishape, dtype=dtype, device=device) y = util.randn(A.oshape, dtype=dtype, device=device) xp = device.xp with device: lhs = xp.vdot(A * x, y) rhs = xp.vdot(x, A.H * y) xp.testing.assert_allclose(lhs, rhs, atol=1e-5, rtol=1e-5)
def check_linop_unitary(A, dtype=np.float, device=backend.cpu_device): device = backend.Device(device) x = util.randn(A.ishape, dtype=dtype, device=device) xp = device.xp with device: xp.testing.assert_allclose(A.H * A * x, x, atol=1e-5, rtol=1e-5)
def test_UnitaryTransform(self): shape = [6] lamda = 1.0 A = linop.FFT(shape) P = prox.UnitaryTransform(prox.L2Reg(shape, lamda), A) x = util.randn(shape) y = P(0.1, x) npt.assert_allclose(y, x / (1 + lamda * 0.1))
def check_linop_normal(self, A, device=backend.cpu_device, dtype=np.float): device = backend.Device(device) x = util.randn(A.ishape, dtype=dtype, device=device) xp = device.xp with device: lhs = A.H * A * x rhs = A.N * x xp.testing.assert_allclose(lhs, rhs, atol=1e-2, rtol=1e-3)
def test_proxg_LinearLeastSquares(self): n = 5 mat = np.eye(n) + 0.1 * util.randn([n, n]) A = linop.MatMul([n, 1], mat) x = util.randn([n, 1]) y = A(x) lamda = 0.1 x_lstsq = np.linalg.solve(np.matmul(mat.conjugate().T, mat) + lamda * np.eye(n), np.matmul(mat.conjugate().T, y)) proxg = prox.L2Reg([n, 1], lamda) x_rec = app.LinearLeastSquares( A, y, alg_name='GradientMethod', max_iter=1000, proxg=proxg).run() npt.assert_allclose(x_rec, x_lstsq) x_rec = app.LinearLeastSquares(A, y, max_iter=1000, proxg=proxg, alg_name='PrimalDualHybridGradient').run() npt.assert_allclose(x_rec, x_lstsq)
def test_L2ConstrainedMinimization(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) eps = 0 def proxg(lamda, x): return x / (1 + lamda) x_rec = app.L2ConstrainedMinimization(A, y, proxg, eps, show_pbar=False).run() npt.assert_allclose(x_rec, x, atol=1e-3)
def test_LinearLeastSquares(self): n = 5 mat = np.eye(n) + 0.1 * util.randn([n, n]) A = linop.MatMul([n, 1], mat) x = util.randn([n, 1]) y = A(x) x_lstsq = np.linalg.lstsq(mat, y, rcond=-1)[0] x_rec = app.LinearLeastSquares(A, y).run() npt.assert_allclose(x_rec, x_lstsq) x_rec = app.LinearLeastSquares( A, y, alg_name='GradientMethod', max_iter=1000).run() npt.assert_allclose(x_rec, x_lstsq) x_rec = app.LinearLeastSquares(A, y, max_iter=1000, alg_name='PrimalDualHybridGradient').run() npt.assert_allclose(x_rec, x_lstsq)
def test_AsType(self): shape = [5] A = linop.AsType(shape, np.complex, np.float) x = util.randn(shape) npt.assert_allclose(A(x), x) self.check_linop_linear(A, dtypes=[np.float]) self.check_linop_adjoint(A, dtypes=[np.float]) self.check_linop_pickleable(A)
def __init__(self, A, dtype=np.float, device=backend.cpu_device, max_iter=30, show_pbar=True): self.x = util.randn(A.ishape, dtype=dtype, device=device) alg = PowerMethod(A, self.x, max_iter=max_iter) super().__init__(alg, show_pbar=show_pbar)
def test_Transpose(self): shape = [3, 4] A = linop.Transpose(shape) self.check_linop_adjoint(A) self.check_linop_linear(A) self.check_linop_unitary(A) self.check_linop_pickleable(A) x = util.randn(shape) npt.assert_allclose(A(x), np.transpose(x))
def test_ConvolveFilter(self): devices = [backend.cpu_device] if config.cupy_enabled: devices.append(backend.Device(0)) for device in devices: for mode in ['full', 'valid']: W_shape = [2, 3] x = util.randn([3, 4], device=device) A = linop.ConvolveFilter(W_shape, x, mode=mode) check_linop_linear(A, device=device) check_linop_adjoint(A, device=device) check_linop_pickleable(A) W_shape = [4, 2, 3] x = util.randn([4, 3, 4], device=device) A = linop.ConvolveFilter(W_shape, x, mode=mode, input_multi_channel=True) check_linop_linear(A, device=device) check_linop_adjoint(A, device=device) check_linop_pickleable(A) W_shape = [4, 2, 3] x = util.randn([3, 4], device=device) A = linop.ConvolveFilter(W_shape, x, mode=mode, output_multi_channel=True) check_linop_linear(A, device=device) check_linop_adjoint(A, device=device) check_linop_pickleable(A) W_shape = [4, 2, 2, 3] x = util.randn([2, 3, 4], device=device) A = linop.ConvolveFilter(W_shape, x, mode=mode, input_multi_channel=True, output_multi_channel=True) check_linop_linear(A, device=device) check_linop_adjoint(A, device=device) check_linop_pickleable(A)
def test_Add(self): shape = [5] I = linop.Identity(shape) A = linop.Add([I, I]) x = util.randn(shape) npt.assert_allclose(A(x), 2 * x) check_linop_linear(A) check_linop_adjoint(A) check_linop_pickleable(A)
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_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)