{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Extract texture features\n", "========================\n", "\n", "This example shows how to extract texture features from the tissue\n", "image.\n", "\n", "Textures features give give a measure of how the image intensity at\n", "different distances and angles varies by calculating a grey-level\n", "co-occurrence matrix\n", "([GLCM](https://en.wikipedia.org/wiki/Co-occurrence_matrix)). The GLCM\n", "includes the number of times that grey-level $j$ occurs at a distance\n", "$d$ and at an angle $\\\\theta$ from grey-level $i$. From this data,\n", "different features (`props`) are calculated. See also\n", "`skimage.feature.greycomatrix`.\n", "\n", "Texture features are calculated by using `features = 'texture'`, which\n", "calls {func}`squidpy.im.ImageContainer.features_texture`. In addition to\n", "`feature_name` and `channels`, we can also specify the following\n", "`features_kwargs`:\n", "\n", "> - `distances` - distances that are taken into account for finding\n", "> repeating patterns.\n", "> - `angles` - range on which values are binned. Default is the whole\n", "> image range.\n", "> - `props` - texture features that are extracted from the GLCM.\n", "\n", ":::{seealso}\n", "\n", " See {doc}`compute_features` for general usage\n", " of {func}`squidpy.im.calculate_image_features`.\n", " \n", ":::\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import squidpy as sq" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Let\\'s load the fluorescence Visium dataset and calculate texture\n", "features with default `features_kwargs`.\n", "\n", "Note that for texture features it may make sense to compute them over a\n", "larger crop size to include more context, e.g., `spot_scale = 2` or\n", "`spit_scale = 4` which will extract crops with double or four times the\n", "radius than the original Visium spot size. For more details on the image\n", "cropping, See {doc}`compute_crops`.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# get spatial dataset including high-resolution tissue image\n", "img = sq.datasets.visium_fluo_image_crop()\n", "adata = sq.datasets.visium_fluo_adata_crop()\n", "\n", "# calculate texture features and save in key \"texture_features\"\n", "sq.im.calculate_image_features(\n", " adata,\n", " img,\n", " features=\"texture\",\n", " key_added=\"texture_features\",\n", " spot_scale=2,\n", " show_progress_bar=False,\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The result is stored in {attr}`anndata.AnnData.obsm`\\ `['texture_features']`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
| \n", " | texture_ch-0_contrast_dist-1_angle-0.00 | \n", "texture_ch-0_contrast_dist-1_angle-0.79 | \n", "texture_ch-0_contrast_dist-1_angle-1.57 | \n", "texture_ch-0_contrast_dist-1_angle-2.36 | \n", "texture_ch-0_dissimilarity_dist-1_angle-0.00 | \n", "texture_ch-0_dissimilarity_dist-1_angle-0.79 | \n", "texture_ch-0_dissimilarity_dist-1_angle-1.57 | \n", "texture_ch-0_dissimilarity_dist-1_angle-2.36 | \n", "texture_ch-0_homogeneity_dist-1_angle-0.00 | \n", "texture_ch-0_homogeneity_dist-1_angle-0.79 | \n", "... | \n", "texture_ch-2_homogeneity_dist-1_angle-1.57 | \n", "texture_ch-2_homogeneity_dist-1_angle-2.36 | \n", "texture_ch-2_correlation_dist-1_angle-0.00 | \n", "texture_ch-2_correlation_dist-1_angle-0.79 | \n", "texture_ch-2_correlation_dist-1_angle-1.57 | \n", "texture_ch-2_correlation_dist-1_angle-2.36 | \n", "texture_ch-2_ASM_dist-1_angle-0.00 | \n", "texture_ch-2_ASM_dist-1_angle-0.79 | \n", "texture_ch-2_ASM_dist-1_angle-1.57 | \n", "texture_ch-2_ASM_dist-1_angle-2.36 | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| AAACGAGACGGTTGAT-1 | \n", "42.783204 | \n", "79.464035 | \n", "41.904014 | \n", "82.624826 | \n", "1.983783 | \n", "2.753093 | \n", "1.973759 | \n", "2.743151 | \n", "0.753973 | \n", "0.725217 | \n", "... | \n", "0.570987 | \n", "0.504941 | \n", "0.883396 | \n", "0.787901 | \n", "0.872758 | \n", "0.790485 | \n", "0.040632 | \n", "0.035577 | \n", "0.041006 | \n", "0.035397 | \n", "
| AAAGGGATGTAGCAAG-1 | \n", "82.756940 | \n", "144.883230 | \n", "76.546612 | \n", "159.714604 | \n", "3.349644 | \n", "4.369327 | \n", "3.171514 | \n", "4.603538 | \n", "0.692667 | \n", "0.666414 | \n", "... | \n", "0.538010 | \n", "0.466651 | \n", "0.938821 | \n", "0.914061 | \n", "0.947862 | \n", "0.887927 | \n", "0.016620 | \n", "0.013672 | \n", "0.016786 | \n", "0.013555 | \n", "
| AAATGGCATGTCTTGT-1 | \n", "27.093979 | \n", "48.276535 | \n", "23.560334 | \n", "49.362415 | \n", "2.416785 | \n", "3.209199 | \n", "2.249740 | \n", "3.271754 | \n", "0.565910 | \n", "0.525931 | \n", "... | \n", "0.571515 | \n", "0.503682 | \n", "0.878716 | \n", "0.781444 | \n", "0.873200 | \n", "0.786576 | \n", "0.033804 | \n", "0.028822 | \n", "0.034247 | \n", "0.028759 | \n", "
| AAATGGTCAATGTGCC-1 | \n", "24.198313 | \n", "36.550901 | \n", "18.040215 | \n", "46.083141 | \n", "2.222673 | \n", "2.732854 | \n", "1.925904 | \n", "3.103483 | \n", "0.645956 | \n", "0.621034 | \n", "... | \n", "0.578051 | \n", "0.511233 | \n", "0.988060 | \n", "0.979815 | \n", "0.987566 | \n", "0.979121 | \n", "0.016216 | \n", "0.013678 | \n", "0.016297 | \n", "0.013659 | \n", "
| AAATTAACGGGTAGCT-1 | \n", "21.413928 | \n", "39.826111 | \n", "23.691475 | \n", "47.908006 | \n", "1.281552 | \n", "1.779400 | \n", "1.349581 | \n", "1.883277 | \n", "0.821503 | \n", "0.798561 | \n", "... | \n", "0.577136 | \n", "0.507679 | \n", "0.954380 | \n", "0.926691 | \n", "0.954430 | \n", "0.922658 | \n", "0.026097 | \n", "0.022120 | \n", "0.026564 | \n", "0.022041 | \n", "
5 rows × 60 columns
\n", "