Skip to content

Numpy access to ancestral and derived state columns in TreeSequence #2632

Closed

Description

@jeromekelleher

It would be really nice if we could access the ancestral and derived states as numpy string arrays on the tree sequence like

ts.mutations_derived_state
ts.sites_ancestral_state

which return the actual string states (rather than bytes and offsets, like the Tables API).

In the common single-allele case, we can easily do this:

ancestral_state = tables.sites.ancestral_state.view("S1")
Out[64]: array([b'T', b'G', b'A', ..., b'T', b'T', b'A'], dtype='|S1')

However, this is a byte array and "not recommended". I guess we should try to return the values as proper numpy str types?

Can easily do this but it makes a copy (I assume?)

derived_state.astype(str)
Out[63]:
array(['T', 'G', 'A', ..., 'T', 'T', 'A'], dtype='<U1')

I haven't managed to make a direct view of the memory that gets treated as a unicode string.

As a first pass we could just error out of the states aren't all single chars (ascii chars even), and I guess we could just copy the data in a way that's numpy string friendly when we have variable length alleles? I guess it would be good to know if this is possible before painting ourselves into a corner.

Related: #2631

Activity

  1. added
    enhancement
    Python API
    on Nov 8, 2022
  2. benjeffery commented on Nov 8, 2022

    @benjeffery
    Member

    tables.sites.ancestral_state.view("S1").astype(str, copy=False) works for me, and I think avoids the copy?

  3. jeromekelleher commented on Nov 9, 2022

    @jeromekelleher
    MemberAuthor

    No, this doesn't work unfortunately - I think copy=False is a request that numpy does its best to fulfil, when possible. I just tried it out there and the OWNDATA flag is false when you do .astype(str, copy=False).

    I don't get why the view dtype=U1 doesn't work though - aren't these UTF-8 encoded single byte strings?

  4. benjeffery commented on Nov 17, 2022

    @benjeffery
    Member

    I don't get why the view dtype=U1 doesn't work though - aren't these UTF-8 encoded single byte strings?

    numpy.dtype("U1").itemsize is 4 as there has to be room for the largest, 4-byte unicode character.

    numpy.dtype("S1").itemsize is 1. So I don't think we can get to a unicode dtype without a copy or some low-level hacking.

  5. moved this from Small things to Bigger things in Accessible Issueson Apr 30, 2025
  6. benjeffery commented on Jun 9, 2025

    @benjeffery
    Member

    Related to #2426

  7. jeromekelleher commented on Jun 10, 2025

    @jeromekelleher
    MemberAuthor

    Note that the current plan for this is to make a numpy stringdype in C

  8. benjeffery commented on Jun 26, 2025

    @benjeffery
    Member

    Closed in #3228

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

Metadata

Metadata

Labels

Python APIenhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions