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
Implemented Quantity-aware wrappers for assert_allclose and linspace #2402
Conversation
I can write docs if there are no objections to including this. |
I like it! |
:func:`numpy.testing.assert_allclose`. | ||
""" | ||
if isinstance(actual, Quantity) and isinstance(desired, Quantity): | ||
np.testing.assert_allclose(actual.value, desired.to(actual.unit).value, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had written a similar utility function for testing and called it assert_quantity but with different semantics.
If you write it like that, then at least you have to describe in the docstring that you convert desired
to actual
and then e.g. atol
applies to those numbers.
There's quite a bit of tests in Astropy already where asserts on quantities are split in two lines ... one testing values and the other testing units ... maybe assert_quantity
makes sense in addition and you leave this one as is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I guess there are two use-cases - one where you check if the values are equivalent, and one where you check where they are exactly equal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I guess users could also expect quantities for atol
to work?
I think this shows that it's often not 100% straight-forward how to write a quantity-aware wrapper and we have to look at each one and make sure the docstring explains what it really does.
I don't know if this is a good idea. Will make some code a bit shorter, but also has the potential for confusion (accidentally use the wrong @astrofrog If we do add it, could you explicity recommend the import convention you have proposed above
and say that this should be avoided
because it can lead to confusion with code that uses
? There's the uncertainties package that has the unumpy package where "u" means uncertainty. It's probably not a big deal, but maybe "qnp" ("q" for "quantity") might be less confusing (especially if users use astropy quantities and nduncertainties)? |
""" | ||
if isinstance(start, Quantity) and isinstance(stop, Quantity): | ||
return np.linspace(start.value, stop.to(start.unit).value, num=num, | ||
endpoint=endpoint, retstep=retstep, dtype=dtype) * start.unit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my numpy, linspace
doesn't accept a dtype
option!?
In [7]: np.linspace(1, 10, dtype=float)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-8979a0b673b3> in <module>()
----> 1 np.linspace(1, 10, dtype=float)
TypeError: linspace() got an unexpected keyword argument 'dtype'
In [8]: np.__version__
Out[8]: '1.8.1'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so maybe arguments that don't care about quantities should be passed via **kwargs
to avoid this kind of issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed--it should just work as a transparent replacement for the Numpy ones, keeping in mind that the signatures for some of these functions have changed between Numpy versions :/
I'm not very keen on this, unless it is really obvious that one couldn't change the numpy functions to work this way (and then work via |
Actually..., it seems at least with python3 and numpy 1.9.0-dev,
|
@mhvk - huh, you're right - even with compatible but different units:
|
@astrofrog - for For |
It sounds like we may need to think about this more, so we'll definitely miss the feature freeze. I'm re-scheduling to 1.0. |
This will get closed by #3273, so leaving the milestone. |
Superseded by #3273 |
As discussed in #1274, it would be great to have Quantity-aware wrappers to some Numpy functions. We don't have to monkey-patch these, we can just provide them in a separate namespace. This PR can be used e.g. as:
I'd like to include a few functions like this in 0.4 if there are no objections.