xarray_einstats.stats.get_default_dims#

xarray_einstats.stats.get_default_dims(dims)[source]#

Get default dims on which to perfom an operation.

Whenever a function from xarray_einstats.stats is called with dims=None (the default) this function is called to choose the default dims on which to operate out of the list with all the dims present.

This function is thought to be monkeypatched by domain specific applications as shown in the examples.

Parameters:
dimslist of str

List with all the dimensions of the input DataArray in the order they appear.

Returns:
list of str

List with the dimensions on which to apply the operation. xarray_einstats defaults to applying the operation to all dimensions. Monkeypatch this function to get a different result.

Examples

The xarray_einstats default behaviour is operating (averaging in this case) over all dimensions present in the input DataArray:

from xarray_einstats import stats, tutorial
da = tutorial.generate_mcmc_like_dataset(3)["mu"]
stats.hmean(da)
<xarray.DataArray 'mu' ()>
0.1086

Here we show how to monkeypatch get_default_dims to get a different default behaviour. If you use xarray_einstats and {doc}`arviz:index` to work with MCMC results, operating over chain and dim only might be a better default:

def get_default_dims(dims):
    out = [dim for dim in ("chain", "draw") if dim in dims]
    if not out:  # if chain nor draw are present fall back to all dims
        return dims
    return out
stats.get_default_dims = get_default_dims
stats.hmean(da)
<xarray.DataArray 'mu' (team: 6)>
0.03888 0.3158 0.0665 0.2233 0.2556 0.3426
Coordinates:
  * team     (team) <U1 'a' 'b' 'c' 'd' 'e' 'f'

You can still use dims explicitly to average over any custom dimension

stats.hmean(da, dims="team")
<xarray.DataArray 'mu' (chain: 4, draw: 10)>
0.3059 0.1958 0.6869 0.01057 0.1915 ... 0.4187 0.05455 0.7643 0.5093 0.4906
Coordinates:
  * chain    (chain) int64 0 1 2 3
  * draw     (draw) int64 0 1 2 3 4 5 6 7 8 9