def test_creation(self): self.assertRaises((Exception, ArgumentError), TrajectoryLineSegment, \ LineSegment.from_points([Point(0, 0), Point(1, 1)]), -1) self.assertRaises((Exception, ArgumentError), TrajectoryLineSegment, None, 1) self.assertRaises((Exception, ArgumentError), TrajectoryLineSegment, \ LineSegment.from_points([Point(0, 0), Point(1, 1)]), None)
def test_three_lines(self): traj_line_segments = [self.create_trajectory_line_seg((0.0, 0.0), (2.0, 0.0), 0), \ self.create_trajectory_line_seg(start=(0.0, 1.0), end=(2.0, 1.0), traj_id=2), \ self.create_trajectory_line_seg(start=(0.0, 2.0), end=(2.0, 2.0), traj_id=1)] res = get_representative_line_from_trajectory_line_segments(traj_line_segments, 1, 0) expected = [Point(0, 1), Point(2, 1)] self.verify_iterable_works_more_than_once(iterable=res, list_ob=expected)
def test_two_line_segs(self): points = [[Point(0, 1), Point(1, 1)], \ [Point(0, 0), Point(1, 0)]] expected = [[Point(0, 0.5), (1, 0.5)]] res = the_whole_enchilada(point_iterable_list=points, \ epsilon=100, min_neighbors=1, min_num_trajectories_in_cluster=2, min_vertical_lines=2, min_prev_dist=1.0) self.verify_iterable_works_more_than_once(iterable=res, list_ob=expected)
def test_simple_lines(self): traj_line_segments = [self.create_trajectory_line_seg(start=(0.0, 0.0), end=(1.0, 0.0), traj_id=0), \ self.create_trajectory_line_seg(start=(0.0, 1.0), end=(1.0, 1.0), traj_id=1)] res = get_representative_line_from_trajectory_line_segments(traj_line_segments, 1, 0) expected = [Point(0, 0.5), Point(1, 0.5)] for p in res: self.assertEquals(p.__class__, Point) self.verify_iterable_works_more_than_once(iterable=res, list_ob=expected)
def test_two_short_line_endpoints_get_picked(self): lines = [LineSegment.from_points([Point(0, 0), Point(1, 0)]), \ LineSegment.from_points([Point(0, 1), Point(1, 1)])] traj_lines = [TrajectoryLineSegment(lines[0], 0), \ TrajectoryLineSegment(lines[1], 1)] res = get_representative_trajectory_average_inputs(trajectory_line_segments=traj_lines, \ min_prev_dist=1, min_lines=2) expected = 2 self.assertEquals(len(res), expected)
def test_four_line_sets_should_result(self): lines = [LineSegment.from_points([Point(0, 0), Point(1, 0)]), \ LineSegment.from_points([Point(0.5, 1), Point(1.5, 1)])] traj_lines = [TrajectoryLineSegment(lines[0], 0), \ TrajectoryLineSegment(lines[1], 1)] res = get_representative_trajectory_average_inputs(trajectory_line_segments=traj_lines, \ min_prev_dist=0.5, min_lines=1) expected = 4 self.assertEquals(len(res), expected)
def test_single_line_seg(self): points = [[Point(0, 0), Point(2, 0)]] expected = [[Point(0, 0), Point(2, 0)]] 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) self.verify_point_iterable_almost_equals_list(iterable=res, expected_list=expected)
def test_three_vertical_points_in_a_row_small_spacing(self): points = [[Point(0, 0), Point(0, 1.1), Point(0, 2.2)]] expected = [[Point(0, 0), Point(0, 2.2)]] 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) self.verify_point_iterable_almost_equals_list(iterable=res, expected_list=expected)
def test_three_points_in_a_row_negative_diagonal(self): points = [[Point(0, 20), Point(10, 10), Point(20, 0)]] expected = [[Point(0, 20), Point(20, 0)]] res = the_whole_enchilada(point_iterable_list=points, \ epsilon=0, \ min_neighbors=0, min_num_trajectories_in_cluster=1, \ min_vertical_lines=1, \ min_prev_dist=(2 * math.sqrt(2.0) - DECIMAL_MAX_DIFF_FOR_EQUALITY)) self.verify_point_iterable_almost_equals_list(iterable=res, expected_list=expected)
def test_builds_graph_correctly_with_empty_trajectories(self): trajectories = [[], \ [Point(0, 0), Point(1, 1), Point(2, 2)], \ [], \ [Point(10, 0), Point(11, 0), Point(12, 0)], \ [Point(0, 10), Point(0, 11), Point(0, 12)], \ []] filtered_trajs = [] cur_index = 0 for traj in trajectories: if len(traj) > 0: filtered_trajs.append(FilteredTrajectory(traj, cur_index)) cur_index += 1 pt_graph = build_point_graph(filtered_trajectories=filtered_trajs) expected_neighbors = [set([1]), set([0, 2]), set([1]), \ set([4]), set([3, 5]), set([4]), \ set([7]), set([6, 8]), set([7])] expected_trajectory_ids = [0, 0, 0, \ 1, 1, 1, \ 2, 2, 2] self.verify_pt_graph(trajectories=trajectories, \ expected_neighbors=expected_neighbors, \ expected_trajectory_ids=expected_trajectory_ids, \ pt_graph=pt_graph)
def test_computes_components_correctly_when_all_in_same_component(self): trajectories = [[], \ [Point(0, 0), Point(1, 1), Point(2, 2)], \ [], \ [Point(10, 0), Point(11, 0), Point(12, 0)], \ [Point(0, 10), Point(0, 11), Point(0, 12)], \ []] filtered_trajs = [] cur_index = 0 for traj in trajectories: if len(traj) > 0: filtered_trajs.append(FilteredTrajectory(traj, cur_index)) cur_index += 1 pt_graph = build_point_graph(filtered_trajectories=filtered_trajs) def dummy_find_other_neighbors_func(pt_node, pt_graph): other_neighbors_table = [[3], [], [], [0], [], [6], [5], [], []] return other_neighbors_table[pt_node.index] compute_graph_component_ids(pt_graph=pt_graph, \ find_other_neighbors_func=dummy_find_other_neighbors_func) expected_component_ids = [0] * 9 for pt_node in pt_graph: self.assertEquals(pt_node.graph_component_id, \ expected_component_ids[pt_node.index], \ "pt node comp id at index " + str(pt_node.index) + \ " is " + str(pt_node.graph_component_id) + \ " but expected " + \ str(expected_component_ids[pt_node.index]))
def test_partition_happens_with_three_points_in_a_row_horizontally(self): points = [[Point(0, 0), Point(2, 0), Point(4, 0)]] expected = [[Point(0, 0), Point(4, 0)]] res = the_whole_enchilada(point_iterable_list=points, \ epsilon=0, \ min_neighbors=0, min_num_trajectories_in_cluster=1, \ min_vertical_lines=1, \ min_prev_dist=4) self.verify_iterable_works_more_than_once(iterable=res, list_ob=expected) self.verify_point_iterable_almost_equals_list(iterable=res, expected_list=expected)
def create_line(self, horizontal_start, horizontal_end, line_set_ids, trajectory_id, orig_pos=None): if orig_pos == None: orig_pos = random.randint(0, 1000) return {'trajectory_line_segment': \ TrajectoryLineSegment(LineSegment.from_points([Point(horizontal_start, random.randint(0, 1000)), \ Point(horizontal_end, random.randint(0, 1000))]), \ trajectory_id, orig_pos), \ 'line_set_ids': line_set_ids}
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 read_test_file(file_name): file = open(file_name) num_dimensions = int(file.next()) if num_dimensions != 2: raise Exception("didn't make this for this") num_traj = int(file.next()) exp_id = 0 all_traj = [] for line in file: tokens = line.split() id = int(tokens[0]) num_points = int(tokens[1]) if id != exp_id: raise Exception("something wrong") traj = [] for i in filter(lambda x: x % 2 == 0, xrange(0, num_points)): x = float(tokens[i + 2]) try: y = float(tokens[i + 3]) except IndexError: print "error on index " + str(i) + " with id " + str(id) traj.append(Point(x, y)) all_traj.append(traj) exp_id += 1 file.close() return all_traj
def build_test_object(self, points, model_cost, encoding_cost, start, end): traj = Trajectory(0) traj.points.extend(map(lambda point: Point(point[0], point[1]), points)) return {'trajectory': traj, 'model_cost': model_cost, \ 'encoding_cost': encoding_cost, 'total_cost': model_cost + encoding_cost, \ 'start': start, 'end': end}
def get_representative_line_from_rotated_line_segments(trajectory_line_segments, min_vertical_lines, min_prev_dist): inputs = get_representative_trajectory_average_inputs(trajectory_line_segments=trajectory_line_segments, \ min_prev_dist=min_prev_dist, min_lines=min_vertical_lines) out = [] for line_seg_averaging_input in inputs: vert_val = get_mean_vertical_coordinate_in_line_segments(line_seg_averaging_input) out.append(Point(line_seg_averaging_input['horizontal_position'], vert_val)) return out
def test_parrallel_distance_joins_two_lines_segs(self): points = [[Point(0, 0), Point(1, 0)], \ [Point(2, 0), Point(3, 0)]] expected = [[Point(0, 0), Point(1, 0), Point(2, 0), Point(3, 0)]] res = the_whole_enchilada(point_iterable_list=points, \ epsilon=1, \ min_neighbors=1, min_num_trajectories_in_cluster=2, \ min_vertical_lines=1, \ min_prev_dist=1) self.verify_point_iterable_almost_equals_list(iterable=res, expected_list=expected)
def test_reads_file(self): expected = [[Point(1, 2), Point(3, 4)], \ [Point(2, 4), Point(6, 8)], \ [Point(1, 3), Point(5, 7)], \ [Point(9, 8)]] res = read_test_file(os.path.dirname(__file__) + "\\dummy_input.txt") exp_iter = iter(expected) for traj in res: exp_line = exp_iter.next() self.assertListEqual(exp_line, traj)
def test_normal_turning_line(self): points = [[ Point(0, 0), Point(20, 20), Point(40, 0), Point(60, 20), Point(80, 0) ]] expected = [[ Point(0, 0), Point(20, 20), Point(40, 0), Point(60, 20), Point(80, 0) ]] res = the_whole_enchilada(point_iterable_list=points, \ epsilon=100, \ min_neighbors=3, min_num_trajectories_in_cluster=1, \ min_vertical_lines=1, \ min_prev_dist=1) self.verify_point_iterable_almost_equals_list(iterable=res, expected_list=expected)
def test_two_vertical_line_segments(self): points = [[Point(0, 0), Point(0, 1)], \ [Point(1, 0), Point(1, 1)]] expected = [[Point(0.5, 0.0), Point(0.5, 1)]] res = the_whole_enchilada(point_iterable_list=points, \ epsilon=100, min_neighbors=0, min_num_trajectories_in_cluster=1, min_vertical_lines=1, min_prev_dist=0) self.verify_point_iterable_almost_equals_list(iterable=res, expected_list=expected)
def test_trajectory_loops_around(self): points = [[ Point(0, 1000.0), Point(1000.0, 2000.0), Point(2000.0, 1000.0), Point(1000.0, 0), Point(0, 1000.0) ]] expected = [[Point(0, 1000), Point(1000, 1000), Point(2000, 1000)]] res = the_whole_enchilada(point_iterable_list=points, \ epsilon=3000, \ min_neighbors=3, min_num_trajectories_in_cluster=1, \ min_vertical_lines=1, \ min_prev_dist=10) self.verify_point_iterable_almost_equals_list(iterable=res, expected_list=expected)
def test_two_45_degree_line_segments(self): points = [[Point(0, 1), Point(1, 0)], \ [Point(1, 2), Point(2, 1)]] expected = [[Point(0.5, 1.5), Point(1.5, 0.5)]] res = the_whole_enchilada(point_iterable_list=points, \ epsilon=math.sqrt(2.0), min_neighbors=1, min_num_trajectories_in_cluster=2, \ min_vertical_lines=2, \ min_prev_dist=math.sqrt(2.0) - DECIMAL_MAX_DIFF_FOR_EQUALITY) self.verify_point_iterable_almost_equals_list(iterable=res, expected_list=expected)
def test_four_points_in_a_row_slight_diagonally(self): points = [[ Point(0, 0), Point(100, 10), Point(200, 20), Point(300, 30) ]] expected = [[Point(0, 0), Point(300, 30)]] res = the_whole_enchilada(point_iterable_list=points, \ epsilon=0, \ min_neighbors=0, min_num_trajectories_in_cluster=1, \ min_vertical_lines=1, \ min_prev_dist=1) self.verify_point_iterable_almost_equals_list(iterable=res, expected_list=expected)
def test_obvious_NOT_partition(self): trajectory = [Point(0, 0), Point(1, 1), Point(2, 0)] expected_par = [0, 1, 2] self.verify_iterable_works_more_than_once(expected_par, \ call_partition_trajectory(trajectory_point_list=trajectory))
def __init__(self, x, y): ClusterCandidate.__init__(self) self.point = Point(x, y)
def test_doesnt_partition_big_diamond(self): traj = [Point(0, 100), Point(100, 200), Point(200, 100), Point(100, 0), Point(0, 100)] expected_par = [0, 1, 2, 3, 4] self.verify_iterable_works_more_than_once(call_partition_trajectory(trajectory_point_list=traj), \ expected_par)
def test_three_points_in_a_row_diagonal_shorter(self): traj = [Point(0, 0), Point(1, 1), Point(2, 2)] expected_par = [0, 2] self.verify_iterable_works_more_than_once(call_partition_trajectory(trajectory_point_list=traj), \ expected_par)
def test_three_points_horizontal_big_spacing(self): traj = [Point(0, 0), Point(200, 0), Point(444, 0)] expected_par = [0, 2] self.verify_iterable_works_more_than_once(call_partition_trajectory(trajectory_point_list=traj), \ expected_par)
def test_not_enough_points(self): self.assertRaises(ValueError, call_partition_trajectory, [Point(1, 1)]) self.assertRaises(ValueError, call_partition_trajectory, [])
def test_longer_obvious_partition(self): trajectory = [Point(0, 0), Point(10000, 1), Point(20000, 0), Point(30000, 2), Point(40000, 1)] expected_par = [0, 4] self.verify_iterable_works_more_than_once(expected_par, \ call_partition_trajectory(trajectory_point_list=trajectory))