def test_get_track_missing(self): # test failure to get track from set when set does have contents, but # TID is not present ts = TrackSet([Track(0), Track(1), Track(3)]) nt.assert_raises( IndexError, ts.get_track, 2 )
def test_set_id(self): t = Track() nose.tools.assert_equal(t.id, 0) t.id = 2 nose.tools.assert_equal(t.id, 2) t.id = 1345634 nose.tools.assert_equal(t.id, 1345634)
def test_all_frame_ids_single_track(self): # From a single track n = 10 t = Track(1) for i in range(n): t.append(TrackState(i)) ts = TrackSet([t]) nt.assert_equal(ts.all_frame_ids(), set(range(10)))
def test_ts_feat_desc(self): t = Track() f0 = Feature(mag=0) d0 = Descriptor(4) d0[:] = 0 t.append(TrackState(0, f0, d0)) f1 = Feature(mag=1) d1 = Descriptor(4) d1[:] = 1 t.append(TrackState(1, f1, d1)) nose.tools.assert_equal(t.size, 2) nose.tools.assert_equal(len(t), 2) ts0 = t.find_state(0) nose.tools.assert_equal(ts0.feature, f0) numpy.testing.assert_equal(ts0.descriptor, d0) ts1 = t.find_state(1) nose.tools.assert_equal(ts1.feature, f1) numpy.testing.assert_equal(ts1.descriptor, d1) # Delete local descriptor references, get track states and check # descriptor values manually del f0, f1, d0, d1, ts0, ts1 # Now, only the track-state in C++ has feature/descriptor references numpy.testing.assert_equal(t.find_state(0).descriptor, [0, 0, 0, 0]) numpy.testing.assert_equal(t.find_state(1).descriptor, [1, 1, 1, 1])
def test_iteration(self): t = Track() t.append(TrackState(0)) t.append(TrackState(1)) t.append(TrackState(5)) t.append(TrackState(9)) nose.tools.assert_equal( [ts.frame_id for ts in t], [0, 1, 5, 9] )
def test_all_frame_ids_single_track(self): # From a single track n = 10 t = Track(1) for i in range(n): t.append(TrackState(i)) ts = TrackSet([t]) nt.assert_equal( ts.all_frame_ids(), set(range(10)) )
def test_initial_id(self): t = Track() nose.tools.assert_equal(t.id, 0) t = Track(0) nose.tools.assert_equal(t.id, 0) t = Track(-1) nose.tools.assert_equal(t.id, -1) t = Track(15) nose.tools.assert_equal(t.id, 15)
def to_ObjectTrackSet(tracks): """Create an ObjectTrackSet from a dict whose keys are track IDs and values are lists of pairs of Kwiver timestamps and Kwiver DetectedObjects""" # Modeled after similar code in srnn_tracker.py result = [] for tid, states in tracks.items(): t = Track(id=tid) for ts, do in states: ots = ObjectTrackState(ts.get_frame(), ts.get_time_usec(), do) if not t.append(ots): raise ValueError("Unsorted input to to_ObjectTrackSet") result.append(t) return ObjectTrackSet(result)
def test_track_getitem(self): # this is the same test as test_track_find, but using the get-item # accessor syntax t = Track() t.append(TrackState(0)) t.append(TrackState(1)) t.append(TrackState(5)) t.append(TrackState(9)) ts = t[0] nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 0) ts = t[1] nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 1) ts = t[5] nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 5) ts = t[9] nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 9) nose.tools.assert_raises(IndexError, t.find_state, 10) t.append(TrackState(10)) nose.tools.assert_is_not_none(t[10]) nose.tools.assert_equal(t[10].frame_id, 10)
def test_set_id(self): t = Track() nose.tools.assert_equal(t.id, 0) t.id = 2 nose.tools.assert_equal(t.id, 2) t.id = 1345634 nose.tools.assert_equal(t.id, 1345634) # Shouldn't be able to set floats def set(): t.id = 1.5 nose.tools.assert_raises(ctypes.ArgumentError, set)
def test_new_nonempty(self): n = 10 tracks = [Track(i) for i in xrange(n)] ts = TrackSet(tracks) nt.assert_true(ts, "Invalid track set instance constructed") nt.assert_equal(len(ts), n) nt.assert_equal(ts.size(), n)
def _new(self, track_list): """ :param track_list: List of tracks to initialize the set with :type track_list: collections.Iterable[Track] | None """ if track_list is None: track_list = [] # noinspection PyCallingNonCallable c_track_array = (Track.c_ptr_type() * len(track_list))(*(t.c_pointer for t in track_list)) return self._call_cfunc( 'vital_object_trackset_new', [ctypes.c_size_t, ctypes.POINTER(Track.c_ptr_type())], [len(track_list), c_track_array], self.C_TYPE_PTR)
def test_get_track_single(self): # get track when only track t = Track(0) ts = TrackSet([t]) u = ts.get_track(0) # Check that they're the same underlying instance nt.assert_equal(ctypes.addressof(t.c_pointer.contents), ctypes.addressof(u.c_pointer.contents))
def test_track_getitem(self): # this is the same test as test_track_find, but using the get-item # accessor syntax t = Track() t.append(TrackState(0)) t.append(TrackState(1)) t.append(TrackState(5)) t.append(TrackState(9)) ts = t[0] nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 0) ts = t[1] nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 1) ts = t[5] nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 5) ts = t[9] nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 9) nose.tools.assert_raises( IndexError, t.find_state, 10 ) t.append(TrackState(10)) nose.tools.assert_is_not_none(t[10]) nose.tools.assert_equal(t[10].frame_id, 10)
def test_tracklist_accessor(self): n = 10 tracks = [Track(i) for i in range(n)] ts = TrackSet(tracks) ts_tracks = ts.tracks() nt.assert_equal(len(ts_tracks), n) for i, t in enumerate(ts_tracks): nt.assert_equal(t.id, i)
def test_all_frame_ids_multitrack(self): # Across multiple tracks n = 10 t1 = Track(1) for i in range(0, n): t1.append(TrackState(i)) t2 = Track(2) for i in range(n, n + 5): t2.append(TrackState(i)) ts = TrackSet([t1, t2]) nt.assert_equal(ts.all_frame_ids(), set(range(n + 5)))
def test_ts_append(self): t = Track() nose.tools.assert_equal(t.size, 0) nose.tools.assert_equal(len(t), 0) ts = TrackState(10) nose.tools.assert_true(t.append(ts)) nose.tools.assert_equal(t.size, 1) nose.tools.assert_equal(len(t), 1) ts = TrackState(11) nose.tools.assert_true(t.append(ts)) nose.tools.assert_equal(t.size, 2) nose.tools.assert_equal(len(t), 2) # Other properties that should not be different than default nose.tools.assert_equal(t.first_frame, 10) nose.tools.assert_equal(t.last_frame, 11) nose.tools.assert_false(t.is_empty)
def get_track(self, tid): """ Get the track in this set with the specified ID :raises IndexError: If no tracks in this set match the given ID. :param tid: The ID of the track to get :type tid: int :return: New Track instance referring to the track with the given ID in this set, or None if no tracks in this set have the given ID. :rtype: Track """ track_cptr = self._call_cfunc('vital_trackset_get_track', [self.C_TYPE_PTR, ctypes.c_int64], [self, tid], Track.c_ptr_type()) if not track_cptr: raise IndexError(tid) return Track(from_cptr=track_cptr)
def test_get_track_multiple(self): n = 10 tracks = [Track(i) for i in range(n)] ts = TrackSet(tracks) # Check that they're the same underlying instance for i in range(n): nt.assert_equal( ctypes.addressof(ts.get_track(i).c_pointer.contents), ctypes.addressof(tracks[i].c_pointer.contents))
def tracks(self): """ Get the list of all tracks contained in this set (new instances). :return: list of new Track instances of tracks contained in this set :rtype: list[Track] """ c_ptr_arr = self._call_cfunc('vital_trackset_tracks', [self.C_TYPE_PTR], [self], ctypes.POINTER(Track.c_ptr_type())) tracks = [] for cptr in c_ptr_arr[:self.size()]: cptr = Track.c_ptr_type()(cptr.contents) tracks.append(Track(from_cptr=cptr)) free_void_ptr(c_ptr_arr) return tracks
def test_ts_append_outoforder(self): t = Track() nose.tools.assert_true(t.append(TrackState(10))) nose.tools.assert_false(t.append(TrackState(10))) nose.tools.assert_false(t.append(TrackState(9))) nose.tools.assert_false(t.append(TrackState(0))) nose.tools.assert_false(t.append(TrackState(-1))) nose.tools.assert_true(t.append(TrackState(11))) nose.tools.assert_false(t.append(TrackState(11))) # After all that there should only be two states in there for frames 10 # and 11. nose.tools.assert_equal(t.size, 2) nose.tools.assert_equal(len(t), 2) nose.tools.assert_equal(t.all_frame_ids(), {10, 11})
def test_iteration(self): t = Track() t.append(TrackState(0)) t.append(TrackState(1)) t.append(TrackState(5)) t.append(TrackState(9)) nose.tools.assert_equal([ts.frame_id for ts in t], [0, 1, 5, 9])
def test_last_frame(self): # no tracks ts = TrackSet() nt.assert_equal(ts.last_frame(), 0) # one track t = Track(1) t.append(TrackState(1)) t.append(TrackState(2)) ts = TrackSet([t]) nt.assert_equal(ts.last_frame(), 2) # two tracks t2 = Track(2) t2.append(TrackState(3)) ts = TrackSet([t, t2]) nt.assert_equal(ts.last_frame(), 3)
def subset_tracks(trackset, keep_fraction=0.75): """ randomly drop a fraction of the track states per track in the given set, creating and returning new tracks in a new track-set. :type trackset: TrackSet :type keep_fraction: float """ log = logging.getLogger(__name__) new_tracks = [] for t in trackset.tracks(): nt = Track(t.id) msg = 'track %d:' % t.id, for ts in t: if numpy.random.rand() < keep_fraction: nt.append(ts) msg += '.', else: msg += 'X', log.info(' '.join(msg)) new_tracks.append(nt) return TrackSet(new_tracks)
def projected_tracks(lmap, cmap): """ Use the cameras to project the landmarks back into their images. :type lmap: LandmarkMap :type cmap: CameraMap """ tracks = [] cam_d = cmap.as_dict() landmark_d = lmap.as_dict() for lid, l in landmark_d.iteritems(): t = Track(lid) tracks.append(t) # Sort camera iteration to make sure that we go in order of frame IDs for fid in sorted(cam_d): cam = cam_d[fid] f = Feature(cam.project(l.loc)) t.append(TrackState(fid, f)) assert t.size == len(cam_d) return TrackSet(tracks)
def test_tracklist_accessor(self): n = 10 tracks = [Track(i) for i in xrange(n)] ts = TrackSet(tracks) ts_tracks = ts.tracks() nt.assert_equal(len(ts_tracks), n) for i, t in enumerate(ts_tracks): nt.assert_equal(t.id, i) # constructing a new trackset from the returned list of tracks should # yield a trackset whose accessor should return the same list of tracks # (same C/C++ instances). ts2 = TrackSet(ts_tracks) ts2_tracks = ts2.tracks() for i in xrange(n): ctypes.addressof(ts2_tracks[0].c_pointer.contents) \ == ctypes.addressof(ts2_tracks[0].c_pointer.contents)
def test_all_frame_ids_multitrack(self): # Across multiple tracks n = 10 t1 = Track(1) for i in range(0, n): t1.append(TrackState(i)) t2 = Track(2) for i in range(n, n+5): t2.append(TrackState(i)) ts = TrackSet([t1, t2]) nt.assert_equal( ts.all_frame_ids(), set(range(n+5)) )
def test_initial_size(self): t = Track() nose.tools.assert_equal(t.size, 0) nose.tools.assert_equal(len(t), 0)
def test_new_nonempty(self): n = 10 tracks = [Track(i) for i in range(n)] ts = TrackSet(tracks) nt.assert_equal(len(ts), n) nt.assert_equal(ts.size(), n)
def test_initial_all_frame_ids(self): t = Track() s = t.all_frame_ids() nose.tools.assert_equal(len(s), 0)
def test_track_find(self): t = Track() t.append(TrackState(0)) t.append(TrackState(1)) t.append(TrackState(5)) t.append(TrackState(9)) ts = t.find_state(0) nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 0) ts = t.find_state(1) nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 1) ts = t.find_state(5) nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 5) ts = t.find_state(9) nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 9) nose.tools.assert_raises(IndexError, t.find_state, 10) t.append(TrackState(10)) nose.tools.assert_is_not_none(t.find_state(10)) nose.tools.assert_equal(t.find_state(10).frame_id, 10)
def test_new(self): t = Track()
def test_track_find(self): t = Track() t.append(TrackState(0)) t.append(TrackState(1)) t.append(TrackState(5)) t.append(TrackState(9)) ts = t.find_state(0) nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 0) ts = t.find_state(1) nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 1) ts = t.find_state(5) nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 5) ts = t.find_state(9) nose.tools.assert_is_not_none(ts) nose.tools.assert_equal(ts.frame_id, 9) nose.tools.assert_raises( IndexError, t.find_state, 10 ) t.append(TrackState(10)) nose.tools.assert_is_not_none(t.find_state(10)) nose.tools.assert_equal(t.find_state(10).frame_id, 10)
def test_initial_firstlast_frame(self): t = Track() nose.tools.assert_equal(t.first_frame, 0) nose.tools.assert_equal(t.last_frame, 0)
def test_initial_is_empty(self): t = Track() nose.tools.assert_true(t.is_empty)