def test_graph_net_and_tvl1_same_for_pure_l1(max_iter=100, decimal=2):
    ###############################################################
    # graph_net_solver and tvl1_solver should give same results
    # when l1_ratio = 1.
    ###############################################################

    X, y, _, mask = _make_data()
    alpha = .1
    unmasked_X = np.rollaxis(X, -1, start=0)
    unmasked_X = np.array([x[mask] for x in unmasked_X])

    # results should be exactly the same for pure lasso
    a = tvl1_solver(unmasked_X, y, alpha, 1., mask, loss="mse",
                    max_iter=max_iter)[0]
    b = _graph_net_squared_loss(unmasked_X, y, alpha, 1.,
                                max_iter=max_iter,
                                mask=mask)[0]

    mask = nibabel.Nifti1Image(mask.astype(np.float), np.eye(4))
    X = nibabel.Nifti1Image(X.astype(np.float), np.eye(4))
    for standardize in [True, False]:
        sl = BaseSpaceNet(
            alphas=alpha, l1_ratios=1., mask=mask, penalty="graph-net",
            max_iter=max_iter, standardize=standardize).fit(X, y)
        tvl1 = BaseSpaceNet(
            alphas=alpha, l1_ratios=1., mask=mask, penalty="tv-l1",
            max_iter=max_iter, standardize=standardize).fit(X, y)

        # Should be exactly the same (except for numerical errors).
        # However because of the TV-L1 prox approx, results might be 'slightly'
        # different.
        np.testing.assert_array_almost_equal(a, b, decimal=decimal)
        np.testing.assert_array_almost_equal(sl.coef_, tvl1.coef_,
                                             decimal=decimal)
def test_graph_net_and_tvl1_same_for_pure_l1_logistic(max_iter=20,
                                                      decimal=2):
    ###############################################################
    # graph_net_solver and tvl1_solver should give same results
    # when l1_ratio = 1.
    ###############################################################

    iris = load_iris()
    X, y = iris.data, iris.target
    y = y > 0.
    alpha = 1. / X.shape[0]
    X_, mask_ = to_niimgs(X, (2, 2, 2))
    mask = mask_.get_data().astype(np.bool).ravel()

    # results should be exactly the same for pure lasso
    a = _graph_net_logistic(X, y, alpha, 1., mask=mask,
                            max_iter=max_iter)[0]
    b = tvl1_solver(X, y, alpha, 1., loss="logistic", mask=mask,
                    max_iter=max_iter)[0]
    for standardize in [True, False]:
        sl = SpaceNetClassifier(
            alphas=alpha,
            l1_ratios=1.,
            max_iter=max_iter,
            mask=mask_,
            penalty="graph-net",
            standardize=standardize).fit(
            X_,
            y)
        tvl1 = SpaceNetClassifier(
            alphas=alpha,
            l1_ratios=1.,
            max_iter=max_iter,
            mask=mask_,
            penalty="tv-l1",
            standardize=standardize).fit(
            X_,
            y)

    # should be exactly the same (except for numerical errors)
    np.testing.assert_array_almost_equal(a, b, decimal=decimal)
    np.testing.assert_array_almost_equal(sl.coef_[0], tvl1.coef_[0],
                                         decimal=decimal)
示例#3
0
def test_tvl1_solver_raises_value_error_if_invalid_loss():
    pytest.raises(ValueError, lambda loss: tvl1_solver(
        np.array([[1]]), None, None, None, None, loss=loss),
        "invalidloss")
示例#4
0
def test_tvl1_solver_raises_value_error_if_invalid_loss():
    assert_raises(ValueError, lambda loss: tvl1_solver(
        np.array([[1]]), None, None, None, None, loss=loss),
        "invalidloss")