{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "ef68d510",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "65bb50b6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy import ndimage\nimport numpy as np\na = np.array(([0,1,1,1,1],\n              [0,0,1,1,1],\n              [0,1,1,1,1],\n              [0,1,1,1,0],\n              [0,1,1,0,0]))\nndimage.distance_transform_edt(a)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 0.    ,  1.    ,  1.4142,  2.2361,  3.    ],\n       [ 0.    ,  0.    ,  1.    ,  2.    ,  2.    ],\n       [ 0.    ,  1.    ,  1.4142,  1.4142,  1.    ],\n       [ 0.    ,  1.    ,  1.4142,  1.    ,  0.    ],\n       [ 0.    ,  1.    ,  1.    ,  0.    ,  0.    ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "4cff6a1c",
            "cell_type": "markdown",
            "source": "With a sampling of 2 units along x, 1 along y:\n",
            "metadata": {}
        },
        {
            "id": "b8a4dcab",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ndimage.distance_transform_edt(a, sampling=[2,1])",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 0.    ,  1.    ,  2.    ,  2.8284,  3.6056],\n       [ 0.    ,  0.    ,  1.    ,  2.    ,  3.    ],\n       [ 0.    ,  1.    ,  2.    ,  2.2361,  2.    ],\n       [ 0.    ,  1.    ,  2.    ,  1.    ,  0.    ],\n       [ 0.    ,  1.    ,  1.    ,  0.    ,  0.    ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "0636c9c9",
            "cell_type": "markdown",
            "source": "Asking for indices as well:\n",
            "metadata": {}
        },
        {
            "id": "72be97ee",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "edt, inds = ndimage.distance_transform_edt(a, return_indices=True)\ninds",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[[0, 0, 1, 1, 3],\n        [1, 1, 1, 1, 3],\n        [2, 2, 1, 3, 3],\n        [3, 3, 4, 4, 3],\n        [4, 4, 4, 4, 4]],\n       [[0, 0, 1, 1, 4],\n        [0, 1, 1, 1, 4],\n        [0, 0, 1, 4, 4],\n        [0, 0, 3, 3, 4],\n        [0, 0, 3, 3, 4]]], dtype=int32)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d5d95388",
            "cell_type": "markdown",
            "source": "With arrays provided for inplace outputs:\n",
            "metadata": {}
        },
        {
            "id": "aeffd1b1",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "indices = np.zeros(((np.ndim(a),) + a.shape), dtype=np.int32)\nndimage.distance_transform_edt(a, return_indices=True, indices=indices)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 0.    ,  1.    ,  1.4142,  2.2361,  3.    ],\n       [ 0.    ,  0.    ,  1.    ,  2.    ,  2.    ],\n       [ 0.    ,  1.    ,  1.4142,  1.4142,  1.    ],\n       [ 0.    ,  1.    ,  1.4142,  1.    ,  0.    ],\n       [ 0.    ,  1.    ,  1.    ,  0.    ,  0.    ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a91331c5",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "indices",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[[0, 0, 1, 1, 3],\n        [1, 1, 1, 1, 3],\n        [2, 2, 1, 3, 3],\n        [3, 3, 4, 4, 3],\n        [4, 4, 4, 4, 4]],\n       [[0, 0, 1, 1, 4],\n        [0, 1, 1, 1, 4],\n        [0, 0, 1, 4, 4],\n        [0, 0, 3, 3, 4],\n        [0, 0, 3, 3, 4]]], dtype=int32)"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}