@@ -23,18 +23,11 @@ of the elements respectively: `->`, space as delimiter and parenthesis:
2323 dimension order in xarray doesn't matter and there isn't much to be done without knowing
2424 the dimension names.
2525
26- :::{attention}
27- We also provide some cruder wrappers with syntax closer to einops.
28- We are experimenting on trying to find the right spot between being clear,
29- semantic and flexible yet concise.
30-
31- These ` raw_ ` wrappers like {func}` xarray_einstats.einops.raw_rearrange `
32- impose several extra constraints to accepted xarray inputs, in addition
33- to dimension names being strings.
34-
35- The example data we are using on this page uses single word alphabetical
36- dimensions names which allows us to demonstrate both side by side.
37- :::
26+ However, there are also many cases in which dimension names in xarray will be strings
27+ without any spaces nor parenthesis in them. So similarly to the option of
28+ doing ` da.stack(dim=("dim1", "dim2")) ` which can't be used for all valid
29+ dimension names but is generally easier to write and less error prone,
30+ ` xarray_einstats.einops ` also provides two possible syntaxes.
3831
3932The guiding principle of the einops module is to take the input expressions
4033in our list of str/list/dict and translate them to valid einops expressions
@@ -45,20 +38,60 @@ and thus support "partial" expressions that cover only the dimensions
4538that will be modified.
4639
4740Another important consideration is to take into account that _ in xarray_ ,
48- dimension names should not matter, hence the constraint of using dicts
41+ dimension order should not matter, hence the constraint of using dicts
4942on the left side. Imposing this constraint also
5043makes our job of filling in the "partial" expressions much easier.
5144We do accept that in the right side as we can generate sensible
5245default names.
5346
54- As for the ` raw_ ` wrappers, in order to avoid rewriting the partial
55- expression filling logic, their behaviour is very simplified:
47+ As for the alternative API, its syntax is much closer to that in einops,
48+ as it is string base, but it does add some extra constraints to the dimension names
49+ that are compatible with it.
50+
51+ To avoid rewriting the partial expression filling logic, their behaviour is very simplified:
56521 . Split the expression in two if possible using ` -> `
57532 . Convert each side to list of str/list/dict following the rules of the complete wrappers
58543 . Call the complete wrapper
5955
6056This has an extra and a bit hidden advantage. einops supports
6157_ explicit_ ellipsis but we don't, to us an ellipsis is not writing
6258the dimension name in the expression. Therefore, ` . ` are valid
63- in our ` raw_ ` expressions, we convert those to "full xarray" expressions
59+ in our string expressions, we convert those to "full xarray" expressions
6460which support everything and we don't need extra logic to handle dots either.
61+
62+ ## Examples
63+
64+ Given a {class}` ~xarray.DataArray ` ` da ` with dimensions ` a ` , ` b ` , ` c ` and ` d ` ,
65+ the table below shows the result of equivalent expressions
66+ and the dimensions (and order) present in their output:
67+
68+ ``` python
69+ # list syntax
70+ rearrange(da, [" c" , " d" , " a" , " b" ])
71+ # string syntax
72+ rearrange(da, " c d a b" )
73+ # dims in output: `c`, `d`, `a`, `b`
74+
75+ # ----------------------------
76+
77+ # list syntax
78+ rearrange(
79+ da,
80+ [{" e" : [" c" , " d" ]}, {" f" : [" a" , " b" ]}]
81+ )
82+ # string syntax
83+ rearrange(da, " (c d)=e (a b)=f" )
84+ # dims in output: `e`, `f`
85+
86+ # ----------------------------
87+
88+ # list syntax
89+ rearrange(
90+ da,
91+ [" a2" , " c" , " a1" , {" e" : [" d" , " b" ]}],
92+ pattern_in = [{" a" : [" a1" , " a2" ]}]
93+ )
94+ # string syntax
95+ rearrange(da, " (a1 a2)=a -> a1 c a2 (d b)=e" )
96+ # dims in output: `a1`, `c`, `a2`, `e`
97+ ```
0 commit comments