Data¶
Datastructures¶
Defines commonly used data structures for a convenient exchange format.
Note that collections.namedtuple are used for fast data exchange objects but they do
not support docstring in Python 2. Setting the docstring is only possible in Python 3.
So depending on the Python version that was used to compile the documentation the documentation for
the datastructures are missing.
-
class
Detection[source]¶ Bases:
tupleA
Detectionis one data point produced where (possibly) a bee was observed.-
id¶ int or str – identifier for detection
-
timestamp¶ time representation – timestamp of detection
-
x¶ float – x position in image coordinates
-
y¶ float – y position in image coordinates
-
orientation¶ float – the orientation of the tag/bee (zrotation in schema)
-
beeId¶ int or iterable – interpretation of id
-
-
class
Track[source]¶ Bases:
tupleA
Trackis a group ofDetectionobjects produced by (possibly) one bee.To avoid handing a lot of data that we won’t need only the identifiers and timestamps are stored/provided in a
Track. Specific data for a detection could be retrieved via theDataWrapper.Depending on the
DataWrapperimplementation the detections are also stored as objects in the meta field. The key identifier for the list of detections in the meta field isDETKEY.Note
It might be necessary to clear the meta dictionary after some processing steps to free memory.
-
timestamps¶ iterable – iterable with timestamps of detections
-
-
class
Score[source]¶ Bases:
tupleA
Scoredefines how similar aTrackobject is with a truth path.Example
This is an example for a
Trackand a resultingScore:Track(id=1, ids=['A', 'A', 'B', 'B', 'C', 'C'], timestamps=(...), meta={}) Score(value=0.33, track_id=1, truth_id='A', calc_id='A', metrics=ScoreMetrics(), alternatives=['B', 'C'])
-
value¶ float – The scoring value calculated via a scoring function using the
ScoreMetrics
-
metrics¶ ScoreMetrics– metrics describing the differences betweenTrackobjects
-
-
class
ScoreMetrics[source]¶ Bases:
tupleA
ScoreMetricsprovides metrics to determine how similar twoTrackobjects are.Example
Assuming
gap = 2Truth Track Test Track Metric _ _ Gap Match (gap_left = True) _ _ Gap Match A A Match _ _ Gap Match A B Mismatch _ B Insert A A Match A _ Delete _ _ Gap Match A _ Delete (gap_right = False) The following ScoreMetrics belongs to the track comparison in the table above.:
ScoreMetrics(track_length=5, truth_track_length=100, adjusted_length=10, id_matches=2, id_mismatches=1, inserts=1, deletes=1, gap_matches=4, gap_left=True, gap_right=False)
-
truth_track_length¶ int – length of truth track we are matching against
-
id_matches¶ int – number of matched ids
-
id_mismatches¶ int – number of mismatched ids
-
gap_matches¶ int – correctly matched gaps
-
gap_left¶ bool – correctly identified gap to the left
-
gap_right¶ bool – correctly identified gap to the right
-
Constants¶
Constants that are used e.g. as keys in the meta dictionary in Detection and Track.
-
CAMKEY= 'camId'¶ This key is used in
Detectionto mark the camera the detection is associated with.
-
TRUTHKEY= 'truthId'¶ This key is used in
TrackandDetectionto store the truth id of the frame object for training and validation.
DataWrapper¶
Provides classes for abstract access to Detection and Track objects.
There are two abstract classes describing the interface of a DataWrapper.
A DataWrapper is only providing access to data, whereas the DataWrapperTruth
also considers ground truth data. This distinction has the following reasons:
- No accidental tracking on truth data (e.g. truth ids)
- Better performance in generation and access of data
- Smaller interface for production usage
There are currently three implementations:
DataWrapperBinary:- Directly integrates with bb_binary and is implemented with a focus on performance.
DataWrapperPandas:- Uses Pandas as backend. Therefore it is more complicated to generate an instance of this DataWrapper. But it offers more possibilities in using other data sources or performing experimens with data, that is not available in bb_binary.
DataWrapperTracks:- This is a DataWrapper to organize the access to
Trackobjects to be able to perform tracking and validation on track level (assigning tracks to other tracks instead of detections to tracks).
-
class
DataWrapper[source]¶ Bases:
objectAbstract class that describes the access to detections.
-
get_camids(frame_object=None)[source]¶ Returns an iterable with camera ids.
If frame_object is
Nonereturns all available camera ids. Otherwise it returns the camera ids theDetectionobjects of frame_object are associated with. So possible multiple ids if frame_object is aTrackand a single id if frame_object is aDetection.Keyword Arguments: Returns: iterable structure with camera ids
Return type: listof int
-
get_detection(detection_id)[source]¶ Returns all information concerning the
Detectionwith detection_id.Parameters: detection_id (int or str) – the id of the detection as used in the data schema Returns: data formated as DetectionReturn type: Detection
-
get_detections(detection_ids)[source]¶ Returns all information concerning the
Detectionobjects with detection_ids.Parameters: detection_ids ( listof int or str) – iterable structure with detection idsReturns: iterable structure with DetectionobjectsReturn type: listofDetection
-
get_frame_objects(cam_id=None, timestamp=None)[source]¶ Gets all detections or tracks on cam_id in frame with timestamp.
Keyword Arguments: Returns: iterable structure with
DetectionorTrackdepending onDataWrapperimplementation.Return type:
-
get_neighbors(frame_object, cam_id, radius=10, timestamp=None)[source]¶ Gets all detections or tracks in the neighborhood of the given frame_object.
Parameters: Keyword Arguments: - radius (Optional int) – the radius to search in image coordinates
- timestamp (Optional timestamp) – consider frame objects of frame with different timestamp
Returns: iterable structure with
DetectionorTrackdepending onDataWrapperimplementation.Return type:
-
-
class
DataWrapperTruth[source]¶ Bases:
bb_tracking.data.datawrapper.DataWrapperSpecial wrapper for truth data.
This is separated from
DataWrapperfor performance and convenience reasons.The main difference is, that this class will merge ground truth data with pipeline output and generate truth tracks that can be used for training and validation.
-
fp_id= -1¶ int – id for possibly false positive detections (no matching truth_id)
-
code_unknown= 'unknown'¶ str – default value for readability
-
get_all_detection_ids()[source]¶ Returns the ids of all positives and false positives.
Returns: tuple containing: Return type: tuple
-
get_truth_track(truth_id, cam_id=None)[source]¶ Returns the
Trackto the given truth_id.Parameters: truth_id (int or str) – we are using the truth_id to identify a certain track Keyword Arguments: cam_id (Optional int) – the cam to consider Returns: the track to the given truth_id Return type: Track
-
get_truth_tracks(cam_id=None)[source]¶ Returns an iterator over all the truth tracks.
Keyword Arguments: cam_id (Optional int) – restrict on tracks on this camera Returns: iterator over all the truth TracksReturn type: iterator
-
get_truthid(frame_object)[source]¶ Returns the truth id for the corresponding frame_object id or
None.Keyword Arguments: frame_object ( DetectionorTrackor id) – frame object or the id of theDetectionso extract the truth id fromReturns: the truth id for the frame_object or fp_idif frame_object is a false positiveReturn type: int or str
-
get_truthids(cam_id=None, frame_object=None)[source]¶ Returns all truth ids of either the whole truth data or restricted on some parameters.
Note
cam_id and frame_object can not be used together!
Keyword Arguments: Returns: all the truth ids that might be associated with a frame object or camera.
Return type:
-
DataWrapperBinary¶
This implementations of DataWrapper are using dictionaries with shared data objects as
backend. Therefore they are tuned for fast performance and not for data analysis on the
detection data.
The DataWrapperBinary implementations are only compatible with bb_binary data.
If have other data formats either try to convert them
or use DataWrapperPandas.
-
class
DataWrapperBinary(repository, meta_keys=None, position_scale=1.0, **kwargs)[source]¶ Bases:
bb_tracking.data.datawrapper.DataWrapperClass for fast access to detections via bb_binary
Repository.This class is recommended for performance reasons and only compatible to the bb_binary format. It uses dictionaries for fast lookup and cheap memory because the
Detectionobjects are shared between the different dictionaries.-
__init__(repository, meta_keys=None, position_scale=1.0, **kwargs)[source]¶ Necessary initialization to organize detection data.
Parameters: repository (
bb_binary.Repository) – bb_binary Repository with detectionsKeyword Arguments: - meta_keys (Optional
dict) –{detecion_key: meta_key}mapping that is added as meta field in detections (detection fields defined in the bb_binary schema) - (Optional (position_scale) – float:): rescaling factor for position data
- **kwargs (
dict) – keyword arguments forRepository.iter_frames()
- meta_keys (Optional
-
get_camids(frame_object=None)[source]¶ Returns an iterable with camera ids.
If frame_object is
Nonereturns all available camera ids. Otherwise it returns the camera ids theDetectionobjects of frame_object are associated with. So possible multiple ids if frame_object is aTrackand a single id if frame_object is aDetection.Keyword Arguments: Returns: iterable structure with camera ids
Return type: listof int
-
get_detection(detection_id)[source]¶ Returns all information concerning the
Detectionwith detection_id.Parameters: detection_id (int or str) – the id of the detection as used in the data schema Returns: data formated as DetectionReturn type: Detection
-
get_detections(detection_ids)[source]¶ Returns all information concerning the
Detectionobjects with detection_ids.Parameters: detection_ids ( listof int or str) – iterable structure with detection idsReturns: iterable structure with DetectionobjectsReturn type: listofDetection
-
get_frame_objects(cam_id=None, timestamp=None)[source]¶ Gets all detections or tracks on cam_id in frame with timestamp.
Keyword Arguments: Returns: iterable structure with
DetectionorTrackdepending onDataWrapperimplementation.Return type:
-
get_neighbors(frame_object, cam_id, radius=10, timestamp=None)[source]¶ Gets all detections or tracks in the neighborhood of the given frame_object.
Parameters: Keyword Arguments: - radius (Optional int) – the radius to search in image coordinates
- timestamp (Optional timestamp) – consider frame objects of frame with different timestamp
Returns: iterable structure with
DetectionorTrackdepending onDataWrapperimplementation.Return type:
-
-
class
DataWrapperTruthBinary(repo_detections, repo_truth, radius, meta_keys=None, **kwargs)[source]¶ Bases:
bb_tracking.data.datawrapper_binary.DataWrapperBinary,bb_tracking.data.datawrapper.DataWrapperTruthClass for fast access to truth data via bb_binary
Repository.This class is recommended for performance reasons and only compatible to the bb_binary format. It uses dictionaries for fast lookup and cheap memory because the
Detectionobjects are shared between the different dictionaries.Note
The generation and access to the objects is not as fast as in
DataWrapperBinary. This is no problem, because the truth data is most likely not as big as real datasets.-
__init__(repo_detections, repo_truth, radius, meta_keys=None, **kwargs)[source]¶ Necessary initialization to organize detection data.
Parameters: - repo_detections (
bb_binary.Repository) – Repository with detections - repo_truth (
bb_binary.Repository) – Repository with truth data - radius (int) – merge detections and truth via distance
Keyword Arguments: - repo_detections (
-
get_all_detection_ids()[source]¶ Returns the ids of all positives and false positives.
Returns: tuple containing: Return type: tuple
-
get_truth_track(truth_id, cam_id=None)[source]¶ Returns the
Trackto the given truth_id.Parameters: truth_id (int or str) – we are using the truth_id to identify a certain track Keyword Arguments: cam_id (Optional int) – the cam to consider Returns: the track to the given truth_id Return type: Track
-
get_truth_tracks(cam_id=None)[source]¶ Returns an iterator over all the truth tracks.
Keyword Arguments: cam_id (Optional int) – restrict on tracks on this camera Returns: iterator over all the truth TracksReturn type: iterator
-
get_truthid(frame_object)[source]¶ Returns the truth id for the corresponding frame_object id or
None.Keyword Arguments: frame_object ( DetectionorTrackor id) – frame object or the id of theDetectionso extract the truth id fromReturns: the truth id for the frame_object or fp_idif frame_object is a false positiveReturn type: int or str
-
get_truthids(cam_id=None, frame_object=None)[source]¶ Returns all truth ids of either the whole truth data or restricted on some parameters.
Note
cam_id and frame_object can not be used together!
Keyword Arguments: Returns: all the truth ids that might be associated with a frame object or camera.
Return type:
-
DataWrapperPandas¶
Provides classes for easy access to detections beyond data formats.
This DataWrapppers are using Pandas as backend. This helps when you are using data from other data sources, but the instantiation is a bit more complicated. You basically create a Pandas DataFrame and provide a mapping from required default names to the column names in your Pandas DataFrame.
Row lookups in Pandas are also quite expensive so this this classes are quite slow and not recommended for production.
Note
If not used for experiments in the future they might be removed.
Todo
Refactor detection cache and add cache for frame trees.
-
class
DataWrapperPandas(detections, cols=None, duplicates_radius=None, meta_keys=None)[source]¶ Bases:
bb_tracking.data.datawrapper.DataWrapperClass for flexible access to detections.
This class is recommended for testing or data exploration. The data input is configurable and possible additional attributes are not discarded. But it is not as efficient as the
DataWrapperBinaryclass in terms of memory and lookup speed.-
beeId_digits= 12¶ int – number of digits use to encode a
beeIDon a tag
-
__init__(detections, cols=None, duplicates_radius=None, meta_keys=None)[source]¶ Necessary initialization to reformat detections dataframe.
Parameters: detections (
pd.DataFrame) – pandas dataframe with detectionsKeyword Arguments:
-
cols= None¶ obj`dict` – strings to identify certain columns
-
duplicates_radius= None¶ int – detections within this radius are considered to be duplicates
-
mean_duplicates_merge_columns= None¶ obj`dict` – use mean value for this columns when merge duplicates
-
detections= None¶ pd.DataFrame– pandas dataframe with all detections
-
clean_data(data)[source]¶ Clean the dataframes to use a common format to access all the data.
Parameters: data ( pd.DataFrame) – pandas dataframeReturns: clean dataframe Return type: pd.DataFrame
-
get_camids(frame_object=None)[source]¶ Returns an iterable with camera ids.
If frame_object is
Nonereturns all available camera ids. Otherwise it returns the camera ids theDetectionobjects of frame_object are associated with. So possible multiple ids if frame_object is aTrackand a single id if frame_object is aDetection.Keyword Arguments: Returns: iterable structure with camera ids
Return type: listof int
-
get_detection(detection_id)[source]¶ Returns all information concerning the
Detectionwith detection_id.Parameters: detection_id (int or str) – the id of the detection as used in the data schema Returns: data formated as DetectionReturn type: Detection
-
get_detections(detection_ids)[source]¶ Returns all information concerning the
Detectionobjects with detection_ids.Parameters: detection_ids ( listof int or str) – iterable structure with detection idsReturns: iterable structure with DetectionobjectsReturn type: listofDetection
-
get_frame_objects(cam_id=None, timestamp=None)[source]¶ Gets all detections or tracks on cam_id in frame with timestamp.
Keyword Arguments: Returns: iterable structure with
DetectionorTrackdepending onDataWrapperimplementation.Return type:
-
get_neighbors(frame_object, cam_id, radius=10, timestamp=None)[source]¶ Gets all detections or tracks in the neighborhood of the given frame_object.
Parameters: Keyword Arguments: - radius (Optional int) – the radius to search in image coordinates
- timestamp (Optional timestamp) – consider frame objects of frame with different timestamp
Returns: iterable structure with
DetectionorTrackdepending onDataWrapperimplementation.Return type:
-
-
class
DataWrapperTruthPandas(detections, truth, radius, merge_cols=None, **kwargs)[source]¶ Bases:
bb_tracking.data.datawrapper_pandas.DataWrapperPandas,bb_tracking.data.datawrapper.DataWrapperTruthSpecial wrapper for truth data with a Pandas Backend.
-
__init__(detections, truth, radius, merge_cols=None, **kwargs)[source]¶ Necessary initialization to reformat detections dataframe.
Also performs merge of detections and truth dataframes.
Parameters: - detections (
pd.DataFrame) – pandas dataframe with detections - truth (
pd.DataFrame) – pandas dataframe with truth - radius (int) – merge detections and truth via distance
Keyword Arguments: - merge_cols (
dict) –{key in truth: new key in detections}mapping. - **kwargs (
dict) – keyword arguments forDataWrapperPandas
- detections (
-
duplicate_cols= None¶ tuple of str – columns to identify track duplicates
-
clean_data(data)[source]¶ Clean the dataframes to use a common format to access all the data.
Parameters: data ( pd.DataFrame) – pandas dataframeReturns: clean dataframe Return type: pd.DataFrame
-
get_all_detection_ids()[source]¶ Returns the ids of all positives and false positives.
Returns: tuple containing: Return type: tuple
-
get_truth_track(truth_id, cam_id=None)[source]¶ Returns the
Trackto the given truth_id.Parameters: truth_id (int or str) – we are using the truth_id to identify a certain track Keyword Arguments: cam_id (Optional int) – the cam to consider Returns: the track to the given truth_id Return type: Track
-
get_truth_tracks(cam_id=None)[source]¶ Returns an iterator over all the truth tracks.
Keyword Arguments: cam_id (Optional int) – restrict on tracks on this camera Returns: iterator over all the truth TracksReturn type: iterator
-
get_truthid(frame_object)[source]¶ Returns the truth id for the corresponding frame_object id or
None.Keyword Arguments: frame_object ( DetectionorTrackor id) – frame object or the id of theDetectionso extract the truth id fromReturns: the truth id for the frame_object or fp_idif frame_object is a false positiveReturn type: int or str
-
get_truthids(cam_id=None, frame_object=None)[source]¶ Returns all truth ids of either the whole truth data or restricted on some parameters.
Note
cam_id and frame_object can not be used together!
Keyword Arguments: Returns: all the truth ids that might be associated with a frame object or camera.
Return type:
-
DataWrapperTracks¶
This implementations of DataWrapper are also using dictionaries with shared data
objects as backend. The difference to DataWrapperBinary is that they are managing
Track objects instead of Detection objects. Therefore the DataWrapperTracks
implementations have to know where a track starts and where it ends.
Sometimes it is necessary to have additional information about detections for training and
validation purposes. So it is posssible to inject another instance of DataWrapper and
DataWrapperTracks will delegate tasks it can not fullfill to this instance.
-
class
DataWrapperTracks(tracks, cam_timestamps, data=None)[source]¶ Bases:
bb_tracking.data.datawrapper.DataWrapperClass for access to tracks.
This class is implemented with a focus on performance. It is possible to inject a
DataWrapperfor other, non performant related tasks.-
__init__(tracks, cam_timestamps, data=None)[source]¶ Initialization for DataWrapperTracks
Note
The data keyword argument is optional, but sometimes it is convenient to have one
DataWrapperthat provides access toTrackandDetection.Parameters: - tracks (
listofTrack) – Iterable withTrack - cam_timestamps (
dict) –{cam_id: sorted list of timestamps}mapping. Same asDataWrapperBinary.cam_timestamps.
Keyword Arguments: data (Optional
DataWrapper) – A DataWrapper provides access to detections.- tracks (
-
data= None¶ DataWrapper– Optional DataWrapper instance to access detection data
-
get_camids(frame_object=None)[source]¶ Returns an iterable with camera ids.
If frame_object is
Nonereturns all available camera ids. Otherwise it returns the camera ids theDetectionobjects of frame_object are associated with. So possible multiple ids if frame_object is aTrackand a single id if frame_object is aDetection.Keyword Arguments: Returns: iterable structure with camera ids
Return type: listof int
-
get_detection(*args)[source]¶ Returns all information concerning the
Detectionwith detection_id.Parameters: detection_id (int or str) – the id of the detection as used in the data schema Returns: data formated as DetectionReturn type: Detection
-
get_detections(*args)[source]¶ Returns all information concerning the
Detectionobjects with detection_ids.Parameters: detection_ids ( listof int or str) – iterable structure with detection idsReturns: iterable structure with DetectionobjectsReturn type: listofDetection
-
get_track(track_id)[source]¶ Returns all information concerning the track with track_id.
Parameters: track_id (int or str) – the id of the track as used in the data scheme Returns: data formated as TrackReturn type: Track
-
get_tracks(track_ids)[source]¶ Same as
get_track()but with multiple ids.Parameters: track_ids ( listof int or str) – Iterable structure with track idsReturns: iterable structure with TrackReturn type: listofTrack
-
get_frame_objects(cam_id=None, timestamp=None)[source]¶ Gets all detections or tracks on cam_id in frame with timestamp.
Keyword Arguments: Returns: iterable structure with
DetectionorTrackdepending onDataWrapperimplementation.Return type:
-
get_frame_objects_starting(cam_id=None, timestamp=None)[source]¶ Gets all tracks starting on cam_id in frame with timestamp.
Keyword Arguments: - cam_id (Optional int) – the cam to consider, starts with smallest cam if None
- timestamp (Optional timestamp) – frame with timestamp, starts with first frame if None
Returns: iterable structure with
TrackReturn type:
-
get_neighbors(frame_object, cam_id, radius=10, timestamp=None)[source]¶ Gets all detections or tracks in the neighborhood of the given frame_object.
Parameters: Keyword Arguments: - radius (Optional int) – the radius to search in image coordinates
- timestamp (Optional timestamp) – consider frame objects of frame with different timestamp
Returns: iterable structure with
DetectionorTrackdepending onDataWrapperimplementation.Return type:
-
-
class
DataWrapperTruthTracks(tracks, cam_timestamps, data, **kwargs)[source]¶ Bases:
bb_tracking.data.datawrapper_tracks.DataWrapperTracks,bb_tracking.data.datawrapper.DataWrapperTruthClass for access to tracks and truth data.
As
DataWrapperTruthTracksare only used in the context of training and validation it is necessary to inject aDataWrapperTruthinstance. This implementation will delegate all truth data related tasks to the implementation inDataWrapperTracks.data.-
__init__(tracks, cam_timestamps, data, **kwargs)[source]¶ Initialization for DataWrapperTruthTracks
Parameters: - tracks (
listofTrack) – Iterable withTrack - cam_timestamps (
dict) –{cam_id: sorted list of timestamps}mapping - data (
DataWrapperTruth) – A DataWrapper that provides access to truth data.
Keyword Arguments: **kwargs (
dict) – Keyword arguments forDataWrapperTrackswithout data.- tracks (
-
get_all_detection_ids(*args, **kwargs)[source]¶ Returns the ids of all positives and false positives.
Returns: tuple containing: Return type: tuple
-
get_truth_track(*args, **kwargs)[source]¶ Returns the
Trackto the given truth_id.Parameters: truth_id (int or str) – we are using the truth_id to identify a certain track Keyword Arguments: cam_id (Optional int) – the cam to consider Returns: the track to the given truth_id Return type: Track
-
get_truth_tracks(*args, **kwargs)[source]¶ Returns an iterator over all the truth tracks.
Keyword Arguments: cam_id (Optional int) – restrict on tracks on this camera Returns: iterator over all the truth TracksReturn type: iterator
-
get_truthid(*args, **kwargs)[source]¶ Returns the truth id for the corresponding frame_object id or
None.Keyword Arguments: frame_object ( DetectionorTrackor id) – frame object or the id of theDetectionso extract the truth id fromReturns: the truth id for the frame_object or fp_idif frame_object is a false positiveReturn type: int or str
-