{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Extract custom features\n", "=======================\n", "\n", "This example shows how to extract features from the tissue image using a\n", "custom function.\n", "\n", "The custom feature calculation function can be any python function that\n", "takes an image as input, and returns a list of features. Here, we show a\n", "simple example by defining a function to calculate the mean of the\n", "images.\n", "\n", "Custom features are calculated by using `features = 'custom'`, which\n", "calls {func}`squidpy.im.ImageContainer.features_custom`. In addition to\n", "`feature_name` and `channels` we can specify the following\n", "`features_kwargs`:\n", "\n", "> - `func` - custom feature extraction function.\n", "> - `additional_layers` - names of image layers that should be passed\n", "> to `func` together with `layer`.\n", "> - additional keyword arguments for `func`.\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 H&E Visium dataset.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "41f1b42e3b674e8ea8bdde6a43081367", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0.00/89.9M [00:00, ?B/s]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# get spatial dataset including high-resolution tissue image\n", "img = sq.datasets.visium_hne_image_crop()\n", "adata = sq.datasets.visium_hne_adata_crop()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Define a custom feature extraction function.\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def mean_fn(arr):\n", " \"\"\"Compute mean of arr.\"\"\"\n", " import numpy as np\n", "\n", " return np.mean(arr)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Now we can extract features using [mean\\_fn]{.title-ref} by providing it\n", "within `features_kwargs`.\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sq.im.calculate_image_features(\n", " adata,\n", " img,\n", " features=\"custom\",\n", " features_kwargs={\"custom\": {\"func\": mean_fn}},\n", " key_added=\"custom_features\",\n", " show_progress_bar=False,\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The result is stored in {attr}`anndata.AnnData.obsm`\\ `['custom_features']`.\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
| \n", " | mean_fn_0 | \n", "
|---|---|
| AAAGACCCAAGTCGCG-1 | \n", "101.868998 | \n", "
| AAAGGGATGTAGCAAG-1 | \n", "102.959180 | \n", "
| AAAGTCACTGATGTAA-1 | \n", "107.924715 | \n", "
| AAATGGCATGTCTTGT-1 | \n", "97.444851 | \n", "
| AAATGGTCAATGTGCC-1 | \n", "102.854690 | \n", "