Skip to content

Commit

Permalink
convolution python 3 compatiblity
Browse files Browse the repository at this point in the history
  • Loading branch information
cldougl committed Jun 12, 2018
1 parent 1a47439 commit 70e3127
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,22 @@
order: 4
---
{% raw %}
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="New-to-Plotly?">New to Plotly?<a class="anchor-link" href="#New-to-Plotly?">&#182;</a></h4><p>Plotly's Python library is free and open source! <a href="https://plot.ly/python/getting-started/">Get started</a> by downloading the client and <a href="https://plot.ly/python/getting-started/">reading the primer</a>.
<h4 id="New-to-Plotly?">New to Plotly?<a class="anchor-link" href="#New-to-Plotly?">&#194;&#182;</a></h4><p>Plotly's Python library is free and open source! <a href="https://plot.ly/python/getting-started/">Get started</a> by downloading the client and <a href="https://plot.ly/python/getting-started/">reading the primer</a>.
<br>You can set up Plotly to work in <a href="https://plot.ly/python/getting-started/#initialization-for-online-plotting">online</a> or <a href="https://plot.ly/python/getting-started/#initialization-for-offline-plotting">offline</a> mode, or in <a href="https://plot.ly/python/getting-started/#start-plotting-online">jupyter notebooks</a>.
<br>We also have a quick-reference <a href="https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf">cheatsheet</a> (new!) to help you get started!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Imports">Imports<a class="anchor-link" href="#Imports">&#182;</a></h4><p>The tutorial below imports <a href="http://www.numpy.org/">NumPy</a>, <a href="https://plot.ly/pandas/intro-to-pandas-tutorial/">Pandas</a>, <a href="https://www.scipy.org/">SciPy</a> and <a href="https://plot.ly/python/getting-started/">Plotly</a>.</p>
<h4 id="Imports">Imports<a class="anchor-link" href="#Imports">&#194;&#182;</a></h4><p>The tutorial below imports <a href="http://www.numpy.org/">NumPy</a>, <a href="https://plot.ly/pandas/intro-to-pandas-tutorial/">Pandas</a>, <a href="https://www.scipy.org/">SciPy</a> and <a href="https://plot.ly/python/getting-started/">Plotly</a>.</p>

</div>
</div>
Expand All @@ -43,7 +41,7 @@ <h4 id="Imports">Imports<a class="anchor-link" href="#Imports">&#182;</a></h4><p
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">plotly.plotly</span> <span class="kn">as</span> <span class="nn">py</span>
<span class="kn">import</span> <span class="nn">plotly.graph_objs</span> <span class="kn">as</span> <span class="nn">go</span>
<span class="kn">from</span> <span class="nn">plotly.tools</span> <span class="kn">import</span> <span class="n">FigureFactory</span> <span class="k">as</span> <span class="n">FF</span>
<span class="kn">import</span> <span class="nn">plotly.figure_factory</span> <span class="kn">as</span> <span class="nn">ff</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="kn">as</span> <span class="nn">pd</span>
Expand All @@ -57,12 +55,11 @@ <h4 id="Imports">Imports<a class="anchor-link" href="#Imports">&#182;</a></h4><p
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Import-Data">Import Data<a class="anchor-link" href="#Import-Data">&#182;</a></h4><p>Let us import some stock data to apply convolution on.</p>
<h4 id="Import-Data">Import Data<a class="anchor-link" href="#Import-Data">&#194;&#182;</a></h4><p>Let us import some stock data to apply convolution on.</p>

</div>
</div>
Expand All @@ -75,7 +72,7 @@ <h4 id="Import-Data">Import Data<a class="anchor-link" href="#Import-Data">&#182
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">stock_data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&#39;https://raw.githubusercontent.com/plotly/datasets/master/stockdata.csv&#39;</span><span class="p">)</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">stock_data</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">15</span><span class="p">]</span>

<span class="n">table</span> <span class="o">=</span> <span class="n">FF</span><span class="o">.</span><span class="n">create_table</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
<span class="n">table</span> <span class="o">=</span> <span class="n">ff</span><span class="o">.</span><span class="n">create_table</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
<span class="n">py</span><span class="o">.</span><span class="n">iplot</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">&#39;stockdata-peak-fitting&#39;</span><span class="p">)</span>
</pre></div>

Expand All @@ -87,10 +84,14 @@ <h4 id="Import-Data">Import Data<a class="anchor-link" href="#Import-Data">&#182
<div class="output">


<div class="output_area"><div class="prompt output_prompt">Out[2]:</div>
<div class="output_area">

