def test_l1_unconstrained_fb(): options = { 'tol': 1e-6, 'iter': 5000, 'update_iter': 50, 'record_iters': False } ISNR = 50. sigma = 10**(-ISNR / 20.) size = 32 epsilon = np.sqrt(size + 2. * np.sqrt(size)) * sigma x = np.linspace(0, 30, size) W = np.ones((size, )) y = W * x + np.random.normal(0, sigma, size) g = grad_operators.l2_norm(sigma, y, linear_operators.diag_matrix_operator(W)) g.beta = 1 / sigma**2 psi = linear_operators.diag_matrix_operator(W) gamma = 50 h = prox_operators.l1_norm(gamma, psi) h.beta = 1. f = prox_operators.real_prox() z, diag = primal_dual.FBPD(x, options, g, f, h) z_fb = x mu = 0.5 * sigma**2 for it in range(5000): z_fb = h.prox(z_fb - mu * g.grad(z_fb), mu) h = prox_operators.l1_norm(gamma * sigma**2, psi) solution = h.prox(y, 1) assert (diag['max_iter'] < 500) assert (np.isclose(z, z_fb, 1e-3).all()) assert (np.isclose(z, solution, 1e-3).all())
def test_l1_constrained(): options = { 'tol': 1e-5, 'iter': 5000, 'update_iter': 50, 'record_iters': False } ISNR = 40. sigma = 10**(-ISNR / 20.) size = 1024 epsilon = np.sqrt(size + 2. * np.sqrt(size)) * sigma x = np.linspace(0, 1 * np.pi, size) W = np.ones((size, )) y = W * x + np.random.normal(0, sigma, size) p = prox_operators.l2_ball(epsilon, y, linear_operators.diag_matrix_operator(W)) wav = ['db1', 'db4'] levels = 6 shape = (size, ) psi = linear_operators.dictionary(wav, levels, shape) h = prox_operators.l1_norm(np.max(np.abs(psi.dir_op(y))) * 1e-3, psi) h.beta = 1. f = prox_operators.real_prox() z, diag = primal_dual.FBPD(y, options, None, f, h, p) assert (np.linalg.norm(z - W * y) < epsilon * 1.05) assert (diag['max_iter'] < 500) #testing warm start z1, diag1 = primal_dual.FBPD_warm_start(z, diag['y'], diag['z'], diag['w'], options, None, f, h, p) assert (diag1['max_iter'] < diag['max_iter'])
def test_l1_unconstrained(): options = { 'tol': 1e-5, 'iter': 500, 'update_iter': 50, 'record_iters': False } ISNR = 20. sigma = 10**(-ISNR / 20.) size = 1024 epsilon = np.sqrt(size + 2. * np.sqrt(size)) * sigma x = np.linspace(0, 1 * np.pi, size) W = np.ones((size, )) y = W * x + np.random.normal(0, sigma, size) g = grad_operators.l2_norm(sigma, y, linear_operators.diag_matrix_operator(W)) wav = ['db1', 'db4'] levels = 6 shape = (size, ) psi = linear_operators.dictionary(wav, levels, shape) h = prox_operators.l1_norm(np.max(np.abs(psi.dir_op(y))) * 5e-3, psi) h.beta = 1. f = prox_operators.real_prox() z, diag = primal_dual.FBPD(y, options, g, f, h)
def l1_unconstrained_solver( data, warm_start, sigma, weights, psi, beta=1e-3, options={ 'tol': 1e-5, 'iter': 5000, 'update_iter': 50, 'record_iters': False, 'positivity': False, 'real': False }): """ Solve unconstrained l1 regularization problem """ phi = linear_operators.diag_matrix_operator(weights) g = grad_operators.l2_norm(sigma, data, phi) g.beta = np.max(np.abs(weights))**2 / sigma**2 if beta <= 0: h = None else: h = prox_operators.l1_norm(beta, psi) f = None if options['real'] == True: if options["positivity"] == True: f = prox_operators.positive_prox() else: f = prox_operators.real_prox() return primal_dual.FBPD(warm_start, options, g, f, h)
def l1_constrained_solver( data, warm_start, sigma, weights, psi, beta=1e-3, options={ 'tol': 1e-5, 'iter': 5000, 'update_iter': 50, 'record_iters': False, 'positivity': False, 'real': False }): """ Solve constrained l1 regularization problem """ phi = linear_operators.diag_matrix_operator(weights) size = len(np.ravel(data)) epsilon = np.sqrt(size + 2 * np.sqrt(2 * size)) * sigma p = prox_operators.l2_ball(epsilon, data, phi) p.beta = np.max(np.abs(weights))**2 f = None if options['real'] == True: f = prox_operators.real_prox() if options["positivity"] == True: f = prox_operators.positive_prox() h = prox_operators.l1_norm(np.max(np.abs(psi.dir_op(data))) * beta, psi) return primal_dual.FBPD(warm_start, options, None, f, h, p)
def l1_poissonian_unconstrained_solver( data, warm_start, weights, background, psi, beta=1e-3, options={ 'tol': 1e-5, 'iter': 5000, 'update_iter': 50, 'record_iters': False, 'positivity': False, 'real': False }): """ Solve unconstrained l1 regularization problem with poisson noise """ phi = linear_operators.diag_matrix_operator(weights) p = prox_operators.poisson_loglike(data, background, phi) p.beta = np.max(np.abs(weights))**2 if beta <= 0: h = None else: h = prox_operators.l1_norm(beta, psi) if options['real'] == True: if options["positivity"] == True: f = prox_operators.positive_prox() else: raise Exception("Positivity required.") f = None return primal_dual.FBPD(warm_start, options, None, f, h, p)
def l1_poissonian_constrained_solver( data, warm_start, sigma, weights, background, psi, beta=1e-3, options={ 'tol': 1e-5, 'iter': 5000, 'update_iter': 50, 'record_iters': False, 'positivity': False, 'real': False }): """ Solve constrained l1 regularization problem with poisson noise """ phi = linear_operators.diag_matrix_operator(weights) size = len(np.ravel(data)) epsilon = sigma p = prox_operators.poisson_loglike_ball(epsilon, data, background, 50, phi) p.beta = np.max(np.abs(weights))**2 f = None if options['real'] == True: if options["positivity"] == True: f = prox_operators.positive_prox() else: raise Exception("Positivity required.") h = prox_operators.l1_norm(np.max(np.abs(psi.dir_op(data))) * beta, psi) return primal_dual.FBPD(warm_start, options, None, f, h, p)
def test_l1_norm_op(): gamma = 2 inp = np.random.normal(0, 10., (10, 10)) out = np.maximum(0, np.abs(inp) - gamma) * \ np.exp(complex(0, 1) * np.angle(inp)) op = prox_operators.l1_norm(gamma) assert (op.fun(inp) >= 0) assert np.allclose(op.prox(inp, 1), out, 1e-6) gamma = np.abs(np.random.normal(0, 3., (10, 10))) inp = np.random.normal(0, 10., (10, 10)) out = np.maximum(0, np.abs(inp) - gamma) * \ np.exp(complex(0, 1) * np.angle(inp)) op = prox_operators.l1_norm(gamma) assert (op.fun(inp) >= 0) assert np.allclose(op.prox(inp, 1), out, 1e-6)
def tv_constrained_separation_solver( data, warm_start, sigma, weights, psi1, gamma=1, beta=1e-3, options={ 'tol': 1e-5, 'iter': 5000, 'update_iter': 50, 'record_iters': False, 'positivity': False, 'real': False }): """ Solve constrained l1 regularization problem for signal separation """ def m_forward(x): return (x[0, :, :] + x[1, :, :]) * weights def m_backward(x): out = np.zeros((2, x.shape[0], x.shape[1]), dtype=complex) out[0] = x * weights out[1] = x * weights return out phi = linear_operators.function_wrapper(m_forward, m_backward) size = len(np.ravel(data)) epsilon = np.sqrt(size + 2 * np.sqrt(2 * size)) * sigma p = prox_operators.l2_ball(epsilon, data, phi) p.beta = np.max(np.abs(weights))**2 * 4 f = None if options['real'] == True: f = prox_operators.real_prox() if options["positivity"] == True: f = prox_operators.positive_prox() def forward(x): if x.ndim == 2: out = np.zeros((2, x.shape[0] - 1, x.shape[1] - 1)) out[0, :, :] = (x[:-1, :-1] - x[1:, :-1]) / 2 out[1, :, :] = (x[:-1, :-1] - x[:-1, 1:]) / 2 return out else: raise Exception("Sorry, only two dimensions for now.") def backward(x): if x.ndim == 3: out = np.zeros((x.shape[1] + 1, x.shape[2] + 1)) out[:-1, :-1] += x[0, :, :] / 2 out[:-1, :-1] += x[1, :, :] / 2 out[1:, :-1] += -x[0, :, :] / 2 out[:-1, 1:] += -x[1, :, :] / 2 return out else: raise Exception("Sorry, only two dimensions for now.") def w1_forward(x): return psi1.dir_op(x[0, :, :]) def w2_forward(x): return forward(x[1, :, :]) def w1_backward(x): out = psi1.adj_op(x) buff = np.zeros((2, out.shape[0], out.shape[1]), dtype=complex) buff[0] = out return buff def w2_backward(x): out = backward(x) buff = np.zeros((2, out.shape[0], out.shape[1]), dtype=complex) buff[1] = out return buff psi1_wrapper = linear_operators.function_wrapper(w1_forward, w1_backward) psi2_wrapper = linear_operators.function_wrapper(w2_forward, w2_backward) h = prox_operators.l1_norm( np.max(np.abs(psi1.dir_op(data))) * beta, psi1_wrapper) h.beta = 1 r = prox_operators.l21_norm( np.sqrt(np.max(np.sum(np.abs(forward(data))**2, axis=0))) * beta * gamma, 0, psi2_wrapper) r.beta = 1 out = np.zeros((2, warm_start.shape[0], warm_start.shape[1]), dtype=complex) out[0] = warm_start out[1] = warm_start return primal_dual.FBPD(out, options, None, f, h, p, r)
size = 1024 epsilon = np.sqrt(size + 2. * np.sqrt(size)) * sigma x = np.linspace(0, 1 * np.pi, size) W = np.ones((size, )) y = W * x + np.random.normal(0, sigma, size) p = prox_operators.l2_ball(epsilon, y, linear_operators.diag_matrix_operator(W)) p.beta = 1. wav = ['db1', 'db4'] levels = 6 shape = (size, ) psi = linear_operators.dictionary(wav, levels, shape) h = prox_operators.l1_norm(np.max(np.abs(psi.dir_op(y))) * 1e-3, psi) h.beta = 1. f = prox_operators.real_prox() z, diag = primal_dual.FBPD(y, options, None, f, h, p, None) plt.plot(np.real(y)) plt.plot(np.real(x)) plt.plot(np.real(z)) plt.legend(['data', 'true', 'fit']) SNR = np.log10( np.sqrt(np.sum(np.abs(x)**2)) / np.sqrt(np.sum(np.abs(x - z)**2))) * 20. plt.title('SNR = ' + str(SNR)) plt.savefig(output_dir + '1d_constrained_example.png')