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()
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()
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)
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
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
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)
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)
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))
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
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)
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)
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)
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
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
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
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])
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
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)
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)
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)
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)
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
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
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))
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]
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)
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])
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)
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
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)