Skip to content

error when using broadcast_arrays with coordinates #649

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

Closed
rabernat opened this issue Nov 9, 2015 · 5 comments · Fixed by #648
Closed

error when using broadcast_arrays with coordinates #649

rabernat opened this issue Nov 9, 2015 · 5 comments · Fixed by #648

Comments

@rabernat
Copy link
Contributor

rabernat commented Nov 9, 2015

I frequently use broadcast_arrays to to feed xray variables to non-xray libraries (e.g. gsw.) Often I need to broadcast the coordinates and variables in order to do call functions that take both as arguments.

I have found that broadcast_arrays doesn't work as I expect with coordinates. For example

import xray
import numpy as np
ds = xray.Dataset({'a': (['y','x'], np.ones((20,10)))},
                  coords={'x': (['x'], np.arange(10)),
                          'y': (['y'], np.arange(20))})
xbc, ybc, abc = xray.broadcast_arrays(ds.x, ds.y, ds.a)

This raises ValueError: an index variable must be defined with 1-dimensional data.

If I change the last line to

xbc, ybc, abc = xray.broadcast_arrays(1*ds.x, 1*ds.y, ds.a)

it works fine.

@ocefpaf
Copy link
Contributor

ocefpaf commented Nov 9, 2015

Hi @rabernat,

Most gsw functions will call np.broadcast_arrays for you internally. So you can pass ds.a.values instead. It is ugly, I know. but consistent when using libraries that expect numpy array.

@rabernat
Copy link
Contributor Author

rabernat commented Nov 9, 2015

Most gsw functions will call np.broadcast_arrays for you internally.

I know.

So you can pass ds.a.values instead. It is ugly, I know. but consistent when using libraries that expect numpy array.

This only works if the all the arrays are appropriately shaped to begin with. It does not work in this case.

np.broadcast_arrays(ds.x.values,ds.y.values,ds.a.values)

raises ValueError: shape mismatch: objects cannot be broadcast to a single shape. The great advantage of xray's broadcasting is that it is "aware" of the relationship between axes and coordinates, independently of the shape of the underlying numpy arrays.

@ocefpaf
Copy link
Contributor

ocefpaf commented Nov 9, 2015

Got it. Thanks!

I guess I've been living around numpy.arrays way too long... Time to experiment this brave new world of labeled arrays 😉

@shoyer
Copy link
Member

shoyer commented Nov 10, 2015

This is yet another issue that magically just works after merging #648!

I'll add your example to the test suite as a regression test.

I was actually holding off on widely advertising xray.broadcast_arrays for exactly this reason -- there wasn't an easy way to fix this before, and this was a pretty obvious edge case. Now I think we can probably safely guarantee the behavior of broadcast_arrays.

@rabernat
Copy link
Contributor Author

@shoyer awesome!

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

Successfully merging a pull request may close this issue.

3 participants