{ "cells": [ { "cell_type": "markdown", "id": "b539ec69-af48-4780-9ab4-0dddc6755e33", "metadata": {}, "source": [ "# Subselecting Data" ] }, { "cell_type": "code", "execution_count": 1, "id": "010937e5-9ecd-41f7-8be3-0e489a3bf786", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%reload_ext autoreload\n", "%autoreload 2\n", "\n", "import spatialproteomics\n", "import pandas as pd\n", "import xarray as xr\n", "xr.set_options(display_style='text')" ] }, { "cell_type": "markdown", "id": "d0c0428c-6879-4cbd-8e19-1fa3039d16f6", "metadata": {}, "source": [ "One of the key features of `spatialproteomics` is the ability to slice our image data quickly and intuitively. We start by loading our _spatialproteomics_ object." ] }, { "cell_type": "code", "execution_count": 2, "id": "b563d01d-8498-4749-b532-9ca2a6e309c4", "metadata": { "tags": [] }, "outputs": [], "source": [ "ds = xr.load_dataset('../../data/BNHL_166_4_I2_LK_2.zarr', engine='zarr')" ] }, { "cell_type": "markdown", "id": "f4f44d5b-255d-47e6-b275-c0f36d20039b", "metadata": {}, "source": [ "## Slicing Channels and Spatial Coordinates\n", "\n", "To slice specific channels of the image we simply use `.pp` accessor together with the familiar bracket `[]` indexing." ] }, { "cell_type": "code", "execution_count": 3, "id": "e7ee6482-5fb0-4862-82dd-b30a2fb10b35", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n",
       "Dimensions:         (channels: 1, y: 3000, x: 3000, labels: 8, la_props: 2,\n",
       "                     cells: 12560, features: 3)\n",
       "Coordinates:\n",
       "  * cells           (cells) int64 1 2 3 4 5 6 ... 12556 12557 12558 12559 12560\n",
       "  * channels        (channels) <U11 'CD4'\n",
       "  * features        (features) <U10 '_labels' 'centroid-0' 'centroid-1'\n",
       "  * la_props        (la_props) <U6 '_color' '_name'\n",
       "  * labels          (labels) int64 1 2 3 4 5 6 7 8\n",
       "  * x               (x) int64 0 1 2 3 4 5 6 ... 2994 2995 2996 2997 2998 2999\n",
       "  * y               (y) int64 0 1 2 3 4 5 6 ... 2994 2995 2996 2997 2998 2999\n",
       "Data variables:\n",
       "    _image          (channels, y, x) uint8 1 1 2 0 0 0 0 1 1 ... 1 1 0 0 0 0 1 0\n",
       "    _la_properties  (labels, la_props) <U20 'C1' ... 'Vascular (CD31+CD34)'\n",
       "    _obs            (cells, features) float64 7.0 613.3 ... 2.249e+03 2.237e+03\n",
       "    _segmentation   (y, x) int64 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0
" ], "text/plain": [ "\n", "Dimensions: (channels: 1, y: 3000, x: 3000, labels: 8, la_props: 2,\n", " cells: 12560, features: 3)\n", "Coordinates:\n", " * cells (cells) int64 1 2 3 4 5 6 ... 12556 12557 12558 12559 12560\n", " * channels (channels) <xarray.Dataset>\n", "Dimensions: (channels: 2, y: 3000, x: 3000, labels: 8, la_props: 2,\n", " cells: 12560, features: 3)\n", "Coordinates:\n", " * cells (cells) int64 1 2 3 4 5 6 ... 12556 12557 12558 12559 12560\n", " * channels (channels) <U11 'CD4' 'CD8'\n", " * features (features) <U10 '_labels' 'centroid-0' 'centroid-1'\n", " * la_props (la_props) <U6 '_color' '_name'\n", " * labels (labels) int64 1 2 3 4 5 6 7 8\n", " * x (x) int64 0 1 2 3 4 5 6 ... 2994 2995 2996 2997 2998 2999\n", " * y (y) int64 0 1 2 3 4 5 6 ... 2994 2995 2996 2997 2998 2999\n", "Data variables:\n", " _image (channels, y, x) uint8 1 1 2 0 0 0 0 1 1 ... 1 1 2 1 1 1 2 1\n", " _la_properties (labels, la_props) <U20 'C1' ... 'Vascular (CD31+CD34)'\n", " _obs (cells, features) float64 7.0 613.3 ... 2.249e+03 2.237e+03\n", " _segmentation (y, x) int64 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0" ], "text/plain": [ "\n", "Dimensions: (channels: 2, y: 3000, x: 3000, labels: 8, la_props: 2,\n", " cells: 12560, features: 3)\n", "Coordinates:\n", " * cells (cells) int64 1 2 3 4 5 6 ... 12556 12557 12558 12559 12560\n", " * channels (channels) <xarray.Dataset>\n", "Dimensions: (channels: 56, y: 101, x: 101, labels: 8, la_props: 2,\n", " cells: 0, features: 3)\n", "Coordinates:\n", " * cells (cells) int64 \n", " * channels (channels) <U11 'DAPI' 'Helios' 'CD10' ... 'CD79a' 'Ki-67'\n", " * features (features) <U10 '_labels' 'centroid-0' 'centroid-1'\n", " * la_props (la_props) <U6 '_color' '_name'\n", " * labels (labels) int64 1 2 3 4 5 6 7 8\n", " * x (x) int64 50 51 52 53 54 55 56 ... 145 146 147 148 149 150\n", " * y (y) int64 50 51 52 53 54 55 56 ... 145 146 147 148 149 150\n", "Data variables:\n", " _image (channels, y, x) uint8 5 4 5 4 5 4 5 4 4 ... 2 2 2 1 2 2 2 2\n", " _la_properties (labels, la_props) <U20 'C1' ... 'Vascular (CD31+CD34)'\n", " _obs (cells, features) float64 \n", " _segmentation (y, x) int64 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0" ], "text/plain": [ "\n", "Dimensions: (channels: 56, y: 101, x: 101, labels: 8, la_props: 2,\n", " cells: 0, features: 3)\n", "Coordinates:\n", " * cells (cells) int64 \n", " * channels (channels) <xarray.Dataset>\n", "Dimensions: (channels: 2, y: 101, x: 101, labels: 8, la_props: 2,\n", " cells: 0, features: 3)\n", "Coordinates:\n", " * cells (cells) int64 \n", " * channels (channels) <U11 'CD4' 'CD8'\n", " * features (features) <U10 '_labels' 'centroid-0' 'centroid-1'\n", " * la_props (la_props) <U6 '_color' '_name'\n", " * labels (labels) int64 1 2 3 4 5 6 7 8\n", " * x (x) int64 50 51 52 53 54 55 56 ... 145 146 147 148 149 150\n", " * y (y) int64 50 51 52 53 54 55 56 ... 145 146 147 148 149 150\n", "Data variables:\n", " _image (channels, y, x) uint8 0 0 0 0 0 0 1 1 0 ... 2 1 1 1 1 1 4 1\n", " _la_properties (labels, la_props) <U20 'C1' ... 'Vascular (CD31+CD34)'\n", " _obs (cells, features) float64 \n", " _segmentation (y, x) int64 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0" ], "text/plain": [ "\n", "Dimensions: (channels: 2, y: 101, x: 101, labels: 8, la_props: 2,\n", " cells: 0, features: 3)\n", "Coordinates:\n", " * cells (cells) int64 \n", " * channels (channels) <xarray.Dataset>\n", "Dimensions: (channels: 56, y: 3000, x: 3000, labels: 1, la_props: 2,\n", " cells: 1073, features: 3)\n", "Coordinates:\n", " * channels (channels) <U11 'DAPI' 'Helios' 'CD10' ... 'CD79a' 'Ki-67'\n", " * features (features) <U10 '_labels' 'centroid-0' 'centroid-1'\n", " * la_props (la_props) <U6 '_color' '_name'\n", " * labels (labels) int64 4\n", " * x (x) int64 0 1 2 3 4 5 6 ... 2994 2995 2996 2997 2998 2999\n", " * y (y) int64 0 1 2 3 4 5 6 ... 2994 2995 2996 2997 2998 2999\n", " * cells (cells) int64 3 11 49 71 80 ... 12504 12516 12554 12558\n", "Data variables:\n", " _image (channels, y, x) uint8 4 4 4 4 5 4 4 3 4 ... 2 2 2 2 2 2 2 2\n", " _la_properties (labels, la_props) <U20 'C2' 'Lymphatic (PDPN)'\n", " _obs (cells, features) float64 4.0 774.5 ... 2.266e+03 2.232e+03\n", " _segmentation (y, x) int64 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0" ], "text/plain": [ "\n", "Dimensions: (channels: 56, y: 3000, x: 3000, labels: 1, la_props: 2,\n", " cells: 1073, features: 3)\n", "Coordinates:\n", " * channels (channels) <xarray.Dataset>\n", "Dimensions: (channels: 56, y: 3000, x: 3000, labels: 1, la_props: 2,\n", " cells: 5891, features: 3)\n", "Coordinates:\n", " * channels (channels) <U11 'DAPI' 'Helios' 'CD10' ... 'CD79a' 'Ki-67'\n", " * features (features) <U10 '_labels' 'centroid-0' 'centroid-1'\n", " * la_props (la_props) <U6 '_color' '_name'\n", " * labels (labels) int64 7\n", " * x (x) int64 0 1 2 3 4 5 6 ... 2994 2995 2996 2997 2998 2999\n", " * y (y) int64 0 1 2 3 4 5 6 ... 2994 2995 2996 2997 2998 2999\n", " * cells (cells) int64 1 4 8 10 12 ... 12444 12469 12473 12505 12522\n", "Data variables:\n", " _image (channels, y, x) uint8 4 4 4 4 5 4 4 3 4 ... 2 2 2 2 2 2 2 2\n", " _la_properties (labels, la_props) <U20 'C0' 'T (CD3)'\n", " _obs (cells, features) float64 7.0 613.3 ... 2.346e+03 1.865e+03\n", " _segmentation (y, x) int64 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0" ], "text/plain": [ "\n", "Dimensions: (channels: 56, y: 3000, x: 3000, labels: 1, la_props: 2,\n", " cells: 5891, features: 3)\n", "Coordinates:\n", " * channels (channels) <xarray.Dataset>\n", "Dimensions: (channels: 56, y: 3000, x: 3000, labels: 3, la_props: 2,\n", " cells: 3571, features: 3)\n", "Coordinates:\n", " * channels (channels) <U11 'DAPI' 'Helios' 'CD10' ... 'CD79a' 'Ki-67'\n", " * features (features) <U10 '_labels' 'centroid-0' 'centroid-1'\n", " * la_props (la_props) <U6 '_color' '_name'\n", " * labels (labels) int64 4 5 6\n", " * x (x) int64 0 1 2 3 4 5 6 ... 2994 2995 2996 2997 2998 2999\n", " * y (y) int64 0 1 2 3 4 5 6 ... 2994 2995 2996 2997 2998 2999\n", " * cells (cells) int64 2 3 5 6 9 11 ... 12539 12554 12555 12557 12558\n", "Data variables:\n", " _image (channels, y, x) uint8 4 4 4 4 5 4 4 3 4 ... 2 2 2 2 2 2 2 2\n", " _la_properties (labels, la_props) <U20 'C2' ... 'Stroma (CD90)'\n", " _obs (cells, features) float64 5.0 769.1 ... 2.266e+03 2.232e+03\n", " _segmentation (y, x) int64 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0" ], "text/plain": [ "\n", "Dimensions: (channels: 56, y: 3000, x: 3000, labels: 3, la_props: 2,\n", " cells: 3571, features: 3)\n", "Coordinates:\n", " * channels (channels) <xarray.Dataset>\n", "Dimensions: (channels: 56, y: 3000, x: 3000, labels: 7, la_props: 2,\n", " cells: 10488, features: 3)\n", "Coordinates:\n", " * channels (channels) <U11 'DAPI' 'Helios' 'CD10' ... 'CD79a' 'Ki-67'\n", " * features (features) <U10 '_labels' 'centroid-0' 'centroid-1'\n", " * la_props (la_props) <U6 '_color' '_name'\n", " * labels (labels) int64 2 3 4 5 6 7 8\n", " * x (x) int64 0 1 2 3 4 5 6 ... 2994 2995 2996 2997 2998 2999\n", " * y (y) int64 0 1 2 3 4 5 6 ... 2994 2995 2996 2997 2998 2999\n", " * cells (cells) int64 1 2 3 4 5 6 ... 10484 10485 10486 10487 10488\n", "Data variables:\n", " _image (channels, y, x) uint8 4 4 4 4 5 4 4 3 4 ... 2 2 2 2 2 2 2 2\n", " _la_properties (labels, la_props) <U20 '#FFFF00' ... 'Vascular (CD31+CD34)'\n", " _obs (cells, features) float64 7.0 613.3 ... 2.266e+03 2.232e+03\n", " _segmentation (y, x) int64 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0" ], "text/plain": [ "\n", "Dimensions: (channels: 56, y: 3000, x: 3000, labels: 7, la_props: 2,\n", " cells: 10488, features: 3)\n", "Coordinates:\n", " * channels (channels) <xarray.Dataset>\n", "Dimensions: (cells: 6901, celltype_levels: 3, channels: 56,\n", " y: 2000, x: 2000, labels: 9, la_props: 2,\n", " neighborhoods: 4, nh_props: 2, features: 18)\n", "Coordinates:\n", " * cells (cells) int64 1 2 3 4 5 ... 6897 6898 6899 6900 6901\n", " * celltype_levels (celltype_levels) <U8 'labels' 'labels_1' 'labels_2'\n", " * channels (channels) <U11 'DAPI' 'TIM3' ... 'ki-67' 'CD38'\n", " * features (features) <U14 'BCL-2' 'BCL-6' ... 'ki-67'\n", " * la_props (la_props) <U6 '_color' '_name'\n", " * labels (labels) int64 1 2 3 4 5 6 7 8 9\n", " * neighborhoods (neighborhoods) int64 0 1 3 4\n", " * nh_props (nh_props) <U6 '_color' '_name'\n", " * x (x) int64 0 1 2 3 4 5 ... 1995 1996 1997 1998 1999\n", " * y (y) int64 0 1 2 3 4 5 ... 1995 1996 1997 1998 1999\n", "Data variables:\n", " _celltype_predictions (cells, celltype_levels) <U11 'T' 'T' ... 'T_tox'\n", " _image (channels, y, x) uint8 1 1 1 0 1 1 0 ... 3 1 2 3 2 3\n", " _la_properties (labels, la_props) <U11 '#e6194B' 'B' ... 'T'\n", " _nh_properties (neighborhoods, nh_props) <U14 'lightgreen' ... 'N...\n", " _obs (cells, features) float64 1.0 1.0 0.0 ... 904.5 0.0\n", " _segmentation (y, x) int64 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0" ], "text/plain": [ "\n", "Dimensions: (cells: 6901, celltype_levels: 3, channels: 56,\n", " y: 2000, x: 2000, labels: 9, la_props: 2,\n", " neighborhoods: 4, nh_props: 2, features: 18)\n", "Coordinates:\n", " * cells (cells) int64 1 2 3 4 5 ... 6897 6898 6899 6900 6901\n", " * celltype_levels (celltype_levels) <xarray.Dataset>\n", "Dimensions: (cells: 709, celltype_levels: 3, channels: 56,\n", " y: 2000, x: 2000, labels: 9, la_props: 2,\n", " neighborhoods: 1, nh_props: 2, features: 18)\n", "Coordinates:\n", " * celltype_levels (celltype_levels) <U8 'labels' 'labels_1' 'labels_2'\n", " * channels (channels) <U11 'DAPI' 'TIM3' ... 'ki-67' 'CD38'\n", " * features (features) <U14 'BCL-2' 'BCL-6' ... 'ki-67'\n", " * la_props (la_props) <U6 '_color' '_name'\n", " * labels (labels) int64 1 2 3 4 5 6 7 8 9\n", " * neighborhoods (neighborhoods) int64 0\n", " * nh_props (nh_props) <U6 '_color' '_name'\n", " * x (x) int64 0 1 2 3 4 5 ... 1995 1996 1997 1998 1999\n", " * y (y) int64 0 1 2 3 4 5 ... 1995 1996 1997 1998 1999\n", " * cells (cells) int64 1571 1858 1895 1912 ... 6204 6247 6293\n", "Data variables:\n", " _celltype_predictions (cells, celltype_levels) <U11 'Dendritic' ... 'T'\n", " _image (channels, y, x) uint8 1 1 1 0 1 1 0 ... 3 1 2 3 2 3\n", " _la_properties (labels, la_props) <U11 '#e6194B' 'B' ... 'T'\n", " _nh_properties (neighborhoods, nh_props) <U14 'lightgreen' 'Neigh...\n", " _obs (cells, features) float64 0.0 1.0 0.0 ... 395.1 0.0\n", " _segmentation (y, x) int64 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0" ], "text/plain": [ "\n", "Dimensions: (cells: 709, celltype_levels: 3, channels: 56,\n", " y: 2000, x: 2000, labels: 9, la_props: 2,\n", " neighborhoods: 1, nh_props: 2, features: 18)\n", "Coordinates:\n", " * celltype_levels (celltype_levels)