Skip to content

simplify() can't cope with an np.array of standard integers #8

Closed

Description

@hyanwong

Slightly surprisingly, I can't pass a numpy array of type np.int to ts.simplify() on my machine, as it defaults to np.int64, and is then downcast somewhere in simplify, causing the error:

TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe'

Activity

  1. petrelharp commented on Nov 17, 2018

    @petrelharp
    Contributor

    To which argument are you giving the int64s? Can you give a MWE?

  2. hyanwong commented on Nov 17, 2018

    @hyanwong
    MemberAuthor

    It's to do with my post on the SLiM mailing list just now. I wanted to select all the "samples" that were true samples (i.e. at time==0), so I did

    samples = [0,1,2,3,4,5]
    terminal_nodes = [n.id for n in ts.nodes() if n.is_sample() and n.time==0]
    terminal_samples = np.array(terminal_nodes)[samples] #or with dtype=np.int
    ts.simplify(terminal_samples)
    

    which produces the error

  3. petrelharp commented on Nov 19, 2018

    @petrelharp
    Contributor

    I see, thanks. It's an msprime issue, probably fixable around this point in the code, but I am not familiar enough with the python-C relationship to know quite what to do. Here's a MWE.

    ts = msprime.simulate(10)
    ts.simplify([0,1,2,3])  # WORKS
    ts.simplify(np.array([0,1,2,3]))  # FAILS
    
  4. hyanwong commented on Nov 20, 2018

    @hyanwong
    MemberAuthor

    Thanks. Will take it up with @jeromekelleher . But I thought this was the place to report msprime bugs?

  5. petrelharp commented on Nov 20, 2018

    @petrelharp
    Contributor

    Er, yes. I got my wires crossed. =)

  6. jeromekelleher commented on Nov 20, 2018

    @jeromekelleher
    Member

    I don't think there's an easy way to fix this from the C side, as we really do have to have an array of 32 bit ints. The numpy APIs are somewhat mysterious on how they convert things under the hood, but this is the behaviour it has. We probably could have a simple check in Python which would create a new array of i32s if the input array cannot be cast safely into the required type. We should really do this universally across all functions that take an array of IDs as an argument though, which makes it a bit more work.

    It's not a hard change to make though, and it would be a definite usability improvement.

  7. jeromekelleher commented on Nov 23, 2018

    @jeromekelleher
    Member

    What we need to do is something like:

    samples = np.array(samples).astype(np.int32)
    

    I think this should fall through efficiently when the input is in the correct form, and give us what we need in the other cases.

  8. hyanwong commented on Nov 23, 2018

    @hyanwong
    MemberAuthor

    Fine. Just to be pernickety, I wondered if this does bounds checking if e.g. it is an int64 array with numbers > 4294967296. Not that I can ever foresee this being a sensible number, but it would be as well to catch e.g. with an assert.

    (I had a look in the docs, but it was unclear to me if any such check was done by numpy)

  9. petrelharp commented on Nov 23, 2018

    @petrelharp
    Contributor
    >>> np.array([4294967296, 1 + 4294967296]).astype(np.int32)
    array([0, 1], dtype=int32)
    
  10. jeromekelleher commented on Nov 25, 2018

    @jeromekelleher
    Member

    https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html

    So, the "safe" approach is what the C code is doing, which prevents any casting. There doesn't seem to be an option to throw an error when a particular value can't be cast (presumably for performance). I guess we have a choice here between potentially obscure bugs when someone puts in large values and they get unexpectedly rounded or some inconvience when we require the user to call .astype(np.int32) in client code.

    Probably the more user friendly thing to do is to the unsafe casts within tskit.

  11. petrelharp commented on Dec 7, 2018

    @petrelharp
    Contributor

    Another place this issue has appeared, and is annoying:

    tables = ts.dump_tables()
    flags = tables.nodes.flags
    # remove all SAMPLE flags
    flags = np.bitwise_or(flags, (~ msprime.NODE_IS_SAMPLE))
    flags[samples] = np.bitwise_or(flags[samples], msprime.NODE_IS_SAMPLE)
    tables.nodes.set_columns(flags=flags, population=tables.nodes.population,
            individual=tables.nodes.individual, time=tables.nodes.time,
            metadata=tables.nodes.metadata, 
            metadata_offset=tables.nodes.metadata_offset)
    

    producing

    Traceback (most recent call last):
      File "<stdin>", line 4, in <module>
      File "/home/peter/.local/lib/python3.6/site-packages/msprime-0.6.2-py3.6-linux-x86_64.egg/msprime/tables.py", line 464, in set_columns
        metadata_offset=metadata_offset)
    TypeError: Cannot cast array data from dtype('int64') to dtype('uint32') according to the rule 'safe'
    

    A workaround is instead of msprime.NODE_IS_SAMPLE to use

    NODE_IS_SAMPLE = np.array(msprime.NODE_IS_SAMPLE, dtype='uint32')
    
  12. added 2 commits that reference this issue on May 28, 2019
  13. jeromekelleher commented on Jun 4, 2019

    @jeromekelleher
    Member

    Closed in #185.

  14. jeromekelleher commented on Jun 4, 2019

    @jeromekelleher
    Member

    See #192 for planned wider applications of the casting function.

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