Skip to content

Instantly share code, notes, and snippets.

Created September 23, 2014 13:08
Show Gist options
  • Save anonymous/5a3840276fb657156740 to your computer and use it in GitHub Desktop.
Save anonymous/5a3840276fb657156740 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"kernelspec": {
"codemirror_mode": {
"name": "python",
"version": 2
},
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"name": "",
"signature": "sha256:c6b20cd8d53f8ea9741634a10b29baebc627d394ba844bafc86f3642f07d655e"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Interactive Curve-Fitting in the IPython Notebook\n",
"\n",
"**Important:** Widgets will not display in \"static\" notebooks until IPython 3.0. They must be backed by a live IPython kernel. Therefore, to view the full widget display, you must download this notebook and run it locally using IPython 2.x or greater."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%matplotlib inline\n",
"from lmfit import Model, Fitter\n",
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `lmfit`\n",
"The [lmfit package](http://cars9.uchicago.edu/software/python/lmfit/) provides layers of high-level interfaces to the nonlinear optimization and curve fitting algorithms in scipy.\n",
"* `Parameter` objects allow fit parameters to be bounded (`min`, `max`) or held fixed (`vary=False`).\n",
"* The `Model` class provides a general way to wrap a user-defined function as a fitting model.\n",
"* `Fitter` provides a curve-fitting workflow akin to popular GUI-based tools while retaining programmatic access to the underlying objects.\n",
"\n",
"`BaseFitter` introduces no new dependencies; `MPLFitter` requires matplotlib; `NotebookFitter` uses IPython's rich display to represent each `Parameter` as a group of IPython widgets. `Fitter` will point to the richest implementation available.\n",
"\n",
"## Interactive Fitting Widget\n",
"\n",
"Select any model from the drop-down menu.\n",
"\n",
"Text input widgets will appear corresponding to the parameters for that model. If the model supports automatic guessing, the values will be initialized according to `model.guess(data, ...)`, and an auto-guess button will be available to re-generate these values at any time.\n",
"\n",
"The values in the text input widgets will serve as an initial guess, and they can be edited by the user. Text inputs for `min` and `max` appear when the associated checkboxes are checked. At all times, `fitter.current_params` is maintained in sync with the values shown in the widget.\n",
"\n",
"When satisfied with the initial guess, click \"Fit\". The plot will update to show the data, the initial guess fit (gray), and the best fit (red). The attribute `fitter.current_result` will contain the result object created by the latest execution of `fit`. The text widgets and the property `fitter.current_params` will be updated from the initial fit to the latest best fit."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data = np.array([1,2,3,4,5,4,3,2])\n",
"x = np.arange(len(data))\n",
"\n",
"fitter = Fitter(data, x=x)\n",
"\n",
"fitter"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAEACAYAAACTXJylAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAH5FJREFUeJzt3XmcnFWV//HPScK+I0IkhC0ghgTRoEAEJKwmEVF0RNmC\ngBGVARWUZeb388l1VARn1LDvSJBFQWRkSRTRIAiKLCFkAZMwmQSQEBYDYYec+eM8bXc6vVV1Vz/1\nVH3fr1e/0kt11elO97du33uee83dERGR8hhQdAEiIlIZBbeISMkouEVESkbBLSJSMgpuEZGSUXCL\niJRMl8FtZleY2RIze7TN+35oZnPN7BEzu8nMNqh9mSIi0qK7EfeVwNh27/stMMLddwb+BpxRi8JE\nRKRjXQa3u98NvNjufXe4+4r8zb8AW9SoNhER6UBv57iPBW7vi0JERKRnqg5uM/t34E13v7YP6xER\nkW4MquaTzOwLwHhgvy5uo01QRESq4O7W3Q26fAG2Bh5t8/ZYYDawSTef593ddz2/AJOKrqHymsdO\nA/d4ybz19XFTi66tGb7/jVC76i/+pSfZ2V074HXAvcAOZrbYzI4FzgXWBe4ws4fN7IKKn06kRhaf\nAxPnr/y+Ly6ARecWU4+I1EKXUyXuflgH776iRrVIL7nPut1sJDD+RJg3HO6fC4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jjH\n3e8GXiy6jmq4+zPuPiN/fTkwF9i82Koq4+6v5q+uTqyflCpAzGwLYDxwGdBlZ0AdK2XdZrYBsJe7\nXwGxFlfG0M7tDyzoKLShNsGti3PqgJltDXwQ+EuxlVTGzAaY2QxgCfAHd59TdE0V+jHwLWBF0YVU\nyYHfmdkDZjax6GIqtA2w1MyuNLOHzOxSM1u76KKq9Hng2s4+WIvg1mpnwfJpkhuBr+Uj79Jw9xXu\n/gFgC+CjZdp3wswOAp5194cp6agV2MPdPwiMA07Ipw7LYhAwCrjA3UcBrwCnF1tS5cxsdeATwA2d\n3aYWwf0UMLTN20OJUbf0AzNbDfgl8DN3v7noeqqV/4l7G/ChomupwEeAg/N54uuAfc1sSsE1VcTd\n/57/uxT4FTH1WRZPAk+6+1/zt28kgrxsxgEP5v8HHapFcD8AbG9mW+fPHJ8Dfl2Dx5F2zMyAy4E5\n7v6TouuplJltYmYb5q+vBRwAPFxsVT3n7v/m7kPdfRviT93fu/uEouvqKTNb28zWy19fBzgQKE13\nlbs/Ayw2s/fm79ofmF1gSdU6jHji71RVF+B0pewX55jZdcDewLvMbDHwbXe/suCyemoP4Ehgppm1\nBN4Z7j6twJoq8R7gqnxFfQBwtbvfWXBNvVG2acPNgF/F8z+DgGvc/bfFllSxE4Fr8kHjAuCYguup\nSP6EuT/Q5fqCLsARESkZHV0mIlIyCm4RkZJRcIuIlIyCW0SkZBTcIiIlo+AWESkZBbeISMkouEVE\nSub/AFxZ+h0/q6B1AAAAAElFTkSuQmCC\n",
"text": [
"<matplotlib.figure.Figure at 0xab352f2c>"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fitter.current_result.values"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"{'amplitude': 26.028242096009546,\n",
" 'center': 3.9949292344958436,\n",
" 'fwhm': 5.2960364040704793,\n",
" 'sigma': 2.2490196295557534}"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reuse the Fitter on Different Data\n",
"\n",
"Unlike `Model`, `Fitter` is \"stateful;\" it remembers that it has been called, and it uses each best fit as the initial guess for the next fit (unless a new guess is provided).\n",
"\n",
"Execute the cell below and revisit to the fitter above. (Or create a new view below; multiple views of the same instance of `fitter` all stay in sync.) Clicking \"Fit\" will use the most recent best fit to `data` as the initial guesss for fitting `other_data`."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"other_data = np.array([10, 11, 12, 11, 10, 8, 7, 6])\n",
"fitter.data = other_data"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Select a User-Defined Model\n",
"\n",
"Any subclass of `Model` will be included in the drop-down menu. Look for 'UserDefinedModel' in the menu below."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def func(x, a=1):\n",
" \"a very simple model indeed\"\n",
" return x + a\n",
"\n",
"class UserDefinedModel(lmfit.Model):\n",
" def __init__(self, *args, **kwargs):\n",
" super(UserDefinedModel, self).__init__(func, *args, **kwargs)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data = np.array([1,2,3,4,5,4,3,2])\n",
"x = np.arange(len(data))\n",
"\n",
"fitter = lmfit.Fitter(data, x=x)\n",
"\n",
"fitter"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"<lmfit.ui.ipy_fitter.NotebookFitter at 0xab59abac>"
]
},
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAEACAYAAACuzv3DAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAEoVJREFUeJzt3W+MXXd95/H3J3GQA3Gx2lRuiScbhENbZLS4qYIJdJmu\noE0s6vYBUlOpGwkJN2IXO2pVtL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iDXJIaZ5BLUuMMcklq3D8DPcCRx3/xsFQAAAAASUVORK5CYII=\n",
"text": [
"<matplotlib.figure.Figure at 0xab45608c>"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"See the docstring for more, including complete access to the plot elements."
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment