<jupyter_start><jupyter_text>Extract custom features=======================This example shows how to extract features from the tissue image using acustom function.The custom feature calculation function can be any python function thattakes an image as input, and returns a list of features. Here, we show asimple example by defining a function to calculate the mean of theimages.Custom features are calculated by using `features = 'custom'`, whichcalls `squidpy.im.ImageContainer.features_custom`. In addition to`feature_name` and `channels` we can specify the following`features_kwargs`:> -   `func` - custom feature extraction function.> -   `additional_layers` - names of image layers that should be passed>     to `func` together with `layer`.> -   additional keyword arguments for `func`.::: {.seealso}See `sphx_glr_auto_examples_image_compute_features.py` for general usageof `squidpy.im.calculate_image_features`.:::<jupyter_code>import scanpy as sc
import squidpy as sq<jupyter_output><empty_output><jupyter_text>Let\'s load the H&E Visium dataset.<jupyter_code># get spatial dataset including high-resolution tissue image
img = sq.datasets.visium_hne_image_crop()
adata = sq.datasets.visium_hne_adata_crop()<jupyter_output><empty_output><jupyter_text>Define a custom feature extraction function.<jupyter_code>def mean_fn(arr):
    """Compute mean of arr."""
    import numpy as np

    return np.mean(arr)<jupyter_output><empty_output><jupyter_text>Now we can extract features using [mean\_fn]{.title-ref} by providing itwithin `features_kwargs`.<jupyter_code>sq.im.calculate_image_features(
    adata,
    img,
    features="custom",
    features_kwargs={"custom": {"func": mean_fn}},
    key_added="custom_features",
    show_progress_bar=False,
)<jupyter_output><empty_output><jupyter_text>The result is stored in `adata.obsm['custom_features']`.<jupyter_code>adata.obsm["custom_features"].head()<jupyter_output><empty_output><jupyter_text>Use `squidpy.pl.extract` to plot the histogram features on the tissueimage or have a look at [our interactive visualizationtutorial](../../external_tutorials/tutorial_napari.ipynb) to learn howto use our interactive `napari` plugin.<jupyter_code>sc.pl.spatial(
    sq.pl.extract(adata, "custom_features"),
    color=[None, "mean_fn_0"],
    bw=True,
)<jupyter_output><empty_output><jupyter_text>You can also pass more than one image layer to the custom featureextraction function. For this, specify the necessary additional layernames using `additional_layers` in `features_kwargs`. The specifiedimage layers will be passed to the custom feature extraction function.Here, we show this behavior by defining a feature extraction functionthat sums two image layers:<jupyter_code>def sum_fn(arr, extra_layer):
    """Compute sum of two image layers."""
    import numpy as np

    return np.sum(arr + extra_layer)


img.add_img(img["image"].values, layer="extra_layer")

sq.im.calculate_image_features(
    adata,
    img,
    layer="image",
    features="custom",
    features_kwargs={"custom": {"func": sum_fn, "additional_layers": ["extra_layer"]}},
    key_added="custom_features",
    show_progress_bar=False,
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10801502817783344, 'fraction_numerical': 0.00031308703819661864, 'mean_word_length': 6.1, 'pattern_counts': {'":': 5, '<': 29, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '371178', 'license': 'MIT', 'n_tokens_mistral': 982, 'n_tokens_neox': 947, 'n_words': 274, 'path': 'docs/source/auto_examples/image/compute_custom_features.ipynb', 'repo_name': 'theislab/squidpy_notebooks', 'size': '3194'}