Skip to content

Support alignment/broadcasting with unlabeled dimensions of size 1 #2171

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

Open
shoyer opened this issue May 22, 2018 · 5 comments
Open

Support alignment/broadcasting with unlabeled dimensions of size 1 #2171

shoyer opened this issue May 22, 2018 · 5 comments

Comments

@shoyer
Copy link
Member

shoyer commented May 22, 2018

Sometimes, it's convenient to include placeholder dimensions of size 1, which allows for removing any ambiguity related to the order of output dimensions.

Currently, this is not supported with xarray:

>>> xr.DataArray([1], dims='x') + xr.DataArray([1, 2, 3], dims='x')
ValueError: arguments without labels along dimension 'x' cannot be aligned because they have different dimension sizes: {1, 3}

>>> xr.Variable(('x',), [1]) + xr.Variable(('x',), [1, 2, 3])
ValueError: operands cannot be broadcast together with mismatched lengths for dimension 'x': (1, 3)

However, these operations aren't really ambiguous. With size 1 dimensions, we could logically do broadcasting like NumPy arrays, e.g.,

>>> np.array([1]) + np.array([1, 2, 3])
array([2, 3, 4])

This would be particularly convenient if we add keepdims=True to xarray operations (#2170).

@ambrosejcarr
Copy link

We'd really like to be able to do this for broadcastable matrix multiplication. See e.g. spacetx/starfish#983

Do you have a sense of how this might be implemented, or a suggested work around in the mean time?

@shoyer
Copy link
Member Author

shoyer commented Feb 10, 2019

I think there are three places where you would need to fix this:

Index based alignment in align():

'arguments without labels along dimension %r cannot be '

Unified dimensions for broadcasting variables, which is defined in two places:

def _unified_dims(variables):

def unified_dim_sizes(

The easiest work around is probably to call .squeeze() on any objects with size 1 dimensions -- then xarray's broadcasting/alignment should all work fine.

@stale
Copy link

stale bot commented Jan 10, 2021

In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity

If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically

@stale stale bot added the stale label Jan 10, 2021
@dcherian dcherian removed the stale label Jan 10, 2021
@gagebeni

This comment was marked as off-topic.

@shoyer

This comment was marked as off-topic.

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