Example #1
0
    def testCountOverlapIndices(self):
        """
    Test that the internal method _countOverlapIndices works as expected.
    """
        # Create a fake set of encodings.
        encoder = RandomDistributedScalarEncoder(name="encoder",
                                                 resolution=1.0,
                                                 w=5,
                                                 n=5 * 20)
        midIdx = encoder._maxBuckets / 2
        encoder.bucketMap[midIdx - 2] = numpy.array(range(3, 8))
        encoder.bucketMap[midIdx - 1] = numpy.array(range(4, 9))
        encoder.bucketMap[midIdx] = numpy.array(range(5, 10))
        encoder.bucketMap[midIdx + 1] = numpy.array(range(6, 11))
        encoder.bucketMap[midIdx + 2] = numpy.array(range(7, 12))
        encoder.bucketMap[midIdx + 3] = numpy.array(range(8, 13))
        encoder.minIndex = midIdx - 2
        encoder.maxIndex = midIdx + 3

        # Indices must exist
        with self.assertRaises(ValueError):
            encoder._countOverlapIndices(midIdx - 3, midIdx - 2)
        with self.assertRaises(ValueError):
            encoder._countOverlapIndices(midIdx - 2, midIdx - 3)

        # Test some overlaps
        self.assertEqual(encoder._countOverlapIndices(midIdx - 2, midIdx - 2),
                         5, "_countOverlapIndices didn't work")
        self.assertEqual(encoder._countOverlapIndices(midIdx - 1, midIdx - 2),
                         4, "_countOverlapIndices didn't work")
        self.assertEqual(encoder._countOverlapIndices(midIdx + 1, midIdx - 2),
                         2, "_countOverlapIndices didn't work")
        self.assertEqual(encoder._countOverlapIndices(midIdx - 2, midIdx + 3),
                         0, "_countOverlapIndices didn't work")
Example #2
0
    def testOverlapOK(self):
        """
    Test that the internal method _overlapOK works as expected.
    """
        # Create a fake set of encodings.
        encoder = RandomDistributedScalarEncoder(name="encoder",
                                                 resolution=1.0,
                                                 w=5,
                                                 n=5 * 20)
        midIdx = encoder._maxBuckets / 2
        encoder.bucketMap[midIdx - 3] = numpy.array(list(range(
            4, 9)))  # Not ok with
        # midIdx-1
        encoder.bucketMap[midIdx - 2] = numpy.array(list(range(3, 8)))
        encoder.bucketMap[midIdx - 1] = numpy.array(list(range(4, 9)))
        encoder.bucketMap[midIdx] = numpy.array(list(range(5, 10)))
        encoder.bucketMap[midIdx + 1] = numpy.array(list(range(6, 11)))
        encoder.bucketMap[midIdx + 2] = numpy.array(list(range(7, 12)))
        encoder.bucketMap[midIdx + 3] = numpy.array(list(range(8, 13)))
        encoder.minIndex = midIdx - 3
        encoder.maxIndex = midIdx + 3

        self.assertTrue(encoder._overlapOK(midIdx, midIdx - 1),
                        "_overlapOK didn't work")
        self.assertTrue(encoder._overlapOK(midIdx - 2, midIdx + 3),
                        "_overlapOK didn't work")
        self.assertFalse(encoder._overlapOK(midIdx - 3, midIdx - 1),
                         "_overlapOK didn't work")

        # We'll just use our own numbers
        self.assertTrue(encoder._overlapOK(100, 50, 0),
                        "_overlapOK didn't work for far values")
        self.assertTrue(encoder._overlapOK(100, 50, encoder._maxOverlap),
                        "_overlapOK didn't work for far values")
        self.assertFalse(encoder._overlapOK(100, 50, encoder._maxOverlap + 1),
                         "_overlapOK didn't work for far values")
        self.assertTrue(encoder._overlapOK(50, 50, 5),
                        "_overlapOK didn't work for near values")
        self.assertTrue(encoder._overlapOK(48, 50, 3),
                        "_overlapOK didn't work for near values")
        self.assertTrue(encoder._overlapOK(46, 50, 1),
                        "_overlapOK didn't work for near values")
        self.assertTrue(encoder._overlapOK(45, 50, encoder._maxOverlap),
                        "_overlapOK didn't work for near values")
        self.assertFalse(encoder._overlapOK(48, 50, 4),
                         "_overlapOK didn't work for near values")
        self.assertFalse(encoder._overlapOK(48, 50, 2),
                         "_overlapOK didn't work for near values")
        self.assertFalse(encoder._overlapOK(46, 50, 2),
                         "_overlapOK didn't work for near values")
        self.assertFalse(encoder._overlapOK(50, 50, 6),
                         "_overlapOK didn't work for near values")