{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### New to Plotly?\n", "Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n", "
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n", "
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Imports\n", "The tutorial below imports [NumPy](http://www.numpy.org/), [Pandas](https://plotly.com/pandas/intro-to-pandas-tutorial/), [SciPy](https://www.scipy.org/) and [PeakUtils](http://pythonhosted.org/PeakUtils/)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "from plotly.tools import FigureFactory as FF\n", "\n", "import numpy as np\n", "import pandas as pd\n", "import scipy\n", "import peakutils\n", "\n", "from scipy import signal" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Import Data\n", "Let us import some stock data for our fitting:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stock_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/stockdata.csv')\n", "df = stock_data[0:15]\n", "\n", "table = FF.create_table(df)\n", "py.iplot(table, filename='stockdata-peak-fitting')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Original Plot\n", "Let us plot the `SBUX` column of the data and highlight a section we will fit to:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "left_endpt=1857\n", "right_endpt=1940\n", "\n", "original_trace = go.Scatter(\n", " x = [j for j in range(len(stock_data['SBUX']))],\n", " y = stock_data['SBUX'][0:left_endpt].tolist() + [None for k in range(right_endpt - left_endpt)] +\n", " stock_data['SBUX'][right_endpt + 1:len(stock_data['SBUX'])].tolist(),\n", " mode = 'lines',\n", " name = 'Full Data',\n", " marker = dict(color = 'rgb(160,200,250)')\n", ")\n", "\n", "highlighted_trace = go.Scatter(\n", " x = [j for j in range(left_endpt, right_endpt)],\n", " y = stock_data['SBUX'][left_endpt:right_endpt],\n", " mode = 'lines',\n", " name = 'Highlighted Section',\n", " marker = dict(color = 'rgb(0,56,210)')\n", ")\n", "\n", "data = [original_trace, highlighted_trace,]\n", "py.iplot(data, filename='stock-data-SBUX')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Peak Detection\n", "Before we are able to apply `Peak Fitting` we need to detect the peaks in this waveform to properly specify a peak to fit to." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [j for j in range(len(stock_data))][left_endpt:right_endpt]\n", "y = stock_data['SBUX'][left_endpt:right_endpt]\n", "y = y.tolist()\n", "\n", "cb = np.array(y)\n", "indices = peakutils.indexes(cb, thres=0.75, min_dist=0.1)\n", "\n", "trace = go.Scatter(\n", " x=x,\n", " y=y,\n", " mode='lines',\n", " marker=dict(\n", " color='rgb(0,56,210)'\n", " ),\n", " name='Highlighted Plot'\n", ")\n", "\n", "trace2 = go.Scatter(\n", " x=indices + left_endpt,\n", " y=[y[j] for j in indices],\n", " mode='markers',\n", " marker=dict(\n", " size=8,\n", " color='rgb(255,0,0)',\n", " symbol='cross'\n", " ),\n", " name='Detected Peaks'\n", ")\n", "\n", "data = [trace, trace2]\n", "py.iplot(data, filename='stock-data-with-peaks')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Peak Fitting\n", "Since we have detected all the local maximum points on the data, we can now isolate a few peaks and superimpose a fitted gaussian over one." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def gaussian(x, mu, sig):\n", " return np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))\n", "\n", "first_index = indices[6]\n", "left_gauss_bound = 1894\n", "right_gauss_bound = 1910\n", "\n", "x_values_1 = np.asarray(x[left_gauss_bound-left_endpt:right_gauss_bound-left_endpt])\n", "y_values_1 = np.asarray(y[left_gauss_bound-left_endpt:right_gauss_bound-left_endpt])\n", "\n", "gaussian_params_1 = peakutils.gaussian_fit(x_values_1, y_values_1, center_only=False)\n", "gaussian_y_1 = [gaussian(x_dummy, gaussian_params_1[1], 1.5) for x_dummy in x_values_1]\n", "\n", "trace = go.Scatter(\n", " x=x,\n", " y=y,\n", " mode='lines',\n", " marker=dict(\n", " color='rgb(0,56,210)'\n", " ),\n", " name='Highlighted Plot'\n", ")\n", "\n", "trace2 = go.Scatter(\n", " x=indices + left_endpt,\n", " y=[y[j] for j in indices],\n", " mode='markers',\n", " marker=dict(\n", " size=8,\n", " color='rgb(255,0,0)',\n", " symbol='cross'\n", " ),\n", " name='Detected Peaks'\n", ")\n", "\n", "trace3 = go.Scatter(\n", " #x=x_values_1,\n", " x=[item_x + 1.5 for item_x in x_values_1],\n", " y=[item_y + 38.2 for item_y in gaussian_y_1],\n", " mode='lines',\n", " marker=dict(\n", " size=2,\n", " color='rgb(200,0,250)',\n", " ),\n", " name='Gaussian Fit'\n", ")\n", "\n", "data = [trace, trace2, trace3]\n", "py.iplot(data, filename='stock-data-with-peaks-and-fit')" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting git+https://github.com/plotly/publisher.git\n", " Cloning https://github.com/plotly/publisher.git to /var/folders/ld/6cl3s_l50wd40tdjq2b03jxh0000gp/T/pip-Fa_UTY-build\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.10\n", " Uninstalling publisher-0.10:\n", " Successfully uninstalled publisher-0.10\n", " Running setup.py install for publisher ... \u001b[?25l-\b \b\\\b \bdone\n", "\u001b[?25hSuccessfully installed publisher-0.10\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead.\n", " \"You should import from nbconvert instead.\", ShimWarning)\n", "/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you \"Save\" this notebook before running this command? Remember to save, always save.\n", " warnings.warn('Did you \"Save\" this notebook before running this command? '\n" ] } ], "source": [ "from IPython.display import display, HTML\n", "\n", "display(HTML(''))\n", "display(HTML(''))\n", "\n", "! pip install git+https://github.com/plotly/publisher.git --upgrade\n", "import publisher\n", "publisher.publish(\n", " 'python-Peak-Fitting.ipynb', 'python/peak-fitting/', 'Peak Fitting | plotly',\n", " 'Learn how to fit to peaks in Python',\n", " title='Peak Fitting in Python | plotly',\n", " name='Peak Fitting',\n", " language='python',\n", " page_type='example_index', has_thumbnail='false', display_as='peak-analysis', order=5,\n", " ipynb= '~notebook_demo/119')" ] }, { "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }