Skip to content

Commit ae3c313

Browse files
committed
More overlap improvements
1 parent 86e101c commit ae3c313

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

docs/source/user-stories/overlaps.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ kernelspec:
1313

1414
# Overlapping Groups
1515

16-
Generally group-by problems involve non-overlapping groups. Consider the following group of labels:
16+
Generally group-by problems involve non-overlapping groups.
17+
18+
Consider the following group of labels:
1719

1820
```{code-cell}
1921
import numpy as np
@@ -40,21 +42,27 @@ we get (note the reduction over `x` is implicit here):
4042
xarray_reduce(da, labels, func="sum")
4143
```
4244

43-
Now let's calculate the `sum` where `labels` is either `1` or `2`. The trick is to add a new dimension to `labels` of size `2` and assign a new label `4` in the appropriate locations.
45+
Now let's _also_ calculate the `sum` where `labels` is either `1` or `2`.
46+
We could easily compute this using the grouped result but here we use this simple example for illustration.
47+
The trick is to add a new dimension with new labels (here `4`) in the appropriate locations.
4448
```{code-cell}
45-
# assign expanded=4 where label == 1 or 2, and -1 otherwise
49+
# assign 4 where label == 1 or 2, and -1 otherwise
4650
newlabels = xr.where(labels.isin([1, 2]), 4, -1)
4751
48-
# alternative:
52+
# concatenate along a new dimension y;
53+
# note y is not present on da
4954
expanded = xr.concat([labels, newlabels], dim="y")
5055
expanded
5156
```
5257

53-
Now we reduce over `x` _and_ `y` (again implicitly) to get the appropriate sum under `label=4` (and `label=-1`). We can discard the value accumulated under `label=-1` later.
58+
Now we reduce over `x` _and_ the new dimension `y` (again implicitly) to get the appropriate sum under
59+
`label=4` (and `label=-1`). We can discard the value accumulated under `label=-1` later.
5460
```{code-cell}
5561
xarray_reduce(da, expanded, func="sum")
5662
```
5763

64+
This way we compute all the reductions we need, in a single pass over the data.
65+
5866
This technique generalizes to more complicated aggregations. The trick is to
5967
- generate appropriate labels
6068
- concatenate these new labels along a new dimension (`y`) absent on the object being reduced (`da`), and

0 commit comments

Comments
 (0)