Skip to content

Commit

Permalink
add utility function for generating parent indices
Browse files Browse the repository at this point in the history
  • Loading branch information
schlegelp committed Dec 2, 2023
1 parent 14f46c6 commit 6c920b3
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions fastcore/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import numpy as np


def node_indices(A, B):
"""For each node ID in A find its index in B.
Typically `A` will be parent IDs and `B` will be node IDs.
Negative IDs (= parents of root nodes) will be passed through.
Note that there is no check whether all IDs in A actually exist in B. If
an ID in A does not exist in B it gets a negative index (i.e. like roots).
"""
ix_dict = dict(zip(B, np.arange(len(B))))

return np.array([ix_dict.get(p, -1) for p in A])

0 comments on commit 6c920b3

Please sign in to comment.