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
{{ message }}
Repository navigation
simplify() can't cope with an np.array of standard integers #8
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'
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)
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
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.
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)
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.
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
Activity
petrelharp commentedon Nov 17, 2018
To which argument are you giving the
int64s? Can you give a MWE?hyanwong commentedon Nov 17, 2018
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
which produces the error
petrelharp commentedon Nov 19, 2018
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.
hyanwong commentedon Nov 20, 2018
Thanks. Will take it up with @jeromekelleher . But I thought this was the place to report msprime bugs?
petrelharp commentedon Nov 20, 2018
Er, yes. I got my wires crossed. =)
jeromekelleher commentedon Nov 20, 2018
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.
jeromekelleher commentedon Nov 23, 2018
What we need to do is something like:
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.
hyanwong commentedon Nov 23, 2018
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)
petrelharp commentedon Nov 23, 2018
jeromekelleher commentedon Nov 25, 2018
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.
petrelharp commentedon Dec 7, 2018
Another place this issue has appeared, and is annoying:
producing
A workaround is instead of
msprime.NODE_IS_SAMPLEto useFixes issue tskit-dev#8
Fixes issue tskit-dev#8
Fixes issue tskit-dev#8
Fixes issue tskit-dev#8
Fixes issue tskit-dev#8
Fixes issue tskit-dev#8
Merge pull request #185 from hyanwong/allow_compatible_array_types
jeromekelleher commentedon Jun 4, 2019
Closed in #185.
jeromekelleher commentedon Jun 4, 2019
See #192 for planned wider applications of the casting function.