Skip to content

Cast ID arguments to int32 using to_np_int32 #192

Closed

Description

@jeromekelleher

Any place we are providing a list of IDs as input to a function we should use the to_np_int32 function defined in #185 to safely convert them.

These will be in the Tables API (node IDs, site IDs, etc), and where we compute stats (GNN, eg, takes lists of samples).

Note there are still a number of functions that don't use the numpy C API (e.g., Tree.pairwise_diversity and TreeSequence.trees) but Python lists instead, which are overdue being updated (#64).

Activity

  1. added this to the Python 0.1.6 milestone on Jun 11, 2019
  2. hyanwong commented on Jun 20, 2019

    @hyanwong
    Member

    I think we should generalise the casting function, so that it works for e.g. np.uint8 too (I've just hit this when inputting genotypes: [-1, 0] was invisibly converted to [255, 0], which was hard to track down). The alteration below seems a relatively trivial adjustment of

    def to_np_int32(int_array):
    (I added an extra bit of logic to allow casting from signed to unsigned, which we will need). However, I think the function should be taken out of tables.py and put somewhere more general. Any suggestions as to where?

    def safe_np_int_conversion(int_array, dtype=np.int32, copy=True):
        """
        A few functions require arrays of type np.int32. To allow passing standard numpy
        integer arrays (dtype=np.int64) we cast but check bounds to avoid wrap-around
        conversion errors (numpy doesn't seem to provide this functionality)
        """
        int_array = np.array(int_array)
        if int_array.size == 0:
            return int_array.astype(dtype, copy=copy)  # Allow empty arrays of any type
        try:
            return int_array.astype(dtype, casting='safe', copy=copy)
        except TypeError:
            bounds = np.iinfo(dtype)
            if np.all(int_array >= bounds.min) and np.all(int_array <= bounds.max):
                if int_array.dtype.kind == 'i' and np.dtype(dtype).kind == 'u':
                    # Allow casting from int to unsigned int, since we have checked bounds
                    casting = 'unsafe'
                else:
                    # Raise a TypeError when we try to convert from, e.g., a float.
                    casting = 'same_kind'
                return int_array.astype(dtype, casting=casting, copy=copy)
            else:
                raise OverflowError("Cannot convert safely to {} type".format(dtype))
    
  3. petrelharp commented on Jun 21, 2019

    @petrelharp
    Contributor

    Perhaps in a (new) util.py?

  4. hyanwong commented on Jun 21, 2019

    @hyanwong
    Member

    I will do this. Assign it to me.

  5. jeromekelleher commented on Jun 21, 2019

    @jeromekelleher
    MemberAuthor

    Thanks! Done/.

  6. jeromekelleher commented on Jun 24, 2019

    @jeromekelleher
    MemberAuthor

    Does #245 cover every usage of numpy int arrays as input @hyanwong?

  7. hyanwong commented on Jun 24, 2019

    @hyanwong
    Member

    As you may have seen, I have only written tests for np.int32, np.uint32, np.int8, np.uint8. I should probably expand this, which is easy to do.

    I think I have the generic logic correct, but it's not tested for every type.

  8. jeromekelleher commented on Jun 24, 2019

    @jeromekelleher
    MemberAuthor

    The function itself is good, no need to test it for other dtypes. Are we using it to convert all inputs into tskit (i.e., every tables API function, every stats input) is the question?

  9. jeromekelleher commented on Jul 3, 2019

    @jeromekelleher
    MemberAuthor

    The function itself is good, no need to test it for other dtypes. Are we using it to convert all inputs into tskit (i.e., every tables API function, every stats input) is the question?

    Where are we with this @hyanwong - are we filtering all integer array inputs through this function?

  10. hyanwong commented on Jul 3, 2019

    @hyanwong
    Member

    No, I haven't changed all the APIs to use this yet. Sorry - slipped off my radar. Is there a simple way to find all those methods that need converting?

  11. hyanwong commented on Jul 3, 2019

    @hyanwong
    Member

    I'm guessing they all have test cases, so I could add the tests first (providing arrays that I expect to be convertible and those that I expect to be non-convertible), and then sprinkle in the conversions until the tests pass.

  12. jeromekelleher commented on Jul 3, 2019

    @jeromekelleher
    MemberAuthor

    No, I haven't changed all the APIs to use this yet. Sorry - slipped off my radar. Is there a simple way to find all those methods that need converting?

    Not really - there's some leg work to be done going through the API (tables and stats APIs are the main user of numpy APIs.)

  13. jeromekelleher commented on Aug 13, 2019

    @jeromekelleher
    MemberAuthor

    Closed in #245

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions