def testGetOverlap(self): A = SDR((103, )) B = SDR((103, )) assert (A.getOverlap(B) == 0) A.dense[:10] = 1 B.dense[:20] = 1 A.dense = A.dense B.dense = B.dense assert (A.getOverlap(B) == 10) A.dense[:20] = 1 A.dense = A.dense assert (A.getOverlap(B) == 20) A.dense[50:60] = 1 B.dense[0] = 0 A.dense = A.dense B.dense = B.dense assert (A.getOverlap(B) == 19) # Test wrong dimensions C = SDR((1, 1, 1, 1, 103)) C.randomize(.5) try: A.getOverlap(C) except RuntimeError: pass else: self.fail()
def testExampleUsage(self): # Make an SDR with 9 values, arranged in a (3 x 3) grid. X = SDR(dimensions=(3, 3)) # These three statements are equivalent. X.dense = [[0, 1, 0], [0, 1, 0], [0, 0, 1]] assert (X.dense.tolist() == [[0, 1, 0], [0, 1, 0], [0, 0, 1]]) assert ([list(v) for v in X.coordinates] == [[0, 1, 2], [1, 1, 2]]) assert (list(X.sparse) == [1, 4, 8]) X.coordinates = [[0, 1, 2], [1, 1, 2]] assert (X.dense.tolist() == [[0, 1, 0], [0, 1, 0], [0, 0, 1]]) assert ([list(v) for v in X.coordinates] == [[0, 1, 2], [1, 1, 2]]) assert (list(X.sparse) == [1, 4, 8]) X.sparse = [1, 4, 8] # Access data in any format, SDR will automatically convert data formats, # even if it was not the format used by the most recent assignment to the # SDR. assert (X.dense.tolist() == [[0, 1, 0], [0, 1, 0], [0, 0, 1]]) assert ([list(v) for v in X.coordinates] == [[0, 1, 2], [1, 1, 2]]) assert (list(X.sparse) == [1, 4, 8]) # Data format conversions are cached, and when an SDR value changes the # cache is cleared. X.sparse = [1, 2, 3] # Assign new data to the SDR, clearing the cache. X.dense # This line will convert formats. X.dense # This line will resuse the result of the previous line X = SDR((1000, 1000)) data = X.dense data[0, 4] = 1 data[444, 444] = 1 X.dense = data assert (list(X.sparse) == [4, 444444])
def testStr(self): A = SDR((103, )) B = SDR((100, 100, 1)) A.dense[0] = 1 A.dense[9] = 1 A.dense[102] = 1 A.dense = A.dense assert (str(A) == "SDR( 103 ) 0, 9, 102") A.zero() assert (str(A) == "SDR( 103 )") B.dense[0, 0, 0] = 1 B.dense[99, 99, 0] = 1 B.dense = B.dense assert (str(B) == "SDR( 100, 100, 1 ) 0, 9999")
def testChaining(self): A = SDR([10, 10]) B = Reshape(A, [100]) C = Reshape(B, [4, 25]) D = B.reshape([1, 100]) # Test convenience method. A.dense.fill(1) A.dense = A.dense assert (len(C.sparse) == A.size) assert (len(D.sparse) == A.size) del B
def testDenseInplace(self): # Check that assigning dense data to itself (ie: sdr.dense = sdr.dense) # is significantly faster than copying the data on assignment. # Also, it should not be *too* much faster because this test-case is # tuned to very fast in both situations. A = SDR(100 * 1000) B = np.copy(A.dense) copy_time = time.clock() for i in range(100): A.dense = B copy_time = time.clock() - copy_time inplace_time = time.clock() for i in range(100): A.dense = A.dense inplace_time = time.clock() - inplace_time assert (inplace_time < copy_time / 3)
def testChaining(self): A = SDR([10, 10]) B = Reshape(A, [100]) C = Reshape(B, [4, 25]) D = Reshape(B, [1, 100]) A.dense.fill(1) A.dense = A.dense assert (len(C.sparse) == A.size) assert (len(D.sparse) == A.size) del B
def testSetSDR(self): A = SDR((103, )) B = SDR((103, )) A.sparse = [66] B.setSDR(A) assert (B.dense[66] == 1) assert (B.getSum() == 1) B.dense[77] = 1 B.dense = B.dense A.setSDR(B) assert (set(A.sparse) == set((66, 77))) # Test wrong dimensions assigned C = SDR((2, 4, 5, 1, 1, 1, 1, 3)) C.randomize(.5) try: A.setSDR(C) except RuntimeError: pass else: self.fail()
def testDense(self): A = SDR((103, )) B = SDR((100, 100, 1)) A.dense # Test is the same buffer every time A.dense[0] = 1 A.dense[99] = 1 assert (A.dense[0] + A.dense[99] == 2) # Test modify in-place A.dense = A.dense assert (set(A.sparse) == set((0, 99))) # Test dense dimensions assert (B.dense.shape == (100, 100, 1)) # No crash with dimensions B.dense[0, 0, 0] += 1 B.dense[66, 2, 0] += 1 B.dense[99, 99, 0] += 1 B.dense = B.dense # Test wrong dimensions assigned C = SDR((A.size + 1)) C.randomize(.5) test_cases = [ (SDR(1), SDR(2)), (SDR(100), SDR((100, 1))), (SDR((1, 100)), SDR((100, 1))), ] for left, right in test_cases: try: left.dense = right.dense except RuntimeError: pass else: self.fail() # Test assign data. A.dense = np.zeros(A.size, dtype=np.int16) A.dense = np.ones(A.size, dtype=np.uint64) A.dense = np.zeros(A.size, dtype=np.int8) A.dense = [1] * A.size B.dense = [[[1]] * 100 for _ in range(100)]
def testGetSparsity(self): A = SDR((103, )) assert (A.getSparsity() == 0) A.dense = np.ones(A.size) assert (A.getSparsity() == 1)
def testGetSum(self): A = SDR((103, )) assert (A.getSum() == 0) A.dense = np.ones(A.size) assert (A.getSum() == 103)