{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "(einops_tutorial)=\n", "# Einops tutorial (ported)\n", "\n", "This tutorial is a port using `xarray-einstats` of the [einops basics tutorial](https://einops.rocks/1-einops-basics)\n", "\n", "## Einops meets xarray!\n", "\n", "We don't write \n", "```python\n", "y = x.transpose(0, 2, 3, 1)\n", "```\n", "nor we write the more comprehensible alternative\n", "```python\n", "y = rearrange(x, 'b c h w -> b h w c')\n", "```\n", "we write comprehensible code and use labeled arrays\n", "\n", "```python\n", "y_da = rearrange(x_da, 'batch height width channel')\n", "# or, also equivalent\n", "y_da = x_da.einops.rearrange('batch height width channel')\n", "```\n", "`x_da` is a {class}`xarray.DataArray` whose dimensions are already labeled, thus,\n", "we can skip the left side that defines the names of the dimensions.\n", "\n", "`xarray-einstats` wraps [`einops`](https://einops.rocks/) functions to extend them to work on xarray objects.\n", "\n", "## What's in this tutorial?\n", "\n", "- fundamentals: reordering, composition and decomposition of axes\n", "- operations: `rearrange`, `reduce`, `repeat`\n", "- how much you can do with a single operation!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Preparations" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Examples are given for numpy. This code also setups ipython/jupyter\n", "# so that numpy arrays in the output are displayed as images\n", "import numpy\n", "import xarray\n", "from xarray_einstats.tutorial import display_np_arrays_as_images\n", "\n", "display_np_arrays_as_images()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ":::{note}\n", "This cell above configures jupyter to display _numpy_ arrays as images, which is a great visual help to understand\n", "the operations performed by einops. To take advantage of this we use `.values`. In some specific cases, we also omit it in order to show the values of the dimensions of the DataArray. If you are running this yourself we encourage you to try both views\n", ":::" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load a batch of images to play with\n", "\n", ":::{admonition} Download the data for local use\n", ":class: dropdown\n", "\n", "The images are stored as a [zarr store](https://zarr.dev/) in the xarray-einstats repo. You can download it directly from your browser using https://download-directory.github.io/ and\n", "pasting [https://github.com/arviz-devs/xarray-einstats/tree/main/docs/source/tutorials/einops-image.zarr](https://github.com/arviz-devs/xarray-einstats/tree/main/docs/source/tutorials/einops-image.zarr) there. Then move the file if necessary, uncompress and rename it so you can load it with {func}`~xarray.open_zarr` as shown below.\n", ":::" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset> Size: 1MB\n", "Dimensions: (batch: 6, height: 96, width: 96, channel: 3)\n", "Dimensions without coordinates: batch, height, width, channel\n", "Data variables:\n", " ims (batch, height, width, channel) float64 1MB 1.0 0.902 ... 0.8039