class TestPoint(ClusterCandidate): def __init__(self, x, y): ClusterCandidate.__init__(self) self.point = Point(x, y) def distance_to_candidate(self, other_candidate): return self.point.distance_to(other_candidate.point)
def __init__(self, x, y): ClusterCandidate.__init__(self) self.point = Point(x, y)
def run_test_case(point, start, end, expected): line_seg = LineSegment.from_tuples(start, end) point = Point(point[0], point[1]) self.assertAlmostEquals(expected, point.distance_to_projection_on(line_seg), \ delta=DECIMAL_MAX_DIFF_FOR_EQUALITY)
def test_one_long_line_joins_two_short_lines(self): points = [[Point(0, 1), Point(20, 1), Point(40, 1), Point(60, 1), Point(80, 1)], \ [Point(0, 0), Point(20, 0)], \ [Point(60, 2), Point(80, 2)]] expected = [[Point(0, 0.5), Point(20, 0.5), Point(60, 1.5), Point(80, 1.5)]] res = the_whole_enchilada(point_iterable_list=points, \ epsilon=1, min_neighbors=2, min_num_trajectories_in_cluster=3, min_vertical_lines=2, min_prev_dist=10.0) self.verify_iterable_works_more_than_once(iterable=res, list_ob=expected)
def test_three_by_three_two_clusters(self): points = [[Point(0, 1), Point(2, 1), Point(4, 1)], \ [Point(0, 0), Point(2, 0), Point(4, 0)], \ [Point(0, 3), Point(2, 3), Point(4, 3)]] expected = [[Point(0, 0.5), Point(4, 0.5)], \ [Point(0, 3), Point(4, 3)]] res = the_whole_enchilada(point_iterable_list=points, \ epsilon=1, min_neighbors=0, min_num_trajectories_in_cluster=1, min_vertical_lines=1, min_prev_dist=1.0) self.verify_iterable_works_more_than_once(iterable=res, list_ob=expected)
def test_hooks_get_called_with_correct_stuff(self): part_hook_called = [False] clusters_hook_called = [False] def test_partition_hook(points): expected_partitions = iter([self.create_simple_line_seg((0, 0), (4, 0)), \ self.create_simple_line_seg((0, 1), (4, 1)), \ self.create_simple_line_seg((0, 3), (4, 3))]) i = 0 for traj in points: exp = expected_partitions.next() self.verify_lines_almost_equal(exp, traj.line_segment) i += 1 self.assertRaises(StopIteration, expected_partitions.next) part_hook_called[0] = True def test_clusters_hook(clusters): cluster_one = [self.create_simple_line_seg((0, 0), (4, 0)), \ self.create_simple_line_seg((0, 1), (4, 1))] cluster_two = [self.create_simple_line_seg((0, 3), (4, 3))] expected_clusters = [cluster_one, cluster_two] def line_seg_groups_equal(a, b): for actual in a: if not any(map(lambda x: self.lines_almost_equal(x, actual), b)): return False return len(a) == len(b) def cluster_expected(cluster): for exp in expected_clusters: if line_seg_groups_equal(exp, cluster): expected_clusters.remove(exp) return True return False for actual_cluster in clusters: actual_traj_set = map(lambda x: x.line_segment, \ actual_cluster.get_trajectory_line_segments()) self.assertTrue(cluster_expected(actual_traj_set), \ " cluster: " + str(actual_traj_set) + \ " is not expected. here are the expected " + \ " line seg clusters: " + str(expected_clusters)) self.assertTrue(len(expected_clusters) == 0, " failed, still " + \ " have this left in expected: " + str(expected_clusters)) clusters_hook_called[0] = True points = [[Point(0, 0), Point(2, 0), Point(4, 0)], \ [Point(0, 1), Point(2, 1), Point(4, 1)], \ [Point(0, 3), Point(2, 3), Point(4, 3)]] expected = [[Point(0, 0.5), Point(4, 0.5)], \ [Point(0, 3), Point(4, 3)]] res = the_whole_enchilada(point_iterable_list=points, \ epsilon=1, min_neighbors=0, min_num_trajectories_in_cluster=1, \ min_vertical_lines=1, min_prev_dist=1.0, \ partitioned_points_hook=test_partition_hook, \ clusters_hook=test_clusters_hook) self.verify_iterable_works_more_than_once(iterable=res, list_ob=expected) self.assertTrue(part_hook_called[0]) self.assertTrue(clusters_hook_called[0])
def create_simple_line_seg(self, start, end): return LineSegment(Point(start[0], start[1]), Point(end[0], end[1]))