Skip to content

Commit

Permalink
Merge pull request #6 from funkelab/tracks_layer_update
Browse files Browse the repository at this point in the history
Use single attribute for location when making tracks layer
  • Loading branch information
cmalinmayor authored Apr 3, 2024
2 parents be7b3e9 + 304792a commit 7820cca
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/motile_toolbox/visualization/napari_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import networkx as nx
import numpy as np

from motile_toolbox.candidate_graph import NodeAttr


def assign_tracklet_ids(graph: nx.DiGraph) -> nx.DiGraph:
"""Add a tracklet_id attribute to a graph by removing division edges,
Expand Down Expand Up @@ -36,17 +38,17 @@ def assign_tracklet_ids(graph: nx.DiGraph) -> nx.DiGraph:


def to_napari_tracks_layer(
graph, frame_key="t", location_keys=("y", "x"), properties=()
graph, frame_key=NodeAttr.TIME.value, location_key=NodeAttr.POS.value, properties=()
):
"""Function to take a networkx graph and return the data needed to add to
a napari tracks layer.
Args:
graph (nx.DiGraph): _description_
frame_key (str, optional): Key in graph attributes containing time frame.
Defaults to "t".
location_keys (tuple, optional): Keys in graph node attributes containing
location. Should be in order: (Z), Y, X. Defaults to ("y", "x").
Defaults to NodeAttr.TIME.value.
location_key (str, optional): Key in graph node attributes containing
location. Defaults to NodeAttr.POS.value.
properties (tuple, optional): Keys in graph node attributes to add
to the visualization layer. Defaults to (). NOTE: not working now :(
Expand All @@ -65,13 +67,16 @@ def to_napari_tracks_layer(
case of track splitting, or more than one (the track has multiple
parents, but only one child) in the case of track merging.
"""
napari_data = np.zeros((graph.number_of_nodes(), len(location_keys) + 2))
for _, loc in graph.nodes(data=location_key):
ndim = len(loc)
break
napari_data = np.zeros((graph.number_of_nodes(), ndim + 2))
napari_properties = {prop: np.zeros(graph.number_of_nodes()) for prop in properties}
napari_edges = {}
graph, intertrack_edges = assign_tracklet_ids(graph)
for index, node in enumerate(graph.nodes(data=True)):
node_id, data = node
location = [data[loc_key] for loc_key in location_keys]
location = data[location_key]
napari_data[index] = [data["tracklet_id"], data[frame_key], *location]
for prop in properties:
if prop in data:
Expand Down

0 comments on commit 7820cca

Please sign in to comment.