def testResolution(self): p = ScalarEncoderParameters() p.activeBits = 10 p.minimum = 0 p.maximum = 100 p.resolution = .5 enc = ScalarEncoder(p) sdr1 = SDR(enc.parameters.size) sdr2 = SDR(enc.parameters.size) enc.encode(.0, sdr1) enc.encode(.1, sdr2) assert (sdr1 == sdr2) enc.encode(.0, sdr1) enc.encode(.6, sdr2) assert (sdr1.getOverlap(sdr2) == 9) enc.encode(70, sdr1) enc.encode(72.5, sdr2) assert (sdr1.getOverlap(sdr2) == 5) enc.encode(70, sdr1) enc.encode(75, sdr2) assert (sdr1.getOverlap(sdr2) == 0) enc.encode(60, sdr1) enc.encode(80, sdr2) assert (sdr1.getOverlap(sdr2) == 0)
def testRadius(self): p = ScalarEncoderParameters() p.activeBits = 10 p.minimum = 0 p.maximum = 100 p.radius = 10 enc = ScalarEncoder(p) sdr1 = SDR(enc.parameters.size) sdr2 = SDR(enc.parameters.size) enc.encode(77, sdr1) enc.encode(77, sdr2) assert (sdr1.getOverlap(sdr2) == 10) enc.encode(0, sdr1) enc.encode(1, sdr2) assert (sdr1.getOverlap(sdr2) == 9) enc.encode(60, sdr1) enc.encode(69, sdr2) assert (sdr1.getOverlap(sdr2) == 1) enc.encode(45, sdr1) enc.encode(55, sdr2) assert (sdr1.getOverlap(sdr2) == 0)
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 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)