示例#1
0
 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)
示例#2
0
    def testAddNoise(self):
        A = SDR((103, ))
        B = SDR((103, ))
        A.randomize(.1)
        B.setSDR(A)
        A.addNoise(.5)
        assert (A.getOverlap(B) == 5)

        A.randomize(.3, 42)
        B.randomize(.3, 42)
        A.addNoise(.5)
        B.addNoise(.5)
        assert (A != B)

        A.randomize(.3, 42)
        B.randomize(.3, 42)
        A.addNoise(.5, 42)
        B.addNoise(.5, 42)
        assert (A == B)
示例#3
0
 def testKeepAlive(self):
     """ If there is a reference to an SDR's data then the SDR must be alive """
     # Test Dense
     A = SDR(20).dense
     assert ((A == [0] * 20).all())
     # Test Sparse
     B = SDR(100)
     B.randomize(.5)
     B_sparse = B.sparse
     B_copy = SDR(B)
     del B
     assert ((B_sparse == B_copy.sparse).all())
     # Test Coordinates
     C = SDR([100, 100])
     C.randomize(.5)
     C_data = C.coordinates
     C_copy = SDR(C)
     del C
     assert ((C_data[0] == C_copy.coordinates[0]).all())
     assert ((C_data[1] == C_copy.coordinates[1]).all())
示例#4
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()
示例#5
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()
示例#6
0
    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)]
示例#7
0
    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()
示例#8
0
 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),
     ]
     seed = 99
     for sparsities in test_cases:
         sdrs = []
         for S in sparsities:
             inp = SDR(10000)
             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)
示例#9
0
 def testMirroring(self):
     A = SDR(100)
     A.randomize(.05)
     Ax10 = SDR(100 * 10)
     Ax10.concatenate([A] * 10)
     assert (Ax10.getSum() == 100 * 10 * .05)
示例#10
0
 def testRandomizeEqNe(self):
     A = SDR((103, ))
     B = SDR((103, ))
     A.randomize(.1)
     B.randomize(.1)
     assert (A != B)
     A.randomize(.1, 0)
     B.randomize(.1, 0)
     assert (A != B)
     A.randomize(.1, 42)
     B.randomize(.1, 42)
     assert (A == B)
示例#11
0
 def testMirroring(self):
     A = SDR(200)
     Ax10 = Concatenation([A] * 10)
     A.randomize(.33)
     assert (.30 < Ax10.getSparsity() and Ax10.getSparsity() < .36)
示例#12
0
 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])