コード例 #1
0
    def testSeed(self):
        """
    Test that initializing twice with the same seed returns identical encodings
    and different when not specified
    """
        encoder1 = RandomDistributedScalarEncoder(name="encoder1",
                                                  resolution=1.0,
                                                  seed=42)
        encoder2 = RandomDistributedScalarEncoder(name="encoder2",
                                                  resolution=1.0,
                                                  seed=42)
        encoder3 = RandomDistributedScalarEncoder(name="encoder3",
                                                  resolution=1.0,
                                                  seed=-1)
        encoder4 = RandomDistributedScalarEncoder(name="encoder4",
                                                  resolution=1.0,
                                                  seed=-1)

        e1 = encoder1.encode(23.0)
        e2 = encoder2.encode(23.0)
        e3 = encoder3.encode(23.0)
        e4 = encoder4.encode(23.0)

        self.assertEqual((e1 == e2).sum(), encoder1.getWidth(),
                         "Same seed gives rise to different encodings")

        self.assertNotEqual((e1 == e3).sum(), encoder1.getWidth(),
                            "Different seeds gives rise to same encodings")

        self.assertNotEqual((e3 == e4).sum(), encoder1.getWidth(),
                            "seeds of -1 give rise to same encodings")
コード例 #2
0
    def testResolution(self):
        """
    Test that numbers within the same resolution return the same encoding.
    Numbers outside the resolution should return different encodings.
    """
        encoder = RandomDistributedScalarEncoder(name="encoder",
                                                 resolution=1.0)

        # Since 23.0 is the first encoded number, it will be the offset.
        # Since resolution is 1, 22.9 and 23.4 should have the same bucket index and
        # encoding.
        e23 = encoder.encode(23.0)
        e23p1 = encoder.encode(23.1)
        e22p9 = encoder.encode(22.9)
        e24 = encoder.encode(24.0)
        self.assertEqual(e23.sum(), encoder.w)
        self.assertEqual(
            (e23 == e23p1).sum(), encoder.getWidth(),
            "Numbers within resolution don't have the same encoding")
        self.assertEqual(
            (e23 == e22p9).sum(), encoder.getWidth(),
            "Numbers within resolution don't have the same encoding")
        self.assertNotEqual(
            (e23 == e24).sum(), encoder.getWidth(),
            "Numbers outside resolution have the same encoding")

        e22p9 = encoder.encode(22.5)
        self.assertNotEqual(
            (e23 == e22p9).sum(), encoder.getWidth(),
            "Numbers outside resolution have the same encoding")
コード例 #3
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")
コード例 #4
0
    def testMissingValues(self):
        """
    Test that missing values and NaN return all zero's.
    """
        encoder = RandomDistributedScalarEncoder(name="encoder",
                                                 resolution=1.0)
        empty = encoder.encode(SENTINEL_VALUE_FOR_MISSING_DATA)
        self.assertEqual(empty.sum(), 0)

        empty = encoder.encode(float("nan"))
        self.assertEqual(empty.sum(), 0)
コード例 #5
0
    def testOffset(self):
        """
    Test that offset is working properly
    """
        encoder = RandomDistributedScalarEncoder(name="encoder",
                                                 resolution=1.0)
        encoder.encode(23.0)
        self.assertEqual(
            encoder._offset, 23.0,
            "Offset not specified and not initialized to first input")

        encoder = RandomDistributedScalarEncoder(name="encoder",
                                                 resolution=1.0,
                                                 offset=25.0)
        encoder.encode(23.0)
        self.assertEqual(
            encoder._offset, 25.0,
            "Offset not initialized to specified constructor"
            " parameter")
コード例 #6
0
    def testWriteRead(self):
        original = RandomDistributedScalarEncoder(name="encoder",
                                                  resolution=1.0,
                                                  w=23,
                                                  n=500,
                                                  offset=0.0)

        originalValue = original.encode(1)

        proto1 = RandomDistributedScalarEncoderProto.new_message()
        original.write(proto1)

        # Write the proto to a temp file and read it back into a new proto
        with tempfile.TemporaryFile() as f:
            proto1.write(f)
            f.seek(0)
            proto2 = RandomDistributedScalarEncoderProto.read(f)

        encoder = RandomDistributedScalarEncoder.read(proto2)

        self.assertIsInstance(encoder, RandomDistributedScalarEncoder)
        self.assertEqual(encoder.resolution, original.resolution)
        self.assertEqual(encoder.w, original.w)
        self.assertEqual(encoder.n, original.n)
        self.assertEqual(encoder.name, original.name)
        self.assertEqual(encoder.verbosity, original.verbosity)
        self.assertEqual(encoder.minIndex, original.minIndex)
        self.assertEqual(encoder.maxIndex, original.maxIndex)
        encodedFromOriginal = original.encode(1)
        encodedFromNew = encoder.encode(1)
        self.assertTrue(numpy.array_equal(encodedFromNew, originalValue))
        self.assertEqual(original.decode(encodedFromNew),
                         encoder.decode(encodedFromOriginal))
        self.assertEqual(original.random.getSeed(), encoder.random.getSeed())

        for key, value in original.bucketMap.items():
            self.assertTrue(numpy.array_equal(value, encoder.bucketMap[key]))
コード例 #7
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")
コード例 #8
0
    def testOverlapStatistics(self):
        """
    Check that the overlaps for the encodings are within the expected range.
    Here we ask the encoder to create a bunch of representations under somewhat
    stressful conditions, and then verify they are correct. We rely on the fact
    that the _overlapOK and _countOverlapIndices methods are working correctly.
    """
        seed = getSeed()

        # Generate about 600 encodings. Set n relatively low to increase
        # chance of false overlaps
        encoder = RandomDistributedScalarEncoder(resolution=1.0,
                                                 w=11,
                                                 n=150,
                                                 seed=seed)
        encoder.encode(0.0)
        encoder.encode(-300.0)
        encoder.encode(300.0)
        self.assertTrue(validateEncoder(encoder, subsampling=3),
                        "Illegal overlap encountered in encoder")
コード例 #9
0
 def testEncodeInvalidInputType(self):
     encoder = RandomDistributedScalarEncoder(name="encoder",
                                              resolution=1.0,
                                              verbosity=0)
     with self.assertRaises(TypeError):
         encoder.encode("String")