Esempio n. 1
0
def test_mtl():
    rho_noise = 0.3
    SNR = 1
    n_epochs, n_channels, n_sources, n_times = 5, 20, 10, 30
    pb_name = "MTL"
    tol = 1e-7

    X, all_epochs, B_star, S_star = get_data_me(
        dictionary_type="Gaussian", noise_type="Gaussian_iid",
        n_epochs=n_epochs, n_channels=n_channels, n_times=n_times,
        n_sources=n_sources, n_active=3, rho_noise=rho_noise,
        SNR=SNR)

    Y = np.mean(all_epochs, axis=0)
    sigma_min = get_sigma_min(Y)
    alpha_max = get_alpha_max(X, Y, sigma_min, pb_name=pb_name)

    alpha_div = 5
    alpha = alpha_max / alpha_div

    B_mtl, S_inv, E, gaps = solver(
        X, Y, alpha, alpha_max, sigma_min, B0=None,
        tol=tol, pb_name=pb_name, n_iter=10000)
    gap = gaps[-1]
    np.testing.assert_array_less(gap, tol)

    _, _, E, gaps = solver(
        X, Y, alpha, alpha_max, sigma_min, B0=B_mtl,
        tol=tol, pb_name=pb_name, n_iter=10000)
    np.testing.assert_equal(len(E), 2)
    gap = gaps[-1]
    np.testing.assert_array_less(gap, tol * E[0])
Esempio n. 2
0
def test_mtl_me():
    rho_noise = 0.3
    SNR = 1
    n_epochs, n_channels, n_sources, n_times = 5, 20, 10, 30
    pb_name = "MTLME"
    tol = 1e-7

    X, all_epochs, _, _ = get_data_me(dictionary_type="Gaussian",
                                      noise_type="Gaussian_iid",
                                      n_epochs=n_epochs,
                                      n_channels=n_channels,
                                      n_times=n_times,
                                      n_sources=n_sources,
                                      n_active=3,
                                      rho_noise=rho_noise,
                                      SNR=SNR)

    Y = np.mean(all_epochs, axis=0)
    sigma_min = get_sigma_min(Y)

    alpha_max = get_alpha_max(X, all_epochs, sigma_min, pb_name=pb_name)

    alpha_div = 1.1
    alpha = alpha_max / alpha_div

    gap = solver(X,
                 all_epochs,
                 alpha,
                 sigma_min,
                 B0=None,
                 tol=tol,
                 pb_name=pb_name,
                 n_iter=10000)[-1]
    np.testing.assert_array_less(gap, tol)
Esempio n. 3
0
def test_update_sigma_glasso():
    rho_noise = 0.8
    SNR = 1
    n_epochs, n_channels, n_sources, n_times = 5, 20, 10, 30

    X, all_epochs, B_star, (_, S_star) = get_data_me(
        dictionary_type="Gaussian",
        noise_type="Gaussian_multivariate",
        n_epochs=n_epochs,
        n_channels=n_channels,
        n_times=n_times,
        n_sources=n_sources,
        n_active=3,
        rho_noise=rho_noise,
        SNR=SNR)

    emp_cov = np.zeros((n_channels, n_channels))
    for i in range(n_epochs):
        emp_cov += all_epochs[i, :, :] @ all_epochs[i, :, :].T
    emp_cov /= (n_times * n_epochs)

    alpha_prec = 0.001

    covariance, precision = update_sigma_glasso(emp_cov,
                                                alpha_prec,
                                                cov_init=None,
                                                mode='cd',
                                                tol=1e-4,
                                                enet_tol=1e-4,
                                                max_iter=100,
                                                verbose=False,
                                                return_costs=False,
                                                eps=np.finfo(np.float64).eps,
                                                return_n_iter=False)
Esempio n. 4
0
def tests_sgcl(dictionary_type="Gaussian",
               noise_type="Gaussian_iid",
               rho_noise=0.3,
               SNR=0.5,
               n_channels=20,
               n_times=30,
               n_sources=10,
               n_active=3,
               gap_freq=100,
               active_set_freq=1,
               S_freq=10,
               n_iter=10**6,
               p_alpha_max=0.9,
               tol=10**-4):

    X, all_epochs, _, _ = get_data_me(dictionary_type="Gaussian",
                                      noise_type=noise_type,
                                      n_channels=n_channels,
                                      n_times=n_times,
                                      n_sources=n_sources,
                                      n_active=n_active,
                                      rho_noise=rho_noise,
                                      SNR=SNR,
                                      n_epochs=50)

    Y = all_epochs.mean(axis=0)
    sigma_min = get_sigma_min(Y)
    alpha_max = get_alpha_max(X, Y, sigma_min, "SGCL")
    alpha = alpha_max * p_alpha_max
    print("alpha = %.2e" % alpha)
    print("sigma_min = %.2e" % sigma_min)

    all_epochs = np.zeros((1, *Y.shape))
    all_epochs[0] = Y

    B_sgcl, S_inv_sgcl, E, (gaps, gaps_accel) = solver(
        X,
        Y,
        alpha,
        alpha_max,
        sigma_min,
        B0=None,
        n_iter=n_iter,
        gap_freq=gap_freq,
        active_set_freq=active_set_freq,
        S_freq=S_freq,
        pb_name="SGCL",
        use_accel=True,
        tol=tol,
        verbose=True)

    log_gap = np.log10(gaps[-1])
    log_gap_accel = np.log10(gaps_accel[-1])
    assert log_gap_accel < np.log10(tol) * E[0] or \
        log_gap < np.log10(tol) * E[0]
