Skip to content

Add broadcast function to the API #670

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

Merged
merged 1 commit into from
Jan 1, 2016
Merged

Conversation

shoyer
Copy link
Member

@shoyer shoyer commented Dec 4, 2015

This is a renaming and update of the existing xray.broadcast_arrays function,
which now works properly in the light of #648.

xref #649
cc @rabernat

Examples

Broadcast two data arrays against one another to fill out their dimensions:

>>> a = xray.DataArray([1, 2, 3], dims='x')
>>> b = xray.DataArray([5, 6], dims='y')
>>> a
<xray.DataArray (x: 3)>
array([1, 2, 3])
Coordinates:
  * x        (x) int64 0 1 2
>>> b
<xray.DataArray (y: 2)>
array([5, 6])
Coordinates:
  * y        (y) int64 0 1
>>> a2, b2 = xray.broadcast(a, b)
>>> a2
<xray.DataArray (x: 3, y: 2)>
array([[1, 1],
       [2, 2],
       [3, 3]])
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1
>>> b2
<xray.DataArray (x: 3, y: 2)>
array([[5, 6],
       [5, 6],
       [5, 6]])
Coordinates:
  * y        (y) int64 0 1
  * x        (x) int64 0 1 2

Fill out the dimensions of all data variables in a dataset:

>>> ds = xray.Dataset({'a': a, 'b': b})
>>> ds2, = xray.broadcast(ds)  # use tuple unpacking to extract one dataset
>>> ds2
<xray.Dataset>
Dimensions:  (x: 3, y: 2)
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) int64 0 1
Data variables:
    a        (x, y) int64 1 1 2 2 3 3
    b        (x, y) int64 5 6 5 6 5 6

@shoyer
Copy link
Member Author

shoyer commented Dec 8, 2015

Any comments? This needs a rebase, but is otherwise ready for review

@@ -1276,15 +1276,15 @@ def test_broadcast_arrays(self):

x = DataArray(np.random.randn(2, 3), dims=['a', 'b'])
y = DataArray(np.random.randn(3, 2), dims=['b', 'a'])
x2, y2 = broadcast_arrays(x, y)
x2, y2 = broadcast(x, y)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can there be a test for broadcasting coordinates, not just data variables? That is something I use frequently, and I currently have to hack it (as in #649).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add some more explicit tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@jhamman
Copy link
Member

jhamman commented Dec 8, 2015

LGTM.

This is a renaming and update of the existing `xray.broadcast_arrays` function,
which now works properly in the light of GH648.

Examples
--------

Broadcast two data arrays against one another to fill out their dimensions:

    >>> a = xray.DataArray([1, 2, 3], dims='x')
    >>> b = xray.DataArray([5, 6], dims='y')
    >>> a
    <xray.DataArray (x: 3)>
    array([1, 2, 3])
    Coordinates:
      * x        (x) int64 0 1 2
    >>> b
    <xray.DataArray (y: 2)>
    array([5, 6])
    Coordinates:
      * y        (y) int64 0 1
    >>> a2, b2 = xray.broadcast(a, b)
    >>> a2
    <xray.DataArray (x: 3, y: 2)>
    array([[1, 1],
           [2, 2],
           [3, 3]])
    Coordinates:
      * x        (x) int64 0 1 2
      * y        (y) int64 0 1
    >>> b2
    <xray.DataArray (x: 3, y: 2)>
    array([[5, 6],
           [5, 6],
           [5, 6]])
    Coordinates:
      * y        (y) int64 0 1
      * x        (x) int64 0 1 2

Fill out the dimensions of all data variables in a dataset:

    >>> ds = xray.Dataset({'a': a, 'b': b})
    >>> ds2, = xray.broadcast(ds)  # use tuple unpacking to extract one dataset
    >>> ds2
    <xray.Dataset>
    Dimensions:  (x: 3, y: 2)
    Coordinates:
      * x        (x) int64 0 1 2
      * y        (y) int64 0 1
    Data variables:
        a        (x, y) int64 1 1 2 2 3 3
        b        (x, y) int64 5 6 5 6 5 6
shoyer added a commit that referenced this pull request Jan 1, 2016
Add broadcast function to the API
@shoyer shoyer merged commit e014b61 into pydata:master Jan 1, 2016
@shoyer shoyer deleted the broadcast branch January 1, 2016 22:13
@shoyer
Copy link
Member Author

shoyer commented Jan 1, 2016

thanks for the feedback, guys!

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 this pull request may close these issues.

3 participants