Exemple #1
0
def test_VectorSpaceData_instantiation():
    """Test VectorSpaceData initialization"""

    # unlabeled, no domain
    ds = smlb.VectorSpaceData(dimensions=3)
    validate_data_interface(ds)
    assert ds.dimensions == 3 and ds.domain is None
    assert not ds.is_labeled and not ds.is_finite
    assert (ds.samples([[1, 2, 3], [4, 5, 6]]) == [[1, 2, 3], [4, 5, 6]]).all()
    with pytest.raises(smlb.BenchmarkError):
        ds.labels()

    # unlabeled, domain
    ds = smlb.VectorSpaceData(dimensions=3, domain=(1, 5))
    validate_data_interface(ds)
    assert ds.dimensions == 3 and len(ds.domain) == 3
    assert not ds.is_labeled and not ds.is_finite
    assert (ds.samples([[1, 2, 3], [4, 5, 5]]) == [[1, 2, 3], [4, 5, 5]]).all()
    with pytest.raises(smlb.InvalidParameterError):
        ds.samples([[1, 2, 3], [4, 5, 6]])
    with pytest.raises(smlb.BenchmarkError):
        ds.labels()

    # labeled, no domain
    f = lambda arg: np.sum(arg, axis=1)  # noqa: E731
    ds = smlb.VectorSpaceData(dimensions=2, function=f)
    validate_data_interface(ds)
    assert ds.dimensions == 2 and ds.domain is None
    assert ds.is_labeled and not ds.is_finite
    assert (ds.labels([[1, 2], [3, 4]]) == [3, 7]).all()
Exemple #2
0
def test_RandomVectorSampler_1():
    """Simple test cases."""

    # without labels
    ds = smlb.VectorSpaceData(dimensions=2)
    ss = smlb.RandomVectorSampler(size=30, rng=0,
                                  domain=[[0, 1], [-3, 2]]).fit(ds).apply(ds)
    assert ss.samples().shape == (30, 2)
    assert (ss.samples()[:, 0] >= 0).all() and (ss.samples()[:, 1] >= -3).all()
    assert (ss.samples()[:, 0] <= 1).all() and (ss.samples()[:, 1] <= 2).all()

    ss = smlb.RandomVectorSampler(size=3, rng=0,
                                  domain=[[0, 1], [0, 1]]).fit(ds).apply(ds)
    ss2 = smlb.RandomVectorSampler(size=3, rng=0).fit(ds).apply(ds)
    assert ss2.samples().shape == (3, 2)
    assert (ss.samples() == ss2.samples()).all()

    ss1 = smlb.RandomVectorSampler(size=3, rng=0).fit(ds).apply(ds)
    ss2 = smlb.RandomVectorSampler(size=3, rng=0).fit(ds).apply(ds)
    assert (ss1.samples() == ss2.samples()).all()

    ss3 = smlb.RandomVectorSampler(size=3, rng=1).fit(ds).apply(ds)
    assert (ss1.samples() != ss3.samples()).any()

    # with labels
    with pytest.raises(smlb.BenchmarkError):
        ds = smlb.VectorSpaceData(dimensions=2,
                                  function=lambda x: np.sum(x, axis=1),
                                  domain=[-np.inf, np.inf])
        smlb.RandomVectorSampler(size=3, rng=0).fit(ds).apply(ds)

    ds = smlb.VectorSpaceData(dimensions=2,
                              function=lambda x: np.sum(x, axis=1),
                              domain=[0, 1])
    ss = smlb.RandomVectorSampler(size=3, rng=0).fit(ds).apply(ds)
    assert ss.samples().shape == (3, 2)

    ss1 = smlb.RandomVectorSampler(size=3, rng=0).fit(ds).apply(ds)
    ss2 = smlb.RandomVectorSampler(size=3, rng=0).fit(ds).apply(ds)
    assert (ss1.samples() == ss2.samples()).all() and (ss1.labels()
                                                       == ss2.labels()).all()

    ss3 = smlb.RandomVectorSampler(size=3, rng=1).fit(ds).apply(ds)
    assert (ss1.samples() != ss3.samples()).any()
Exemple #3
0
def fixture_VectorSpaceData_parabola_1d():
    f = lambda v: v[0]**2
    ds = smlb.VectorSpaceData(dimensions=1, function=f, domain=[-2, 2])
    return ds
Exemple #4
0
 def _create_ds_ss(dim, size=0, rng=0):
     f = lambda v: np.sum(np.power(v, 2), axis=1)  # noqa E731
     dataset = smlb.VectorSpaceData(dim, f, [-2, 2])
     sampler = smlb.GridSampler(size, rng=rng)
     return dataset, sampler