def testNonPeriodicBottomUp(self): """Test Non-periodic encoder bottom-up""" l = ScalarEncoder(name='scalar', n=14, w=5, minval=1, maxval=10, periodic=False, forced=True) print "\nTesting non-periodic encoder encoding, resolution of %f..." % \ l.resolution self.assertTrue((l.encode(1) == numpy.array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=defaultDtype)).all()) self.assertTrue((l.encode(2) == numpy.array([0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=defaultDtype)).all()) self.assertTrue((l.encode(10) == numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1], dtype=defaultDtype)).all()) # Test that we get the same encoder when we construct it using resolution # instead of n d = l.__dict__ l = ScalarEncoder(name='scalar', resolution=1, w=5, minval=1, maxval=10, periodic=False, forced=True) self.assertEqual(l.__dict__, d) # Test that we get the same encoder when we construct it using radius # instead of n l = ScalarEncoder(name='scalar', radius=5, w=5, minval=1, maxval=10, periodic=False, forced=True) self.assertEqual(l.__dict__, d) # ------------------------------------------------------------------------- # Test the input description generation and topDown decoding of a non-periodic # encoder v = l.minval print "\nTesting non-periodic encoder decoding, resolution of %f..." % \ l.resolution while v < l.maxval: output = l.encode(v) decoded = l.decode(output) print "decoding", output, "(%f)=>" % v, l.decodedToStr(decoded) (fieldsDict, fieldNames) = decoded self.assertEqual(len(fieldsDict), 1) (ranges, desc) = fieldsDict.values()[0] self.assertEqual(len(ranges), 1) (rangeMin, rangeMax) = ranges[0] self.assertEqual(rangeMin, rangeMax) self.assertTrue(abs(rangeMin - v) < l.resolution) topDown = l.topDownCompute(output)[0] print "topdown =>", topDown self.assertTrue((topDown.encoding == output).all()) self.assertTrue(abs(topDown.value - v) <= l.resolution) # Test bucket support bucketIndices = l.getBucketIndices(v) print "bucket index =>", bucketIndices[0] topDown = l.getBucketInfo(bucketIndices)[0] self.assertTrue(abs(topDown.value - v) <= l.resolution / 2) self.assertEqual(topDown.scalar, topDown.value) self.assertTrue((topDown.encoding == output).all()) # Next value v += l.resolution / 4 # Make sure we can fill in holes decoded = l.decode(numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1])) (fieldsDict, fieldNames) = decoded self.assertEqual(len(fieldsDict), 1) (ranges, desc) = fieldsDict.values()[0] self.assertTrue(len(ranges) == 1 and numpy.array_equal(ranges[0], [10, 10])) print "decodedToStr of", ranges, "=>", l.decodedToStr(decoded) decoded = l.decode(numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1])) (fieldsDict, fieldNames) = decoded self.assertEqual(len(fieldsDict), 1) (ranges, desc) = fieldsDict.values()[0] self.assertTrue(len(ranges) == 1 and numpy.array_equal(ranges[0], [10, 10])) print "decodedToStr of", ranges, "=>", l.decodedToStr(decoded) #Test min and max l = ScalarEncoder(name='scalar', n=14, w=3, minval=1, maxval=10, periodic=False, forced=True) decoded = l.topDownCompute(numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1]))[0] self.assertEqual(decoded.value, 10) decoded = l.topDownCompute(numpy.array([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))[0] self.assertEqual(decoded.value, 1) #Make sure only the last and first encoding encodes to max and min, and there is no value greater than max or min l = ScalarEncoder(name='scalar', n=140, w=3, minval=1, maxval=141, periodic=False, forced=True) for i in range(137): iterlist = [0 for _ in range(140)] for j in range(i, i+3): iterlist[j] =1 npar = numpy.array(iterlist) decoded = l.topDownCompute(npar)[0] self.assertTrue(decoded.value <= 141) self.assertTrue(decoded.value >= 1) self.assertTrue(decoded.value < 141 or i==137) self.assertTrue(decoded.value > 1 or i == 0) # ------------------------------------------------------------------------- # Test the input description generation and top-down compute on a small number # non-periodic encoder l = ScalarEncoder(name='scalar', n=15, w=3, minval=.001, maxval=.002, periodic=False, forced=True) print "\nTesting non-periodic encoder decoding, resolution of %f..." % \ l.resolution v = l.minval while v < l.maxval: output = l.encode(v) decoded = l.decode(output) print "decoding", output, "(%f)=>" % v, l.decodedToStr(decoded) (fieldsDict, fieldNames) = decoded self.assertEqual(len(fieldsDict), 1) (ranges, desc) = fieldsDict.values()[0] self.assertEqual(len(ranges), 1) (rangeMin, rangeMax) = ranges[0] self.assertEqual(rangeMin, rangeMax) self.assertTrue(abs(rangeMin - v) < l.resolution) topDown = l.topDownCompute(output)[0].value print "topdown =>", topDown self.assertTrue(abs(topDown - v) <= l.resolution / 2) v += l.resolution / 4 # ------------------------------------------------------------------------- # Test the input description generation on a large number, non-periodic encoder l = ScalarEncoder(name='scalar', n=15, w=3, minval=1, maxval=1000000000, periodic=False, forced=True) print "\nTesting non-periodic encoder decoding, resolution of %f..." % \ l.resolution v = l.minval while v < l.maxval: output = l.encode(v) decoded = l.decode(output) print "decoding", output, "(%f)=>" % v, l.decodedToStr(decoded) (fieldsDict, fieldNames) = decoded self.assertEqual(len(fieldsDict), 1) (ranges, desc) = fieldsDict.values()[0] self.assertEqual(len(ranges), 1) (rangeMin, rangeMax) = ranges[0] self.assertEqual(rangeMin, rangeMax) self.assertTrue(abs(rangeMin - v) < l.resolution) topDown = l.topDownCompute(output)[0].value print "topdown =>", topDown self.assertTrue(abs(topDown - v) <= l.resolution / 2) v += l.resolution / 4 # ------------------------------------------------------------------------- # Test setting fieldStats after initialization if False: #TODO: remove all this? (and fieldstats from ScalarEncoder (if applicable) )? # Modified on 11/20/12 12:53 PM - setFieldStats not applicable for ScalarEncoder l = ScalarEncoder(n=14, w=3, minval=100, maxval=800, periodic=True, forced=True) l.setFieldStats("this", {"this":{"min":1, "max":8}}) l = ScalarEncoder(name='scalar', n=14, w=3, minval=100, maxval=800, periodic=True, forced=True) l.setFieldStats("this", {"this":{"min":1, "max":8}}) self.assertTrue((l.encode(3) == numpy.array([0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=defaultDtype)).all()) self.assertTrue((l.encode(3.1) == l.encode(3)).all()) self.assertTrue((l.encode(3.5) == numpy.array([0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], dtype=defaultDtype)).all()) self.assertTrue((l.encode(3.6) == l.encode(3.5)).all()) self.assertTrue((l.encode(3.7) == l.encode(3.5)).all()) self.assertTrue((l.encode(4) == numpy.array([0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0], dtype=defaultDtype)).all()) self.assertTrue((l.encode(1) == numpy.array([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], dtype=defaultDtype)).all()) self.assertTrue((l.encode(1.5) == numpy.array([1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=defaultDtype)).all()) self.assertTrue((l.encode(7) == numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], dtype=defaultDtype)).all()) self.assertTrue((l.encode(7.5) == numpy.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], dtype=defaultDtype)).all()) l = ScalarEncoder(name='scalar', n=14, w=5, minval=100, maxval=1000, periodic=False, forced=True) l.setFieldStats("this", {"this":{"min":1, "max":10}}) print "\nTesting non-periodic encoding using setFieldStats, resolution of %f..." % \ l.resolution self.assertTrue((l.encode(1) == numpy.array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=defaultDtype)).all()) self.assertTrue((l.encode(2) == numpy.array([0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=defaultDtype)).all()) self.assertTrue((l.encode(10) == numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1], dtype=defaultDtype)).all())
def testDecodeAndResolution(self): """Testing periodic encoder decoding, resolution of """ l = self._l print l.resolution v = l.minval while v < l.maxval: output = l.encode(v) decoded = l.decode(output) print "decoding", output, "(%f)=>" % v, l.decodedToStr(decoded) (fieldsDict, fieldNames) = decoded self.assertEqual(len(fieldsDict), 1) (ranges, desc) = fieldsDict.values()[0] self.assertEqual(len(ranges), 1) (rangeMin, rangeMax) = ranges[0] self.assertEqual(rangeMin, rangeMax) self.assertTrue(abs(rangeMin - v) < l.resolution) topDown = l.topDownCompute(output)[0] print "topdown =>", topDown self.assertTrue((topDown.encoding == output).all()) self.assertTrue(abs(topDown.value - v) <= l.resolution / 2) # Test bucket support bucketIndices = l.getBucketIndices(v) print "bucket index =>", bucketIndices[0] topDown = l.getBucketInfo(bucketIndices)[0] self.assertTrue(abs(topDown.value - v) <= l.resolution / 2) self.assertEqual(topDown.value, l.getBucketValues()[bucketIndices[0]]) self.assertEqual(topDown.scalar, topDown.value) self.assertTrue((topDown.encoding == output).all()) # Next value v += l.resolution / 4 # ----------------------------------------------------------------------- # Test the input description generation on a large number, periodic encoder l = ScalarEncoder(name='scalar', radius=1.5, w=3, minval=1, maxval=8, periodic=True, forced=True) print "\nTesting periodic encoder decoding, resolution of %f..." % \ l.resolution # Test with a "hole" decoded = l.decode(numpy.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0])) (fieldsDict, fieldNames) = decoded self.assertEqual(len(fieldsDict), 1) (ranges, desc) = fieldsDict.values()[0] self.assertTrue(len(ranges) == 1 and numpy.array_equal(ranges[0], [7.5, 7.5])) print "decodedToStr of", ranges, "=>", l.decodedToStr(decoded) # Test with something wider than w, and with a hole, and wrapped decoded = l.decode(numpy.array([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0])) (fieldsDict, fieldNames) = decoded self.assertEqual(len(fieldsDict), 1) (ranges, desc) = fieldsDict.values()[0] self.assertTrue(len(ranges) == 2 and numpy.array_equal(ranges[0], [7.5, 8]) \ and numpy.array_equal(ranges[1], [1, 1])) print "decodedToStr of", ranges, "=>", l.decodedToStr(decoded) # Test with something wider than w, no hole decoded = l.decode(numpy.array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])) (fieldsDict, fieldNames) = decoded self.assertEqual(len(fieldsDict), 1) (ranges, desc) = fieldsDict.values()[0] self.assertTrue(len(ranges) == 1 and numpy.array_equal(ranges[0], [1.5, 2.5])) print "decodedToStr of", ranges, "=>", l.decodedToStr(decoded) # Test with 2 ranges decoded = l.decode(numpy.array([1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0])) (fieldsDict, fieldNames) = decoded self.assertEqual(len(fieldsDict), 1) (ranges, desc) = fieldsDict.values()[0] self.assertTrue(len(ranges) == 2 and numpy.array_equal(ranges[0], [1.5, 1.5]) \ and numpy.array_equal(ranges[1], [5.5, 6.0])) print "decodedToStr of", ranges, "=>", l.decodedToStr(decoded) # Test with 2 ranges, 1 of which is narrower than w decoded = l.decode(numpy.array([0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0])) (fieldsDict, fieldNames) = decoded self.assertEqual(len(fieldsDict), 1) (ranges, desc) = fieldsDict.values()[0] self.assertTrue(len(ranges) == 2 and numpy.array_equal(ranges[0], [1.5, 1.5]) \ and numpy.array_equal(ranges[1], [5.5, 6.0])) print "decodedToStr of", ranges, "=>", l.decodedToStr(decoded)