def testSparsity(self): test_cases = [ (0.5, 0.5), (0.1, 0.9), (0.25, 0.3), (0.5, 0.5, 0.5), (0.95, 0.95, 0.95), (0.10, 0.10, 0.60), (0.0, 1.0, 1.0), (0.5, 0.5, 0.5, 0.5), (0.11, 0.20, 0.05, 0.04, 0.03, 0.01, 0.01, 0.02, 0.02, 0.02), ] size = 10000 seed = 99 X = SDR(size) for sparsities in test_cases: sdrs = [] for S in sparsities: inp = SDR(size) inp.randomize(S, seed) seed += 1 sdrs.append(inp) X.union(sdrs) mean_sparsity = np.product(list(1 - s for s in sparsities)) assert (X.getSparsity() >= (2. / 3.) * (1 - mean_sparsity)) assert (X.getSparsity() <= (4. / 3.) * (1 - mean_sparsity))
def testExampleUsage(self): A = SDR(10) B = SDR(10) U = SDR(A.dimensions) A.sparse = [0, 1, 2, 3] B.sparse = [2, 3, 4, 5] U.union(A, B) assert (set(U.sparse) == set([0, 1, 2, 3, 4, 5]))
def testInPlace(self): A = SDR(1000) B = SDR(1000) A.randomize(.50) B.randomize(.50) A.union(A, B) assert (A.getSparsity() >= .75 - .05 and A.getSparsity() <= .75 + .05) A.union(B.randomize(.50), A.randomize(.50)) assert (A.getSparsity() >= .75 - .05 and A.getSparsity() <= .75 + .05)
def testReturn(self): A = SDR(10).randomize(.5) B = SDR(10).randomize(.5) X = SDR(A.dimensions) Y = X.union(A, B) assert (X is Y)