Skip to content

Commit

Permalink
Add helper to auto layout track graph
Browse files Browse the repository at this point in the history
  • Loading branch information
edeno committed Oct 9, 2024
1 parent 81c0d5d commit e9a0ef3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions track_linearization/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# flake8: noqa
from track_linearization.core import get_linearized_position
from track_linearization.utils import (
get_auto_linear_edge_order_spacing,
make_actual_vs_linearized_position_movie,
make_track_graph,
plot_graph_as_1D,
Expand Down
40 changes: 40 additions & 0 deletions track_linearization/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Tuple

import matplotlib.animation as animation
import matplotlib.pyplot as plt
import networkx as nx
Expand Down Expand Up @@ -370,3 +372,41 @@ def _update_plot(time_ind):
fig, _update_plot, frames=n_time, interval=1000 / frame_rate, blit=True
)
line_ani.save(movie_name + ".mp4", writer=writer)


def get_auto_linear_edge_order_spacing(
track_graph: nx.Graph,
start_node: object = None,
spacing_between_unconnected_components=15.0,
) -> Tuple[np.ndarray, np.ndarray]:
"""Automatically determine the linear edge order and spacing
using a depth-first search starting from the `start_node`.
If adjacent edges are not connected, in the 1D representation
of the track graph, then the edges are separated by a fixed spacing.
Parameters
----------
track_graph : nx.Graph
start_node : object, optional
Start node of , by default None
spacing_between_unconnected_components : float, optional
Spacing between edges in 1D if not, by default 15.0
Returns
-------
linear_edge_order : np.ndarray
Linear order of edges
linear_edge_spacing : np.ndarray
Spacing between edges
"""
linear_edge_order = list(nx.traversal.edge_bfs(track_graph, source=start_node))
is_connected_component = ~(
np.abs(np.array(linear_edge_order)[:-1, 1] - np.array(linear_edge_order)[1:, 0])
> 0
)
linear_edge_spacing = (
~is_connected_component * spacing_between_unconnected_components
)

return linear_edge_order, linear_edge_spacing

0 comments on commit e9a0ef3

Please sign in to comment.