-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Changes in PyArray_FromAny between 1.5.x and 1.6.x #291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
In 1.7, there's a different error. It feels like this interface needs a solid set of tests to nail down what it's actually expected to do. In the documentation, it says that shape, typestr, and version are required parameters: http://docs.scipy.org/doc/numpy/reference/arrays.interface.html#python-side but neither 1.5 nor 1.6 were enforcing that. Here's what it gives me in 1.7 master:
Here's one way of using it that works for me in both 1.6/1.7:
|
Looks like the documentation was the same in 1.5: http://docs.scipy.org/doc/numpy-1.5.x/reference/arrays.interface.html#python-side |
I think that Sage wants |
(But like I said on the list, I think 1.5's handling of this was kind of array_interface, so I'm not sure what the best solution for sage's use case is.) |
Yes, the goal would be for |
Hi, I have looked at this issue on sage side for a while. What strikes me is how different arange and linspace behave in 1.6.x (haven't looked at 1.7): sage: import numpy sage: numpy.arange(10.0) array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) sage: numpy.linspace(0,9,10) array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9], dtype=object) I would have expected the output to be identical and certainly to have the same type. That was certainly the case in 1.5.x. My point here is that the return type is inconsistent between a function that is coded purely in C in numpy compared to one that use some python. For reference what happens in linspace is: I also tried to change the array_interface definition in sage from just {'typestr': '=f8'} to {'shape': (), 'typestr': '=f8', 'version':3} but that didn't do anything for that particular example. |
We should make sure that 1.7 does the same thing here that 1.5 did. Even if this is "off-label" use, it's a reasonable use-case, and if the array factory function used to inspect the array interface to get type information on its objects, then it should continue to do that. This should not have changed. I think this is a blocker for 1.7 |
The following example breaks on 1.6.x / HEAD --- Updated! class Foo(object): def __init__(self, value) self.value = value def __float__(self): return float(self.value) @property def __array_interface__(self): return {'typestr': '=f8'} f = Foo(0.5) import numpy result2 = numpy.array(f) assert result2.dtype == numpy.dtype('f8') |
What is causing this regression is that PyArray_GetArrayParamsFromObject called form PyArray_Any has different semantics with respect to the array_interface than _array_find_type did --- at least for a single objects. It seems like we need to add code into PyArray_GetArrayParamsFromObject that allows only typestr to be queried from the ArrayInterface and returns that type information (instead of the full array) when possible. This relies on later code outside of GetArrayParamsFromObject to actually produce the array. This should likely be done with a separate function call not exposed to the API. |
Thank you for your investigation. I am guessing mwhansen tried his code with numpy-1.6.1, I don't know if he tested 1.6.2. My result with linspace is still present in 1.6.2 and it sounds in line with what you are saying. |
Yes, all the 1.6 releases will have this problem. This can be fixed. |
Hmm, it seems that the reason this has worked is because the Foo object was coerced into a float without going through the array interface (PyArray_FromInterface) at all. The array interface specifies that the data should be available through the buffer interface, which isn't the case for Foo.. The following works for me:
But this does not:
When supplying shape information:
Is this still regarded as a bug? I think that would change the way the array interface was supposed to work.. (See also: documentation) |
This is now the major source of doctest failures in my "work in progress" sagemath fedora package. $ grep "ValueError: Missing __array_interface__ shape" ~/.sage/tmp/test.log | wc -l 132 $ rpm -q numpy numpy-1.7.0-0.3.b1.fc19.x86_64 |
Thanks @pcpa for reporting the log. It looks like what has to be done to fix this issue is described in the In the meantime, I've added it to the release TODO (#396). |
Many thanks. I confirm it corrects the sagemath issues. Testing a sagemath 5.4.beta1 build. The only minor issue I see is the pattern: |
feat: Add vorr[q]_[s8|s16|s32|u8|u16|u32|s64|u64]
In Numpy 1.5.x, we have
In 1.6, we get the following,
This seems to be do to the changes in PyArray_FromAny introduced in
http://github.com/mwhansen/numpy/commit/2635398db3f26529ce2aaea4028a8118844f3c48
. In particular, _array_find_type used to be used to query our
array_interface attribute, and it no longer seems to work.
It should be reproducible with the following minimal example:
The text was updated successfully, but these errors were encountered: