Skip to content

Commit

Permalink
refactor: use networkx traversal instead of homebrew
Browse files Browse the repository at this point in the history
  • Loading branch information
danielolsen committed Apr 13, 2022
1 parent e6147f7 commit 867a7d2
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions prereise/gather/griddata/hifld/data_access/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,26 +261,18 @@ def _generate_linear_spanning_tree(segments):
# The minimum spanning tree wasn't good, go to the next iteration
pass

# Identify an endpoint of the minimum spanning tree
# Identify an endpoint of the linear minimum spanning tree
node_degrees = dict(nx.degree(tree))
endpoint = min(k for k, v in node_degrees.items() if v == 1)
last_endpoint = None
# Traverse the tree, adding segments in order
joined_segments = []
while True:
next_endpoints = list(set(tree[endpoint]) - {last_endpoint})
assert len(next_endpoints) == 1
next_endpoint = next_endpoints[0]
(seg1, which1), (seg2, _) = endpoint, next_endpoints[0]
traversal = nx.algorithms.traversal.dfs_edges(tree, source=endpoint)
for (seg1, which1), (seg2, _) in traversal:
if seg1 == seg2:
if which1 == "start":
joined_segments += linear_segments[seg1]
else:
joined_segments += linear_segments[seg1][::-1]
if node_degrees[next_endpoint] == 1:
break
last_endpoint = endpoint
endpoint = next_endpoint

return joined_segments

Expand Down

0 comments on commit 867a7d2

Please sign in to comment.