def test_tracklet_interpolate(real_tracklets): data = np.stack(list(real_tracklets[0].values()))[:10] inds = np.arange(len(data)) gap = 2 inds[len(inds) // 2:] += gap tracklet = Tracklet(data, inds) assert len(tracklet) == len(data) new_tracklet = tracklet.interpolate(max_gap=1) assert len(new_tracklet) == len(data) new_tracklet = tracklet.interpolate(max_gap=gap) assert len(new_tracklet) == len(data) + gap missing_inds = list(set(range(inds.max())).difference(inds)) assert np.all(new_tracklet.data[missing_inds, :, 2] == 0.5)
def fake_stitcher(): inds = np.arange(TRACKLET_LEN) data = np.random.rand(inds.size, N_DETS, 3) track = Tracklet(data, inds) idx = np.linspace(0, inds.size, N_TRACKLETS + 1, dtype=int) tracklets = TrackletStitcher.split_tracklet(track, idx[1:-1]) return TrackletStitcher(tracklets, n_tracks=2)
def fake_tracklet(): inds = np.arange(TRACKLET_START, TRACKLET_START + TRACKLET_LEN) data = np.empty((TRACKLET_LEN, N_DETS, 4)) data[..., :2] = np.arange(N_DETS).reshape(-1, 1) * [1, 1] data[..., 2] = 1 data[..., 3] = TRACKLET_ID return Tracklet(data, inds)
def test_tracklet_affinities(fake_tracklet): other_tracklet = Tracklet(fake_tracklet.data, fake_tracklet.inds + TRACKLET_LEN) _ = fake_tracklet.dynamic_similarity_with(other_tracklet) _ = fake_tracklet.dynamic_dissimilarity_with(other_tracklet) _ = fake_tracklet.shape_dissimilarity_with(other_tracklet) _ = fake_tracklet.box_overlap_with(other_tracklet) _ = fake_tracklet.motion_affinity_with(other_tracklet) _ = fake_tracklet.distance_to(other_tracklet)
def _reconstruct_tracks_as_tracklets(df): """ Parameters: ----------- df: DataFrame loaded from an .h5 tracks file (obtained from `stitch_tracklets()`) """ from deeplabcut.refine_training_dataset.stitch import Tracklet tracklets = [] for _, group in df.groupby("individuals", axis=1): temp = group.dropna(how="all") inds = temp.index.to_numpy() track = Tracklet(temp.to_numpy().reshape((len(temp), -1, 3)), inds) track = track.interpolate(max_gap=len(group)) tracklets.append(track) return tracklets
def test_tracklet(tracklet): assert len(tracklet) == TRACKLET_LEN assert tracklet.likelihood == 1 assert tracklet.identity == TRACKLET_ID assert tracklet.start == TRACKLET_START assert tracklet.end == TRACKLET_START + TRACKLET_LEN - 1 np.testing.assert_equal( tracklet.centroid, np.full((TRACKLET_LEN, 2), np.arange(tracklet.data.shape[1]).mean())) tracklet2 = Tracklet(tracklet.data, tracklet.inds + TRACKLET_LEN) assert tracklet not in tracklet2 tracklet_new = tracklet + tracklet2 tracklet_new -= tracklet np.testing.assert_equal(tracklet_new.data, tracklet2.data) np.testing.assert_equal(tracklet_new.inds, tracklet2.inds) tracklet2 = tracklet + tracklet assert tracklet2.contains_duplicates()
def test_tracklet(fake_tracklet): assert len(fake_tracklet) == TRACKLET_LEN assert fake_tracklet.likelihood == 1 assert fake_tracklet.identity == TRACKLET_ID assert fake_tracklet.start == TRACKLET_START assert fake_tracklet.end == TRACKLET_START + TRACKLET_LEN - 1 np.testing.assert_equal( fake_tracklet.centroid, np.full((TRACKLET_LEN, 2), np.arange(N_DETS).mean()) ) fake_tracklet2 = Tracklet(fake_tracklet.data, fake_tracklet.inds + TRACKLET_LEN) assert fake_tracklet not in fake_tracklet2 tracklet = fake_tracklet + fake_tracklet2 tracklet -= fake_tracklet np.testing.assert_equal(tracklet.data, fake_tracklet2.data) np.testing.assert_equal(tracklet.inds, fake_tracklet2.inds) tracklet2 = fake_tracklet + fake_tracklet assert tracklet2.contains_duplicates()
def test_tracklet_monotonic_indices(fake_tracklet): tracklet_inv = Tracklet(fake_tracklet.data[::-1], fake_tracklet.inds[::-1]) np.testing.assert_equal(fake_tracklet.inds, tracklet_inv.inds) np.testing.assert_equal(fake_tracklet.xy, tracklet_inv.xy)
def test_tracklet_wrong_inputs(fake_tracklet): with pytest.raises(ValueError): _ = Tracklet(fake_tracklet.data[..., :2], fake_tracklet.inds) with pytest.raises(ValueError): _ = Tracklet(fake_tracklet.data[:TRACKLET_LEN - 2], fake_tracklet.inds)
def make_fake_tracklets(): tracklet = fake_tracklet() tracklet_single = Tracklet(tracklet.data[:, :1], tracklet.inds) return tracklet, tracklet_single