Skip to content
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

Remove the need for uncertainties.unumpy for NumPy arrays #47

Open
mikofski opened this issue Apr 6, 2016 · 8 comments
Open

Remove the need for uncertainties.unumpy for NumPy arrays #47

mikofski opened this issue Apr 6, 2016 · 8 comments

Comments

@mikofski
Copy link

mikofski commented Apr 6, 2016

if I monkeypatch Variable I can use my existing code without modifications:

>>> from uncertainties import Variable, ufloat, umath, unumpy
>>> Variable.sin = umath.sin
>>> a = ufloat(1.23, 0.045)
1.23+/-0.045
>>> b = np.array([a,a,a])
array([1.23+/-0.0345, 1.23+/-0.0345, 1.23+/-0.0345], dtype=object)
>>> np.sin(b)
array([0.9424888019316975+/-0.01153120158579534,
       0.9424888019316975+/-0.01153120158579534,
       0.9424888019316975+/-0.01153120158579534], dtype=object)

although unfortunately it doesn't save anytime over using the looped methods already in unumpy.

@lebigot
Copy link
Collaborator

lebigot commented Apr 25, 2016

Interesting. Do you have a pointer to the NumPy documentation for this? This would remove the need for umath altogether.

PS: The correct monkey patch is actually UFloat.sin = …: this makes np.sin([a, 2*a]) work (it doesn't, with the patch above, as Variable represents only independent variables, not expressions like 2*a, which depends on a).

@mikofski
Copy link
Author

See NumPy issue 7519. This is actually an undesirable side affect of the current implementation of ufuncs. Apparently the new __ndarray_ufunc__ is the correct way to achieve this behavior, but I haven't tested it yet. I'm sorry. I opened this ticket before I received these responses.

There is more info re: __ndarray_ufunc__ in the proposal: "A mechanism for overriding ufuncs".

Perhaps I should change the title of this issue to, "reimplement unumpy.uarray using __ndarray_ufunc__" Or should I just close this? IMO it is probably unstable to leverage this side effect. I have already seen inconsistent results in AlgoPy, and was advised not to use it but instead use the UTPM methods. See AlgoPy issue 48

Do you think there is any interest in a pull request for overloading ndarray ufuncs using __ndarray_ufunc__? Currently, I have met my (employers) needs by developing a wrapper inspired by your wrapper.

@lebigot lebigot changed the title add umath to Variable so that they can be called directly using numpy ufuncs Remove the need of uncertainties.umath for NumPy arrays Aug 14, 2016
@lebigot lebigot changed the title Remove the need of uncertainties.umath for NumPy arrays Remove the need of uncertainties.unumpy for NumPy arrays Aug 14, 2016
@lebigot lebigot changed the title Remove the need of uncertainties.unumpy for NumPy arrays Remove the need for uncertainties.unumpy for NumPy arrays Aug 14, 2016
@lebigot
Copy link
Collaborator

lebigot commented Jul 3, 2017

Reference: as of NumPy 1.13, a nicer universal function overriding mechanism is available (which is I guess consistent with the Mechanism for Overriding UFuncs?): https://github.com/numpy/numpy/releases/tag/v1.13.0rc1.

@lebigot
Copy link
Collaborator

lebigot commented Oct 7, 2018

PS: __numpy_ufunc__ (NumPy 1.11?) is now __array_ufunc__ (NumPy 1.13).

See:

@mikofski
Copy link
Author

mikofski commented Oct 8, 2018

Hi @lebigot, I'm super sorry, but it's not likely I'll get to work on this, so if you choose to close it, I'll understand. Thanks

@lebigot
Copy link
Collaborator

lebigot commented Oct 8, 2018

No need to be sorry: we can all only do our best. :) I started investigating this. There might be a simple solution.

@lebigot
Copy link
Collaborator

lebigot commented Aug 4, 2019

@TomNicholas
Copy link

If you re-implement this using the new __array_function__ protocol, then uncertainties arrays could potentially become compatible with wrapping in xarray data objects.

xarray currently wraps either numpy arrays or dask arrays (for distributed computation), but we are in the process of generalising this to wrap any array which implements the __array_function__ protocol. This has been driven mostly by interest in wrapping unit-aware arrays (such as those provided by pint or astropy) and in sparse arrays, but it would be awesome to be able to wrap uncertainty-aware arrays too!

It should even be possible once this is implemented to wrap arrays in a nested fashion, so you could have a unit-aware, uncertainty-propagating, distributed array, wrapped in xarray's high-level objects 😮

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

No branches or pull requests

3 participants