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

Add tests and documentation with improvement of downcast type compatibility (part of #845) #963

Merged
merged 4 commits into from
Dec 30, 2019

Conversation

jthielen
Copy link
Contributor

@jthielen jthielen commented Dec 30, 2019

As a part of #845, this PR adds tests for downcast type compatibility with Sparse's COO and NumPy's MaskedArray, along with more careful handling of downcast types throughout the library. Also included is new documentation on array type compatibility, including the type casting hierarchy digraph by @shoyer and @crusaderky.

While this PR doesn't fully bring Pint's downcast type compatibility to a completed state, I think this gets it "good enough" for the upcoming release, and the remaining issues are fairly well defined:

Because of that, I think this can close #845, but if @hgrecco you want that kept open until the above items are resolved, let me know.

@hgrecco
Copy link
Owner

hgrecco commented Dec 30, 2019

I agree with closing 845 and, if needed opening more specific issues referencing the upstream repos

@hgrecco
Copy link
Owner

hgrecco commented Dec 30, 2019

bors r+

bors bot added a commit that referenced this pull request Dec 30, 2019
963: Add tests and documentation with improvement of downcast type compatibility (part of #845) r=hgrecco a=jthielen

As a part of #845, this PR adds tests for downcast type compatibility with Sparse's `COO` and NumPy's `MaskedArray`, along with more careful handling of downcast types throughout the library. Also included is new documentation on array type compatibility, including the type casting hierarchy digraph by @shoyer and @crusaderky.

While this PR doesn't fully bring Pint's downcast type compatibility to a completed state, I think this gets it "good enough" for the upcoming release, and the remaining issues are fairly well defined:

- MaskedArray non-commutativity (#633 / numpy/numpy#15200)
- Dask compatibility (#883)
- Addition of CuPy tests (no issue on issue tracker yet)

Because of that, I think this can close #845, but if @hgrecco you want that kept open until the above items are resolved, let me know.

- [x] Closes #37; Closes #845
- [x] Executed ``black -t py36 . && isort -rc . && flake8`` with no errors
- [x] The change is fully covered by automated unit tests
- [x] Documented in docs/ as appropriate
- [x] Added an entry to the CHANGES file


Co-authored-by: Jon Thielen <github@jont.cc>
@bors
Copy link
Contributor

bors bot commented Dec 30, 2019

Build succeeded

@jthielen
Copy link
Contributor Author

jthielen commented Dec 30, 2019

After testing this downstream with MetPy, the issue in #37 (comment) reappeared, making it so that multiplying by a Unit on the right gives an object MaskedArray of Quantities instead of a Quantity with MaskedArray magnitude. This unfortunately ends up being a pretty bad breaking change that makes the situation worse than before.

However, when I reverted the change to Unit replacing __array_wrap__/__array_prepare__ with __array_ufunc__, the Sparse test now fails, since Sparse.COO times Unit requires the multiply ufunc to be implemented on Unit (since Sparse rightly defers)!

So, unless I'm missing a fix that works for both Sparse and MaskedArray, there is a dilemma:

Or, put another way, given that we can't have both, for which type of array would we rather have array * ureg.m work:

  • Sparse.COO (and other ufunc-reliant types)?
  • MaskedArray (as it was before)?

In either case, a follow-up PR to document the issue is likely needed (since the ideal behavior cannot be realized until numpy/numpy#15200 is resolved).

@hgrecco What are your thoughts on this?

@hgrecco
Copy link
Owner

hgrecco commented Dec 30, 2019

This PR is good step into the right direction. __array_ufunc__ (or similar) is a much better protocol than the previous one and something that pint was waiting for several years. My suggestion would be to keep this PR, document the problem and alert users what is the problem. Hopefully, this will be fixed in numpy soon.

bors bot added a commit that referenced this pull request Dec 31, 2019
966: Update documentation to address MaskedArray breaking change r=hgrecco a=jthielen

As discussed in #963 (comment), #963 introduced a breaking change with MaskedArrays with the implementation of `__array_ufunc__` on Unit. This PR documents this breaking change as prompted by #963 (comment) (with the hope that numpy/numpy#15200 can be resolved soon!)

- ~~Closes # (insert issue number)~~
- [x] Executed ``black -t py36 . && isort -rc . && flake8`` with no errors
- ~~The change is fully covered by automated unit tests~~ (documentation update only)
- [x] Documented in docs/ as appropriate
- [x] Added an entry to the CHANGES file


Co-authored-by: Jon Thielen <github@jont.cc>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants