Skip to content

Expose a mutations_edge np array #3189

Closed

Description

@hyanwong

It's hard to efficiently get the parent of a mutation at the moment. But I think we calculate and store the edge associated with each mutation when initialising tree sequence. Could we provide fast array access to this, which would allow parents to be found easily. This is probably part of the "numba"-fying of tskit.

I'm hitting this because of a weird edge-case in compute_mutation_times when the parent and the child nodes of an edge are "next to" each other in time, to with FP accuracy. That function rounds up the "mean" mutation time - i.e. sets it to the parent time, which then fails TS checks. I really want to check for this and round down, but I can't find the parent node of a mutation efficiently to do this.

Activity

  1. benjeffery commented on Jun 6, 2025

    @benjeffery
    Member

    The edge for a mutation is stored in each tsk_mutation_t an array of which is stored in tsk_treeseq_t as site_mutations_mem (essentially a ragged array for mutations by site). I believe that this could be exposed by mapping a numpy structured array onto this memory, essentially providing the array for free. I'm sure there are some hairy details, but in principle, this should work.

  2. jeromekelleher commented on Jun 6, 2025

    @jeromekelleher
    Member

    I think the better thing would be to compute a numpy array for this when it's requested. The memory isn't worth saving. So,

    @property
    def mutations_edge(self):
        if self._mutations_edge is None:
                self._mutations_edge = self.ll_ts.mutations_edge
        return self._mutations_edge
    
    

    and in CPython we'd make a new numpy array and fill it in by iterating over the mutations. There's no point in getting into hair memory details here, we can keep the code simple and use the documented C API.

  3. jeromekelleher commented on Jun 6, 2025

    @jeromekelleher
    Member

    (Note, I think we'll end up doing something similar with sites_ancestral_state and mutations_derived_state as we'll need to compute a new Numpy StringDtype array.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions