xarray-einstats

0.10.0 · active · verified Fri Apr 10

xarray-einstats is a Python library that provides a high-level API to combine the symbolic array manipulation of `einstats` with `xarray`'s labeled dimensions for statistical operations and linear algebra. It aims to simplify common data analysis tasks on `xarray.DataArray` and `xarray.Dataset` objects, enabling operations like reduction, broadcasting, and reshaping with `einops`-like patterns. The current version is 0.10.0, and it generally follows an irregular release cadence, with updates typically driven by new features or important bug fixes.

Warnings

Install

Imports

Quickstart

This example demonstrates creating an xarray DataArray and then using `xarray-einstats.stats.mean` to calculate the mean along a specific dimension. It also shows a basic `einops`-like pattern for combining and summing dimensions using a dictionary in the `dims` argument.

import xarray as xr
import numpy as np
import xarray_einstats.stats as xestats

# Create a dummy xarray DataArray
data = xr.DataArray(
    np.random.normal(size=(2, 3, 4)),
    coords={"a": [0, 1], "b": [0, 1, 2], "c": [0, 1, 2, 3]},
    dims=["a", "b", "c"],
)

print("Original data:\n", data)

# Calculate the mean along dimension 'b'
mean_b = xestats.mean(data, dims="b")
print("\nMean along 'b' dimension:\n", mean_b)

# Use einops-like patterns for more complex operations
# Reshape 'b c' into a new dimension '(b c)' and then sum
sum_bc_flattened = xestats.sum(data, dims={"b c": "_"}) # '_' signifies a new dimension combining 'b' and 'c'
print("\nSum after flattening 'b' and 'c' dimensions:\n", sum_bc_flattened)

view raw JSON →