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 l2_unconstrained_solver( data, warm_start, sigma, weights, psi, sigma_signal=1e-3, options={ 'tol': 1e-5, 'iter': 5000, 'update_iter': 50, 'record_iters': False, 'positivity': True, '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 h = prox_operators.l2_square_norm(sigma_signal, psi) f = None if options['real'] == True: if options["positivity"] == True: f = prox_operators.positive_prox() else: f = prox_operators.reality_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_diag_matrix_op(): A = np.random.normal(0, 10., (10)) * 1j op = linear_operators.diag_matrix_operator(A) inp = np.random.normal(0, 10., (10)) out = A * inp forward_operator(op, inp, out) inp = np.random.normal(0, 10., (10)) out = np.conj(A) * inp adjoint_operator(op, inp, out)
def tv_unconstrained_solver( data, warm_start, sigma, weights, 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: 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.") psi = linear_operators.function_wrapper(forward, backward) h = prox_operators.l21_norm(beta, 0, 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 tv_constrained_solver( data, warm_start, sigma, weights, beta=1e-3, options={ 'tol': 1e-5, 'iter': 5000, 'update_iter': 50, 'record_iters': False, 'positivity': False, 'real': False }): """ Solve constrained tv 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() 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.") psi = linear_operators.function_wrapper(forward, backward) h = prox_operators.l21_norm( np.sqrt(np.max(np.sum(np.abs(psi.dir_op(data))**2), axis=0)) * beta, 0, psi) return primal_dual.FBPD(warm_start, options, None, f, h, p)
output_dir = 'output/' options = {'tol': 1e-5, 'iter': 5000, '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) 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))
sys.path.insert(0, '..') output_dir = 'output/' options = {'tol': 1e-5, 'iter': 5000, '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)) g.beta = 1. / sigma**2 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) plt.plot(np.real(y)) plt.plot(np.real(x)) plt.plot(np.real(z))