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 testExampleUsage(self): assert (issubclass(Reshape, SDR)) # Convert SDR dimensions from (4 x 4) to (8 x 2) A = SDR([4, 4]) B = Reshape(A, [8, 2]) A.coordinates = ([1, 1, 2], [0, 1, 2]) assert ((np.array(B.coordinates) == ([2, 2, 5], [0, 1, 0])).all())
def testCoordinates(self): A = SDR((103, )) B = SDR((100, 100, 1)) C = SDR((2, 4, 5, 1, 1, 1, 1, 3)) A.coordinates B.coordinates = [[0, 55, 99], [0, 11, 99], [0, 0, 0]] assert (B.dense[0, 0, 0] == 1) assert (B.dense[55, 11, 0] == 1) assert (B.dense[99, 99, 0] == 1) C.randomize(.5) assert (len(C.coordinates) == len(C.dimensions)) # Test wrong dimensions assigned C = SDR((2, 4, 5, 1, 1, 1, 1, 3)) C.randomize(.5) try: A.coordinates = C.coordinates except RuntimeError: pass else: self.fail()