示例#1
0
def test_gradient():
    """ Test the gradients. """
    h = double_gamma_hrf(1.0, 30)
    n_times = 100
    n_times_valid = n_times - len(h) + 1

    y = np.random.randn(1, n_times)
    x0 = np.random.randn(1, n_times_valid)

    hth = np.convolve(h[::-1], h)
    htY = np.r_[[np.convolve(h[::-1], y_, mode='valid') for y_ in y]]

    def grad(x):
        return _grad_t_analysis(x, hth, htY)

    def approx_grad(x):
        def obj(x):
            return _obj_t_analysis(x, y, h, 0.0)

        return approx_fprime(x.ravel(), obj, epsilon=1e-10)

    err = np.linalg.norm(approx_grad(x0) - grad(x0))
    err /= np.linalg.norm(approx_grad(x0))
    try:
        assert err < 1.0e-2
    except AssertionError as e:
        plt.figure("[{0}] Failed estimation".format("approx-grad/grad"))
        plt.plot(approx_grad(x0).T, lw=3.0, label="approx-grad")
        plt.plot(grad(x0).T, ls='--', lw=3.0, label="grad")
        plt.title("Approximated gradient / closed-form gradient")
        plt.legend()
        plt.show()
        raise e
示例#2
0
def test_fista_decrease(momentum, max_iter):
    """ Test that the BOLD-TV cost function deacrease. """
    h = double_gamma_hrf(1.0, 30)
    n_times = 100
    n_times_valid = n_times - len(h) + 1

    y = np.random.randn(1, n_times)
    x0 = np.random.randn(1, n_times_valid)

    hth = np.convolve(h[::-1], h)
    htY = np.r_[[np.convolve(h[::-1], y_, mode='valid') for y_ in y]]

    lipsch_cst = lipsch_cst_from_kernel(h, n_times_valid)
    step_size = 1.0 / lipsch_cst
    lbda = 1.0

    def grad(x):
        return _grad_t_analysis(x, hth, htY)

    def obj(x):
        return _obj_t_analysis(x, y, h, lbda=lbda)

    def prox(x, step_size):
        return np.r_[[tv1_1d(x_, lbda * step_size) for x_ in x]]

    x1 = fista(grad,
               obj,
               prox,
               x0,
               momentum=momentum,
               max_iter=max_iter,
               step_size=step_size)

    assert _obj_t_analysis(x0, y, h, lbda) >= _obj_t_analysis(x1, y, h, lbda)
示例#3
0
def test_convolution():
    """ Test the convolution. """
    t_r = 1.0
    n_time_hrf = 30
    n_time_valid = 100
    n_samples = 10
    h = double_gamma_hrf(t_r, n_time_hrf)
    H = make_toeplitz(h, n_time_valid).T
    u = np.random.randn(n_samples, n_time_valid)
    h_, u_ = torch.Tensor(h), torch.Tensor(u)
    np.testing.assert_allclose(hu_numpy(h, u), u.dot(H), rtol=1e-2)
    np.testing.assert_allclose(hu_tensor(h_, u_).numpy(), u.dot(H), rtol=1e-2)
示例#4
0
def test_rev_convolution():
    """ Test the reverse convolution. """
    t_r = 1.0
    n_time_hrf = 30
    n_time_valid = 100
    n_samples = 10
    h = double_gamma_hrf(t_r, n_time_hrf)
    H = make_toeplitz(h, n_time_valid).T
    u = np.random.randn(n_samples, n_time_valid)
    x = hu_numpy(h, u)
    h_, x_ = torch.Tensor(h), torch.Tensor(x)
    np.testing.assert_allclose(htx_numpy(h, x), x.dot(H.T), rtol=1e-2)
    np.testing.assert_allclose(htx_numpy(h, x),
                               htx_tensor(h_, x_).numpy(),
                               rtol=1e-2)
示例#5
0
def test_hth_id_convolution():
    """ Test the hth-convolution. """
    t_r = 1.0
    n_time_hrf = 30
    n_time_valid = 100
    n_samples = 10
    h = double_gamma_hrf(t_r, n_time_hrf)
    H = make_toeplitz(h, n_time_valid).T
    u = np.random.randn(n_samples, n_time_valid)
    hth = np.convolve(h[::-1], h)
    hth_, u_ = torch.Tensor(hth), torch.Tensor(u)
    np.testing.assert_allclose(hth_id_u_numpy(hth, u),
                               u - u.dot(H.dot(H.T)),
                               rtol=1e-2)
    np.testing.assert_allclose(hth_id_u_numpy(hth, u),
                               hth_id_u_tensor(hth_, u_),
                               rtol=1e-2)
示例#6
0
def test_loss_coherence():
    """ Test the loss function coherence. """
    t_r = 1.0
    n_time_hrf = 30
    n_time_valid = 100
    n_time = n_time_valid + n_time_hrf - 1
    n_samples = 10
    h = double_gamma_hrf(t_r, n_time_hrf)
    u = np.random.randn(n_samples, n_time_valid)
    x = np.random.randn(n_samples, n_time)
    u_ = check_tensor(u)
    x_ = check_tensor(x)
    lbda = 0.1
    kwargs = dict(h=h, n_times_valid=n_time_valid, n_layers=10)
    net_solver = LpgdTautStringHRF(**kwargs)
    loss = float(net_solver._loss_fn(x_, lbda, u_))
    loss_ = _obj_t_analysis(u, x, h, lbda)
    np.testing.assert_allclose(loss, loss_, rtol=1e-3)
    print(f"Archiving '{__file__}' under '{args.plots_dir}'")
    shutil.copyfile(__file__, os.path.join(args.plots_dir, __file__))

    ###########################################################################
    # Parameters to set for the experiment
    hrf_time_frames = 30
    nx = ny = nz = 10
    lw = 7

    ###########################################################################
    # Real data loading
    t_r = 0.735
    n_times_valid = args.n_time_frames - hrf_time_frames + 1

    h = double_gamma_hrf(t_r, hrf_time_frames)
    D = (np.eye(n_times_valid, k=-1) - np.eye(n_times_valid, k=0))[:, :-1]
    H = make_toeplitz(h, n_times_valid).T

    # load data
    sub1_img = 'data/6025086_20227_MNI_RS.nii.gz'
    sub2_img = 'data/6025837_20227_MNI_RS.nii.gz'

    masker = NiftiMasker(standardize=True,
                         detrend=True,
                         low_pass=0.1,
                         high_pass=0.01,
                         t_r=t_r,
                         memory='__cache_dir__')  # noqa: E128
    masker.fit([sub1_img, sub2_img])