Пример #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 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")
Пример #4
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")
Пример #5
0
    def testGetMethods(self):
        """
    Test that the getWidth, getDescription, and getDecoderOutputFieldTypes
    methods work.
    """
        encoder = RandomDistributedScalarEncoder(name="theName",
                                                 resolution=1.0,
                                                 n=500)
        self.assertEqual(encoder.getWidth(), 500,
                         "getWidth doesn't return the correct result")

        self.assertEqual(encoder.getDescription(), [("theName", 0)],
                         "getDescription doesn't return the correct result")

        self.assertEqual(
            encoder.getDecoderOutputFieldTypes(), (FieldMetaType.float, ),
            "getDecoderOutputFieldTypes doesn't return the correct"
            " result")