{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Segmentation\n", "Segmentation describes the process of finding cells in an image. This can be done either on the nucleus or on whole cell level. Spatialproteomics provides wrappers for StarDist, mesmer and cellpose." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [], "source": [ "%reload_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [] }, "outputs": [], "source": [ "import spatialproteomics\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import xarray as xr" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [] }, "outputs": [], "source": [ "# loading the input data\n", "ds = xr.open_zarr(\"../../data/segmentation_example.zarr\").pp[1000:1300, 1000:1300].pp.drop_layers(keep=\"_image\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n", "Dimensions: (channels: 4, y: 301, x: 301)\n", "Coordinates:\n", " * channels (channels) <U11 'DAPI' 'Na/K ATPase' 'CD68' 'CD11c'\n", " * x (x) int64 1000 1001 1002 1003 1004 ... 1296 1297 1298 1299 1300\n", " * y (y) int64 1000 1001 1002 1003 1004 ... 1296 1297 1298 1299 1300\n", "Data variables:\n", " _image (channels, y, x) uint8 dask.array<chunksize=(4, 301, 301), meta=np.ndarray>
<xarray.Dataset>\n", "Dimensions: (channels: 4, y: 301, x: 301, cells: 330, features: 2)\n", "Coordinates:\n", " * channels (channels) <U11 'DAPI' 'Na/K ATPase' 'CD68' 'CD11c'\n", " * x (x) int64 1000 1001 1002 1003 1004 ... 1297 1298 1299 1300\n", " * y (y) int64 1000 1001 1002 1003 1004 ... 1297 1298 1299 1300\n", " * cells (cells) int64 1 2 3 4 5 6 7 8 ... 324 325 326 327 328 329 330\n", " * features (features) <U10 'centroid-0' 'centroid-1'\n", "Data variables:\n", " _image (channels, y, x) uint8 dask.array<chunksize=(4, 301, 301), meta=np.ndarray>\n", " _segmentation (y, x) uint16 0 1 1 1 1 1 1 1 ... 324 324 324 324 324 324 0\n", " _obs (cells, features) float64 1.005e+03 1.011e+03 ... 1.003e+03
<xarray.Dataset>\n", "Dimensions: (channels: 3, y: 301, x: 301)\n", "Coordinates:\n", " * channels (channels) <U11 'CD11c' 'CD68' 'DAPI'\n", " * x (x) int64 1000 1001 1002 1003 ... 1298 1299 1300\n", " * y (y) int64 1000 1001 1002 1003 ... 1298 1299 1300\n", "Data variables:\n", " _image (channels, y, x) uint8 dask.array<chunksize=(3, 301, 301), meta=np.ndarray>\n", " _cellpose_segmentation (channels, y, x) uint16 0 0 0 0 0 0 ... 320 0 0 0 0
<xarray.Dataset>\n", "Dimensions: (channels: 3, y: 301, x: 301)\n", "Coordinates:\n", " * channels (channels) <U11 'CD11c' 'CD68' 'DAPI'\n", " * x (x) int64 1000 1001 1002 1003 ... 1298 1299 1300\n", " * y (y) int64 1000 1001 1002 1003 ... 1298 1299 1300\n", "Data variables:\n", " _image (channels, y, x) uint8 dask.array<chunksize=(3, 301, 301), meta=np.ndarray>\n", " _cellpose_segmentation (channels, y, x) uint16 0 0 0 0 0 0 ... 320 0 0 0 0\n", " _merged_segmentation (y, x) uint16 0 0 0 0 0 25 25 25 ... 0 0 0 0 0 0 0 0
<xarray.Dataset>\n", "Dimensions: (labels: 3, la_props: 2, channels: 3, x: 301,\n", " y: 301, cells: 296, features: 3)\n", "Coordinates:\n", " * labels (labels) int64 1 2 3\n", " * la_props (la_props) <U6 '_color' '_name'\n", " * channels (channels) <U11 'CD11c' 'CD68' 'DAPI'\n", " * x (x) int64 1000 1001 1002 1003 ... 1298 1299 1300\n", " * y (y) int64 1000 1001 1002 1003 ... 1298 1299 1300\n", " * cells (cells) int64 1 2 3 4 5 6 ... 292 293 294 295 296\n", " * features (features) <U10 '_labels' 'centroid-0' 'centroid-1'\n", "Data variables:\n", " _la_properties (labels, la_props) object '#C8A1A1' ... 'Macrophage'\n", " _image (channels, y, x) uint8 dask.array<chunksize=(3, 301, 301), meta=np.ndarray>\n", " _cellpose_segmentation (channels, y, x) uint16 0 0 0 0 0 0 ... 320 0 0 0 0\n", " _merged_segmentation (y, x) uint16 0 0 0 0 0 25 25 25 ... 0 0 0 0 0 0 0 0\n", " _obs (cells, features) float64 1.0 1.04e+03 ... 1.003e+03\n", " _segmentation (y, x) uint16 0 0 0 0 0 25 25 25 ... 0 0 0 0 0 0 0 0
<xarray.Dataset>\n", "Dimensions: (channels: 4, y: 301, x: 301)\n", "Coordinates:\n", " * channels (channels) <U11 'DAPI' 'Na/K ATPase' 'CD68' 'CD11c'\n", " * x (x) int64 1000 1001 1002 1003 1004 ... 1297 1298 1299 1300\n", " * y (y) int64 1000 1001 1002 1003 1004 ... 1297 1298 1299 1300\n", "Data variables:\n", " _image (channels, y, x) uint8 dask.array<chunksize=(4, 301, 301), meta=np.ndarray>\n", " dapi_seg (y, x) int64 11 11 11 11 11 11 11 ... 348 348 348 348 348 348\n", " macro_seg (y, x) uint16 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0\n", " dendritic_seg (y, x) uint16 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0