From 65e789369cdcfd10258c0399d581833f8af06ae0 Mon Sep 17 00:00:00 2001 From: Philipp Schlegel Date: Sun, 5 Jan 2025 12:54:47 +0000 Subject: [PATCH 1/2] _generate_segments: do not ignore isolated nodes (match fastcore) --- navis/graph/graph_utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/navis/graph/graph_utils.py b/navis/graph/graph_utils.py index 958a095b..2ab1e31f 100644 --- a/navis/graph/graph_utils.py +++ b/navis/graph/graph_utils.py @@ -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 @@ -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) @@ -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: From be82d6d318b34f30758510a85884b8559ccf61a5 Mon Sep 17 00:00:00 2001 From: Philipp Schlegel Date: Sun, 5 Jan 2025 13:00:04 +0000 Subject: [PATCH 2/2] _generate_segments: return list of arrays in vanilla implementation --- navis/graph/graph_utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/navis/graph/graph_utils.py b/navis/graph/graph_utils.py index 2ab1e31f..ea93d19a 100644 --- a/navis/graph/graph_utils.py +++ b/navis/graph/graph_utils.py @@ -150,14 +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([node]) + 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