Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match behavior between vanilla and fastcore generation of skeleton segments #180

Merged
merged 2 commits into from
Jan 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion 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,8 +150,17 @@ 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)]

# Turn into list of arrays
sequences = [np.array(s) for s in sequences]

# 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(np.array([node]))
lengths.append(0)

if return_lengths:
return sequences, sorted(lengths, reverse=True)
return sequences, np.array(sorted(lengths, reverse=True))
else:
return sequences

Expand Down
Loading