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

Plots get labels from pint arrays #5561

Merged
merged 38 commits into from
Jul 21, 2021
Merged

Conversation

TomNicholas
Copy link
Member

@TomNicholas TomNicholas commented Jul 2, 2021

Stops you needing to call .pint.dequantify() before plotting.

Builds on top of #5568, so that should be merged first.

@pep8speaks
Copy link

pep8speaks commented Jul 2, 2021

Hello @TomNicholas! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-07-21 22:08:37 UTC

@TomNicholas TomNicholas requested a review from keewis July 2, 2021 00:49
@github-actions
Copy link
Contributor

github-actions bot commented Jul 2, 2021

Unit Test Results

         6 files  ±0           6 suites  ±0   53m 47s ⏱️ ±0s
16 204 tests ±0  14 469 ✔️ ±0  1 735 💤 ±0  0 ❌ ±0 
90 420 runs  ±0  82 245 ✔️ ±0  8 175 💤 ±0  0 ❌ ±0 

Results for commit 92cb751. ± Comparison against base commit 92cb751.

♻️ This comment has been updated with latest results.

@TomNicholas TomNicholas removed the request for review from keewis July 2, 2021 03:16
@TomNicholas
Copy link
Member Author

I thought this approach was neat but it seems to have broken some of the tests in test_units...

@TomNicholas
Copy link
Member Author

TomNicholas commented Jul 2, 2021

This should be reimplemented following #3245 (comment). Really another PR should be opened to implement a to_duck_array method and to_numpy, and this PR rebased on top of that to coerce arrays before plotting.

@TomNicholas TomNicholas removed the request for review from keewis July 2, 2021 14:02
@TomNicholas
Copy link
Member Author

Now builds on top of #5568, but fails because that PR is not complete (does not change .values -> .to_numpy() everywhere needed yet).

@TomNicholas TomNicholas added the topic-arrays related to flexible array support label Jul 2, 2021
class TestPlots(PlotTestCase):
def test_units_in_line_plot_labels(self):
arr = np.linspace(1, 10, 3) * unit_registry.Pa
# TODO make coord a Quantity once unit-aware indexes supported
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be possible to specify a non-dimensional coordinate with x="x_coord"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point was that I want this test to eventually test labels on both the x and y axes, but at the moment pint is only involved with the y axis, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's right. I meant to implement this by converting the dimension coordinate to a non-dimension coordinate:

x_coord = xr.DataArray(
    np.linspace(1, 3, 3) * unit_registry.m, dims="x"
)
da = xr.DataArray(data=arr, dims="x", coords={"x_coord": x_coord}, name="pressure")
da.plot.line(x="x_coord")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I try that I still get a UnitStrippedWarning in .to_index_variable()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like a bug. open an issue for it so we don't forget?

import pint

pint_version = LooseVersion(pint.__version__)
pint_array_type = (pint.Quantity,)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the reason for the failures (see #4751 (comment)): we import pint here, but pint also tries to import xarray for type checking. Normally, the circular import would be an error but pytest silences that, apparently, resulting in a partially imported module.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this explains the failures in #5568 too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this properly I see what you mean now. So is this PR just stuck then? Why can we not just check the name rather than the explicit type within xarray (for now at least)?

Copy link
Collaborator

@keewis keewis Jul 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could import pint in the functions that use pint_array_type for now (or add a _get_pint_array_type function to pycompat and call it wherever pint_array_type would be used), and I'll try to get pint to not import xarray (because ideally it should not care about what wraps it). Not sure if that's possible, though.

The best long term fix would be to figure out dask/dask#6635

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keewis It would be good to hear your thoughts on that over on the Pint issue tracker. Right now, to maintain the type casting hierarchy, libraries with duck arrays have to either define a list of types they can wrap (and prohibit all others) or a list which are meant to wrap it (and allow wrapping any other). Pint went with the latter since it was the safest approach as a type high on the graph (https://pint.readthedocs.io/en/stable/numpy.html#Technical-Commentary), but I suppose that if that was problematic, it could be flipped around to a list of allowed types to wrap (with a breaking change), as Dask ended up doing (dask/dask#6393).

But, definitely, this ad hoc approach to the type casting hierarchy will continue to be problematic until dask/dask#6635 is resolved definitely.

Copy link
Member Author

@TomNicholas TomNicholas Jul 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be really nice to fix this properly (and I will bring it up in the xarray dev call now), but for this PR then your suggestion seems to work nicely on #5568 @keewis , thanks.

@TomNicholas TomNicholas mentioned this pull request Jul 8, 2021
8 tasks
@TomNicholas TomNicholas added the plan to merge Final call for comments label Jul 16, 2021
@TomNicholas
Copy link
Member Author

I think this is ready to merge after #5568 is merged?

Copy link
Contributor

@dcherian dcherian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great!

@TomNicholas TomNicholas merged commit 92cb751 into pydata:main Jul 21, 2021
st-bender added a commit to st-bender/xarray that referenced this pull request Aug 10, 2021
* main: (31 commits)
  Refactor index vs. coordinate variable(s) (pydata#5636)
  pre-commit: autoupdate hook versions (pydata#5685)
  Flexible Indexes: Avoid len(index) in map_blocks (pydata#5670)
  Speed up _mapping_repr (pydata#5661)
  update the link to `scipy`'s intersphinx file (pydata#5665)
  Bump styfle/cancel-workflow-action from 0.9.0 to 0.9.1 (pydata#5663)
  pre-commit: autoupdate hook versions (pydata#5660)
  fix the binder environment (pydata#5650)
  Update api.rst (pydata#5639)
  Kwargs to rasterio open (pydata#5609)
  Bump codecov/codecov-action from 1 to 2.0.2 (pydata#5633)
  new blank whats-new for v0.19.1
  v0.19.0 release notes (pydata#5632)
  remove deprecations scheduled for 0.19 (pydata#5630)
  Make typing-extensions optional (pydata#5624)
  Plots get labels from pint arrays (pydata#5561)
  Add to_numpy() and as_numpy() methods (pydata#5568)
  pin fsspec (pydata#5627)
  pre-commit: autoupdate hook versions (pydata#5617)
  Add dataarray scatter with 3d support (pydata#4909)
  ...
dcherian added a commit to kmsquire/xarray that referenced this pull request Aug 11, 2021
* upstream/main: (31 commits)
  Refactor index vs. coordinate variable(s) (pydata#5636)
  pre-commit: autoupdate hook versions (pydata#5685)
  Flexible Indexes: Avoid len(index) in map_blocks (pydata#5670)
  Speed up _mapping_repr (pydata#5661)
  update the link to `scipy`'s intersphinx file (pydata#5665)
  Bump styfle/cancel-workflow-action from 0.9.0 to 0.9.1 (pydata#5663)
  pre-commit: autoupdate hook versions (pydata#5660)
  fix the binder environment (pydata#5650)
  Update api.rst (pydata#5639)
  Kwargs to rasterio open (pydata#5609)
  Bump codecov/codecov-action from 1 to 2.0.2 (pydata#5633)
  new blank whats-new for v0.19.1
  v0.19.0 release notes (pydata#5632)
  remove deprecations scheduled for 0.19 (pydata#5630)
  Make typing-extensions optional (pydata#5624)
  Plots get labels from pint arrays (pydata#5561)
  Add to_numpy() and as_numpy() methods (pydata#5568)
  pin fsspec (pydata#5627)
  pre-commit: autoupdate hook versions (pydata#5617)
  Add dataarray scatter with 3d support (pydata#4909)
  ...
dcherian added a commit to dcherian/xarray that referenced this pull request Aug 13, 2021
* upstream/main: (34 commits)
  Use same bool validator as other inputs (pydata#5703)
  conditionally disable bottleneck (pydata#5560)
  Refactor index vs. coordinate variable(s) (pydata#5636)
  pre-commit: autoupdate hook versions (pydata#5685)
  Flexible Indexes: Avoid len(index) in map_blocks (pydata#5670)
  Speed up _mapping_repr (pydata#5661)
  update the link to `scipy`'s intersphinx file (pydata#5665)
  Bump styfle/cancel-workflow-action from 0.9.0 to 0.9.1 (pydata#5663)
  pre-commit: autoupdate hook versions (pydata#5660)
  fix the binder environment (pydata#5650)
  Update api.rst (pydata#5639)
  Kwargs to rasterio open (pydata#5609)
  Bump codecov/codecov-action from 1 to 2.0.2 (pydata#5633)
  new blank whats-new for v0.19.1
  v0.19.0 release notes (pydata#5632)
  remove deprecations scheduled for 0.19 (pydata#5630)
  Make typing-extensions optional (pydata#5624)
  Plots get labels from pint arrays (pydata#5561)
  Add to_numpy() and as_numpy() methods (pydata#5568)
  pin fsspec (pydata#5627)
  ...
dcherian added a commit to dcherian/xarray that referenced this pull request Aug 13, 2021
* upstream/main: (307 commits)
  Use same bool validator as other inputs (pydata#5703)
  conditionally disable bottleneck (pydata#5560)
  Refactor index vs. coordinate variable(s) (pydata#5636)
  pre-commit: autoupdate hook versions (pydata#5685)
  Flexible Indexes: Avoid len(index) in map_blocks (pydata#5670)
  Speed up _mapping_repr (pydata#5661)
  update the link to `scipy`'s intersphinx file (pydata#5665)
  Bump styfle/cancel-workflow-action from 0.9.0 to 0.9.1 (pydata#5663)
  pre-commit: autoupdate hook versions (pydata#5660)
  fix the binder environment (pydata#5650)
  Update api.rst (pydata#5639)
  Kwargs to rasterio open (pydata#5609)
  Bump codecov/codecov-action from 1 to 2.0.2 (pydata#5633)
  new blank whats-new for v0.19.1
  v0.19.0 release notes (pydata#5632)
  remove deprecations scheduled for 0.19 (pydata#5630)
  Make typing-extensions optional (pydata#5624)
  Plots get labels from pint arrays (pydata#5561)
  Add to_numpy() and as_numpy() methods (pydata#5568)
  pin fsspec (pydata#5627)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plan to merge Final call for comments topic-arrays related to flexible array support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants