Skip to content

Commit

Permalink
_generate_segments: do not ignore isolated nodes (match fastcore)
Browse files Browse the repository at this point in the history
  • Loading branch information
schlegelp committed Jan 5, 2025
1 parent 2b55df3 commit 65e7893
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions navis/graph/graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def _generate_segments(
) -> Union[list, Tuple[list, list]]:
"""Generate segments maximizing segment lengths.
Isolated nodes will be included as segments of length 0.
Parameters
----------
x : TreeNeuron | NeuronList
Expand Down Expand Up @@ -108,6 +110,7 @@ def _generate_segments(
x.nodes.node_id.values, x.nodes.parent_id.values, weights=weight
)

# Find leaf nodes and sort by distance to root
d = dist_to_root(x, igraph_indices=False, weight=weight)
endNodeIDs = x.nodes[x.nodes.type == "end"].node_id.values
endNodeIDs = sorted(endNodeIDs, key=lambda x: d.get(x, 0), reverse=True)
Expand Down Expand Up @@ -147,6 +150,12 @@ def _generate_segments(
lengths = [d[s[0]] - d[s[-1]] for s in sequences]
sequences = [x for _, x in sorted(zip(lengths, sequences), reverse=True)]

# Isolated nodes would not be included in the sequences(because they are treated
# as roots, not leafs. Let's add them manually here.
for node in nx.isolates(x.graph):
sequences.append([node])
lengths.append(0)

if return_lengths:
return sequences, sorted(lengths, reverse=True)
else:
Expand Down

0 comments on commit 65e7893

Please sign in to comment.