From 6107e92044a3ca483723a9b0fe61dd34024b4fe2 Mon Sep 17 00:00:00 2001 From: ZeyanAhmadButt Date: Thu, 26 Nov 2026 00:46:37 +0500 Subject: [PATCH 1/2] DOC: expand microscopy image stack tutorial --- intermediate/BiologyDataset.ipynb | 128 ++++++++++++++++++++++-------- 1 file changed, 96 insertions(+), 32 deletions(-) diff --git a/intermediate/BiologyDataset.ipynb b/intermediate/BiologyDataset.ipynb index 7d4d6ced..c586082e 100644 --- a/intermediate/BiologyDataset.ipynb +++ b/intermediate/BiologyDataset.ipynb @@ -2,39 +2,39 @@ "cells": [ { "cell_type": "markdown", - "id": "0", + "id": "intro", "metadata": {}, "source": [ - "# Datasets in other fields\n", + "# Exploring a microscopy image stack with Xarray\n", "\n", - "\n", - "While there are many Xarray examples from geosciences the Xarray data model is not constrained to that field. Our examples so far solve problems that are highly analogous to the problems faced in biology. In this notebook we will load a dataset based on the [cells3d](https://scikit-image.org/docs/0.25.x/api/skimage.data.html#skimage.data.cells3d) dataset from scikit-image." + "Xarray is useful beyond geoscience: microscopy images are also naturally multi-dimensional. This short example uses the `cells3d` tutorial dataset, which contains two fluorescence channels and a segmentation mask. It demonstrates how named dimensions and coordinates make it easier to inspect, select, crop, and visualize an image volume." ] }, { "cell_type": "code", "execution_count": null, - "id": "1", + "id": "imports", "metadata": {}, "outputs": [], "source": [ + "import matplotlib.pyplot as plt\n", "import xarray as xr" ] }, { "cell_type": "markdown", - "id": "2", + "id": "load", "metadata": {}, "source": [ - "## Loading the Dataset\n", + "## Load and inspect the data\n", "\n", - "This dataset follows a classic pattern in microscopy where we have a stack of images, with two fluoresence channels. In addition to that we have a mask (or labels layer) that identifies individual cells " + "The tutorial loader supplies a small example dataset, so no separate download or domain-specific reader is needed. Inspecting the dimensions first helps make the array structure explicit: `c` is the channel, `z` is depth, and `y` and `x` are the image axes." ] }, { "cell_type": "code", "execution_count": null, - "id": "3", + "id": "dataset", "metadata": {}, "outputs": [], "source": [ @@ -42,77 +42,141 @@ "ds" ] }, + { + "cell_type": "markdown", + "id": "dims", + "metadata": {}, + "source": [ + "The `images` variable holds the fluorescence data and `mask` holds segmentation labels. The channel coordinate lets us select a named channel instead of remembering an integer axis position. These coordinates identify positions in the sample image; they do not encode physical distances." + ] + }, { "cell_type": "code", "execution_count": null, - "id": "4", + "id": "inspect", "metadata": {}, "outputs": [], "source": [ - "ds[\"images\"].max(\"z\").sel(c=\"nuclei\").plot()" + "images = ds[\"images\"]\n", + "print(\"Image dimensions:\", images.dims)\n", + "print(\"Image shape:\", images.shape)\n", + "print(\"Channels:\", images.coords[\"c\"].values)\n", + "print(\"Mask dimensions:\", ds[\"mask\"].dims)" + ] + }, + { + "cell_type": "markdown", + "id": "plane", + "metadata": {}, + "source": [ + "## Select a channel and depth plane\n", + "\n", + "Use `.sel` for coordinate labels such as the channel name. Use `.isel` when selecting by integer position. Here, `method=\"nearest\"` makes the depth selection work even if the requested coordinate is not an exact match." ] }, { "cell_type": "code", "execution_count": null, - "id": "5", + "id": "select-plane", "metadata": {}, "outputs": [], "source": [ - "ds[\"images\"].sel(z=9, method=\"nearest\").sel(c=\"membrane\").plot()" + "nuclei = images.sel(c=\"nuclei\")\n", + "plane = nuclei.sel(z=9, method=\"nearest\")\n", + "plane.plot(cmap=\"gray\")" + ] + }, + { + "cell_type": "markdown", + "id": "crop", + "metadata": {}, + "source": [ + "A labeled crop can be made along the image axes while keeping the depth stack. Taking the maximum across `z` produces a maximum-intensity projection: each pixel shows the brightest value encountered through the selected depth range." ] }, { "cell_type": "code", "execution_count": null, - "id": "6", + "id": "crop-projection", "metadata": {}, "outputs": [], "source": [ - "ds[\"images\"].max(\"z\").sel(c=\"nuclei\").plot()\n", - "ds[\"mask\"].plot.contour(colors=\"Red\")" + "crop = nuclei.isel(z=slice(10, 20), y=slice(80, 180), x=slice(80, 180))\n", + "projection = crop.max(dim=\"z\")\n", + "projection.plot(cmap=\"gray\")" + ] + }, + { + "cell_type": "markdown", + "id": "profile", + "metadata": {}, + "source": [ + "## Summarize intensity across the image\n", + "\n", + "Reducing over `y` and `x` leaves one value per depth plane. Xarray keeps the `z` coordinate attached, so the result can be plotted directly and remains easy to label or align with other depth-dependent measurements." ] }, { "cell_type": "code", "execution_count": null, - "id": "7", + "id": "depth-profile", "metadata": {}, "outputs": [], "source": [ - "ds[\"mask\"].plot()" + "depth_profile = nuclei.mean(dim=(\"y\", \"x\"))\n", + "depth_profile.plot()\n", + "plt.title(\"Mean nuclei-channel intensity by depth\")\n", + "plt.show()" ] }, { "cell_type": "markdown", - "id": "8", + "id": "overlay", "metadata": {}, "source": [ - "## Allows high level computations.\n", + "## Compare an image with its segmentation mask\n", "\n", - "We won't get into here - but this structuring data allows you to leverage Xarray to do powerful operations such as computing per cell properties. See the [Computational Patterns Notebook](./computation/01-high-level-computation-patterns.ipynb) for more." + "The mask labels regions in the same image coordinates. Overlaying the nonzero labels on the fluorescence plane illustrates how Xarray aligns arrays using their named coordinates. The mask is a segmentation result; it is not another fluorescence channel." ] }, { "cell_type": "code", "execution_count": null, - "id": "9", + "id": "overlay-code", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "labels = ds[\"mask\"].sel(z=plane.coords[\"z\"])\n", + "fig, ax = plt.subplots(figsize=(7, 6))\n", + "plane.plot(ax=ax, cmap=\"gray\", add_colorbar=False)\n", + "labels.where(labels > 0).plot(ax=ax, cmap=\"tab20\", alpha=0.45, add_colorbar=False)\n", + "ax.set_title(\"Nuclei fluorescence with segmentation labels\")\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "id": "takeaways", + "metadata": {}, + "source": [ + "## Takeaways\n", + "\n", + "- Named dimensions make the structure of an image volume easier to understand.\n", + "- Coordinate labels support readable selection and keep plots aligned after reductions.\n", + "- The same Xarray operations used for geospatial grids also work for microscopy data.\n", + "\n", + "This example builds on the non-geoscience tutorial request in [issue #172](https://github.com/xarray-contrib/xarray-tutorial/issues/172)." + ] } ], "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3" + "name": "python" } }, "nbformat": 4, From 3bc73b2d11c7492259543fe11a2e48dd7f4bfc25 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:49:36 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- intermediate/BiologyDataset.ipynb | 35 +++++++++++++------------------ 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/intermediate/BiologyDataset.ipynb b/intermediate/BiologyDataset.ipynb index c586082e..c5490fe4 100644 --- a/intermediate/BiologyDataset.ipynb +++ b/intermediate/BiologyDataset.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "intro", + "id": "0", "metadata": {}, "source": [ "# Exploring a microscopy image stack with Xarray\n", @@ -13,7 +13,7 @@ { "cell_type": "code", "execution_count": null, - "id": "imports", + "id": "1", "metadata": {}, "outputs": [], "source": [ @@ -23,7 +23,7 @@ }, { "cell_type": "markdown", - "id": "load", + "id": "2", "metadata": {}, "source": [ "## Load and inspect the data\n", @@ -34,7 +34,7 @@ { "cell_type": "code", "execution_count": null, - "id": "dataset", + "id": "3", "metadata": {}, "outputs": [], "source": [ @@ -44,7 +44,7 @@ }, { "cell_type": "markdown", - "id": "dims", + "id": "4", "metadata": {}, "source": [ "The `images` variable holds the fluorescence data and `mask` holds segmentation labels. The channel coordinate lets us select a named channel instead of remembering an integer axis position. These coordinates identify positions in the sample image; they do not encode physical distances." @@ -53,7 +53,7 @@ { "cell_type": "code", "execution_count": null, - "id": "inspect", + "id": "5", "metadata": {}, "outputs": [], "source": [ @@ -66,7 +66,7 @@ }, { "cell_type": "markdown", - "id": "plane", + "id": "6", "metadata": {}, "source": [ "## Select a channel and depth plane\n", @@ -77,7 +77,7 @@ { "cell_type": "code", "execution_count": null, - "id": "select-plane", + "id": "7", "metadata": {}, "outputs": [], "source": [ @@ -88,7 +88,7 @@ }, { "cell_type": "markdown", - "id": "crop", + "id": "8", "metadata": {}, "source": [ "A labeled crop can be made along the image axes while keeping the depth stack. Taking the maximum across `z` produces a maximum-intensity projection: each pixel shows the brightest value encountered through the selected depth range." @@ -97,7 +97,7 @@ { "cell_type": "code", "execution_count": null, - "id": "crop-projection", + "id": "9", "metadata": {}, "outputs": [], "source": [ @@ -108,7 +108,7 @@ }, { "cell_type": "markdown", - "id": "profile", + "id": "10", "metadata": {}, "source": [ "## Summarize intensity across the image\n", @@ -119,7 +119,7 @@ { "cell_type": "code", "execution_count": null, - "id": "depth-profile", + "id": "11", "metadata": {}, "outputs": [], "source": [ @@ -131,7 +131,7 @@ }, { "cell_type": "markdown", - "id": "overlay", + "id": "12", "metadata": {}, "source": [ "## Compare an image with its segmentation mask\n", @@ -142,7 +142,7 @@ { "cell_type": "code", "execution_count": null, - "id": "overlay-code", + "id": "13", "metadata": {}, "outputs": [], "source": [ @@ -156,7 +156,7 @@ }, { "cell_type": "markdown", - "id": "takeaways", + "id": "14", "metadata": {}, "source": [ "## Takeaways\n", @@ -170,11 +170,6 @@ } ], "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, "language_info": { "name": "python" }