<div class="prompt output_prompt">Out[2]:</div>



<div class="output_html rendered_html output_subarea output_execute_result">
<iframe id="igraph" scrolling="no" style="border:none;" seamless="seamless" src="https://plot.ly/~AdamKulidjian/873.embed" height="530px" width="100%"></iframe>
<iframe id="igraph" scrolling="no" style="border:none;" seamless="seamless" src="https://plot.ly/~chelsea_lyn/17356.embed" height="530px" width="100%"></iframe>
</div>

</div>
Expand All @@ -99,12 +100,11 @@ <h4 id="Import-Data">Import Data<a class="anchor-link" href="#Import-Data">&#182
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Convolve-Two-Signals">Convolve Two Signals<a class="anchor-link" href="#Convolve-Two-Signals">&#182;</a></h4><p><code>Convolution</code> is a type of transform that takes two functions <code>f</code> and <code>g</code> and produces another function via an integration. In particular, the convolution $(f*g)(t)$ is defined as:</p>
<h4 id="Convolve-Two-Signals">Convolve Two Signals<a class="anchor-link" href="#Convolve-Two-Signals">&#194;&#182;</a></h4><p><code>Convolution</code> is a type of transform that takes two functions <code>f</code> and <code>g</code> and produces another function via an integration. In particular, the convolution $(f*g)(t)$ is defined as:</p>
$$
\begin{align*}
\int_{-\infty}^{\infty} {f(\tau)g(t - \tau)d\tau}
Expand All @@ -116,33 +116,35 @@ <h4 id="Convolve-Two-Signals">Convolve Two Signals<a class="anchor-link" href="#
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">15</span><span class="p">)</span>
<span class="n">y_saw</span> <span class="o">=</span> <span class="n">signal</span><span class="o">.</span><span class="n">sawtooth</span><span class="p">(</span><span class="n">t</span><span class="o">=</span><span class="n">x</span><span class="p">)</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">sample</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">15</span><span class="p">)</span>
<span class="n">saw</span> <span class="o">=</span> <span class="n">signal</span><span class="o">.</span><span class="n">sawtooth</span><span class="p">(</span><span class="n">t</span><span class="o">=</span><span class="n">sample</span><span class="p">)</span>

