You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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
(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))
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?
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?
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?
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.
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.)
Activity
hyanwong commentedon Jun 20, 2019
I think we should generalise the casting function, so that it works for e.g.
np.uint8too (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 oftskit/python/tskit/tables.py
Line 1822 in 57ff0ab
petrelharp commentedon Jun 21, 2019
Perhaps in a (new)
util.py?hyanwong commentedon Jun 21, 2019
I will do this. Assign it to me.
jeromekelleher commentedon Jun 21, 2019
Thanks! Done/.
jeromekelleher commentedon Jun 24, 2019
Does #245 cover every usage of numpy int arrays as input @hyanwong?
hyanwong commentedon Jun 24, 2019
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.
jeromekelleher commentedon Jun 24, 2019
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?
jeromekelleher commentedon Jul 3, 2019
Where are we with this @hyanwong - are we filtering all integer array inputs through this function?
hyanwong commentedon Jul 3, 2019
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?
hyanwong commentedon Jul 3, 2019
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.
jeromekelleher commentedon Jul 3, 2019
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.)
jeromekelleher commentedon Aug 13, 2019
Closed in #245