コード例 #1
0
    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])
コード例 #2
0
 def testExampleUsage(self):
     A = SDR(10)
     B = SDR(10)
     C = SDR(20)
     A.sparse = [0, 1, 2]
     B.sparse = [0, 1, 2]
     C.concatenate(A, B)
     assert (set(C.sparse) == set([0, 1, 2, 10, 11, 12]))
コード例 #3
0
 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]))
コード例 #4
0
 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)
コード例 #5
0
    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
コード例 #6
0
    def testSparse(self):
        A = SDR((103, ))
        B = SDR((100, 100, 1))

        A.sparse
        B.sparse = [1, 2, 3, 4]
        assert (all(B.sparse == np.array([1, 2, 3, 4])))

        B.sparse = []
        assert (not B.dense.any())

        # Test wrong dimensions assigned
        C = SDR(1000)
        C.randomize(.98)
        try:
            A.sparse = C.sparse
        except RuntimeError:
            pass
        else:
            self.fail()
コード例 #7
0
    def testDeterminism(self):
        GOLD = SDR(200)
        GOLD.sparse = [
            8, 11, 13, 15, 16, 18, 29, 32, 37, 39, 41, 42, 45, 47, 57, 59, 69,
            71, 72, 75, 80, 84, 88, 94, 95, 96, 99, 101, 106, 116, 121, 126,
            128, 135, 139, 143, 149, 150, 158, 159, 160, 171, 176, 178, 182,
            184, 188, 194, 197, 198
        ]

        gc = GridCellEncoder(size=GOLD.size,
                             sparsity=.25,
                             periods=[6, 8.5, 12, 17, 24],
                             seed=42)

        actual = gc.encode([77, 88])
        print(actual)
        assert (actual == GOLD)
コード例 #8
0
    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
コード例 #9
0
    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()
コード例 #10
0
    def testDeterminism(self):
        """ Verify that the same seed always gets the same results. """
        GOLD = SDR(1000)
        GOLD.sparse = [
            28, 47, 63, 93, 123, 124, 129, 131, 136, 140, 196, 205, 213, 239,
            258, 275, 276, 286, 305, 339, 345, 350, 372, 394, 395, 443, 449,
            462, 468, 471, 484, 514, 525, 557, 565, 570, 576, 585, 600, 609,
            631, 632, 635, 642, 651, 683, 693, 694, 696, 699, 721, 734, 772,
            790, 792, 795, 805, 806, 833, 836, 842, 846, 892, 896, 911, 914,
            927, 936, 947, 953, 955, 962, 965, 989, 990, 996
        ]

        P = RDSE_Parameters()
        P.size = GOLD.size
        P.sparsity = .08
        P.radius = 12
        P.seed = 42
        R = RDSE(P)
        A = R.encode(987654)
        print(A)
        assert (A == GOLD)
コード例 #11
0
 def testZero(self):
     A = SDR((103, ))
     A.sparse = list(range(20))
     A.zero()
     assert (np.sum(A.dense) == 0)