{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Extract histogram features\n", "==========================\n", "\n", "This example shows how to extract histogram features from tissue image.\n", "\n", "Histogram features give a more detailed view than summary features\n", "(`examples_image_compute_summary_features`) by\n", "computing a histogram of each image channel and returning bin-counts for\n", "each Visium spot.\n", "\n", "In addition to `feature_name` and `channels` we can specify the\n", "following `features_kwargs`:\n", "\n", "> - `bins` - number of bins of the histogram, default is 10.\n", "> - `v_range` - range on which values are binned, default is the whole\n", "> image range.\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": [ "Lets load the fluorescence Visium dataset and calculate bin-counts (3\n", "bins) of channels 0 and 1.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "61ec677d85374f6294d5d43b6f21c89c", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/704 [00:00, ?/s]" ] }, "metadata": {}, "output_type": "display_data" } ], "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 histogram features and save in key \"histogram_features\"\n", "sq.im.calculate_image_features(\n", " adata,\n", " img,\n", " features=\"histogram\",\n", " features_kwargs={\"histogram\": {\"bins\": 3, \"channels\": [0, 1]}},\n", " key_added=\"histogram_features\",\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The result is stored in {attr}`anndata.AnnData.obsm`\\ `['histogram_features']`.\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
| \n", " | histogram_ch-0_bin-0 | \n", "histogram_ch-0_bin-1 | \n", "histogram_ch-0_bin-2 | \n", "histogram_ch-1_bin-0 | \n", "histogram_ch-1_bin-1 | \n", "histogram_ch-1_bin-2 | \n", "
|---|---|---|---|---|---|---|
| AAACGAGACGGTTGAT-1 | \n", "28201 | \n", "1649 | \n", "2191 | \n", "32041 | \n", "0 | \n", "0 | \n", "
| AAAGGGATGTAGCAAG-1 | \n", "30072 | \n", "748 | \n", "1221 | \n", "31510 | \n", "529 | \n", "2 | \n", "
| AAATGGCATGTCTTGT-1 | \n", "29032 | \n", "2252 | \n", "757 | \n", "30793 | \n", "1002 | \n", "246 | \n", "
| AAATGGTCAATGTGCC-1 | \n", "28672 | \n", "2411 | \n", "958 | \n", "32041 | \n", "0 | \n", "0 | \n", "
| AAATTAACGGGTAGCT-1 | \n", "29996 | \n", "1116 | \n", "929 | \n", "31367 | \n", "674 | \n", "0 | \n", "