def test_returns_number_of_intersections_for_different_segments(self): result = max_segment_intersections( [Segment(1, 4), Segment(3, 5), Segment(2, 7), Segment(5, 10)]) self.assertEqual(result, 3)
def test_returns_for_points_on_different_values(self): self.assertEqual([1, 0, 0], count_segments_for_points( [Segment(0, 5), Segment(7, 10)], [1, 6, 11])) self.assertEqual([0, 0, 1], count_segments_for_points( [Segment(-10, 10)], [-100, 100, 0])) self.assertEqual([2, 0], count_segments_for_points( [Segment(0, 5), Segment(-3, 2), Segment(7, 10)], [1, 6]))
def extract_segment(self): size = 10 step = 5 epsilon = 0.00089443 df1 = self.race1.df df2 = self.race2.df c1 = df1[[ "timestamp", "position_lat", "position_long", "altitude", "distance" ]].values.T c2 = df2[[ "timestamp", "position_lat", "position_long", "altitude", "distance" ]].values.T c1_matched_c2 = np.zeros(c1.shape[1]) mean_trace = np.zeros( (4, c1.shape[1])) # [pos_lat, pos_long, altitude, distance] timestamps_trace = np.zeros((2, c1.shape[1]), dtype='object') for i2 in range(0, c2.shape[1] - size, step): for i1 in range(0, c1.shape[1] - size, 1): dist2 = np.linalg.norm(c1[1:3, i1:i1 + size] - c2[1:3, i2:i2 + size]) if dist2 <= epsilon: c1_matched_c2[i1:i1 + size] += 1 mean_trace[:3, i1:i1 + size] = (c1[1:4, i1:i1 + size] + c2[1:4, i2:i2 + size]) / 2 # pour la distance on ne calcule pas la moyenne --> ça fausse tout mean_trace[3, i1:i1 + size] = c1[4, i1:i1 + size] timestamps_trace[:, i1:i1 + size] = [ c1[0, i1:i1 + size], c2[0, i2:i2 + size] ] # Segment extractor (list of segment) where = np.where(c1_matched_c2 > 1)[0] diff = np.diff(where) > 1 if diff is False: splitted = [where] else: sep = np.argwhere(diff).T[0] splitted = np.split(where, sep + 1) ret = [mean_trace[:, i] for i in splitted] times = [timestamps_trace[:, i] for i in splitted] # Drop segments shorter than 20 segments_filtered = [i for i in ret if i.shape[1] > 40] times_filtered = [i for i in times if i.shape[1] > 40] # drop les segments qui ne sont pas très correct (match mais pas de façon continue) retour = [] times_2 = [] for segment, time in zip(segments_filtered, times_filtered): diff = np.diff(segment[3, :]) if np.max(diff) < 50: # valide si moins de 50m entre 2 points retour.append(segment) times_2.append(time) # Pack for storage for seg in zip(segments_filtered, times_filtered): positions = seg[0] timestamps1 = seg[1][0] timestamps2 = seg[1][1] self.segments.append( Segment(positions, timestamps1, timestamps2, self.race1.points, self.race2.points, "primitive"))
def load_segments(task_file, audio_folder, max_time): segments = [] for segment_json in task_file["segments"]: segments.append(Segment.from_json(segment_json, audio_folder, max_time)) return segments
def generate_breath_pause(self): return Segment('silent', '', '', AudioSegment.silent(self.length), None, [], [])
def test_returns_empty_list_for_no_points(self): self.assertEqual([], count_segments_for_points( [Segment(0, 5), Segment(6, 7)], []))
def test_returns_for_points_on_border_of_segments(self): self.assertEqual([1, 3, 3, 1, 2], count_segments_for_points( [Segment(0, 5), Segment(2, 10), Segment(2, 7)], [0, 2, 5, 10, 7]))
def test_returns_for_points_on_the_segments(self): self.assertEqual([1, 1, 1, 0], count_segments_for_points( [Segment(0, 5), Segment(2, 7), Segment(10, 15)], [1, 6, 12, 8]))
def test_returns_for_single_point_on_the_intersection_of_segments(self): self.assertEqual([2], count_segments_for_points( [Segment(0, 5), Segment(2, 7)], [3]))
def test_returns_zero_for_single_point_on_the_right_of_segments(self): self.assertEqual([0], count_segments_for_points( [Segment(0, 5), Segment(2, 7)], [10]))
def test_returns_two_for_two_segments_with_intersections(self): result = max_segment_intersections([Segment(1, 3), Segment(2, 5)]) self.assertEqual(result, 2)
def test_returns_one_for_single_segment(self): result = max_segment_intersections([Segment(1, 3)]) self.assertEqual(result, 1)
def extract_segment(self, segment_type): size = 10 step = 5 epsilon = 0.00089443 c1 = self.segment1.positions c2 = self.segment2.positions c1_matched_c2 = np.zeros(c1.shape[1]) mean_trace = np.zeros((4, c1.shape[1])) timestamps_trace = np.zeros((2, c1.shape[1]), dtype='object') for i2 in range(0, c2.shape[1] - size, step): for i1 in range(0, c1.shape[1] - size, 1): dist2 = np.linalg.norm(c1[0:2, i1:i1 + size] - c2[0:2, i2:i2 + size]) if dist2 <= epsilon: c1_matched_c2[i1:i1 + size] += 1 mean_trace[:3, i1:i1 + size] = (c1[0:3, i1:i1 + size] + c2[0:3, i2:i2 + size]) / 2 mean_trace[3, i1:i1 + size] = c1[3, i1:i1 + size] timestamps_trace[:, i1:i1 + size] = [ self.segment1.times1[i1:i1 + size], self.segment2.times1[i2:i2 + size] ] # Segment extractor (list of segment) where = np.where(c1_matched_c2 > 1)[0] diff = np.diff(where) > 1 if diff is False: splitted = [where] else: sep = np.argwhere(diff).T[0] splitted = np.split(where, sep + 1) ret = [mean_trace[:, i] for i in splitted] times = [timestamps_trace[:, i] for i in splitted] # Drop segments shorter than 20 segments_filtered = [i for i in ret if i.shape[1] > 40] times_filtered = [i for i in times if i.shape[1] > 40] retour = [] times_2 = [] for segment, time in zip(segments_filtered, times_filtered): diff = np.diff(segment[3, :]) if np.max(diff) < 50: retour.append(segment) times_2.append(time) # Pack for storage for seg in zip(segments_filtered, times_filtered): positions = seg[0] timestamps1 = seg[1][0] timestamps2 = seg[1][1] self.segments.append( Segment(positions, timestamps1, timestamps2, self.segment1.points1, self.segment2.points1, segment_type))