Ejemplo n.º 1
0
 def testVerbosity(self):
     """
 Test that nothing is printed out when verbosity=0
 """
     _stdout = sys.stdout
     sys.stdout = _stringio = StringIO()
     encoder = RandomDistributedScalarEncoder(name="mv",
                                              resolution=1.0,
                                              verbosity=0)
     output = numpy.zeros(encoder.getWidth(), dtype=defaultDtype)
     encoder.encodeIntoArray(23.0, output)
     encoder.getBucketIndices(23.0)
     sys.stdout = _stdout
     self.assertEqual(len(_stringio.getvalue()), 0,
                      "zero verbosity doesn't lead to zero output")
Ejemplo n.º 2
0
    def testEncoding(self):
        """
    Test basic encoding functionality. Create encodings without crashing and
    check they contain the correct number of on and off bits. Check some
    encodings for expected overlap. Test that encodings for old values don't
    change once we generate new buckets.
    """
        # Initialize with non-default parameters and encode with a number close to
        # the offset
        encoder = RandomDistributedScalarEncoder(name="encoder",
                                                 resolution=1.0,
                                                 w=23,
                                                 n=500,
                                                 offset=0.0)
        e0 = encoder.encode(-0.1)

        self.assertEqual(e0.sum(), 23, "Number of on bits is incorrect")
        self.assertEqual(e0.size, 500, "Width of the vector is incorrect")
        self.assertEqual(
            encoder.getBucketIndices(0.0)[0], encoder._maxBuckets / 2,
            "Offset doesn't correspond to middle bucket")
        self.assertEqual(len(encoder.bucketMap), 1,
                         "Number of buckets is not 1")

        # Encode with a number that is resolution away from offset. Now we should
        # have two buckets and this encoding should be one bit away from e0
        e1 = encoder.encode(1.0)
        self.assertEqual(len(encoder.bucketMap), 2,
                         "Number of buckets is not 2")
        self.assertEqual(e1.sum(), 23, "Number of on bits is incorrect")
        self.assertEqual(e1.size, 500, "Width of the vector is incorrect")
        self.assertEqual(computeOverlap(e0, e1), 22,
                         "Overlap is not equal to w-1")

        # Encode with a number that is resolution*w away from offset. Now we should
        # have many buckets and this encoding should have very little overlap with
        # e0
        e25 = encoder.encode(25.0)
        self.assertGreater(len(encoder.bucketMap), 23,
                           "Number of buckets is not 2")
        self.assertEqual(e25.sum(), 23, "Number of on bits is incorrect")
        self.assertEqual(e25.size, 500, "Width of the vector is incorrect")
        self.assertLess(computeOverlap(e0, e25), 4, "Overlap is too high")

        # Test encoding consistency. The encodings for previous numbers
        # shouldn't change even though we have added additional buckets
        self.assertTrue(
            numpy.array_equal(e0, encoder.encode(-0.1)),
            "Encodings are not consistent - they have changed after new buckets "
            "have been created")
        self.assertTrue(
            numpy.array_equal(e1, encoder.encode(1.0)),
            "Encodings are not consistent - they have changed after new buckets "
            "have been created")
Ejemplo n.º 3
0
    def testMapBucketIndexToNonZeroBits(self):
        """
    Test that mapBucketIndexToNonZeroBits works and that max buckets and
    clipping are handled properly.
    """
        encoder = RandomDistributedScalarEncoder(resolution=1.0, w=11, n=150)
        # Set a low number of max buckets
        encoder._initializeBucketMap(10, None)
        encoder.encode(0.0)
        encoder.encode(-7.0)
        encoder.encode(7.0)

        self.assertEqual(len(encoder.bucketMap), encoder._maxBuckets,
                         "_maxBuckets exceeded")
        self.assertTrue(
            numpy.array_equal(encoder.mapBucketIndexToNonZeroBits(-1),
                              encoder.bucketMap[0]),
            "mapBucketIndexToNonZeroBits did not handle negative"
            " index")
        self.assertTrue(
            numpy.array_equal(encoder.mapBucketIndexToNonZeroBits(1000),
                              encoder.bucketMap[9]),
            "mapBucketIndexToNonZeroBits did not handle negative index")

        e23 = encoder.encode(23.0)
        e6 = encoder.encode(6)
        self.assertEqual((e23 == e6).sum(), encoder.getWidth(),
                         "Values not clipped correctly during encoding")

        ep8 = encoder.encode(-8)
        ep7 = encoder.encode(-7)
        self.assertEqual((ep8 == ep7).sum(), encoder.getWidth(),
                         "Values not clipped correctly during encoding")

        self.assertEqual(
            encoder.getBucketIndices(-8)[0], 0,
            "getBucketIndices returned negative bucket index")
        self.assertEqual(
            encoder.getBucketIndices(23)[0], encoder._maxBuckets - 1,
            "getBucketIndices returned bucket index that is too"
            " large")