Skip to content

Instantly share code, notes, and snippets.

@giacomov
Last active February 23, 2016 00:07
Show Gist options
  • Save giacomov/aacc8feebc745717276a to your computer and use it in GitHub Desktop.
Save giacomov/aacc8feebc745717276a to your computer and use it in GitHub Desktop.
Speed comparison between models in Sherpa, Astropy and astromodels
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING: failed to import sherpa.astro.xspec; XSPEC models will not be available\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:sherpa.astro.all:failed to import sherpa.astro.xspec; XSPEC models will not be available\n"
]
}
],
"source": [
"from sherpa.astro import ui as sherpa\n",
"\n",
"import logging\n",
"logging.getLogger('sherpa').propagate = 0\n",
"\n",
"import astromodels as am\n",
"\n",
"import astropy.modeling.models as apm\n",
"\n",
"import numpy as np\n",
"\n",
"import matplotlib.pyplot as plt\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First define a power-law implemented with numpy. This is the reference, as anything you add around this will slow things down:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Reference implementation, i.e., a minimal implementation\n",
"# of a power law. Whatever we do, we will never be faster\n",
"# than this if we use pure python (no cython)\n",
"\n",
"def plain_numpy_powerlaw(energies, norm, index):\n",
" \n",
" return norm * np.power(energies, index)\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now define the same power law in all 3 packages:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sherpa_powerlaw = sherpa.powerlaw(\"test\")\n",
"sherpa_powerlaw.index = -2\n",
"sherpa_powerlaw.refer = 1.0\n",
"sherpa_powerlaw.ampl = 1.0"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"astromodels_powerlaw = am.powerlaw()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"astropy_powerlaw = apm.PowerLaw1D()\n",
"\n",
"# Astropy uses the formula with the positive index\n",
"\n",
"astropy_powerlaw.alpha = 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Evaluate the powerlaws on an array of energies and plot them, just to check that they are indeed identical:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAY4AAAEWCAYAAABxMXBSAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGBJJREFUeJzt3X2QXWV9wPFvIq8FWRCwrRSziCRxFBsEFKWwl91Naoep\ntmgYSRqFUpXSTaLIRKcWQ9pOZ6pACqEVadxSKMSCBkekLTYiyJCgEMKQkkSJCqVFqW00rNagDds/\nzlmy92Zf7st57nNevp+ZO7v33nPP87szm/PL7zxvIEmSJEmSJEmSJEmSJEmSJEmSJEmScudlsQOY\nwsHAp4DfAfYCO+OGI0nKu37ggvT3G2MGIknaZ2aX2xsGngO2Nrx+NrAdeBJYmr52MvCd9PdDuxKd\nJCl3zgJOYf/EsYUkecwCdgDHAOcA70nf/0y3ApQkTa3bFccDwI8aXutJf34deBr4CvAWYCNwGrAG\nWN+tACVJUzsgdgDA6SRVxphtwBnA3cDlU33wkMNf/vM9Pxk5MGBsklRG3wFe2+6Hu11xZGrPT0YO\n7D9/6e+dtvqqF+cMr2Vw0dA1wIyCPlaVpM1Oz9nu51v5XLPHTndcp+8X5RHre4Rot9NztvP5Vj7T\n7LHNHDfVMSdSML3U93H0kPRxjFkDnNvkuUaBK5kxo7bg0uXbj75z/ejAihW7+s9bflQ2oXZVrSRt\ndnrOdj/fyuea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rGPQ8rGbSR7Nb88fT5WrTSOXLwH\nOJ/kVlUfMGvce98Efg1YRDKKS8olKw5pYoeS9G2M+WfgjxuOGU0fAJ8HjiFJDEcA3wXe0XAMJB3q\nbwAeI+lgf4J9t6sAbgd+nX0b7kiSKm4mycgqgNNJOsHH+xfgzK5GJEnKtcNJ9n3eCnwBOCN9/Uhg\nG/A3keKSJEmSJEmSJEmSJEmSJElt+X9xOLN5DU++cwAAAABJRU5ErkJggg==\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x4c58b10>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"energies = np.logspace(0,2,100)\n",
"\n",
"for f in [lambda x:plain_numpy_powerlaw(x, 1, -2), sherpa_powerlaw, astromodels_powerlaw, astropy_powerlaw]:\n",
" \n",
" this_values = f(energies)\n",
" plt.plot(energies, this_values)\n",
"\n",
"plt.ylabel(\"Differential flux\")\n",
"plt.xlabel(\"Energy\")\n",
"plt.loglog()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Simple speed test</h1>\n",
"\n",
"Let's check how fast the 3 packages can evaluate the function:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 4.58 times longer than the fastest. This could mean that an intermediate result is being cached \n",
"100000 loops, best of 10: 11.1 µs per loop\n"
]
}
],
"source": [
"%timeit -r10 plain_numpy_powerlaw(energies, 1, -2.0)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 4.44 times longer than the fastest. This could mean that an intermediate result is being cached \n",
"10000 loops, best of 10: 23.6 µs per loop\n"
]
}
],
"source": [
"%timeit -r10 sherpa_powerlaw(energies)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10000 loops, best of 10: 21 µs per loop\n"
]
}
],
"source": [
"%timeit -r10 astromodels_powerlaw(energies)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10000 loops, best of 10: 130 µs per loop\n"
]
}
],
"source": [
"%timeit -r10 astropy_powerlaw(energies)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sherpa and astromodels are very similar, and they are around 2x slower than the reference implementation. This is the price of all the nuts and bolts needed for a real model (access the current values of parameters, check for proper inputs and so on). Astropy however is 6x slower."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Simulations of a fit</h1>\n",
"\n",
"A better benchmark is to see how they behave in a more realistic situation. During a fit, the value of the parameters is changed a couple of times, and the function re-evaluated at each time. Let's build a function which \"simulates\" this situation:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"n_iterations = 100\n",
"\n",
"def reference_fit():\n",
" \n",
" _ = plain_numpy_powerlaw(energies, 1.0, -2.0)\n",
"\n",
"def sherpa_fake_fit():\n",
" \n",
" # Iterations for this fake fit\n",
" \n",
" for i in range(n_iterations):\n",
" \n",
" # We use always the same values, to avoid the overhead of generating\n",
" # a random number. This will test anyway the speed of assigning to\n",
" # parameters\n",
" # Also, we assign directly to the .val attribute to avoid the overhead of the\n",
" # machinery which allows you to do sherpa_powerlaw.index = -2.0\n",
" \n",
" sherpa_powerlaw.index.val = -2.0\n",
" sherpa_powerlaw.ampl.val = 1.0\n",
" \n",
" _ = sherpa_powerlaw(energies)\n",
"\n",
"def astromodels_fake_fit():\n",
" \n",
" for i in range(n_iterations):\n",
" \n",
" # We use always the same values, to avoid the overhead of generating\n",
" # a random number. This will test anyway the speed of assigning to\n",
" # parameters\n",
" # Also, we assign directly to the .value attribute to avoid the overhead of the\n",
" # machinery which allows you to do astromodels_powerlaw.index = -2.0\n",
" \n",
" #astromodels_powerlaw.index.value = -2.0\n",
" #astromodels_powerlaw.logK.value = 0.0\n",
" \n",
" _ = astromodels_powerlaw(energies)\n",
"\n",
"def astropy_fake_fit():\n",
" \n",
" for i in range(n_iterations):\n",
" \n",
" # We use always the same values, to avoid the overhead of generating\n",
" # a random number. This will test anyway the speed of assigning to\n",
" # parameters\n",
" # Also, we assign directly to the .value attribute to avoid the overhead of the\n",
" # machinery which allows you to do astropy_powerlaw.alpha = 2.0\n",
" \n",
" astropy_powerlaw.alpha.value = 2.0\n",
" astropy_powerlaw.amplitude.value = 1.0\n",
" \n",
" _ = astropy_powerlaw(energies)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 8.26 times longer than the fastest. This could mean that an intermediate result is being cached \n",
"100000 loops, best of 10: 10.4 µs per loop\n"
]
}
],
"source": [
"%timeit -r10 reference_fit()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100 loops, best of 10: 5.77 ms per loop\n"
]
}
],
"source": [
"%timeit -r10 sherpa_fake_fit()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100 loops, best of 10: 2.07 ms per loop\n"
]
}
],
"source": [
"%timeit -r10 astromodels_fake_fit()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100 loops, best of 10: 18.9 ms per loop\n"
]
}
],
"source": [
"%timeit -r10 astropy_fake_fit()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here astromodels is 2.8x faster than sherpa, while astropy is 9x slower than astromodels, and 3.3x slower than sherpa. With respect to the reference implementation, astromodels is 19x slower, which is quite a lot. This is probably easy to reduce by using Cython, since almost all the overhead which generates that is given by the lookup of attributes and methods."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment