{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Extract segmentation features\n", "=============================\n", "\n", "This example shows how to extract segmentation features from the tissue\n", "image.\n", "\n", "Features extracted from a nucleus segmentation range from the number of\n", "nuclei per image, over nuclei shapes and sizes, to the intensity of the\n", "input channels within the segmented objects. They are very interpretable\n", "features and provide valuable additional information. Segmentation\n", "features are calculated by using `features = 'segmentation'`, which\n", "calls {func}`squidpy.im.ImageContainer.features_segmentation`.\n", "\n", "In addition to `feature_name` and `channels` we can specify the\n", "following `features_kwargs`:\n", "\n", "- `label_layer` - name of label image layer in `img`.\n", "- `props` - segmentation features that are calculated. See\n", " [properties]{.title-ref} in `skimage.measure.regionprops_table`.\n", "\n", ":::{seealso}\n", "- {doc}`compute_segment_fluo` for more\n", " details on calculating a cell-segmentation.\n", "- {doc}`compute_features` for the general\n", " usage of {func}`squidpy.im.calculate_image_features`.\n", " \n", ":::\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "import squidpy as sq" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "First, let\\'s load the fluorescence Visium dataset.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "img = sq.datasets.visium_fluo_image_crop()\n", "adata = sq.datasets.visium_fluo_adata_crop()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Before calculating segmentation features, we need to first calculate a\n", "segmentation using `squidpy.im.segment`.\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sq.im.segment(\n", " img=img,\n", " layer=\"image\",\n", " layer_added=\"segmented_watershed\",\n", " method=\"watershed\",\n", " channel=0,\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Now we can calculate segmentation features. Here, we will calculate the\n", "following features:\n", "\n", "> - number of nuclei `label`.\n", "> - mean area of nuclei `area`.\n", "> - mean intensity of channels 1 (anti-NEUN) and 2 (anti-GFAP) within\n", "> nuclei `mean_intensity`.\n", "\n", "We use `mask_cicle = True` to ensure that we are only extracting\n", "features from the tissue underneath each Visium spot. For more details\n", "on the image cropping, see\n", "`examples_image_compute_crops`.\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6c1eb35afea64b6a8eec6b65f35fec93", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/704 [00:00, ?/s]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sq.im.calculate_image_features(\n", " adata,\n", " img,\n", " layer=\"image\",\n", " features=\"segmentation\",\n", " key_added=\"segmentation_features\",\n", " features_kwargs={\n", " \"segmentation\": {\n", " \"label_layer\": \"segmented_watershed\",\n", " \"props\": [\"label\", \"area\", \"mean_intensity\"],\n", " \"channels\": [1, 2],\n", " }\n", " },\n", " mask_circle=True,\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The result is stored in {attr}`anndata.AnnData.obsm`\\ `['segmentation_features']`.\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
| \n", " | segmentation_label | \n", "segmentation_area_mean | \n", "segmentation_area_std | \n", "segmentation_ch-1_mean_intensity_mean | \n", "segmentation_ch-1_mean_intensity_std | \n", "segmentation_ch-2_mean_intensity_mean | \n", "segmentation_ch-2_mean_intensity_std | \n", "
|---|---|---|---|---|---|---|---|
| AAACGAGACGGTTGAT-1 | \n", "17 | \n", "174.764706 | \n", "291.276810 | \n", "5604.069561 | \n", "3100.506862 | \n", "8997.290710 | \n", "177.888882 | \n", "
| AAAGGGATGTAGCAAG-1 | \n", "14 | \n", "100.785714 | \n", "80.946348 | \n", "5034.146353 | \n", "1625.737796 | \n", "10376.489346 | \n", "564.254124 | \n", "
| AAATGGCATGTCTTGT-1 | \n", "16 | \n", "132.000000 | \n", "147.241723 | \n", "11527.768307 | \n", "12227.308457 | \n", "7725.282284 | \n", "947.987907 | \n", "
| AAATGGTCAATGTGCC-1 | \n", "9 | \n", "243.000000 | \n", "132.341310 | \n", "3581.244911 | \n", "46.124320 | \n", "9664.505991 | \n", "1331.259644 | \n", "
| AAATTAACGGGTAGCT-1 | \n", "7 | \n", "229.142857 | \n", "203.573383 | \n", "9038.077440 | \n", "8707.493743 | \n", "10922.808071 | \n", "3631.149215 | \n", "