Example #1
0
    def test_try_include(self):
        """ Tests if try_include detects a used point and empty cluster_set.

        Tests that the point gets included when threshold = INFINITY.

        Tests that the point is disregarded when threshold = -1

        -------

        """
        used = numpy.ones(DIMENSIONS)
        used[0] = numpy.nan
        self.assertTrue(setlib.try_include(used, arbitrary_set, model),
                        "Used point not detected")
        self.assertFalse(setlib.try_include(point, [], model),
                         "Empty set not detected")
        model.threshold = -1
        self.assertFalse(setlib.try_include(point, arbitrary_set, model),
                         "Incorrectly included")
        model.threshold = INFINITY
        self.assertTrue(setlib.try_include(point, arbitrary_set, model),
                        "Incorrectly excluded")
        self.assertGreater(arbitrary_set[0].size, 1, "Set not updated")
        arbitrary_set[0] = clustlib.Cluster(DIMENSIONS)
        clustlib.update_cluster(point, arbitrary_set[0])
        model.threshold = model.eucl_threshold
Example #2
0
    def test_variance(self):
        """ Tests that the has_variance flag of a cluster updates accordingly.

        -------

        """
        cluster = clustlib.Cluster(2)
        clustlib.update_cluster(numpy.ones(2), cluster)
        self.assertFalse(cluster.has_variance,
                         "Cluster has variance when it should not")
        clustlib.update_cluster(numpy.ones(2) * 2, cluster)
        self.assertTrue(cluster.has_variance, "Cluster variance not updated")
Example #3
0
    def test_has_variance(self):
        """ Tests that has_variance only evaluates to true when it has a non zero std_dev in
        each dimension


        -------

        """
        no_variance = clustlib.has_variance(ones)
        cluster = clustlib.Cluster(DIMENSIONS)
        other = clustlib.Cluster(DIMENSIONS)
        clustlib.update_cluster(point, cluster)
        clustlib.update_cluster(point, other)
        point_a = numpy.ones(2)
        point_b = numpy.ones(2)
        point_a[1] = 2
        point_b[0] = 2
        clustlib.update_cluster(point_a, cluster)
        clustlib.update_cluster(point_b, other)
        merged = clustlib.merge_clusters(other, cluster)
        self.assertFalse(no_variance, "No dimension has variance")
        self.assertFalse(clustlib.has_variance(cluster),
                         "Second dimension has variance")
        self.assertFalse(clustlib.has_variance(other),
                         "First dimension has variance")
        self.assertTrue(clustlib.has_variance(merged),
                        "Both dimensions have variance")
Example #4
0
    def test_cluster_point(self):
        """ Tests that the closest cluster is updated and that a used point is detected.


        -------

        """
        another = clustlib.Cluster(2)
        clustlib.update_cluster(point, another)
        model.discard.append(another)
        model.discard.append(twos)
        clustlib.cluster_point(point, model)
        self.assertEqual(model.discard[0].size, 2)
        nan = numpy.zeros(2)
        nan[0] = numpy.nan
        clustlib.cluster_point(nan, model)
        model.discard = []
Example #5
0
    def test_update_cluster(self):
        """ Tests that a cluster get its attributes updated accordingly.


        -------

        """
        merged = clustlib.merge_clusters(ones, ones)
        size_b4 = merged.size
        sums_b4 = merged.sums[0]
        sums_sq_b4 = merged.sums_sq[0]
        self.assertFalse(clustlib.has_variance(merged),
                         "Cluster has no variance")
        for i in range(5):
            i += 1
            clustlib.update_cluster(other_point, merged)
            self.assertEqual(size_b4 + i, merged.size, "Incorrect size")
            self.assertEqual(sums_b4 + i * 2, merged.sums[0], "Incorrect sum")
            self.assertEqual(sums_sq_b4 + (i * 4), merged.sums_sq[0],
                             "incorrect sums_sq")
            self.assertTrue(merged.has_variance, "Cluster has variance")
Example #6
0
    def test_std_dev(self):
        """ Tests that two computed population standard deviations compute to the same value
        as when computing with numpy


        -------

        """
        yet_another = numpy.ones(2)
        yet_another = yet_another * 4
        fours = clustlib.Cluster(2)
        clustlib.update_cluster(yet_another, fours)
        merged = clustlib.merge_clusters(fours, ones)
        std_dev = clustlib.std_dev(merged)
        self.assertEqual(std_dev[0], numpy.std([1, 4]), "Incorrect std_dev")
        clustlib.update_cluster(point * 5, merged)
        std_dev = clustlib.std_dev(merged)
        res = numpy.std([1, 4, 5])
        std_dev[1] = round(std_dev[1], 5)
        res = round(res, 5)
        self.assertEqual(std_dev[1], res, "Incorrect std_dev")
Example #7
0
DIMENSIONS = 2
NOF_POINTS = 5
NOF_CLUSTERS = 5
model = bfr.Model(mahalanobis_factor=3.0,
                  euclidean_threshold=5.0,
                  merge_threshold=10.0,
                  dimensions=DIMENSIONS,
                  init_rounds=10,
                  nof_clusters=NOF_CLUSTERS)
model.initialized = True
INFINITY = 13371337.0
point = numpy.ones(2)
other_point = point * 2
ones = clustlib.Cluster(2)
twos = clustlib.Cluster(2)
clustlib.update_cluster(point, ones)
clustlib.update_cluster(other_point, twos)
arbitrary_set = [ones, ones]


class PtlibTests(unittest.TestCase):
    """Test cases for the module bfr.ptlib"""


class PtlibTests(unittest.TestCase):
    def test_squared_diff(self):
        """ Tests that the sum of dimensions of ({1, 1} - {2, 2}) ^ 2 == 2


        -------