Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 87 additions & 28 deletions intermediate/BiologyDataset.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
"id": "0",
"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."
]
},
{
Expand All @@ -18,6 +17,7 @@
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import xarray as xr"
]
},
Expand All @@ -26,9 +26,9 @@
"id": "2",
"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."
]
},
{
Expand All @@ -42,77 +42,136 @@
"ds"
]
},
{
"cell_type": "markdown",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4",
"id": "5",
"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": "6",
"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": "7",
"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": "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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6",
"id": "9",
"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": "10",
"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": "11",
"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": "12",
"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": "13",
"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": "14",
"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": {
"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,
Expand Down