Skip to content

Add broadcast function to the API#670

Merged
shoyer merged 1 commit into
pydata:masterfrom
shoyer:broadcast
Jan 1, 2016
Merged

Add broadcast function to the API#670
shoyer merged 1 commit into
pydata:masterfrom
shoyer:broadcast

Conversation

@shoyer

@shoyer shoyer commented Dec 4, 2015

Copy link
Copy Markdown
Member

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

shoyer commented Dec 8, 2015

Copy link
Copy Markdown
Member Author

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

@jhamman

jhamman commented Dec 8, 2015

Copy link
Copy Markdown
Member

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

shoyer commented Jan 1, 2016

Copy link
Copy Markdown
Member Author

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