Fragment#

Warning

The code reference is a work in progress and may contain inconsistencies.

class idtrackerai.Fragment(fragment_identifier: int, start_frame: int, end_frame: int, images: list[int] | ndarray, centroids: list[tuple[float, float]], episodes: list[int] | ndarray, is_an_individual: bool, exclusive_roi: int)[source]#

A collection of Blob s that belong to the same animal or to the same crossing.

The Fragment class represents a sequence of images (or blobs) that are believed to belong to the same animal or to a group of animals that are crossing paths. It contains various attributes and methods to manage and analyze these sequences, including their positions, velocities, identities, and other properties.

P1_vector: ndarray#

P1 probability of each of the possible identities, shape (n_animals,). See also set_P1_from_frequencies()

coexisting_individual_fragments: Sequence[Fragment]#

Individual fragments (i.e. not crossings, where two or more animals are touching) coexisting in time with self. Doesn’t include self.

loaded_images: ndarray#

Fragment’s actual images, in uint8. Intended to be used outside idtrackerai

groundtruth_identity: int#

Ground truth identity assigned externally after validating

acceptable_for_training: bool | None = None#

Boolean to indicate that the fragment was identified sufficiently well and can in principle be used for training. See also the accumulation_manager.py module.

temporary_id: int | None = None#

Temporary identity assigned to the fragment during the cascade of training and identification protocols. From 0 to n_animals-1

accumulable: bool | None = None#

Boolean indicating whether the fragment can be accumulated, i.e. it can potentially be used for training.

is_in_a_global_fragment: bool = False#

Indicates whether the fragment is part of a global fragment

certainty: float = 0.0#

Certainty of the identification, see set_identification_statistics()

P2_vector: ndarray | None = None#

P2 probability of each of the possible identities. See also compute_P2_vector()

identity: int | None = None#

Identity assigned to the fragment during the cascade of training and identification protocols or during the residual identification. From 1 to n_animals

non_consistent: bool = False#

Boolean indicating whether the fragment identity is inconsistent with (duplicated in) its coexisting fragments

ambiguous_identities: ndarray | None = None#

Identities that would be ambiguously assigned during the residual identification process. See also the assigner.py module

used_for_training: bool = False#

Boolean indicating whether the images in the fragment were used to train the identification network during the cascade of training and identification protocols. See also the accumulation_manager.py module.

accumulation_step: int | None = None#

Integer indicating the accumulation step at which the fragment was accumulated. See also the accumulation_manager.py module.

identities_corrected_closing_gaps: list[int] | None = None#

Identities of the fragment assigned during the interpolation of the gaps produced by the crossing fragments. See also the assign_them_all.py module.

identity_corrected_solving_jumps: int | None = None#

Identity of the fragment assigned during the correction of impossible (unrealistic) velocity jumps in the trajectories. See also the correct_impossible_jumps.py module.

identity_is_fixed: bool = False#

Boolean indicating whether the identity is fixed and cannot be modified during the postprocessing. This attribute is given during the residual identification (see assigner.py module)

accumulated_globally: bool = False#

Boolean indicating whether the fragment was accumulated in a global accumulation step of the cascade of training and identification protocols. See also the accumulation_manager.py module.

accumulated_partially: bool = False#

Boolean indicating whether the fragment was accumulated in a partial accumulation step of the cascade of training and identification protocols. See also the accumulation_manager.py module.

forced_crossing: bool = False#

Indicates if the crossing attribute has been forced by set_individual_with_identity_0_as_crossings()

__init__(fragment_identifier: int, start_frame: int, end_frame: int, images: list[int] | ndarray, centroids: list[tuple[float, float]], episodes: list[int] | ndarray, is_an_individual: bool, exclusive_roi: int) → None[source]#
images: ndarray#

Indices of Fragment’s images in the hdf5 files

episodes: ndarray#

Episode of each of the Fragment’s images. It determines the hdf5 file where the image is stored.

exclusive_roi: int = -1#

Exclusive ROI where the fragment belongs to. -1 for disabled exclusive ROIs

frame_by_frame_velocity: ndarray#

Distance travelled by the blob’s centroid between consecutive frames (pixels per frame), shape (n_images - 1,). [0] for single-image fragments

start_position: tuple[float, float]#

X and Y position of the blob’s centroid at the start of the fragment

end_position: tuple[float, float]#

X and Y position of the blob’s centroid at the end of the fragment

__str__() → str[source]#

Return str(self).

property distance_travelled: float#

The distance traveled by the individual in the fragment. It is based on the position of the centroids in consecutive images.

reset(roll_back_to: Literal['fragmentation', 'accumulation'], number_of_animals: int) → None[source]#

Reset attributes of the fragment to a specific part of the algorithm.

Parameters:
  • roll_back_to (str) – Reset all the attributes computed after the specified step: ‘fragmentation’ or ‘accumulation’

  • number_of_animals (int) – Number of animals, used to reset the P1 vector

property assigned_identities: list[int] | list[int | None]#

Assigned identities (list) by the algorithm considering the identification process and the postprocessing steps (correction of impossible velocity jumps and interpolation of crossings).

The fragment can have multiple identities if it is a crossing fragment.

property n_images: int[source]#

Number of images (or blobs) in the fragment.

property is_certain: bool#

Whether the fragment is certain enough to be accumulated.

property has_enough_accumulated_coexisting_fragments: bool#

Whether at least half of the coexisting individual fragments have already been used for training.

This property is used during the partial accumulation. See also the accumulation_manager.py module.

__getstate__() → dict[str, Any][source]#

Helper for pickle.

compute_border_velocity(other: Fragment | None) → float | None[source]#

Velocity necessary to cover the space between two fragments.

Computed as the distance between the centroids at the closest borders of both fragments. Note that it is not divided by the number of frames that separate self and other.

Parameters:

other (Fragment | None) – Another fragment

Returns:

The speed at which an individual should travel to be present in both self and other fragments. None if other is None.

Return type:

float | None

coexist_with(other: Fragment) → bool[source]#

Boolean indicating whether self coexists in time with another fragment.

Parameters:

other (Fragment) – A second fragment

Returns:

True if self and other coexist in time in at least one frame.

Return type:

bool

is_inconsistent_with_coexistent_fragments(temporary_id) → bool[source]#

Check whether a temporary identity is already assigned to any of the fragments coexisting (in frame) with self.

Parameters:

temporary_id (int) – Temporary identity to check.

Returns:

True if identifying self with temporary_id would cause a duplication of identities.

Return type:

bool

set_identification_statistics(predictions_per_img: ndarray, probabilities_per_img: ndarray, n_animals: int) → None[source]#

Computes the statistics necessary for the identification of the fragment (P1_vector and certainty).

Parameters:
  • predictions_per_img (numpy array) – Predicted identity (from 1 to n_animals) of each image, shape (number_of_images_in_fragment,)

  • probabilities_per_img (numpy array) – Probability of the predicted identity of each image, shape (number_of_images_in_fragment,)

  • n_animals (int) – Number of animals.

assign_identity(number_of_animals: int, id_to_roi: list[int] | ndarray) → None[source]#

Assigns the identity to the fragment by considering the fragments coexisting with it (through its P2 vector).

The identity is set to 0 if the P2 vector is ambiguous or if the most likely identity belongs to a different exclusive ROI. If the certainty of the identification is high enough it sets the identity of the fragment as fixed and it won’t be modified during the postprocessing.

compute_P2_vector(number_of_animals: int) → None[source]#

Computes the P2_vector of the fragment.

P2 combines the fragment’s P1 vector with the P1 vectors of its coexisting individual fragments, penalizing the identities that are likely to belong to them. It is all zeros if every identity is excluded.

property certainty_P2: float[source]#

the ratio between its largest and second largest values

Type:

Certainty of the identity following the P2 vector

set_P1_from_frequencies(frequencies: ndarray) → None[source]#

Given the frequencies of an individual fragment, computes the P1 vector.

P1 is the softmax of the frequencies with base 2 for each identity.

Parameters:

frequencies (numpy array) – Number of images assigned to each of the possible identities

get_neighbour_fragment(fragments: Sequence[Fragment], scope: Literal['to_the_past', 'to_the_future']) → Fragment | None[source]#

If it exists, gets the individual fragment with the same assigned identity as self that starts right after self ends, or ends right before self starts (end frames are exclusive).

Parameters:
  • fragments (Sequence[Fragment]) – List of all the fragments in the video

  • scope (Literal["to_the_past", "to_the_future"]) – If “to_the_future” looks for the consecutive fragment wrt self, if “to_the_past” looks for the fragment that precedes self

Returns:

The neighbour fragment with respect to self in the direction specified by scope if it exists. Otherwise None

Return type:

Fragment | None

set_partially_or_globally_accumulated(accumulation_strategy) → None[source]#

Sets accumulated_globally and accumulated_partially according to accumulation_strategy.

Parameters:

accumulation_strategy (Literal["global", "partial"]) – Can be “global” or “partial”

property used_for_pretraining#

Used for pretraining

Deprecated since version 6.0.0.

compute_identification_statistics(*args)[source]#

Compute identification statistics

Deprecated since version 6.0.0: Use set_identification_statistics() instead

static compute_median_softmax()[source]#

Compute median softmax

Deprecated since version 6.0.0: This method is included in set_identification_statistics()

set_certainty_of_individual_fragment()[source]#

Set certainty of individual fragment

Deprecated since version 6.0.0: This method is included in set_identification_statistics()

property properties#

Properties

Deprecated since version 6.0.0: Use summary() instead