{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "5a232940",
            "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": "9037cbca",
            "cell_type": "markdown",
            "source": "Perhaps the simplest case to understand is ``mode='constant', cval=0.0``,\nbecause in this case borders (i.e., where the `weights` kernel, centered\non any one value, extends beyond an edge of `input`) are treated as zeros.\n",
            "metadata": {}
        },
        {
            "id": "f6107399",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\na = np.array([[1, 2, 0, 0],\n              [5, 3, 0, 4],\n              [0, 0, 0, 7],\n              [9, 3, 0, 0]])\nk = np.array([[1,1,1],[1,1,0],[1,0,0]])\nfrom scipy import ndimage\nndimage.convolve(a, k, mode='constant', cval=0.0)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[11, 10,  7,  4],\n       [10,  3, 11, 11],\n       [15, 12, 14,  7],\n       [12,  3,  7,  0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "e0663655",
            "cell_type": "markdown",
            "source": "Setting ``cval=1.0`` is equivalent to padding the outer edge of `input`\nwith 1.0's (and then extracting only the original region of the result).\n",
            "metadata": {}
        },
        {
            "id": "96537c1d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ndimage.convolve(a, k, mode='constant', cval=1.0)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[13, 11,  8,  7],\n       [11,  3, 11, 14],\n       [16, 12, 14, 10],\n       [15,  6, 10,  5]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "15e5a4ad",
            "cell_type": "markdown",
            "source": "With ``mode='reflect'`` (the default), outer values are reflected at the\nedge of `input` to fill in missing values.\n",
            "metadata": {}
        },
        {
            "id": "bd811914",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "b = np.array([[2, 0, 0],\n              [1, 0, 0],\n              [0, 0, 0]])\nk = np.array([[0,1,0], [0,1,0], [0,1,0]])\nndimage.convolve(b, k, mode='reflect')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[5, 0, 0],\n       [3, 0, 0],\n       [1, 0, 0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "21572dd8",
            "cell_type": "markdown",
            "source": "This includes diagonally at the corners.\n",
            "metadata": {}
        },
        {
            "id": "98208d73",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "k = np.array([[1,0,0],[0,1,0],[0,0,1]])\nndimage.convolve(b, k)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[4, 2, 0],\n       [3, 2, 0],\n       [1, 1, 0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d6dcf504",
            "cell_type": "markdown",
            "source": "With ``mode='nearest'``, the single nearest value in to an edge in\n`input` is repeated as many times as needed to match the overlapping\n`weights`.\n",
            "metadata": {}
        },
        {
            "id": "36bbf1a3",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "c = np.array([[2, 0, 1],\n              [1, 0, 0],\n              [0, 0, 0]])\nk = np.array([[0, 1, 0],\n              [0, 1, 0],\n              [0, 1, 0],\n              [0, 1, 0],\n              [0, 1, 0]])\nndimage.convolve(c, k, mode='nearest')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[7, 0, 3],\n       [5, 0, 2],\n       [3, 0, 1]])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}