html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue
https://github.com/pydata/xarray/issues/2714#issuecomment-457799714,https://api.github.com/repos/pydata/xarray/issues/2714,457799714,MDEyOklzc3VlQ29tbWVudDQ1Nzc5OTcxNA==,1217238,2019-01-26T04:05:58Z,2019-01-26T04:07:24Z,MEMBER,"I think this would also work (due to the intrinsic broadcasting behavior of numpy functions):
```python
def get_chebyshev_distances_xarray_ufunc(array, dye_array):
return abs(array - dye_array).max(axis=-2)
```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,403378297
https://github.com/pydata/xarray/issues/2714#issuecomment-457799469,https://api.github.com/repos/pydata/xarray/issues/2714,457799469,MDEyOklzc3VlQ29tbWVudDQ1Nzc5OTQ2OQ==,1217238,2019-01-26T04:02:09Z,2019-01-26T04:07:00Z,MEMBER,"OK, I think you're doing this right. You want an output with shape `['all_sites', 'dye_sites']` right?
My suggestion would be to add an explicit call to `np.broadcast_arrays()` at the start of your applied function. This will make the dimensions a little easier to understand.
```python
def get_chebyshev_distances_xarray_ufunc(array, dye_array):
array, dye_array = np.broadcast_arrays(array, dye_array)
# array is a 3D numpy array with logical dimensions ['all_sites', 'dye_sites', 'dim_1']
# dye_array is a 3D numpy array with logical dimensions ['all_sites', 'dye_sites', 'dim_1']
# compute the distance matrix
# return a numpy array with logical dimensions ['all_sites', 'dye_sites']
```
(edit note: fixed dimensions)","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,403378297
https://github.com/pydata/xarray/issues/2714#issuecomment-457799701,https://api.github.com/repos/pydata/xarray/issues/2714,457799701,MDEyOklzc3VlQ29tbWVudDQ1Nzc5OTcwMQ==,1217238,2019-01-26T04:05:43Z,2019-01-26T04:05:43Z,MEMBER,"actually, scratch that","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,403378297
https://github.com/pydata/xarray/issues/2714#issuecomment-457794714,https://api.github.com/repos/pydata/xarray/issues/2714,457794714,MDEyOklzc3VlQ29tbWVudDQ1Nzc5NDcxNA==,1217238,2019-01-26T02:48:01Z,2019-01-26T03:40:29Z,MEMBER,"The notion of ""core dimensions"" in `apply_ufunc()` is definitely quite tricky to understand.
That said, I think this is (mostly) doing the right thing:
- Your inputs have dimensions `['row_a', 'dim_1']` and `['row_b', 'dim_1']`
- Xarray broadcasts over dimensions that aren't included in ""core dimensions"" , so inputs are broadcast to have dimensions like `['row_a', 'row_b', 'dim_1']` and `['row_a', 'row_b', 'dim_1']`.
This is probably especially confusing because the unlabeled versions of `da` and `db` are given ""broadcastable"" shapes (1000, 1, 100) and (1000, 100) rather than the fully ""broadcast"" shapes of (1000, 1000, 100) and (1000, 1000, 100), which would make it more obvious what is going on.
For your specific use case: maybe you meant to specify `input_core_dims=[['row_a'], ['row_b']]` instead? That version would give inputs with dimensions like `['dim_1', 'row_a']` and `['dim_1', 'row_b']`.
More generally: I think we really need a version of `apply()` that doesn't do this confusing broadcasting and dimension reordering. See https://github.com/pydata/xarray/issues/1130 for discussion about that.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,403378297
https://github.com/pydata/xarray/issues/2714#issuecomment-457798131,https://api.github.com/repos/pydata/xarray/issues/2714,457798131,MDEyOklzc3VlQ29tbWVudDQ1Nzc5ODEzMQ==,1217238,2019-01-26T03:40:14Z,2019-01-26T03:40:14Z,MEMBER,"> > > unlabeled versions of da and db are given ""broadcastable"" shapes (1, 1000, 100) and (1000, 100)
>
> Is it `(1000, 1, 100)` as my code seems to return, or, as you said `(1, 1000, 100)`? Is it deterministic?
That was a typo (I'll fix it). It should be `(1000, 1, 100)`.
The behavior is definitely deterministic, if hard to understand!","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,403378297
https://github.com/pydata/xarray/issues/2714#issuecomment-457798018,https://api.github.com/repos/pydata/xarray/issues/2714,457798018,MDEyOklzc3VlQ29tbWVudDQ1Nzc5ODAxOA==,1217238,2019-01-26T03:38:20Z,2019-01-26T03:38:20Z,MEMBER,"Maybe it would help to describe what you were trying to do here. What should ""one unit"" of your calculation look like?","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,403378297