{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4.7. Implementing an efficient rolling average algorithm with stride tricks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Stride tricks can be useful for local computations on arrays, when the computed value at a given position depends on the neighbor values. Examples include dynamical systems, filters, cellular automata, and so on. In this example, we will implement an efficient rolling average (a particular type of convolution-based linear filter) with NumPy stride tricks.\n", "\n", "The idea is to start from a 1D vector, and make a \"virtual\" 2D array where each line is a shifted version of the previous line. When using stride tricks, this process does not involve any copy, so it is efficient." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "from numpy.lib.stride_tricks import as_strided\n", "%precision 0" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def id(x):\n", " # This function returns the memory\n", " # block address of an array.\n", " return x.__array_interface__['data'][0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n = 5; k = 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = np.linspace(1, n, n); aid = id(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's change the strides of `a` to add shifted rows." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "as_strided(a, (k, n), (a.itemsize, a.itemsize))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "id(a), id(as_strided(a, (k, n)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The last value indicates an out-of-bounds problem: stride tricks can be dangerous as memory access is not checked. Here, we should take edge effects into account by limiting the shape of the array." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "as_strided(a, (k, n - k + 1), (a.itemsize,)*2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's apply this technique to calculate the rolling average of a random increasing signal." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First version using array copies." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def shift1(x, k):\n", " return np.vstack([x[i:n-k+i+1] for i in range(k)])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Second version using stride tricks." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def shift2(x, k):\n", " return as_strided(x, (k, n - k + 1), (8, 8))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b = shift1(a, k); b, id(b) == aid" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "c = shift2(a, k); c, id(c) == aid" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's generate a signal." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n, k = 100, 10\n", "t = np.linspace(0., 1., n)\n", "x = t + .1 * np.random.randn(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We compute the signal rolling average by creating the shifted version of the signal, and averaging along the vertical dimension." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "y = shift2(x, k)\n", "x_avg = y.mean(axis=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's plot the signal and its averaged version." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "f = plt.figure()\n", "plt.plot(x[:-k+1], '-k');\n", "plt.plot(x_avg, '-r');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Benchmarks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's benchmark the first version (creation of the shifted array, and computation of the mean), which involves array copy." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%timeit shift1(x, k)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%timeit y = shift1(x, k)\n", "z = y.mean(axis=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And the second version, using stride tricks." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%timeit shift2(x, k)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%timeit y = shift2(x, k)\n", "z = y.mean(axis=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the first version, most of the time is spent in the array copy, whereas in the stride trick version, most of the time is instead spent in the computation of the average." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "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.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }