:orphan: :py:mod:`traccuracy._tracking_graph` ==================================== .. py:module:: traccuracy._tracking_graph Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: traccuracy._tracking_graph.NodeFlag traccuracy._tracking_graph.EdgeFlag traccuracy._tracking_graph.TrackingGraph Attributes ~~~~~~~~~~ .. autoapisummary:: traccuracy._tracking_graph.logger .. py:data:: logger .. py:class:: NodeFlag An enum containing standard flags that are used to annotate the nodes of a TrackingGraph. Note that the user specified frame and location attributes are also valid node attributes that will be stored on the graph and should not overlap with these values. Additionally, if a graph already has annotations using these strings before becoming a TrackingGraph, this will likely ruin metrics computation! .. py:attribute:: CTC_TRUE_POS :value: 'ctc_tp' .. py:attribute:: CTC_FALSE_POS :value: 'ctc_fp' .. py:attribute:: CTC_FALSE_NEG :value: 'ctc_fn' .. py:attribute:: NON_SPLIT :value: 'ns' .. py:attribute:: TP_DIV :value: 'tp_division' .. py:attribute:: TP_DIV_SKIP :value: 'tp_division_skip' .. py:attribute:: FP_DIV :value: 'fp_division' .. py:attribute:: FN_DIV :value: 'fn_division' .. py:attribute:: WC_DIV :value: 'wrong_child_division' .. py:attribute:: TRUE_POS :value: 'tp' .. py:attribute:: FALSE_POS :value: 'fp' .. py:attribute:: FALSE_NEG :value: 'fn' .. py:attribute:: MIN_BUFFER_CORRECT :value: 'min_buffer_correct' .. py:attribute:: MIN_BUFFER_CORRECT_SKIP :value: 'min_buffer_correct_skip' .. py:method:: has_value(value: str) -> bool :classmethod: Check if a value is one of the enum's values. This can be used to check if other graph annotation strings are colliding with our reserved strings. :param value: Check if the enum contains this value. :returns: True if the value is already in the enum's values, false otherwise :rtype: bool .. py:class:: EdgeFlag An enum containing standard flags that are used to annotate the edges of a TrackingGraph. If a graph already has annotations using these strings before becoming a TrackingGraph, this will likely ruin metrics computation! .. py:attribute:: TRUE_POS :value: 'tp' .. py:attribute:: CTC_FALSE_POS :value: 'ctc_fp' .. py:attribute:: CTC_FALSE_NEG :value: 'ctc_fn' .. py:attribute:: INTERTRACK_EDGE :value: 'intertrack_edge' .. py:attribute:: WRONG_SEMANTIC :value: 'wrong_semantic' .. py:attribute:: FALSE_POS :value: 'fp' .. py:attribute:: FALSE_NEG :value: 'fn' .. py:attribute:: SKIP_FALSE_POS :value: 'skip_fp' .. py:attribute:: SKIP_FALSE_NEG :value: 'skip_fn' .. py:attribute:: SKIP_TRUE_POS :value: 'skip_tp' .. py:class:: TrackingGraph(graph: networkx.DiGraph[collections.abc.Hashable], segmentation: numpy.ndarray | None = None, frame_key: str = 't', label_key: str | None = 'segmentation_id', location_keys: str | tuple[str, Ellipsis] | None = None, name: str | None = None, validate: bool = True, border_margin: float | None = None) A directed graph representing a tracking solution where edges go forward in time. Nodes represent cell detections and edges represent links between detections in the same track. Nodes in the graph must have an attribute that represents time frame (default to 't') and location (defaults to 'x' and 'y'). As in networkx, every cell must have a unique id, but these can be of any (hashable) type. Edges typically connect nodes across consecutive frames, but gap closing or frame skipping edges are valid, which connect nodes in frame t to nodes in frames beyond t+1. We provide common functions for accessing parts of the track graph, for example all nodes in a certain frame, or all previous or next edges for a given node. Additional functionality can be accessed by querying the stored networkx graph with native networkx functions. Currently it is assumed that the structure of the networkx graph as well as the time frame and location of each node is not mutated after construction, although non-spatiotemporal attributes of nodes and edges can be modified freely. .. attribute:: start_frame int, the first frame with a node in the graph .. attribute:: end_frame int, the end of the span of frames containing nodes (one frame after the last frame that contains a node) .. attribute:: nodes_by_frame dict of int -> node_id Maps from frames to all node ids in that frame .. attribute:: frame_key str The name of the node attribute that corresponds to the frame of the node. Defaults to "t". .. attribute:: location_keys tuple of str | str | None Key(s) used to access the location of the cell in space. .. py:property:: nodes :type: networkx.classes.reportviews.NodeView Get all the nodes in the graph, along with their attributes. :returns: Provides set-like operations on the nodes as well as node attribute lookup. :rtype: NodeView .. py:property:: edges :type: networkx.classes.reportviews.OutEdgeView Get all the edges in the graph, along with their attributes. :returns: Provides set-like operations on the edge-tuples as well as edge attribute lookup. :rtype: OutEdgeView .. py:method:: clear_annotations() -> None Resets a TrackingGraph by removing all traccuracy related annotations from the networkx graph Also resets any attributes on the TrackingGraph that are related to annotations .. py:method:: get_location(node_id: collections.abc.Hashable) -> list[float] | tuple[float] | numpy.ndarray Get the spatial location of the node with node_id using self.location_keys. :param node_id: The node_id to get the location of :type node_id: hashable :returns: A list of location values in the same order as self.location_keys :rtype: list of float :raises ValueError if location keys were not provided: .. py:method:: get_nodes_with_flag(flag: NodeFlag) -> set[collections.abc.Hashable] Get all nodes with specified NodeFlag set to True. :param flag: the node flag to query for :type flag: traccuracy.NodeFlag :returns: An iterable of node_ids which have the given flag and the value is True. :rtype: (List(hashable)) .. py:method:: get_edges_with_flag(flag: EdgeFlag) -> set[tuple[collections.abc.Hashable, collections.abc.Hashable]] Get all edges with specified EdgeFlag set to True. :param flag: the edge flag to query for :type flag: traccuracy.EdgeFlag :returns: An iterable of edge ids which have the given flag and the value is True. :rtype: (List(hashable)) .. py:method:: get_divisions() -> list[collections.abc.Hashable] Get all nodes that have at least two edges pointing to the next time frame :returns: a list of node ids for nodes that have more than one child :rtype: list of hashable .. py:method:: get_merges() -> list[collections.abc.Hashable] Get all nodes that have at least two incoming edges from the previous time frame :returns: a list of node ids for nodes that have more than one parent :rtype: list of hashable .. py:method:: set_flag_on_node(_id: collections.abc.Hashable, flag: NodeFlag, value: bool = True) -> None Set an attribute flag for a single node. If the id is not found in the graph, a KeyError will be raised. If the flag already exists, the existing value will be overwritten. :param _id: The node id on which to set the flag. :type _id: Hashable :param flag: The node flag to set. Must be of type NodeFlag - you may not not pass strings, even if they are included in the NodeFlag enum values. :type flag: traccuracy.NodeFlag :param value: Flags can only be set to True or False. Defaults to True. :type value: bool, optional :raises KeyError if the provided id is not in the graph.: :raises ValueError if the provided flag is not a NodeFlag: .. py:method:: remove_flag_from_node(_id: collections.abc.Hashable, flag: NodeFlag) -> None Removes a flag from a node :param _id: The node id for which to discard the flag. :type _id: Hashable :param flag: The node flag to discard. Must be of type NodeFlag - you may not not pass strings, even if they are included in the NodeFlag enum values. :type flag: NodeFlag :raises KeyError if the flag is not present on the node.: .. py:method:: set_flag_on_all_nodes(flag: NodeFlag, value: bool = True) -> None Set an attribute flag for all nodes in the graph. If the flag already exists, the existing values will be overwritten. :param flag: The node flag to set. Must be of type NodeFlag - you may not not pass strings, even if they are included in the NodeFlag enum values. :type flag: traccuracy.NodeFlag :param value: Flags can only be set to True or False. Defaults to True. :type value: bool, optional :raises ValueError if the provided flag is not a NodeFlag.: .. py:method:: set_flag_on_edge(_id: tuple[collections.abc.Hashable, collections.abc.Hashable], flag: EdgeFlag, value: bool = True) -> None Set an attribute flag for an edge. If the flag already exists, the existing value will be overwritten. :param ids: The edge id or list of edge ids to set the attribute for. Edge ids are a 2-tuple of node ids. :type ids: tuple[Hashable] :param flag: The edge flag to set. Must be of type EdgeFlag - you may not pass strings, even if they are included in the EdgeFlag enum values. :type flag: traccuracy.EdgeFlag :param value: Flags can only be set to True or False. Defaults to True. :type value: bool :raises KeyError if edge with _id not in graph.: .. py:method:: remove_flag_from_edge(_id: tuple[collections.abc.Hashable, collections.abc.Hashable], flag: EdgeFlag) -> None Removes flag from a given edge :param ids: The edge id or list of edge ids to discard the attribute for. Edge ids are a 2-tuple of node ids. :type ids: tuple[Hashable] :param flag: The edge flag to discard. Must be of type EdgeFlag - you may not pass strings, even if they are included in the EdgeFlag enum values. :type flag: traccuracy.EdgeFlag :raises KeyError if edge with _id not in graph.: :raises KeyError if flag not present on edge: .. py:method:: set_flag_on_all_edges(flag: EdgeFlag, value: bool = True) -> None Set an attribute flag for all edges in the graph. If the flag already exists, the existing values will be overwritten. :param flag: The edge flag to set. Must be of type EdgeFlag - you may not not pass strings, even if they are included in the EdgeFlag enum values. :type flag: traccuracy.EdgeFlag :param value: Flags can only be set to True or False. Defaults to True. :type value: bool, optional :raises ValueError if the provided flag is not an EdgeFlag.: .. py:method:: get_lineages() -> list[networkx.DiGraph] Gets a list of new nx.DiGraph objects containing all lineages of the current graph. Lineage is defined as all connected components. .. py:method:: get_tracklets(include_division_edges: bool = False) -> list[networkx.DiGraph] Gets a list of new nx.DiGraph objects containing all tracklets of the current graph. Tracklet is defined as all connected components between divisions (daughter to next parent). Tracklets can also start or end with a non-dividing cell. :param include_division_edges: If True, include edges at division. :type include_division_edges: bool, optional .. py:method:: get_skip_edges() -> set[tuple[collections.abc.Hashable, collections.abc.Hashable]] Get all edges that skip one or more frames. :returns: A set of edges that skip one or more frames. Each edge is represented as a tuple of (source_node, target_node). :rtype: set of tuples .. py:method:: is_skip_edge(edge: tuple[collections.abc.Hashable, collections.abc.Hashable]) -> bool