Skip to content

Finding span of a node across trees (and average arity) #2718

Answered by jeromekelleher
hyanwong asked this question in Q&A
Discussion options

You must be logged in to vote

I think this does it:

import msprime
import numpy as np

def span_definition(ts):
    node_spans = np.zeros(ts.num_nodes)
    for tree in ts.trees():
        for u in tree.nodes():
            node_spans[u] += tree.span
    return node_spans

def span(ts):
    num_children = np.zeros(ts.num_nodes, dtype=np.int32)
    span_start = np.zeros(ts.num_nodes)
    node_span = np.zeros(ts.num_nodes)
    
    for interval, edges_out, edges_in in ts.edge_diffs(include_terminal=True):
        for edge in edges_out:
            num_children[edge.parent] -= 1
            if num_children[edge.parent] == 0:
                node_span[edge.parent] += interval.left - span_start[edge.parent]
        for edge in

Replies: 2 comments 7 replies

Comment options

hyanwong
Mar 1, 2023
Maintainer Author

You must be logged in to vote
2 replies
@hyanwong
Comment options

hyanwong Mar 1, 2023
Maintainer Author

@hyanwong
Comment options

hyanwong Mar 1, 2023
Maintainer Author

Comment options

You must be logged in to vote
5 replies
@hyanwong
Comment options

hyanwong Mar 2, 2023
Maintainer Author

@jeromekelleher
Comment options

@hyanwong
Comment options

hyanwong Mar 2, 2023
Maintainer Author

@jeromekelleher
Comment options

@hyanwong
Comment options

hyanwong Mar 2, 2023
Maintainer Author

Answer selected by hyanwong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants