def test_dgapl21l1(): """Test duality gap for L21 + L1 regularization.""" n_orient = 2 M, G, active_set = _generate_tf_data() n_times = M.shape[1] n_sources = G.shape[1] tstep, wsize = 4, 32 n_steps = int(np.ceil(n_times / float(tstep))) n_freqs = wsize // 2 + 1 n_coefs = n_steps * n_freqs phi = _Phi(wsize, tstep, n_coefs) phiT = _PhiT(tstep, n_freqs, n_steps, n_times) for l1_ratio in [0.05, 0.1]: alpha_max = norm_epsilon_inf(G, M, phi, l1_ratio, n_orient) alpha_space = (1. - l1_ratio) * alpha_max alpha_time = l1_ratio * alpha_max Z = np.zeros([n_sources, n_coefs]) shape = (-1, n_steps, n_freqs) # for alpha = alpha_max, Z = 0 is the solution so the dgap is 0 gap = dgap_l21l1(M, G, Z, np.ones(n_sources, dtype=bool), alpha_space, alpha_time, phi, phiT, shape, n_orient, -np.inf)[0] assert_allclose(0., gap) # check that solution for alpha smaller than alpha_max is non 0: X_hat_tf, active_set_hat_tf, E, gap = tf_mixed_norm_solver( M, G, alpha_space / 1.01, alpha_time / 1.01, maxit=200, tol=1e-8, verbose=True, debias=False, n_orient=n_orient, tstep=tstep, wsize=wsize, return_gap=True) # allow possible small numerical errors (negative gap) assert_array_less(-1e-10, gap) assert_array_less(gap, 1e-8) assert_array_less(1, len(active_set_hat_tf)) X_hat_tf, active_set_hat_tf, E, gap = tf_mixed_norm_solver( M, G, alpha_space / 5., alpha_time / 5., maxit=200, tol=1e-8, verbose=True, debias=False, n_orient=n_orient, tstep=tstep, wsize=wsize, return_gap=True) assert_array_less(-1e-10, gap) assert_array_less(gap, 1e-8) assert_array_less(1, len(active_set_hat_tf))
def test_iterative_reweighted_tfmxne(): """Test convergence of irTF-MxNE solver.""" M, G, true_active_set = _generate_tf_data() alpha_space = 38. alpha_time = 0.5 tstep, wsize = [4, 2], [64, 16] X_hat_tf, _, _ = tf_mixed_norm_solver( M, G, alpha_space, alpha_time, maxit=1000, tol=1e-4, wsize=wsize, tstep=tstep, verbose=False, n_orient=1, debias=False) X_hat_bcd, active_set, _ = iterative_tf_mixed_norm_solver( M, G, alpha_space, alpha_time, 1, wsize=wsize, tstep=tstep, maxit=1000, tol=1e-4, debias=False, verbose=False) assert_allclose(X_hat_tf, X_hat_bcd, rtol=1e-3) assert_array_equal(np.where(active_set)[0], true_active_set) alpha_space = 50. X_hat_bcd, active_set, _ = iterative_tf_mixed_norm_solver( M, G, alpha_space, alpha_time, 3, wsize=wsize, tstep=tstep, n_orient=5, maxit=1000, tol=1e-4, debias=False, verbose=False) assert_array_equal(np.where(active_set)[0], [0, 1, 2, 3, 4]) alpha_space = 40. X_hat_bcd, active_set, _ = iterative_tf_mixed_norm_solver( M, G, alpha_space, alpha_time, 2, wsize=wsize, tstep=tstep, n_orient=2, maxit=1000, tol=1e-4, debias=False, verbose=False) assert_array_equal(np.where(active_set)[0], [0, 1, 4, 5])
def test_tf_mxne_vs_mxne(): """Test equivalence of TF-MxNE (with alpha_time=0) and MxNE.""" alpha_space = 60. alpha_time = 0. M, G, active_set = _generate_tf_data() X_hat_tf, active_set_hat_tf, E = tf_mixed_norm_solver(M, G, alpha_space, alpha_time, maxit=200, tol=1e-8, verbose=True, debias=False, n_orient=1, tstep=4, wsize=32) # Also run L21 and check that we get the same X_hat_l21, _, _ = mixed_norm_solver(M, G, alpha_space, maxit=200, tol=1e-8, verbose=False, n_orient=1, active_set_size=None, debias=False) assert_allclose(X_hat_tf, X_hat_l21, rtol=1e-1)
def test_tf_mxne_vs_mxne(): """Test equivalence of TF-MxNE (with alpha_time=0) and MxNE""" alpha_space = 60 alpha_time = 0 M, G, active_set = _generate_tf_data() X_hat, active_set_hat, E = tf_mixed_norm_solver(M, G, alpha_space, alpha_time, maxit=200, tol=1e-8, verbose=True, debias=False, n_orient=1, tstep=4, wsize=32) # Also eggie L21 and check that we get the same X_hat_l21, _, _ = mixed_norm_solver(M, G, alpha_space, maxit=200, tol=1e-8, verbose=False, n_orient=1, active_set_size=None, debias=False) assert_array_almost_equal(X_hat, X_hat_l21, decimal=2)
def test_dgapl21l1(): """Test duality gap for L21 + L1 regularization.""" n_orient = 2 M, G, active_set = _generate_tf_data() n_times = M.shape[1] n_sources = G.shape[1] tstep, wsize = np.array([4, 2]), np.array([64, 16]) n_steps = np.ceil(n_times / tstep.astype(float)).astype(int) n_freqs = wsize // 2 + 1 n_coefs = n_steps * n_freqs phi = _Phi(wsize, tstep, n_coefs) phiT = _PhiT(tstep, n_freqs, n_steps, n_times) for l1_ratio in [0.05, 0.1]: alpha_max = norm_epsilon_inf(G, M, phi, l1_ratio, n_orient) alpha_space = (1. - l1_ratio) * alpha_max alpha_time = l1_ratio * alpha_max Z = np.zeros([n_sources, phi.n_coefs.sum()]) # for alpha = alpha_max, Z = 0 is the solution so the dgap is 0 gap = dgap_l21l1(M, G, Z, np.ones(n_sources, dtype=bool), alpha_space, alpha_time, phi, phiT, n_orient, -np.inf)[0] assert_allclose(0., gap) # check that solution for alpha smaller than alpha_max is non 0: X_hat_tf, active_set_hat_tf, E, gap = tf_mixed_norm_solver( M, G, alpha_space / 1.01, alpha_time / 1.01, maxit=200, tol=1e-8, verbose=True, debias=False, n_orient=n_orient, tstep=tstep, wsize=wsize, return_gap=True) # allow possible small numerical errors (negative gap) assert_array_less(-1e-10, gap) assert_array_less(gap, 1e-8) assert_array_less(1, len(active_set_hat_tf)) X_hat_tf, active_set_hat_tf, E, gap = tf_mixed_norm_solver( M, G, alpha_space / 5., alpha_time / 5., maxit=200, tol=1e-8, verbose=True, debias=False, n_orient=n_orient, tstep=tstep, wsize=wsize, return_gap=True) assert_array_less(-1e-10, gap) assert_array_less(gap, 1e-8) assert_array_less(1, len(active_set_hat_tf))
def test_tf_mxne(): """Test convergence of TF-MxNE solver""" alpha_space = 10. alpha_time = 5. M, G, active_set = _generate_tf_data() X_hat_prox, active_set_hat_prox, E = tf_mixed_norm_solver( M, G, alpha_space, alpha_time, maxit=200, tol=1e-8, verbose=True, n_orient=1, tstep=4, wsize=32) assert_array_equal(np.where(active_set_hat_prox)[0], active_set)
def test_tf_mxne(): """Test convergence of TF-MxNE solver""" alpha_space = 10. alpha_time = 5. M, G, active_set = _generate_tf_data() X_hat_tf, active_set_hat_tf, E = tf_mixed_norm_solver( M, G, alpha_space, alpha_time, maxit=200, tol=1e-8, verbose=True, n_orient=1, tstep=4, wsize=32) assert_array_equal(np.where(active_set_hat_tf)[0], active_set)
def test_tf_mxne(): """Test convergence of TF-MxNE solver.""" alpha_space = 10. alpha_time = 5. M, G, active_set = _generate_tf_data() X_hat_tf, active_set_hat_tf, E, gap_tfmxne = tf_mixed_norm_solver( M, G, alpha_space, alpha_time, maxit=200, tol=1e-8, verbose=True, n_orient=1, tstep=4, wsize=32, return_gap=True) assert_array_less(gap_tfmxne, 1e-8) assert_array_equal(np.where(active_set_hat_tf)[0], active_set)
def test_tf_mxne(): """Test convergence of TF-MxNE solver.""" alpha_space = 10. alpha_time = 5. M, G, active_set = _generate_tf_data() with pytest.warns(None): # CD X_hat_tf, active_set_hat_tf, E, gap_tfmxne = tf_mixed_norm_solver( M, G, alpha_space, alpha_time, maxit=200, tol=1e-8, verbose=True, n_orient=1, tstep=4, wsize=32, return_gap=True) assert_array_less(gap_tfmxne, 1e-8) assert_array_equal(np.where(active_set_hat_tf)[0], active_set)
def test_tf_mxne_vs_mxne(): """Test equivalence of TF-MxNE (with alpha_time=0) and MxNE""" alpha_space = 60 alpha_time = 0 M, G, active_set = _generate_tf_data() X_hat, active_set_hat, E = tf_mixed_norm_solver( M, G, alpha_space, alpha_time, maxit=200, tol=1e-8, verbose=True, debias=False, n_orient=1, tstep=4, wsize=32) # Also run L21 and check that we get the same X_hat_l21, _, _ = mixed_norm_solver( M, G, alpha_space, maxit=200, tol=1e-8, verbose=False, n_orient=1, active_set_size=None, debias=False) assert_array_almost_equal(X_hat, X_hat_l21, decimal=2)