You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Beyond iterating through trees, you may need to traverse the ARG vertically. The {meth}`~NumbaTreeSequence.child_index`method and {class}`ParentIndex` class provide efficient access to parent-child relationships in the edge table within `numba.njit` functions.
261
+
Beyond iterating through trees, you may need to traverse the ARG vertically. The {meth}`~NumbaTreeSequence.child_index` and {meth}`~NumbaTreeSequence.parent_index` methods provide efficient access to parent-child relationships in the edge table within `numba.njit` functions.
259
262
260
-
The {meth}`~NumbaTreeSequence.child_index` method returns an array that allows you to efficiently find all edges where a given node is the parent. Since edges are already sorted by parent in the tskit data model, this is implemented using simple range indexing. For any node `u`, the returned array `child_index[u]` gives a tuple of the start and stop indices in the tskit edge table where node `u` is the parent.
263
+
The {meth}`~NumbaTreeSequence.child_index` method returns an array that allows you to efficiently find all edges where a given node is the parent. Since edges are already sorted by parent in the tskit data model, this is implemented using simple range indexing. For any node `u`, the returned array `child_index[u]` gives a tuple of the start and stop indices in the tskit edge table where node `u` is the parent. The index is calculated on each call to `child_index()` so should be called once.
261
264
262
-
The {meth}`~NumbaTreeSequence.parent_index` method creates a {class}`ParentIndex` that allows you to efficiently find all edges where a given node is the child. Since edges are not sorted by child in the edge table, the returned class contains a custom index that sorts edge IDs by child node (and then by left coordinate). For any node `u`, `parent_index.index_range[u]` gives a tuple of the start and stop indices in the `edge_index` array, and `parent_index.edge_index[start:stop]` gives the actual tskit edge IDs.
265
+
The {meth}`~NumbaTreeSequence.parent_index` method creates a {class}`ParentIndex` that allows you to efficiently find all edges where a given node is the child. Since edges are not sorted by child in the edge table, the returned class contains a custom index that sorts edge IDs by child node (and then by left coordinate). For any node `u`, `parent_index.index_range[u]` gives a tuple of the start and stop indices in the `parent_index.edge_index` array, and `parent_index.edge_index[start:stop]` gives the actual tskit edge IDs.
263
266
264
267
Both can be obtained from a {class}`NumbaTreeSequence`:
265
268
@@ -268,20 +271,22 @@ Both can be obtained from a {class}`NumbaTreeSequence`:
268
271
child_index = numba_ts.child_index()
269
272
parent_index = numba_ts.parent_index()
270
273
271
-
# Example: find all edges where node 5 is the parent
274
+
# Example: find all left coordinates of edges where node 5 is the parent
272
275
start, stop = child_index[5]
273
-
print(f"Node 5 has {stop - start} child edges")
276
+
left_coords = numba_ts.edges_left[start:stop]
277
+
print(left_coords)
274
278
275
-
# Example: find all edges where node 3 is the child
279
+
# Example: find all right coordinates of edges where node 3 is the child
276
280
start, stop = parent_index.index_range[3]
277
-
print(f"Node 3 appears as child in {stop - start} edges")
281
+
right_coords = numba_ts.edges_right[start:stop]
282
+
print(right_coords)
278
283
```
279
284
280
285
These indexes enable efficient algorithms that need to traverse parent-child relationships in the ARG, such as computing descendant sets, ancestral paths, or subtree properties.
281
286
282
287
### Example - descendant span calculation
283
288
284
-
Here's an example of using the ARG traversal classes to calculate the total sequence length over which each node descends from a specified node:
289
+
Here's an example of using the ARG traversal indexes to calculate the total sequence length over which each node descends from a specified node:
0 commit comments