Skip to content

Some simple broadcast_dim method? #2267

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
Hoeze opened this issue Jul 4, 2018 · 9 comments
Closed

Some simple broadcast_dim method? #2267

Hoeze opened this issue Jul 4, 2018 · 9 comments

Comments

@Hoeze
Copy link

Hoeze commented Jul 4, 2018

I've already found xr.broadcast(arrays).
However, I'd like to just add a new dimension with a specific size to one DataArray.
I could not find any simple option to do this.

If there is no such option:

  • add a size parameter to DataArray.expand_dims?
  • DataArray.broadcast_dims({"a": M, "b": N})?
@fmaussion
Copy link
Member

Is exand dims not what you are looking for?

@Hoeze
Copy link
Author

Hoeze commented Jul 4, 2018

No, I'd need something like np.tile.
expand_dims inserts only a dimension of length '1'

@Hoeze
Copy link
Author

Hoeze commented Jul 4, 2018

As an explanation:
I'd like to change my program to only use lazy / chunked calculations in order to save RAM.

I recognized that np.broadcast_to converts the DataArray into a numpy one.
Therefore I needed some xarray way to solve this.

I tried:

DataArray.expand_dims("new_dim").isel("new_dim", np.repeat(0, target_dim_size))

but this really looks ugly and I'm not sure about the performance implications of this.

@shoyer
Copy link
Member

shoyer commented Jul 4, 2018

We could add an optional size argument to expand_dims, with a default value of 1.

@Hoeze
Copy link
Author

Hoeze commented Jul 4, 2018

@shoyer so there is no direct xarray equivalent to np.broadcast_to?

@shoyer
Copy link
Member

shoyer commented Jul 4, 2018

We have xarray.Variable.set_dims, but that's only in the low-level variable API.

@Hoeze
Copy link
Author

Hoeze commented Jul 18, 2018

Maybe related:

Consider the following example to calculate pairwise distances:

x = np.array([[1,2,3,4]])
dist = x.T - x

numpy automatically broadcasts the one-dimensions to get 4x4 matrices and substracts them.

As far as I can see, this example is really hard to recreate with xarray, since there is nearly no possibility to add a new dimension to x and broadcast it properly.

@shoyer
Copy link
Member

shoyer commented Jul 19, 2018

let's continue the repeated dimension discussion over in #1378

@dcherian
Copy link
Contributor

dcherian commented Jul 6, 2019

This should be fixed by the latest expand_dims

@dcherian dcherian closed this as completed Jul 6, 2019
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

4 participants