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

roll doesn't handle periodic boundary conditions well #1875

Closed
shoyer opened this issue Jan 31, 2018 · 5 comments · Fixed by #2360
Closed

roll doesn't handle periodic boundary conditions well #1875

shoyer opened this issue Jan 31, 2018 · 5 comments · Fixed by #2360
Labels
API design needs release breaking changes that should be held until a major release

Comments

@shoyer
Copy link
Member

shoyer commented Jan 31, 2018

DataArray.roll() currently rolls both data variables and coordinates:

>>> arr = xr.DataArray(range(4), [('x', range(0, 360, 90))])
>>> arr.roll(x=2)
<xarray.DataArray (x: 4)>
array([2, 3, 0, 1])
Coordinates:
  * x        (x) int64 180 270 0 90

This is sort of makes sense, but the labels are now all non-monotonic, so you can't even plot the data with xarray. In my experience, you probably want coordinate labels that either look like:

  1. The unrolled original coordinates: [0, 90, 180, 270]
  2. Shifted coordinates: [-180, -90, 0, 90]

It should be easier to accomplish this is in xarray. I currently resort to using roll and manually fixing up coordinates after the fact.

I'm actually not sure if there are any use-cases for the current behavior. Choice (1) would have the virtue of being consistent with shift():

>>> arr.shift(x=2)
<xarray.DataArray (x: 4)>
array([nan, nan,  0.,  1.])
Coordinates:
  * x        (x) int64 0 90 180 270

We could potentially add optional another argument for shifting labels, too, or requiring fixing that up by subtraction.

Note: you might argue that this is overly geoscience specific, and it would be, if this was only for handling a longitude coordinate. But periodic boundary conditions are common in many areas of the physical sciences.

@fujiisoup
Copy link
Member

I like the option 1.
I agree that the circular coordinate is used in many physics fields (actually I'm not from geoscience and I also sometimes use the circular coordinate), but one confusing sene I can think of is when using it in radian, (0, 0.5 pi, pi ...).
Manual overwite would be more explicit

Another option could be to add a circular cooredinate class after #1603 is implemented?

@shoyer shoyer added the needs release breaking changes that should be held until a major release label Mar 1, 2018
@shoyer
Copy link
Member Author

shoyer commented Mar 31, 2018

@mathause @rabernat any opinions here? My sense is that the current version of roll() is not very useful for anyone, but I'd like to check before we change it unilaterally...

@ahuang11
Copy link
Contributor

ahuang11 commented Aug 7, 2018

Just wanted to bump this! Would make my hack neater to interpolate across the prime meridian.
image

@shoyer
Copy link
Member Author

shoyer commented Aug 7, 2018

Yes, it would be nice to change this to behavior (1) above.

There are a few options here:

  • Just change how roll works (as a breaking change) in xarray v0.11, with the argument that the current behavior is broken/useless. This is easiest if we aren't concerned about backwards compatibility, but unfortunately I'm pretty sure at least some people rely on this, e.g., based this StackOverflow post.
  • Deprecate the current behavior, e.g., by inserting a new argument coords=None which by default shifts coordinates (equivalent to coords=True) and raises a warning. In a future version of xarray, switch the default to coords=False.
  • Implement the desired behavior in a new function (roll_axis?) and deprecate roll. This is the preferred option for mature projects like NumPy.

I am loathe to loose the name roll so this second option is probably the best choice. Any interest in putting together a pull request?

@ahuang11
Copy link
Contributor

ahuang11 commented Aug 8, 2018

Option 2 sounds good; I'll try putting together a pull request sometime, hopefully within a week!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API design needs release breaking changes that should be held until a major release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants