Пример #1
0
def test_finite():
    a = SparseArray.fromlist([1, np.inf])
    assert not a.isfinite()
    a = SparseArray.fromlist([1, np.nan])
    assert not a.isfinite()
    a = SparseArray.fromlist([1, 0, 1])
    assert a.isfinite()
Пример #2
0
def test_fromlist_force():
    a = SparseArray.fromlist([1, np.inf], force_finite=True)
    assert a.isfinite() and a.nele() == 1
    a = SparseArray.fromlist([1, np.nan], force_finite=True)
    assert a.isfinite() and a.nele() == 1
    a = SparseArray.fromlist([1, np.nan, np.inf, 1])
    assert a.nele() == 4 and not a.isfinite()
Пример #3
0
def test_concatenate():
    np.random.seed(3)
    uno = create_numpy_array(100, 50)
    dos = create_numpy_array(100, 50)
    suno = SparseArray.fromlist(uno)
    sdos = SparseArray.fromlist(dos)
    res = np.concatenate((uno, dos), axis=0)
    sres = suno.concatenate(sdos).tonparray()
    print((res[:10]))
    print((sres[:10]))
    assert np.all(res == sres)
Пример #4
0
def test_fname():
    from EvoDAG.node import Add
    from EvoDAG import RootGP
    x = np.linspace(-1, 1, 100)
    y = 4.3*x**2 + 3.2 * x - 3.2
    Add.nargs = 10
    gp = RootGP(classifier=False,
                popsize=10,
                generations=2).fit([SparseArray.fromlist(x)], y,
                                   test_set=[SparseArray.fromlist(x)])
    assert gp.signature.count('Ad10') == 1
Пример #5
0
def test_sparse_array_SAE():
    np.random.seed(0)
    uno = create_numpy_array()
    dos = create_numpy_array()
    suno = SparseArray.fromlist(uno)
    sdos = SparseArray.fromlist(dos)
    print((suno.SAE(sdos), np.fabs(uno-dos).sum()))
    assert_almost_equals(suno.SAE(sdos), np.fabs(uno-dos).sum())
    uno[0] = np.nan
    suno = SparseArray.fromlist(uno)
    assert suno.SAE(sdos) == np.inf
Пример #6
0
def test_sparse_array_max():
    np.random.seed(0)
    uno = create_numpy_array(nvalues=50)
    dos = create_numpy_array(nvalues=50)
    r = list(map(max, list(zip(uno, dos))))
    sdos = SparseArray.fromlist(dos)
    rs = SparseArray.fromlist(uno).max(sdos)
    print((uno[:5]))
    print((dos[:5]))
    print((r[:5]))
    print((rs.tonparray()[:5]))
    assert np.all(rs.tonparray() == r)
Пример #7
0
def test_sparse_array_mul():
    np.random.seed(0)
    uno = create_numpy_array(10, 5)
    dos = create_numpy_array(10, 5)
    suno = SparseArray.fromlist(uno)
    sdos = SparseArray.fromlist(dos)
    sr = suno * sdos
    sr = np.array((sr).tolist())
    r = uno * dos
    print((r[:10]))
    print((sr[:10]))
    assert np.all(r == sr)
Пример #8
0
def test_nunion():
    size = 1000
    uno = create_numpy_array(size)
    suno = SparseArray.fromlist(uno)
    dos = create_numpy_array(size)
    sdos = SparseArray.fromlist(dos)
    mask = np.zeros(uno.shape[0], dtype=np.bool)
    mask[~(uno == 0)] = True
    mask[~(dos == 0)] = True
    r = suno.nunion(sdos)
    print((mask.sum(), "-", r))
    assert(mask.sum() == suno.nunion(sdos))
Пример #9
0
def test_sparse_array():
    np.random.seed(0)
    var = create_numpy_array()
    array = SparseArray.fromlist(var)
    print((array.size()))
    assert array.size() == 100
    print((array.print_data()))
    print((var[:50]))
    print((np.array(array.tolist())[:50]))
    assert np.fabs(var - np.array(array.tolist())).sum() == 0
    sp = SparseArray().empty(100, 1000)
    assert sp.size() == 1000
    assert sp.nele() == 100
Пример #10
0
def test_pearson():
    def _sum_of_squares(x):
        return (x**2).sum()
    uno = create_numpy_array(100, 50)
    dos = create_numpy_array(100, 50)
    mx = uno.mean()
    my = dos.mean()
    xm, ym = uno - mx, dos - my
    r_num = np.add.reduce(xm * ym)
    r_den = np.sqrt(_sum_of_squares(xm) * _sum_of_squares(ym))
    r = r_num / r_den
    r2 = SparseArray.fromlist(uno).pearsonr(SparseArray.fromlist(dos))
    print((r, r2))
    assert_almost_equals(r, r2)
Пример #11
0
def test_sparse_array_div():
    np.random.seed(0)
    uno = create_numpy_array(10)
    uno[0] = 0
    dos = create_numpy_array(10)
    dos[1] = 0
    suno = SparseArray.fromlist(uno)
    sdos = SparseArray.fromlist(dos)
    sr = (suno / sdos).tonparray()
    r = uno / dos
    r[1] = 0
    print((r[:10]))
    print((sr[:10]))
    assert np.all(r == sr)
Пример #12
0
def test_sparse_array_sum():
    np.random.seed(0)
    uno = create_numpy_array()
    dos = create_numpy_array()
    suno = SparseArray.fromlist(uno)
    sdos = SparseArray.fromlist(dos)
    zero = suno.constant(0, suno.size())
    r = suno + zero
    assert ((suno - r).fabs()).sum() == 0
    sr = (suno + sdos).tonparray()
    r = uno + dos
    print((r[:10]))
    print((sr[:10]))
    assert np.all(r == sr)
Пример #13
0
def test_nintersection():
    np.random.seed(0)
    size = 15
    uno = create_numpy_array(size)
    suno = SparseArray.fromlist(uno)
    dos = create_numpy_array(size)
    sdos = SparseArray.fromlist(dos)
    mask = np.zeros(uno.shape[0], dtype=np.bool)
    uno_m = ~(uno == 0)
    dos_m = ~(dos == 0)
    mask[uno_m & dos_m] = True
    r = suno.nintersection(sdos)
    print((mask.sum(), "-", r))
    assert mask.sum() == r
Пример #14
0
def test_regression():
    from EvoDAG import RootGP
    from EvoDAG.sparse_array import SparseArray
    x = np.linspace(-1, 1, 100)
    y = 4.3*x**2 + 3.2 * x - 3.2
    gp = RootGP(classifier=False,
                popsize=10,
                generations=2).fit([SparseArray.fromlist(x)], y,
                                   test_set=[SparseArray.fromlist(x)])
    model = gp.model()
    yh = gp.predict()
    assert not model._classifier
    yh1 = model.predict(X=[SparseArray.fromlist(x)])
    spf = SparseArray.fromlist
    assert spf(yh).SSE(spf(yh1)) == 0
Пример #15
0
def test_es_extra_test():
    from EvoDAG import RootGP
    from EvoDAG.sparse_array import SparseArray
    x = np.linspace(-1, 1, 100)
    y = 4.3*x**2 + 3.2 * x - 3.2
    es_extra_test = RootGP.es_extra_test
    RootGP.es_extra_test = MagicMock(side_effect=RuntimeError('Mock'))
    try:
        RootGP(classifier=False,
               popsize=10,
               generations=2).fit([SparseArray.fromlist(x)], y,
                                  test_set=[SparseArray.fromlist(x)])
        assert False
    except RuntimeError:
        RootGP.es_extra_test = es_extra_test
Пример #16
0
def test_slice4():
    np.random.seed(3)
    uno = create_numpy_array(100, 50)
    suno = SparseArray.fromlist(uno)
    suno = suno[10:90]
    print((suno.tonparray()[:10], uno[10:20]))
    assert np.all(suno.tonparray() == uno[10:90])
Пример #17
0
def test_boundaries():
    suno = SparseArray.fromlist([-12, 23, 0.23]).boundaries(-2, 1)
    suno = suno.tonparray()
    assert suno[0] == -2
    assert suno[1] == 1
    print((suno[2]))
    assert suno[2] == 0.23
Пример #18
0
def test_regression():
    from EvoDAG import RootGP
    from EvoDAG.sparse_array import SparseArray
    from EvoDAG.model import Ensemble
    x = np.linspace(-1, 1, 100)
    y = 4.3*x**2 + 3.2 * x - 3.2
    gps = [RootGP(classifier=False,
                  seed=seed,
                  popsize=10,
                  generations=2).fit([SparseArray.fromlist(x)], y,
                                     test_set=[SparseArray.fromlist(x)])
           for seed in range(3)]
    ens = Ensemble([gp.model() for gp in gps])
    hy = np.median([gp.predict() for gp in gps], axis=0)
    hy1 = ens.predict(X=[SparseArray.fromlist(x)])
    assert np.all(hy == hy1)
Пример #19
0
def test_sparse_array_sq():
    np.random.seed(0)
    uno = create_numpy_array()
    suno = SparseArray.fromlist(uno).sq().tonparray()
    print((uno[:10]))
    print((suno[:10]))
    assert np.all(uno**2 == suno)
