def encode(self, location, grid_cells=None): location = list(location) assert (len(location) == 2) if grid_cells is None: grid_cells = SDR((self.size, )) if any(math.isnan(x) for x in location): grid_cells.zero() return grid_cells # Find the distance from the location to each grid cells nearest # receptive field center. # Convert the units of location to hex grid with angle 0, scale 1, offset 0. displacement = location - self.offsets_ radius = np.empty(self.size) for mod_idx in range(len(self.partitions_)): start, stop = self.partitions_[mod_idx] R = self.rot_mats_[mod_idx] displacement[start:stop] = R.dot(displacement[start:stop].T).T radius[start:stop] = self.periods[mod_idx] / 2 # Convert into and out of hexagonal coordinates, which rounds to the # nearest hexagons center. nearest = hexy.cube_to_pixel(hexy.pixel_to_cube(displacement, radius), radius) # Find the distance between the location and the RF center. distances = np.hypot(*(nearest - displacement).T) # Activate the closest grid cells in each module. index = [] for start, stop in self.partitions_: z = int(round(self.sparsity * (stop - start))) index.extend(np.argpartition(distances[start:stop], z)[:z] + start) grid_cells.sparse = index return grid_cells
def testExampleUsage(self): A = SDR(10) B = SDR(10) A.sparse = [2, 3, 4, 5] B.sparse = [0, 1, 2, 3] X = Intersection(A, B) assert ((X.sparse == [2, 3]).all()) B.zero() assert (X.getSparsity() == 0)
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 encode(self, location, grid_cells=None): """ Transform a 2-D coordinate into an SDR. Argument location: pair of coordinates, such as "[X, Y]" Argument grid_cells: Optional, the SDR object to store the results in. Its dimensions must be "[GridCellEncoder.size]" Returns grid_cells, an SDR object. This will be created if not given. """ location = list(location) assert(len(location) == 2) if grid_cells is None: grid_cells = SDR((self.size,)) else: assert(isinstance(grid_cells, SDR)) assert(grid_cells.dimensions == [self.size]) if any(math.isnan(x) for x in location): grid_cells.zero() return grid_cells # Find the distance from the location to each grid cells nearest # receptive field center. # Convert the units of location to hex grid with angle 0, scale 1, offset 0. displacement = location - self.offsets_ radius = np.empty(self.size) for mod_idx in range(len(self.partitions_)): start, stop = self.partitions_[mod_idx] R = self.rot_mats_[mod_idx] displacement[start:stop] = R.dot(displacement[start:stop].T).T radius[start:stop] = self.periods[mod_idx] / 2 # Convert into and out of hexagonal coordinates, which rounds to the # nearest hexagons center. nearest = hexy.cube_to_pixel(hexy.pixel_to_cube(displacement, radius), radius) # Find the distance between the location and the RF center. distances = np.hypot(*(nearest - displacement).T) # Activate the closest grid cells in each module. index = [] for start, stop in self.partitions_: z = int(round(self.sparsity * (stop - start))) index.extend( np.argpartition(distances[start : stop], z)[:z] + start ) grid_cells.sparse = index return grid_cells
def testConstructor(self): assert (issubclass(Intersection, SDR)) # Test 2 Arguments A = SDR(2000) B = SDR(A.size) X = Intersection(A, B) A.randomize(.20) B.randomize(.20) assert (X.getSum() > 0) assert (X.inputs == [A, B]) A.zero() assert (X.getSum() == 0) del X A.zero() B.zero() del B del A # Test 3 Arguments A = SDR(2000) B = SDR(2000) C = SDR(2000) X = Intersection(A, B, C) A.randomize(.6) B.randomize(.6) C.randomize(.6) assert (X.inputs == [A, B, C]) assert (X.getSparsity() > .75 * (.6**3)) assert (X.getSparsity() < 1.25 * (.6**3)) del B del A del X del C # Test 4 Arguments A = SDR(2000) A.randomize(.9) B = SDR(2000) B.randomize(.9) C = SDR(2000) C.randomize(.9) D = SDR(2000) D.randomize(.9) X = Intersection(A, B, C, D) assert (X.inputs == [A, B, C, D]) # Test list constructor X = Intersection([A, B, C, D]) assert (X.size == 2000) assert (X.dimensions == [2000]) assert (X.getSum() > 0) A.zero() assert (X.getSum() == 0) assert (X.inputs == [A, B, C, D])
def testZero(self): A = SDR((103, )) A.sparse = list(range(20)) A.zero() assert (np.sum(A.dense) == 0)