def test_segment_basic_segments_correctly(self): """ A few example time-serieses to be segmented. """ ts = np.array([False, False, True, True, False, False, True, False]) starts_correct, ends_correct = (np.array([2, 6]), np.array([4, 7])) starts, ends = time_series.segment_basic(ts) np.testing.assert_array_equal(starts, starts_correct) np.testing.assert_array_equal(ends, ends_correct) ts = np.array([False, False, True, True, False, False, True, True]) starts_correct, ends_correct = (np.array([2, 6]), np.array([4, 8])) starts, ends = time_series.segment_basic(ts) np.testing.assert_array_equal(starts, starts_correct) np.testing.assert_array_equal(ends, ends_correct) ts = np.array([True, False, True, True, False, False, True, True]) starts_correct, ends_correct = (np.array([0, 2, 6]), np.array([1, 4, 8])) starts, ends = time_series.segment_basic(ts) np.testing.assert_array_equal(starts, starts_correct) np.testing.assert_array_equal(ends, ends_correct)
def clean_traj(traj, cleaning_params): """ Return the start and end timepoints of the clean portions of a trajectory. :param traj: a Trajectory :param cleaning_params: cleaning parameter dictionary :return: list of tuples giving start and end timepoint ids """ speed_threshold = cleaning_params['speed_threshold'] dist_from_wall_threshold = cleaning_params['dist_from_wall_threshold'] min_pause_length = cleaning_params['min_pause_length'] min_trajectory_length = cleaning_params['min_trajectory_length'] # get relevant trajectory information stp_id, etp_id = traj.start_timepoint_id, traj.end_timepoint_id speeds = traj.velocities_a(session) dists_from_wall = traj.distances_from_wall(session) # get mask of all timepoints below speed and distance from wall threshold paused_mask = (speeds < speed_threshold) * (dists_from_wall < dist_from_wall_threshold) # get start and end idxs of pauses pause_starts, pause_ends = time_series.segment_basic(paused_mask) # set paused_mask elements to false when pause is below minimum duration for pd_ctr, pause_duration in enumerate(pause_ends - pause_starts): if pause_duration < min_pause_length: paused_mask[pause_starts[pd_ctr]:pause_ends[pd_ctr]] = False # get starts and ends of active portions of trajectories clean_portions = np.transpose( time_series.segment_basic(~paused_mask, t=np.arange(stp_id, etp_id + 1))) # decrement end timepoint ids since SQL uses inclusive ends clean_portions[:, 1] -= 1 # return only portions that are sufficiently long return clean_portions[(clean_portions[:, 1] - clean_portions[:, 0]) > min_trajectory_length]
def test_segment_basic_segments_correctly_with_external_idxs(self): t = np.arange(100, 200) ts = np.array([False, False, True, True, False, False, True, False]) starts_correct, ends_correct = (np.array([2, 6]) + 100, np.array([4, 7]) + 100) starts, ends = time_series.segment_basic(ts, t) np.testing.assert_array_equal(starts, starts_correct) np.testing.assert_array_equal(ends, ends_correct) ts = np.array([False, False, True, True, False, False, True, True]) starts_correct, ends_correct = (np.array([2, 6]) + 100, np.array([4, 8]) + 100) starts, ends = time_series.segment_basic(ts, t) np.testing.assert_array_equal(starts, starts_correct) np.testing.assert_array_equal(ends, ends_correct) ts = np.array([True, False, True, True, False, False, True, True]) starts_correct, ends_correct = (np.array([0, 2, 6]) + 100, np.array([1, 4, 8]) + 100) starts, ends = time_series.segment_basic(ts, t) np.testing.assert_array_equal(starts, starts_correct) np.testing.assert_array_equal(ends, ends_correct)