Spaces:
Sleeping
Sleeping
File size: 5,346 Bytes
aff3c6f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | """Adapted from https://github.com/funkelab/motile/blob/05fc67f1763afe806f244d10210fa66daa3dca67/motile/track_graph.py.
MIT License
Copyright (c) 2023 Funke lab
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import logging
import networkx as nx
logger = logging.getLogger(__name__)
class TrackGraph(nx.DiGraph):
"""A :class:`networkx.DiGraph` of objects with positions in time and space,
and inter-frame edges between them.
Provides a few convenience methods for time series graphs in addition to
all the methods inherited from :class:`networkx.DiGraph`.
Args:
graph_data (optional):
Optional graph data to pass to the :class:`networkx.DiGraph`
constructor as ``incoming_graph_data``. This can be used to
populate a track graph with entries from a generic
``networkx`` graph.
frame_attribute (``string``, optional):
The name of the node attribute that corresponds to the frame (i.e.,
the time dimension) of the object. Defaults to ``'t'``.
"""
def __init__(self, graph_data=None, frame_attribute="t"):
super().__init__(incoming_graph_data=graph_data)
self.frame_attribute = frame_attribute
self._graph_changed = True
self._update_metadata()
def prev_edges(self, node):
"""Get all edges that point forward into ``node``."""
return self.in_edges(node)
def next_edges(self, node):
"""Get all edges that point forward out of ``node``."""
return self.out_edges(node)
def get_frames(self):
"""Get a tuple ``(t_begin, t_end)`` of the first and last frame
(exclusive) this track graph has nodes for.
"""
self._update_metadata()
return (self.t_begin, self.t_end)
def nodes_by_frame(self, t):
"""Get all nodes in frame ``t``."""
self._update_metadata()
if t not in self._nodes_by_frame:
return []
return self._nodes_by_frame[t]
def _update_metadata(self):
if not self._graph_changed:
return
self._graph_changed = False
if self.number_of_nodes() == 0:
self._nodes_by_frame = {}
self.t_begin = None
self.t_end = None
return
self._nodes_by_frame = {}
for node, data in self.nodes(data=True):
t = data[self.frame_attribute]
if t not in self._nodes_by_frame:
self._nodes_by_frame[t] = []
self._nodes_by_frame[t].append(node)
frames = self._nodes_by_frame.keys()
self.t_begin = min(frames)
self.t_end = max(frames) + 1
# ensure edges point forwards in time
for u, v in self.edges:
t_u = self.nodes[u][self.frame_attribute]
t_v = self.nodes[v][self.frame_attribute]
assert t_u < t_v, (
f"Edge ({u}, {v}) does not point forwards in time, but from "
f"frame {t_u} to {t_v}"
)
self._graph_changed = False
# wrappers around node/edge add/remove methods:
def add_node(self, n, **attr):
super().add_node(n, **attr)
self._graph_changed = True
def add_nodes_from(self, nodes, **attr):
super().add_nodes_from(nodes, **attr)
self._graph_changed = True
def remove_node(self, n):
super().remove_node(n)
self._graph_changed = True
def remove_nodes_from(self, nodes):
super().remove_nodes_from(nodes)
self._graph_changed = True
def add_edge(self, u, v, **attr):
super().add_edge(u, v, **attr)
self._graph_changed = True
def add_edges_from(self, ebunch_to_add, **attr):
super().add_edges_from(ebunch_to_add, **attr)
self._graph_changed = True
def add_weighted_edges_From(self, ebunch_to_add):
super().add_weighted_edges_From(ebunch_to_add)
self._graph_changed = True
def remove_edge(self, u, v):
super().remove_edge(u, v)
self._graph_changed = True
def update(self, edges, nodes):
super().update(edges, nodes)
self._graph_changed = True
def clear(self):
super().clear()
self._graph_changed = True
def clear_edges(self):
super().clear_edges()
self._graph_changed = True
|