{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Benchmarks\n", "\n", "This notebook contains benchmarks against the [pynfft](https://pypi.python.org/pypi/pyNFFT) package, which is a Python wrapper of Fortran code" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'0.1'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Import from main directory\n", "import os, sys\n", "sys.path.append(os.path.abspath('..'))\n", "\n", "import nfft\n", "nfft.__version__" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'1.3.2'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pynfft\n", "pynfft.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Benchmarking the Forward Transform" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define some test data:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def make_forward_data(M, N):\n", " x = -0.5 + np.random.rand(M)\n", " f_hat = np.random.randn(N) + 1j * np.random.randn(N)\n", " return x, f_hat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a utility function around pynfft:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def pynfft_forward(x, f_hat):\n", " M = len(x)\n", " N = len(f_hat)\n", " plan = pynfft.nfft.NFFT(N, M)\n", " plan.x = x\n", " plan.precompute()\n", " plan.f_hat = f_hat\n", " # Need copy because of bug in pynfft 1.x\n", " # See https://github.com/ghisvail/pyNFFT/issues/57\n", " return plan.trafo().copy()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x, f_hat = make_forward_data(1000, 100000)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out1 = nfft.nfft(x, f_hat)\n", "out2 = pynfft_forward(x, f_hat)\n", "np.allclose(out1, out2)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 loops, best of 3: 22.4 ms per loop\n", "10 loops, best of 3: 43.1 ms per loop\n" ] } ], "source": [ "%timeit nfft.nfft(x, f_hat)\n", "%timeit pynfft_forward(x, f_hat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Benchmarking the Adjoint Transform" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define some test data:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def make_adjoint_data(M):\n", " x = -0.5 + np.random.rand(M)\n", " f = np.random.randn(M) + 1j * np.random.randn(M)\n", " return x, f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a utility function around pynfft:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def pynfft_adjoint(x, f, N):\n", " M = len(x)\n", " plan = pynfft.nfft.NFFT(N, M)\n", " plan.x = x\n", " plan.precompute()\n", " plan.f = f\n", " # Need copy because of bug in pynfft 1.x\n", " # See https://github.com/ghisvail/pyNFFT/issues/57\n", " return plan.adjoint().copy()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x, f = make_adjoint_data(1000)\n", "N = 100000" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out1 = nfft.nfft_adjoint(x, f, N)\n", "out2 = pynfft_adjoint(x, f, N)\n", "\n", "np.allclose(out1, out2)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 loops, best of 3: 24.2 ms per loop\n", "10 loops, best of 3: 42.8 ms per loop\n" ] } ], "source": [ "%timeit nfft.nfft_adjoint(x, f, N, sigma=3)\n", "%timeit pynfft_adjoint(x, f, N)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Fast Template Periodogram", "language": "python", "name": "ftp" }, "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.0" } }, "nbformat": 4, "nbformat_minor": 0 }