Пример #20
0
def test_RSE_avg_zero():
    from EvoDAG import EvoDAG
    from EvoDAG.sparse_array import SparseArray

    class EvoDAG2(EvoDAG):
        def __init__(self, **kw):
            super(EvoDAG2, self).__init__(**kw)
            self._times = 0

        def set_regression_mask(self, v):
            mask = np.ones(v.size())
            if self._times == 0:
                mask[10:12] = 0
            else:
                mask[10:13] = 0
            self._mask = SparseArray.fromlist(mask)
            self._times += 1

    x = np.linspace(-1, 1, 100)
    y = 4.3*x**2 + 3.2 * x - 3.2
    y[10:12] = 0
    gp = EvoDAG2(classifier=False,
                 popsize=10,
                 generations=2)
    gp.X = [SparseArray.fromlist(x)]
    gp.y = y
    print(gp._times)
    assert gp._times == 2
    gp.create_population()
    while not gp.stopping_criteria():
        a = gp.random_offspring()
        gp.replace(a)
Пример #21
0
def test_sparse_array_sub2():
    np.random.seed(0)
    uno = create_numpy_array()
    suno = SparseArray.fromlist(uno)
    a = (uno - uno.mean())
    b = (suno - suno.mean()).tonparray()
    assert np.all(a == b)
Пример #22
0
def test_get_data_index():
    np.random.seed(0)
    var = create_numpy_array()
    array = SparseArray.fromlist(var)
    n = SparseArray()
    n.init(array.nele())
    n.set_size(array.size())
    n.set_data_index(array.get_data(), array.get_index())
    assert (array - n).fabs().sum() == 0
Пример #23
0
 def set_regression_mask(self, v):
     mask = np.ones(v.size())
     if self._times == 0:
         mask[10:12] = 0
     else:
         mask[10:13] = 0
     self._mask = SparseArray.fromlist(mask)
     self._times += 1
Пример #24
0
def test_sparse_array_sub():
    np.random.seed(0)
    uno = create_numpy_array()
    dos = create_numpy_array()
    suno = SparseArray.fromlist(uno)
    sdos = SparseArray.fromlist(dos)
    sr = (suno - sdos).tonparray()
    r = uno - dos
    m = r != sr
    print((uno[m]))
    print((dos[m]))
    print((r[m]))
    print((sr[m]))
    assert np.all(r == sr)
    tmp = sdos.constant(0, sdos.size()) - sdos
    tmp.print_data()
    assert np.all(((tmp).tonparray() == -dos))
Пример #25
0
def test_parallel():
    from multiprocessing import Pool
    np.random.seed(0)
    uno = create_numpy_array()
    suno = SparseArray.fromlist(uno)
    p = Pool(2)
    r = p.map(parallel_run, [suno, suno])
    assert suno.fabs().sum() == r[0]
Пример #26
0
def test_sparse_array_sign():
    np.random.seed(0)
    uno = create_numpy_array()
    uno[0] = -1
    suno = SparseArray.fromlist(uno).sign().tonparray()
    uno = np.sign(uno)
    print((uno[:10]))
    print((suno[:10]))
    assert np.all(uno == suno)
Пример #27
0
def test_slice3():
    np.random.seed(3)
    uno = create_numpy_array(100, 50)
    suno = SparseArray.fromlist(uno)
    index = np.array([np.where(uno > 0)[0][0]] * 5)
    index = np.concatenate((index, np.where(uno > 0)[0][1:]), axis=0)
    print((suno[index].tonparray()))
    print((uno[index]))
    assert np.all(suno[index].tonparray() == uno[index])
Пример #28
0
def test_sparse_array_ln():
    np.random.seed(0)
    uno = create_numpy_array()
    suno = SparseArray.fromlist(uno).ln().tonparray()
    uno = np.log(np.fabs(uno))
    uno[np.isinf(uno)] = 0
    print((uno[:10]))
    print((suno[:10]))
    assert np.all(uno == suno)
Пример #29
0
def test_pickle():
    import pickle
    import tempfile
    np.random.seed(0)
    suno = SparseArray.fromlist(create_numpy_array())
    with tempfile.TemporaryFile('w+b') as io:
        pickle.dump(suno, io)
        io.seek(0)
        s = pickle.load(io)
        assert s.SSE(suno) == 0
Пример #30
0
def test_RSE():
    from EvoDAG import RootGP
    from EvoDAG.sparse_array import SparseArray
    from EvoDAG.utils import RSE as rse
    x = np.linspace(-1, 1, 100)
    y = 4.3*x**2 + 3.2 * x - 3.2
    y[10:12] = 0
    gp = RootGP(classifier=False,
                popsize=10,
                generations=2).fit([SparseArray.fromlist(x)], y,
                                   test_set=[SparseArray.fromlist(x)])
    model = gp.model()
    yh = gp.predict()
    assert not model._classifier
    model.predict(X=[SparseArray.fromlist(x)])
    gp._mask = SparseArray.fromlist([2] * yh.shape[0])
    gp.fitness_vs(model._hist[-1])
    print(rse(y, yh), model._hist[-1].fitness_vs)
    assert_almost_equals(rse(y, yh),
                         -model._hist[-1].fitness_vs)