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.25, 0.33, 0.5, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98), ] 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.intersection(sdrs) mean_sparsity = np.product(sparsities) assert (X.getSparsity() >= (2. / 3.) * mean_sparsity) assert (X.getSparsity() <= (4. / 3.) * mean_sparsity)
def testExampleUsage(self): A = SDR(10) B = SDR(10) X = SDR(A.dimensions) A.sparse = [0, 1, 2, 3] B.sparse = [2, 3, 4, 5] X.intersection(A, B) assert (set(X.sparse) == set([2, 3]))
def testInPlace(self): A = SDR(1000) B = SDR(1000) A.randomize(1.00) B.randomize(.50) A.intersection(A, B) assert (A.getSparsity() == .5) A.randomize(1.00) B.randomize(.50) A.intersection(B, A) assert (A.getSparsity() == .5)
def calculateRawAnomaly(activeCols, predictiveCols ): """ :param activeColSDR: SDR with active columns :param predictiveColsSDR: SDR with predictive columns, means columns where some of the cells are predictive :return: Raw anomaly score in range <0.0, 1.0> """ if activeCols.dimensions != predictiveCols.dimensions: raise ValueError("activeColumns must have same dimension as predictiveCellsSDR!") if activeCols.getSum() != 0: intersect = SDR(activeCols.dimensions) intersect.intersection(activeCols, predictiveCols) rawAnomaly = (activeCols.getSum() - intersect.getSum()) / activeCols.getSum() if rawAnomaly<0 or rawAnomaly>1.0: raise ValueError("rawAnomaly out of bounds! <0.0, 1.0>") else: rawAnomaly = 0 return rawAnomaly
def testReturn(self): A = SDR(10).randomize(.5) B = SDR(10).randomize(.5) X = SDR(A.dimensions) Y = X.intersection(A, B) assert (X is Y)