{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Restriction\n\nThis example shows how to use the :py:class:`pylops_distributed.Restriction`\noperator to sample a certain input vector at desired locations ``iava``.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nimport dask.array as da\nimport pylops_distributed\n\nplt.close('all')\nnp.random.seed(10)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's create a signal of size ``nt`` and sampling ``dt`` that is composed\nof three sinusoids at frequencies ``freqs``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nt = 200\ndt = 0.004\n\nfreqs = [5., 3., 8.]\n\nt = np.arange(nt)*dt\nx = np.zeros(nt)\n\nfor freq in freqs:\n    x = x + np.sin(2*np.pi*freq*t)\nx = da.from_array(x, chunks=(nt//4))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First of all, we subsample the signal at random locations and we retain 40%\nof the initial samples.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "perc_subsampling = 0.4\nntsub = int(np.round(nt*perc_subsampling))\n\nisample = np.arange(nt)\niava = np.sort(np.random.permutation(np.arange(nt))[:ntsub])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We then create the restriction and interpolation operators and display\nthe original signal as well as the subsampled signal.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "Rop = pylops_distributed.Restriction(nt, iava, dtype='float64',\n                                     compute=(False, False))\n\n\ny = Rop * x\nxadj = Rop.H * y\n\n# Visualize data\nfig = plt.figure(figsize=(15, 5))\nplt.plot(isample, x, '.-k', lw=3, ms=10, label='all samples')\nplt.plot(iava, y, '.g', ms=25, label='available samples')\nplt.plot(isample, xadj, 'r', lw=3, label='adjont')\nplt.legend()\nplt.title('Data restriction')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally we show how the :py:class:`pylops.Restriction` is not limited to\none dimensional signals but can be applied to sample locations of a specific\naxis of a multi-dimensional array.\nsubsampling locations\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nx, nt = 100, 50\n\nx = np.arange(nx*nt).reshape(nx, nt)\nx = da.from_array(x, chunks=(nx//4, nt//2))\n\nperc_subsampling = 0.4\nnxsub = int(np.round(nx*perc_subsampling))\niava = np.sort(np.random.permutation(np.arange(nx))[:nxsub])\n\nRop = pylops_distributed.Restriction(nx*nt, iava, dims=(nx, nt), dir=0,\n                                     dtype='float64', compute=(False, False))\n\ny = (Rop * x.ravel()).reshape(nxsub, nt)\nxadj = (Rop.H * y.ravel()).reshape(nx, nt)\n\nfig, axs = plt.subplots(1, 3, figsize=(10, 5))\naxs[0].imshow(x, cmap='gray_r')\naxs[0].set_title('Model')\naxs[0].axis('tight')\naxs[1].imshow(y, cmap='gray_r')\naxs[1].set_title('Data')\naxs[1].axis('tight')\naxs[2].imshow(xadj, cmap='gray_r')\naxs[2].set_title('Adjoint')\naxs[2].axis('tight')"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}