<span class="n">data_sample</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">stock_data</span><span class="p">[</span><span class="s1">&#39;SBUX&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">:</span><span class="mi">100</span><span class="p">])</span>
<span class="n">data_sample2</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">stock_data</span><span class="p">[</span><span class="s1">&#39;AAPL&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">:</span><span class="mi">100</span><span class="p">])</span>
<span class="n">convolve_y</span> <span class="o">=</span> <span class="n">signal</span><span class="o">.</span><span class="n">convolve</span><span class="p">(</span><span class="n">y_saw</span><span class="p">,</span> <span class="n">data_sample2</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">data_sample</span><span class="p">)))</span>
<span class="n">y_convolve</span> <span class="o">=</span> <span class="n">signal</span><span class="o">.</span><span class="n">convolve</span><span class="p">(</span><span class="n">saw</span><span class="p">,</span> <span class="n">data_sample2</span><span class="p">)</span>
<span class="n">x_convolve</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">y_convolve</span><span class="p">)))</span>

<span class="n">trace1</span> <span class="o">=</span> <span class="n">go</span><span class="o">.</span><span class="n">Scatter</span><span class="p">(</span>
<span class="n">x</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">data_sample</span><span class="p">)),</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">data_sample</span><span class="p">,</span>
<span class="n">mode</span> <span class="o">=</span> <span class="s1">&#39;lines&#39;</span><span class="p">,</span>
<span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;SBUX&#39;</span>
<span class="p">)</span>

<span class="n">trace2</span> <span class="o">=</span> <span class="n">go</span><span class="o">.</span><span class="n">Scatter</span><span class="p">(</span>
<span class="n">x</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">data_sample</span><span class="p">)),</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">data_sample2</span><span class="p">,</span>
<span class="n">mode</span> <span class="o">=</span> <span class="s1">&#39;lines&#39;</span><span class="p">,</span>
<span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;AAPL&#39;</span>
<span class="p">)</span>

<span class="n">trace3</span> <span class="o">=</span> <span class="n">go</span><span class="o">.</span><span class="n">Scatter</span><span class="p">(</span>
<span class="n">x</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">convolve_y</span><span class="p">)),</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">convolve_y</span><span class="p">,</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">x_convolve</span><span class="p">,</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">y_convolve</span><span class="p">,</span>
<span class="n">mode</span> <span class="o">=</span> <span class="s1">&#39;lines&#39;</span><span class="p">,</span>
<span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;Convolution&#39;</span>
<span class="p">)</span>
Expand All @@ -159,15 +161,22 @@ <h4 id="Convolve-Two-Signals">Convolve Two Signals<a class="anchor-link" href="#
<div class="output">


<div class="output_area"><div class="prompt output_prompt">Out[5]:</div>
<div class="output_area">

<div class="prompt output_prompt">Out[11]:</div>



<div class="output_html rendered_html output_subarea output_execute_result">
<iframe id="igraph" scrolling="no" style="border:none;" seamless="seamless" src="https://plot.ly/~AdamKulidjian/1034.embed" height="525px" width="100%"></iframe>
<iframe id="igraph" scrolling="no" style="border:none;" seamless="seamless" src="https://plot.ly/~chelsea_lyn/17358.embed" height="525px" width="100%"></iframe>
</div>

</div>

</div>
</div>

</div>{% endraw %}
</div>


{% endraw %}
61 changes: 30 additions & 31 deletions _posts/python/signal-analysis/convolution/python-Convolution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
"collapsed": true
},
"outputs": [],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"from plotly.tools import FigureFactory as FF\n",
"import plotly.figure_factory as ff\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
Expand All @@ -48,14 +48,12 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~AdamKulidjian/873.embed\" height=\"530px\" width=\"100%\"></iframe>"
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~chelsea_lyn/17356.embed\" height=\"530px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
Expand All @@ -70,7 +68,7 @@
"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",
"table = ff.create_table(df)\n",
"py.iplot(table, filename='stockdata-peak-fitting')"
]
},
Expand All @@ -93,51 +91,52 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 11,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~AdamKulidjian/1034.embed\" height=\"525px\" width=\"100%\"></iframe>"
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~chelsea_lyn/17358.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 5,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = range(15)\n",
"y_saw = signal.sawtooth(t=x)\n",
"sample = range(15)\n",
"saw = signal.sawtooth(t=sample)\n",
"\n",
"data_sample = list(stock_data['SBUX'][0:100])\n",
"data_sample2 = list(stock_data['AAPL'][0:100])\n",
"convolve_y = signal.convolve(y_saw, data_sample2)\n",
"x = list(range(len(data_sample)))\n",
"y_convolve = signal.convolve(saw, data_sample2)\n",
"x_convolve = list(range(len(y_convolve)))\n",
"\n",
"trace1 = go.Scatter(\n",
" x = range(len(data_sample)),\n",
" x = x,\n",
" y = data_sample,\n",
" mode = 'lines',\n",
" name = 'SBUX'\n",
")\n",
"\n",
"trace2 = go.Scatter(\n",
" x = range(len(data_sample)),\n",
" x = x,\n",
" y = data_sample2,\n",
" mode = 'lines',\n",
" name = 'AAPL'\n",
")\n",
"\n",
"trace3 = go.Scatter(\n",
" x = range(len(convolve_y)),\n",
" y = convolve_y,\n",
" x = x_convolve,\n",
" y = y_convolve,\n",
" mode = 'lines',\n",
" name = 'Convolution'\n",
")\n",
Expand All @@ -149,9 +148,7 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -182,22 +179,24 @@
"output_type": "stream",
"text": [
"Collecting git+https://github.com/plotly/publisher.git\n",
" Cloning https://github.com/plotly/publisher.git to /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-5618SS-build\n",
" Cloning https://github.com/plotly/publisher.git to /private/var/folders/k_/zf24qrfn2kg710j9pdrxzrz40000gn/T/pip-07Be7Z-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 \bdone\n",
"\u001b[?25hSuccessfully installed publisher-0.10\n"
" Found existing installation: publisher 0.11\n",
" Uninstalling publisher-0.11:\n",
" Successfully uninstalled publisher-0.11\n",
" Running setup.py install for publisher ... \u001b[?25ldone\n",
"\u001b[?25hSuccessfully installed publisher-0.11\n",
"\u001b[33mYou are using pip version 9.0.3, however version 10.0.1 is available.\n",
"You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead.\n",
"/Users/chelsea/venv/venv2/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n",
" \"You should import from nbconvert instead.\", ShimWarning)\n",
"/Library/Frameworks/Python.framework/Versions/2.7/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",
"/Users/chelsea/venv/venv2/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"
]
}
Expand Down Expand Up @@ -245,9 +244,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"version": "2.7.14"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

0 comments on commit 70e3127

Please sign in to comment.