Esempio n. 5
0
def test_clar(noise_type="Gaussian_iid",
              rho_noise=0.3,
              SNR=0.5,
              n_channels=20,
              n_times=30,
              n_sources=10,
              n_epochs=50,
              n_active=3,
              gap_freq=50,
              active_set_freq=1,
              S_freq=10,
              n_iter=10**4,
              alpha_under_alpha_max=0.2,
              tol=1e-7):
    X, all_epochs, _, _ = get_data_me(dictionary_type="Gaussian",
                                      noise_type=noise_type,
                                      n_epochs=n_epochs,
                                      n_channels=n_channels,
                                      n_times=n_times,
                                      n_sources=n_sources,
                                      n_active=n_active,
                                      rho_noise=rho_noise,
                                      SNR=SNR)
    Y = np.mean(all_epochs, axis=0)
    sigma_min = get_sigma_min(Y)
    alpha_max = get_alpha_max(X, all_epochs, sigma_min, pb_name="CLAR")
    alpha = alpha_max * \
        alpha_under_alpha_max

    print("alpha = %.2e" % alpha)
    print("alpha = %.2e" % alpha)
    print("sigma_min = %.2e" % sigma_min)
    gaps_me = solver(X,
                     all_epochs,
                     alpha,
                     sigma_min,
                     B0=None,
                     n_iter=n_iter,
                     gap_freq=gap_freq,
                     active_set_freq=active_set_freq,
                     S_freq=S_freq,
                     tol=tol,
                     pb_name="CLAR")[-1]
    gap_me = gaps_me[-1]
    assert gap_me < tol
Esempio n. 6
0
def test_mrce():
    rho_noise = 0.6
    SNR = 1
    n_epochs, n_channels, n_sources, n_times = 5, 20, 10, 30
    pb_name = "mrce"
    tol = 1e-4

    X, all_epochs, B_star, (_, S_star) = get_data_me(
        dictionary_type="Gaussian",
        noise_type="Gaussian_multivariate",
        n_epochs=n_epochs,
        n_channels=n_channels,
        n_times=n_times,
        n_sources=n_sources,
        n_active=3,
        rho_noise=rho_noise,
        SNR=SNR)

    alpha_Sigma_inv = 0.01

    Y = np.mean(all_epochs, axis=0)
    sigma_min = get_sigma_min(Y)
    alpha_max = get_alpha_max(X,
                              all_epochs,
                              sigma_min,
                              pb_name=pb_name,
                              alpha_Sigma_inv=alpha_Sigma_inv)

    alpha = alpha_max * 0.9

    B_mrce, (Sigma,
             Sigma_inv), E, gaps = solver(X,
                                          all_epochs,
                                          alpha,
                                          alpha_max,
                                          sigma_min,
                                          B0=None,
                                          tol=tol,
                                          pb_name=pb_name,
                                          n_iter=1000,
                                          alpha_Sigma_inv=alpha_Sigma_inv)
Esempio n. 7
0
def test_update_sigma_glasso():
    rho_noise = 0.8
    SNR = 1
    n_epochs, n_channels, n_sources, n_times = 5, 20, 10, 30

    all_epochs = get_data_me(dictionary_type="Gaussian",
                             noise_type="Gaussian_multivariate",
                             n_epochs=n_epochs,
                             n_channels=n_channels,
                             n_times=n_times,
                             n_sources=n_sources,
                             n_active=3,
                             rho_noise=rho_noise,
                             SNR=SNR)[1]

    emp_cov = np.zeros((n_channels, n_channels))
    for i in range(n_epochs):
        emp_cov += all_epochs[i, :, :] @ all_epochs[i, :, :].T
    emp_cov /= (n_times * n_epochs)

    alpha_prec = 0.001

    update_sigma_glasso(emp_cov, alpha_prec, enet_tol=1e-4, max_iter=100)
Esempio n. 8
0
SNR = 0.5
n_channels = 20
n_times = 30
n_sources = 10
n_epochs = 50
n_active = 3
gap_freq = 50
update_S_freq = 10
n_iter = 10**4
tol = 1e-7

X, all_epochs, B_star, S_star = get_data_me(dictionary_type="Gaussian",
                                            noise_type="Gaussian_multivariate",
                                            n_epochs=n_epochs,
                                            n_channels=n_channels,
                                            n_times=n_times,
                                            n_sources=n_sources,
                                            n_active=n_active,
                                            rho_noise=rho_noise,
                                            SNR=SNR)
S_star = S_star[-1]

Y = np.mean(all_epochs, axis=0)
sigma_min = get_sigma_min(Y)

alpha_max = get_alpha_max_me(X, all_epochs, sigma_min)
alpha = alpha_max / 5

print("alpha = %.2e" % alpha)
print("sigma_min = %.2e" % sigma_min)
B_clar, S_inv, E, gaps_me = solver(X,