Xarray: Support alignment/broadcasting with unlabeled dimensions of size 1

Created on 22 May 2018  路  2Comments  路  Source: pydata/xarray

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).

API design design question

Most helpful comment

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

Index based alignment in align():
https://github.com/pydata/xarray/blob/6d2076688d4f5466cf77ace2b196e910c1c0fbb8/xarray/core/alignment.py#L143

Unified dimensions for broadcasting variables, which is defined in two places:
https://github.com/pydata/xarray/blob/6d2076688d4f5466cf77ace2b196e910c1c0fbb8/xarray/core/variable.py#L1978
https://github.com/pydata/xarray/blob/6d2076688d4f5466cf77ace2b196e910c1c0fbb8/xarray/core/computation.py#L441

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.

All 2 comments

We'd really like to be able to do this for broadcastable matrix multiplication. See e.g. https://github.com/spacetx/starfish/pull/983

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

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

Index based alignment in align():
https://github.com/pydata/xarray/blob/6d2076688d4f5466cf77ace2b196e910c1c0fbb8/xarray/core/alignment.py#L143

Unified dimensions for broadcasting variables, which is defined in two places:
https://github.com/pydata/xarray/blob/6d2076688d4f5466cf77ace2b196e910c1c0fbb8/xarray/core/variable.py#L1978
https://github.com/pydata/xarray/blob/6d2076688d4f5466cf77ace2b196e910c1c0fbb8/xarray/core/computation.py#L441

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aseyboldt picture aseyboldt  路  5Comments

duncanwp picture duncanwp  路  4Comments

zxdawn picture zxdawn  路  3Comments

blaylockbk picture blaylockbk  路  4Comments

jbusecke picture jbusecke  路  4Comments