Skip to content

Commit

Permalink
classify_nodes: account for bug (?) in numpy.isin
Browse files Browse the repository at this point in the history
  • Loading branch information
schlegelp committed Apr 7, 2024
1 parent 534ecd7 commit 77767a5
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions navis/graph/graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _generate_segments(x: 'core.NeuronObject',
endNodeIDs = sorted(endNodeIDs, key=lambda x: d.get(x, 0), reverse=True)

if config.use_igraph and x.igraph:
g: igraph.Graph = x.igraph
g: igraph.Graph = x.igraph # noqa
# Convert endNodeIDs to indices
id2ix = dict(zip(x.igraph.vs['node_id'], range(len(x.igraph.vs))))
endNodeIDs = [id2ix[n] for n in endNodeIDs]
Expand Down Expand Up @@ -151,7 +151,7 @@ def _connected_components(x: Union['core.TreeNeuron', 'core.MeshNeuron']) -> Lis
assert isinstance(x, (core.TreeNeuron, core.MeshNeuron))

if config.use_igraph and x.igraph:
G: igraph.Graph = x.igraph
G: igraph.Graph = x.igraph # noqa
# Get the vertex clustering
vc = G.components(mode='WEAK')
# Membership maps indices to connected components
Expand Down Expand Up @@ -207,7 +207,7 @@ def _break_segments(x: 'core.NeuronObject') -> list:
x: core.TreeNeuron

if x.igraph and config.use_igraph:
g: Union['igraph.Graph', 'nx.DiGraph'] = x.igraph
g: Union['igraph.Graph', 'nx.DiGraph'] = x.igraph # noqa
end = g.vs.select(_indegree=0).indices
branch = g.vs.select(_indegree_gt=1, _outdegree=1).indices
root = g.vs.select(_outdegree=0).indices
Expand Down Expand Up @@ -308,7 +308,7 @@ def _edge_count_to_root_old(x: 'core.TreeNeuron') -> dict:
"""
current_level: List[int]
g: Union['igraph.Graph', 'nx.DiGraph']
g: Union['igraph.Graph', 'nx.DiGraph'] # noqa
if x.igraph and config.use_igraph:
g = x.igraph
current_level = g.vs(_outdegree=0).indices
Expand Down Expand Up @@ -458,12 +458,23 @@ def classify_nodes(x: "core.NeuronObject", categorical=True, inplace: bool = Tru
# Note: I have tried to optimized the s**t out of this, i.e. every
# single line of code here has been tested for speed. Do not
# change anything unless you know what you're doing!

# Turns out that numpy.isin() recently started to complain if the
# node_ids are uint64 and the parent_ids are int64 (but strangely
# not with 32bit integers). If that's the case we have to convert
# the node_ids to int64.
node_ids = x.nodes.node_id.values
parent_ids = x.nodes.parent_id.values

if node_ids.dtype == np.uint64:
node_ids = node_ids.astype(np.int64)

cl = np.full(len(x.nodes), "slab", dtype="<U6")
cl[~np.isin(x.nodes.node_id, x.nodes.parent_id)] = "end"
cl[~np.isin(node_ids, parent_ids)] = "end"
bp = x.nodes.parent_id.value_counts()
bp = bp.index.values[bp.values > 1]
cl[np.isin(x.nodes.node_id.values, bp)] = "branch"
cl[x.nodes.parent_id.values < 0] = "root"
cl[np.isin(node_ids, bp)] = "branch"
cl[parent_ids < 0] = "root"
if categorical:
cl = pd.Categorical(cl, categories=["end", "branch", "root", "slab"], ordered=False)
x.nodes["type"] = cl
Expand Down Expand Up @@ -838,7 +849,7 @@ def dist_between(x: 'core.NeuronObject',
raise ValueError(f'Need a single TreeNeuron, got {len(x)}')

if isinstance(x, (core.TreeNeuron, core.MeshNeuron)):
G: Union['igraph.Graph',
G: Union['igraph.Graph', # noqa
'nx.DiGraph'] = x.igraph if (x.igraph and config.use_igraph) else x.graph
elif isinstance(x, nx.DiGraph):
G = x
Expand Down

0 comments on commit 77767a5

Please sign in to comment.