{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n==================================\n1D ndsplines vs. scipy.interpolate\n==================================\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport gc\nimport time\n\nfrom scipy import interpolate\nimport ndsplines\n\nimport matplotlib.pyplot as plt\n\n# number of time measurements per input/query size\nn_iter = 10\n\n\ndef timeit(func, n_iter=1, return_samps=True, **func_kwargs):\n    results = np.empty(n_iter, dtype=np.double)\n    for i in range(n_iter):\n        # gc.collect()\n\n        tstart = time.time()\n        func(**func_kwargs)\n        delta = time.time() - tstart\n\n        results[i] = delta\n\n    if return_samps:\n        return results\n    else:\n        return np.mean(results)\n\n\ndef gen_xy(size):\n    x = np.pi * np.linspace(-1, 1, size)\n    y = np.sin(x)\n    return x, y\n\n\ndef gen_xx(size):\n    return 3 * np.pi * np.linspace(-1, 1, size)\n\n\n# make_interp_spline timing\nx_sizes = np.logspace(1, 3, 10, dtype=int)\nt_scipy_build = np.empty((2, x_sizes.size))\nt_ndspl_build = np.empty((2, x_sizes.size))\nfor i, size in enumerate(x_sizes):\n    x, y = gen_xy(size)\n    t_scipy = 10e3 * timeit(interpolate.make_interp_spline, x=x.copy(), y=y,\n                            n_iter=n_iter)\n    t_ndspl = 10e3 * timeit(ndsplines.make_interp_spline, x=x.copy(), y=y,\n                            n_iter=n_iter)\n    t_scipy_build[:, i] = np.mean(t_scipy), np.std(t_scipy)\n    t_ndspl_build[:, i] = np.mean(t_ndspl), np.std(t_ndspl)\n\n# spline query timing\nx, y = gen_xy(7)\nxx_sizes = np.logspace(0, 3, 10, dtype=int)\nt_scipy_call = np.empty((2, xx_sizes.size))\nt_ndspl_npy_call = np.empty((2, xx_sizes.size))\nt_ndspl_pyx_call = np.empty((2, xx_sizes.size))\nfor i, size in enumerate(xx_sizes):\n    xx = gen_xx(size)\n    spl_scipy = interpolate.make_interp_spline(x.copy(), y)\n    spl_ndspl = ndsplines.make_interp_spline(x.copy(), y)\n    spl_ndspl.allocate_workspace_arrays(size)\n    t_scipy = 10e3 * timeit(spl_scipy, x=xx.copy(), n_iter=n_iter)\n    ndsplines.set_impl('cython')\n    t_ndspl_pyx = 10e3 * timeit(spl_ndspl, x=xx.copy(), n_iter=n_iter)\n    ndsplines.set_impl('numpy')\n    t_ndspl_npy = 10e3 * timeit(spl_ndspl, x=xx.copy(), n_iter=n_iter)\n    t_scipy_call[:, i] = np.mean(t_scipy), np.std(t_scipy)\n    t_ndspl_npy_call[:, i] = np.mean(t_ndspl_npy), np.std(t_ndspl_npy)\n    t_ndspl_pyx_call[:, i] = np.mean(t_ndspl_pyx), np.std(t_ndspl_pyx)\n\n# plot results\nfig, axes = plt.subplots(nrows=2)\n\naxes[0].errorbar(x_sizes, t_scipy_build[0], capsize=3, yerr=t_scipy_build[1],\n                 label='scipy')\naxes[0].errorbar(x_sizes, t_ndspl_build[0], capsize=3, yerr=t_ndspl_build[1],\n                 label='ndsplines')\naxes[0].set_title('make_interp_spline')\n\naxes[1].errorbar(xx_sizes, t_scipy_call[0], capsize=3, yerr=t_scipy_call[1],\n                 label='scipy.interpolate')\naxes[1].errorbar(xx_sizes, t_ndspl_npy_call[0], capsize=3, yerr=t_ndspl_npy_call[1],\n                 label='ndsplines npy')\naxes[1].errorbar(xx_sizes, t_ndspl_pyx_call[0], capsize=3, yerr=t_ndspl_pyx_call[1],\n                 label='ndsplines pyx')\naxes[1].set_title('spline.__call__')\n\nfor ax in axes:\n    ax.set_xlabel('input array size')\n    ax.set_ylabel('time [ms]')\n    ax.set_xscale('log')\n    ax.grid()\n\naxes[-1].legend()\nfig.tight_layout()\n\nplt.show()"
      ]
    }
  ],
  "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.7.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}