You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
3.0 KiB
90 lines
3.0 KiB
5 years ago
|
import pytest
|
||
|
|
||
|
from clusterview.algorithms import Algorithms, CentroidGrouping
|
||
|
from clusterview.colors import Color
|
||
|
from clusterview.points import Point, PointSet
|
||
|
|
||
|
|
||
|
@pytest.fixture(autouse=True, scope="function")
|
||
|
def teardown():
|
||
|
"""
|
||
|
Teardown function for after each test. The current pytest best practice
|
||
|
is to run a setup routine, yield, and then run your teardown routine.
|
||
|
"""
|
||
|
yield
|
||
|
Algorithms.clear_centroids()
|
||
|
|
||
|
|
||
|
def test_empty_centroids():
|
||
|
with pytest.raises(ValueError):
|
||
|
Algorithms.euclidean_grouping(None)
|
||
|
|
||
|
|
||
|
def test_wrong_point_set():
|
||
|
centroid_g1 = Point(101, 81, Color.ORANGE, 8, 800, 600)
|
||
|
centroid_g2 = Point(357, 222, Color.RED, 8, 800, 600)
|
||
|
centroid_g3 = Point(728, 47, Color.PURPLE, 8, 800, 600)
|
||
|
|
||
|
centroids = [centroid_g1, centroid_g2, centroid_g3]
|
||
|
|
||
|
Algorithms.set_centroids(centroids)
|
||
|
|
||
|
with pytest.raises(ValueError):
|
||
|
Algorithms.euclidean_grouping(None)
|
||
|
|
||
|
|
||
|
def test_euclidean_distance():
|
||
|
centroid_g1 = Point(101, 81, Color.ORANGE, 8, 800, 600)
|
||
|
centroid_g2 = Point(357, 222, Color.RED, 8, 800, 600)
|
||
|
centroid_g3 = Point(728, 47, Color.PURPLE, 8, 800, 600)
|
||
|
|
||
|
centroids = [centroid_g1, centroid_g2, centroid_g3]
|
||
|
|
||
|
point1_g1 = Point(67, 59, Color.GREY, 8, 800, 600)
|
||
|
point2_g1 = Point(116, 53, Color.GREY, 8, 800, 600)
|
||
|
point3_g1 = Point(144, 105, Color.GREY, 8, 800, 600)
|
||
|
|
||
|
point1_g2 = Point(388, 243, Color.GREY, 8, 800, 600)
|
||
|
point2_g2 = Point(358, 248, Color.GREY, 8, 800, 600)
|
||
|
point3_g2 = Point(426, 202, Color.GREY, 8, 800, 600)
|
||
|
|
||
|
point1_g3 = Point(750, 47, Color.GREY, 8, 800, 600)
|
||
|
point2_g3 = Point(741, 85, Color.GREY, 8, 800, 600)
|
||
|
point3_g3 = Point(700, 72, Color.GREY, 8, 800, 600)
|
||
|
|
||
|
# This PointSet is the PointSet that excludes the centroids.
|
||
|
point_set = PointSet(8, 800, 600)
|
||
|
point_set.add_point(67, 59, Color.GREY)
|
||
|
point_set.add_point(116, 53, Color.GREY)
|
||
|
point_set.add_point(144, 105, Color.GREY)
|
||
|
|
||
|
point_set.add_point(388, 243, Color.GREY)
|
||
|
point_set.add_point(358, 248, Color.GREY)
|
||
|
point_set.add_point(426, 202, Color.GREY)
|
||
|
|
||
|
point_set.add_point(750, 47, Color.GREY)
|
||
|
point_set.add_point(741, 85, Color.GREY)
|
||
|
point_set.add_point(700, 72, Color.GREY)
|
||
|
|
||
|
centroid_grouping_1 = CentroidGrouping(centroid_g1,
|
||
|
[point1_g1, point2_g1, point3_g1])
|
||
|
|
||
|
centroid_grouping_2 = CentroidGrouping(centroid_g2,
|
||
|
[point1_g2, point2_g2, point3_g2])
|
||
|
|
||
|
centroid_grouping_3 = CentroidGrouping(centroid_g3,
|
||
|
[point1_g3, point2_g3, point3_g3])
|
||
|
|
||
|
expected = [centroid_grouping_1, centroid_grouping_2, centroid_grouping_3]
|
||
|
|
||
|
Algorithms.set_centroids(centroids)
|
||
|
actual = Algorithms.euclidean_grouping(point_set)
|
||
|
|
||
|
assert len(actual) == len(expected)
|
||
|
|
||
|
# Since I don't want to figure out what grouping is where I'll accept
|
||
|
# the linearity of `in`.
|
||
|
assert centroid_grouping_1 in actual
|
||
|
assert centroid_grouping_2 in actual
|
||
|
assert centroid_grouping_3 in actual
|