actions/stimulus-lfp-response

This commit is contained in:
Mikkel Elle Lepperød 2021-03-10 13:32:10 +01:00
parent 90a650c468
commit d9d94e0553
7 changed files with 1003 additions and 1005 deletions

View File

@ -1,5 +1,5 @@
registered: '2019-10-14T17:03:05'
data:
results: results.csv
notebook: 20_stimulus-lfp-response.ipynb
html: 20_stimulus-lfp-response.html
notebook: 10-calculate-stimulus-lfp-response.ipynb
html: 10-calculate-stimulus-lfp-response.html

View File

@ -13147,6 +13147,8 @@ div#notebook {
<span class="kn">import</span> <span class="nn">quantities</span> <span class="k">as</span> <span class="nn">pq</span>
<span class="kn">import</span> <span class="nn">shutil</span>
<span class="kn">from</span> <span class="nn">distutils.dir_util</span> <span class="k">import</span> <span class="n">copy_tree</span>
<span class="kn">import</span> <span class="nn">scipy</span>
<span class="kn">import</span> <span class="nn">scipy.signal</span> <span class="k">as</span> <span class="nn">ss</span>
<span class="kn">from</span> <span class="nn">scipy.signal</span> <span class="k">import</span> <span class="n">find_peaks</span>
<span class="kn">from</span> <span class="nn">scipy.interpolate</span> <span class="k">import</span> <span class="n">interp1d</span>
@ -13171,7 +13173,9 @@ div#notebook {
<div class="output_subarea output_stream output_stderr output_text">
<pre>10:26:07 [I] klustakwik KlustaKwik2 version 0.2.6
<pre>19:43:24 [I] klustakwik KlustaKwik2 version 0.2.6
/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:24: TqdmDeprecationWarning: This function will be removed in tqdm==5.0.0
Please use `tqdm.notebook.*` instead of `tqdm._tqdm_notebook.*`
</pre>
</div>
</div>
@ -13200,8 +13204,16 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">output</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;output/stimulus-lfp-response&#39;</span><span class="p">)</span>
<span class="p">(</span><span class="n">output</span> <span class="o">/</span> <span class="s1">&#39;data&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">parents</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1">#############################</span>
<span class="n">perform_zscore</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">perform_zscore</span><span class="p">:</span>
<span class="n">zscore_str</span> <span class="o">=</span> <span class="s2">&quot;-no-zscore&quot;</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">zscore_str</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
<span class="c1">#################################</span>
</pre></div>
</div>
@ -13212,6 +13224,20 @@ div#notebook {
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">output</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;output/stimulus-lfp-response&#39;</span> <span class="o">+</span> <span class="n">zscore_str</span><span class="p">)</span>
<span class="p">(</span><span class="n">output</span> <span class="o">/</span> <span class="s1">&#39;data&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">parents</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">identify_neurons</span> <span class="o">=</span> <span class="n">actions</span><span class="p">[</span><span class="s1">&#39;identify-neurons&#39;</span><span class="p">]</span>
@ -13225,7 +13251,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">channel_groups</span> <span class="o">=</span> <span class="p">[]</span>
@ -13242,7 +13268,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">channel_groups</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">channel_groups</span><span class="p">)</span>
@ -13255,7 +13281,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">get_lim</span><span class="p">(</span><span class="n">action_id</span><span class="p">):</span>
@ -13286,7 +13312,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">signaltonoise</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">ddof</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
@ -13313,7 +13339,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">compute_energy</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">f1</span><span class="p">,</span> <span class="n">f2</span><span class="p">):</span>
@ -13322,6 +13348,19 @@ div#notebook {
<span class="n">mask</span> <span class="o">=</span> <span class="p">(</span><span class="n">f</span> <span class="o">&gt;</span> <span class="n">f1</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">f</span> <span class="o">&lt;</span> <span class="n">f2</span><span class="p">)</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="n">mask</span><span class="p">])</span> <span class="o">*</span> <span class="n">df</span>
<span class="k">def</span> <span class="nf">compute_band_power</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">f1</span><span class="p">,</span> <span class="n">f2</span><span class="p">):</span>
<span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">f1</span><span class="p">)</span> <span class="ow">or</span> <span class="n">np</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">p</span><span class="p">)):</span>
<span class="k">return</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span>
<span class="kn">from</span> <span class="nn">scipy.integrate</span> <span class="k">import</span> <span class="n">simps</span>
<span class="n">dx</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">mask</span> <span class="o">=</span> <span class="p">(</span><span class="n">f</span> <span class="o">&gt;</span> <span class="n">f1</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">f</span> <span class="o">&lt;</span> <span class="n">f2</span><span class="p">)</span>
<span class="c1"># Compute the absolute power by approximating the area under the curve</span>
<span class="n">band_power</span> <span class="o">=</span> <span class="n">simps</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="n">mask</span><span class="p">],</span> <span class="n">dx</span><span class="o">=</span><span class="n">dx</span><span class="p">)</span>
<span class="n">total_power</span> <span class="o">=</span> <span class="n">simps</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">dx</span><span class="o">=</span><span class="n">dx</span><span class="p">)</span>
<span class="n">rel_power</span> <span class="o">=</span> <span class="n">band_power</span> <span class="o">/</span> <span class="n">total_power</span>
<span class="k">return</span> <span class="n">band_power</span><span class="p">,</span> <span class="n">rel_power</span>
</pre></div>
</div>
@ -13331,11 +13370,13 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">find_theta_peak</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">f</span><span class="p">):</span>
<span class="n">mask</span> <span class="o">=</span> <span class="p">(</span><span class="n">f</span> <span class="o">&gt;</span> <span class="mi">6</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">f</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">find_theta_peak</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">f1</span><span class="p">,</span> <span class="n">f2</span><span class="p">):</span>
<span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">p</span><span class="p">)):</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="n">mask</span> <span class="o">=</span> <span class="p">(</span><span class="n">f</span> <span class="o">&gt;</span> <span class="n">f1</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">f</span> <span class="o">&lt;</span> <span class="n">f2</span><span class="p">)</span>
<span class="n">p_m</span> <span class="o">=</span> <span class="n">p</span><span class="p">[</span><span class="n">mask</span><span class="p">]</span>
<span class="n">f_m</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="n">mask</span><span class="p">]</span>
<span class="n">peaks</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">find_peaks</span><span class="p">(</span><span class="n">p_m</span><span class="p">)</span>
@ -13350,52 +13391,40 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">compute_half_width</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">m_p</span><span class="p">,</span> <span class="n">m_f</span><span class="p">):</span>
<span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">m_p</span><span class="p">):</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">compute_half_width</span><span class="p">(</span><span class="n">power</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">max_power</span><span class="p">,</span> <span class="n">max_frequency</span><span class="p">,</span> <span class="n">band</span><span class="p">,</span> <span class="n">band_width</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
<span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">max_power</span><span class="p">):</span>
<span class="k">return</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">]</span> <span class="o">*</span> <span class="mi">3</span>
<span class="c1"># estimate baseline power</span>
<span class="n">low_baseline_mask</span> <span class="o">=</span> <span class="p">(</span><span class="n">freq</span> <span class="o">&gt;</span> <span class="n">band</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">band_width</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">freq</span> <span class="o">&lt;</span> <span class="n">band</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="n">high_baseline_mask</span> <span class="o">=</span> <span class="p">(</span><span class="n">freq</span> <span class="o">&gt;</span> <span class="n">band</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">freq</span> <span class="o">&lt;</span> <span class="n">band</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">band_width</span><span class="p">)</span>
<span class="n">baseline</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">([</span><span class="n">power</span><span class="p">[</span><span class="n">low_baseline_mask</span><span class="p">],</span> <span class="n">power</span><span class="p">[</span><span class="n">high_baseline_mask</span><span class="p">]]))</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">power</span> <span class="o">-</span> <span class="n">baseline</span>
<span class="n">m_p</span> <span class="o">=</span> <span class="n">max_power</span> <span class="o">-</span> <span class="n">baseline</span>
<span class="n">m_f</span> <span class="o">=</span> <span class="n">max_frequency</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">freq</span>
<span class="c1"># estimate half width</span>
<span class="n">m_p_half</span> <span class="o">=</span> <span class="n">m_p</span> <span class="o">/</span> <span class="mi">2</span>
<span class="n">half_p</span> <span class="o">=</span> <span class="n">p</span> <span class="o">-</span> <span class="n">m_p_half</span>
<span class="n">idx_f</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">f</span> <span class="o">&lt;=</span> <span class="n">m_f</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">max</span><span class="p">()</span>
<span class="n">idxs_p1</span><span class="p">,</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">half_p</span><span class="p">[:</span><span class="n">idx_f</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">idxs_p1</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="k">return</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">]</span> <span class="o">*</span> <span class="mi">3</span>
<span class="n">m1</span> <span class="o">=</span> <span class="n">idxs_p1</span><span class="o">.</span><span class="n">max</span><span class="p">()</span>
<span class="n">idxs_p2</span><span class="p">,</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">half_p</span><span class="p">[</span><span class="n">idx_f</span><span class="p">:]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">idxs_p2</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">]</span> <span class="o">*</span> <span class="mi">3</span>
<span class="n">m2</span> <span class="o">=</span> <span class="n">idxs_p2</span><span class="o">.</span><span class="n">min</span><span class="p">()</span> <span class="o">+</span> <span class="n">idx_f</span>
<span class="c1"># plt.plot(f, p)</span>
<span class="c1"># plt.plot(m_f, m_p, marker=&#39;o&#39;, ls=&#39;none&#39;, markersize=10)</span>
<span class="c1"># plt.plot(f[m1], p[m1], marker=&#39;x&#39;, ls=&#39;none&#39;, markersize=10, c=&#39;r&#39;)</span>
<span class="c1"># plt.plot(f[m1+1], p[m1+1], marker=&#39;+&#39;, ls=&#39;none&#39;, markersize=10, c=&#39;r&#39;)</span>
<span class="c1"># plt.plot(f[m2], p[m2], marker=&#39;x&#39;, ls=&#39;none&#39;, markersize=10, c=&#39;k&#39;)</span>
<span class="c1"># plt.plot(f[m2+1], p[m2+1], marker=&#39;+&#39;, ls=&#39;none&#39;, markersize=10, c=&#39;k&#39;)</span>
<span class="c1"># plt.xlim(4,12)</span>
<span class="k">assert</span> <span class="n">p</span><span class="p">[</span><span class="n">m1</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">m_p_half</span> <span class="o">&lt;</span> <span class="n">p</span><span class="p">[</span><span class="n">m1</span><span class="o">+</span><span class="mi">1</span><span class="p">],</span> <span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="n">m1</span><span class="p">],</span> <span class="n">m_p_half</span><span class="p">,</span> <span class="n">p</span><span class="p">[</span><span class="n">m1</span><span class="o">+</span><span class="mi">1</span><span class="p">])</span>
<span class="k">assert</span> <span class="n">p</span><span class="p">[</span><span class="n">m2</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">m_p_half</span> <span class="o">&gt;</span> <span class="n">p</span><span class="p">[</span><span class="n">m2</span><span class="o">+</span><span class="mi">1</span><span class="p">],</span> <span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="n">m2</span><span class="p">],</span> <span class="n">m_p_half</span><span class="p">,</span> <span class="n">p</span><span class="p">[</span><span class="n">m2</span><span class="o">+</span><span class="mi">1</span><span class="p">])</span>
<span class="c1"># assert p[m1] &lt; m_p_half &lt; p[m1+1], (p[m1], m_p_half, p[m1+1])</span>
<span class="c1"># assert p[m2] &gt; m_p_half &gt; p[m2+1], (p[m2], m_p_half, p[m2+1])</span>
<span class="n">f1</span> <span class="o">=</span> <span class="n">interp1d</span><span class="p">([</span><span class="n">half_p</span><span class="p">[</span><span class="n">m1</span><span class="p">],</span> <span class="n">half_p</span><span class="p">[</span><span class="n">m1</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]],</span> <span class="p">[</span><span class="n">f</span><span class="p">[</span><span class="n">m1</span><span class="p">],</span> <span class="n">f</span><span class="p">[</span><span class="n">m1</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]])(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">f2</span> <span class="o">=</span> <span class="n">interp1d</span><span class="p">([</span><span class="n">half_p</span><span class="p">[</span><span class="n">m2</span><span class="p">],</span> <span class="n">half_p</span><span class="p">[</span><span class="n">m2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]],</span> <span class="p">[</span><span class="n">f</span><span class="p">[</span><span class="n">m2</span><span class="p">],</span> <span class="n">f</span><span class="p">[</span><span class="n">m2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]])(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">return</span> <span class="n">f1</span><span class="p">,</span> <span class="n">f2</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">compute_stim_peak</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">s_f</span><span class="p">):</span>
<span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">s_f</span><span class="p">):</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="k">return</span> <span class="n">interp1d</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">p</span><span class="p">)(</span><span class="n">s_f</span><span class="p">)</span>
<span class="k">return</span> <span class="n">f1</span><span class="p">,</span> <span class="n">f2</span><span class="p">,</span> <span class="n">m_p_half</span> <span class="o">+</span> <span class="n">baseline</span>
</pre></div>
</div>
@ -13408,8 +13437,18 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">NFFT</span> <span class="o">=</span> <span class="mi">10000</span>
<span class="n">theta_band_f1</span><span class="p">,</span> <span class="n">theta_band_f2</span> <span class="o">=</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">10</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">compute_stim_peak</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">s_f</span><span class="p">):</span>
<span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">s_f</span><span class="p">):</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="k">return</span> <span class="n">interp1d</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">p</span><span class="p">)(</span><span class="n">s_f</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">compute_relative_peak</span><span class="p">(</span><span class="n">power</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">max_power</span><span class="p">,</span> <span class="n">band</span><span class="p">,</span> <span class="n">band_width</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
<span class="c1"># estimate baseline power</span>
<span class="n">low_baseline_mask</span> <span class="o">=</span> <span class="p">(</span><span class="n">freq</span> <span class="o">&gt;</span> <span class="n">band</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">band_width</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">freq</span> <span class="o">&lt;</span> <span class="n">band</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="n">high_baseline_mask</span> <span class="o">=</span> <span class="p">(</span><span class="n">freq</span> <span class="o">&gt;</span> <span class="n">band</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">freq</span> <span class="o">&lt;</span> <span class="n">band</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">band_width</span><span class="p">)</span>
<span class="n">baseline</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">([</span><span class="n">power</span><span class="p">[</span><span class="n">low_baseline_mask</span><span class="p">],</span> <span class="n">power</span><span class="p">[</span><span class="n">high_baseline_mask</span><span class="p">]]))</span>
<span class="k">return</span> <span class="p">(</span><span class="n">max_power</span> <span class="o">-</span> <span class="n">baseline</span><span class="p">)</span> <span class="o">/</span> <span class="nb">abs</span><span class="p">(</span><span class="n">baseline</span><span class="p">)</span>
</pre></div>
</div>
@ -13419,12 +13458,28 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">row</span><span class="p">):</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">theta_band_f1</span><span class="p">,</span> <span class="n">theta_band_f2</span> <span class="o">=</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">10</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">psd_data</span><span class="p">,</span> <span class="n">freq_data</span> <span class="o">=</span> <span class="p">{},</span> <span class="p">{}</span>
<span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">perform_zscore</span><span class="p">):</span>
<span class="n">action_id</span> <span class="o">=</span> <span class="n">row</span><span class="p">[</span><span class="s1">&#39;action&#39;</span><span class="p">]</span>
<span class="n">channel_group</span> <span class="o">=</span> <span class="n">row</span><span class="p">[</span><span class="s1">&#39;channel_group&#39;</span><span class="p">]</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">f</span><span class="s1">&#39;</span><span class="si">{action_id}</span><span class="s1">_</span><span class="si">{channel_group}</span><span class="s1">&#39;</span>
<span class="n">lfp</span> <span class="o">=</span> <span class="n">data_loader</span><span class="o">.</span><span class="n">lfp</span><span class="p">(</span><span class="n">action_id</span><span class="p">,</span> <span class="n">channel_group</span><span class="p">)</span>
<span class="n">clean_lfp</span> <span class="o">=</span> <span class="n">select_and_clean</span><span class="p">(</span><span class="n">lfp</span><span class="p">)</span>
<span class="n">snls</span> <span class="o">=</span> <span class="n">signaltonoise</span><span class="p">(</span><span class="n">clean_lfp</span><span class="p">)</span>
@ -13434,19 +13489,30 @@ div#notebook {
<span class="n">lim</span> <span class="o">=</span> <span class="n">get_lim</span><span class="p">(</span><span class="n">action_id</span><span class="p">)</span>
<span class="n">mask</span> <span class="o">=</span> <span class="n">get_mask</span><span class="p">(</span><span class="n">lfp</span><span class="p">,</span> <span class="n">lim</span><span class="p">)</span>
<span class="k">if</span> <span class="n">perform_zscore</span><span class="p">:</span>
<span class="n">signal</span> <span class="o">=</span> <span class="n">zscore</span><span class="p">(</span><span class="n">clean_lfp</span><span class="p">[</span><span class="n">mask</span><span class="p">,</span> <span class="n">best_channel</span><span class="p">]</span><span class="o">.</span><span class="n">ravel</span><span class="p">())</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">signal</span> <span class="o">=</span> <span class="n">clean_lfp</span><span class="p">[</span><span class="n">mask</span><span class="p">,</span> <span class="n">best_channel</span><span class="p">]</span><span class="o">.</span><span class="n">ravel</span><span class="p">()</span>
<span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span> <span class="o">=</span> <span class="n">mlab</span><span class="o">.</span><span class="n">psd</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="n">Fs</span><span class="o">=</span><span class="n">lfp</span><span class="o">.</span><span class="n">sampling_rate</span><span class="o">.</span><span class="n">magnitude</span><span class="p">,</span> <span class="n">NFFT</span><span class="o">=</span><span class="n">NFFT</span><span class="p">)</span>
<span class="n">window</span> <span class="o">=</span> <span class="mi">6</span> <span class="o">*</span> <span class="n">lfp</span><span class="o">.</span><span class="n">sampling_rate</span><span class="o">.</span><span class="n">magnitude</span>
<span class="n">theta_f</span><span class="p">,</span> <span class="n">theta_p_max</span> <span class="o">=</span> <span class="n">find_theta_peak</span><span class="p">(</span><span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span><span class="p">)</span>
<span class="c1"># p_xx, freq = mlab.psd(signal, Fs=lfp.sampling_rate.magnitude, NFFT=NFFT)</span>
<span class="n">freq</span><span class="p">,</span> <span class="n">p_xx</span> <span class="o">=</span> <span class="n">ss</span><span class="o">.</span><span class="n">welch</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="n">fs</span><span class="o">=</span><span class="n">lfp</span><span class="o">.</span><span class="n">sampling_rate</span><span class="o">.</span><span class="n">magnitude</span><span class="p">,</span> <span class="n">nperseg</span><span class="o">=</span><span class="n">window</span><span class="p">,</span> <span class="n">nfft</span><span class="o">=</span><span class="n">scipy</span><span class="o">.</span><span class="n">fftpack</span><span class="o">.</span><span class="n">next_fast_len</span><span class="p">(</span><span class="n">window</span><span class="p">))</span>
<span class="n">p_xx</span> <span class="o">=</span> <span class="mi">10</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">p_xx</span><span class="p">)</span>
<span class="n">theta_energy</span> <span class="o">=</span> <span class="n">compute_energy</span><span class="p">(</span><span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">theta_band_f1</span><span class="p">,</span> <span class="n">theta_band_f2</span><span class="p">)</span> <span class="c1"># theta band 6 - 10 Hz</span>
<span class="n">theta_f</span><span class="p">,</span> <span class="n">theta_p_max</span> <span class="o">=</span> <span class="n">find_theta_peak</span><span class="p">(</span><span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">theta_band_f1</span><span class="p">,</span> <span class="n">theta_band_f2</span><span class="p">)</span>
<span class="n">theta_half_f1</span><span class="p">,</span> <span class="n">theta_half_f2</span> <span class="o">=</span> <span class="n">compute_half_width</span><span class="p">(</span><span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">theta_p_max</span><span class="p">,</span> <span class="n">theta_f</span><span class="p">)</span>
<span class="n">theta_bandpower</span><span class="p">,</span> <span class="n">theta_relpower</span> <span class="o">=</span> <span class="n">compute_band_power</span><span class="p">(</span><span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">theta_band_f1</span><span class="p">,</span> <span class="n">theta_band_f2</span><span class="p">)</span>
<span class="n">theta_relpeak</span> <span class="o">=</span> <span class="n">compute_relative_peak</span><span class="p">(</span><span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">theta_p_max</span><span class="p">,</span> <span class="p">[</span><span class="n">theta_band_f1</span><span class="p">,</span> <span class="n">theta_band_f2</span><span class="p">])</span>
<span class="n">theta_half_f1</span><span class="p">,</span> <span class="n">theta_half_f2</span><span class="p">,</span> <span class="n">theta_half_power</span> <span class="o">=</span> <span class="n">compute_half_width</span><span class="p">(</span><span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">theta_p_max</span><span class="p">,</span> <span class="n">theta_f</span><span class="p">,</span> <span class="p">[</span><span class="n">theta_band_f1</span><span class="p">,</span> <span class="n">theta_band_f2</span><span class="p">])</span>
<span class="n">theta_half_width</span> <span class="o">=</span> <span class="n">theta_half_f2</span> <span class="o">-</span> <span class="n">theta_half_f1</span>
<span class="n">theta_half_energy</span> <span class="o">=</span> <span class="n">compute_energy</span><span class="p">(</span><span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">theta_half_f1</span><span class="p">,</span> <span class="n">theta_half_f2</span><span class="p">)</span> <span class="c1"># theta band 6 - 10 Hz</span>
<span class="n">psd_data</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">name</span><span class="p">:</span> <span class="n">p_xx</span><span class="p">})</span>
<span class="n">freq_data</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">name</span><span class="p">:</span> <span class="n">freq</span><span class="p">})</span>
<span class="c1"># stim</span>
@ -13454,27 +13520,33 @@ div#notebook {
<span class="n">stim_p_max</span> <span class="o">=</span> <span class="n">compute_stim_peak</span><span class="p">(</span><span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">stim_freq</span><span class="p">)</span>
<span class="n">stim_half_f1</span><span class="p">,</span> <span class="n">stim_half_f2</span> <span class="o">=</span> <span class="n">compute_half_width</span><span class="p">(</span><span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">stim_p_max</span><span class="p">,</span> <span class="n">stim_freq</span><span class="p">)</span>
<span class="n">stim_half_f1</span><span class="p">,</span> <span class="n">stim_half_f2</span><span class="p">,</span> <span class="n">stim_half_power</span> <span class="o">=</span> <span class="n">compute_half_width</span><span class="p">(</span><span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">stim_p_max</span><span class="p">,</span> <span class="n">stim_freq</span><span class="p">,</span> <span class="p">[</span><span class="n">stim_freq</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">stim_freq</span> <span class="o">+</span> <span class="mi">1</span><span class="p">])</span>
<span class="n">stim_half_width</span> <span class="o">=</span> <span class="n">stim_half_f2</span> <span class="o">-</span> <span class="n">stim_half_f1</span>
<span class="n">stim_energy</span> <span class="o">=</span> <span class="n">compute_energy</span><span class="p">(</span><span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">stim_half_f1</span><span class="p">,</span> <span class="n">stim_half_f2</span><span class="p">)</span>
<span class="n">stim_bandpower</span><span class="p">,</span> <span class="n">stim_relpower</span> <span class="o">=</span> <span class="n">compute_band_power</span><span class="p">(</span><span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">stim_freq</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">stim_freq</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">stim_relpeak</span> <span class="o">=</span> <span class="n">compute_relative_peak</span><span class="p">(</span><span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">stim_p_max</span><span class="p">,</span> <span class="p">[</span><span class="n">stim_freq</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">stim_freq</span> <span class="o">+</span> <span class="mi">1</span><span class="p">])</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">({</span>
<span class="s1">&#39;signal_to_noise&#39;</span><span class="p">:</span> <span class="n">snl</span><span class="p">,</span>
<span class="s1">&#39;best_channel&#39;</span><span class="p">:</span> <span class="n">best_channel</span><span class="p">,</span>
<span class="s1">&#39;theta_freq&#39;</span><span class="p">:</span> <span class="n">theta_f</span><span class="p">,</span>
<span class="s1">&#39;theta_peak&#39;</span><span class="p">:</span> <span class="n">theta_p_max</span><span class="p">,</span>
<span class="s1">&#39;theta_energy&#39;</span><span class="p">:</span> <span class="n">theta_energy</span><span class="p">,</span>
<span class="s1">&#39;theta_bandpower&#39;</span><span class="p">:</span> <span class="n">theta_bandpower</span><span class="p">,</span>
<span class="s1">&#39;theta_relpower&#39;</span><span class="p">:</span> <span class="n">theta_relpower</span><span class="p">,</span>
<span class="s1">&#39;theta_relpeak&#39;</span><span class="p">:</span> <span class="n">theta_relpeak</span><span class="p">,</span>
<span class="s1">&#39;theta_half_f1&#39;</span><span class="p">:</span> <span class="n">theta_half_f1</span><span class="p">,</span>
<span class="s1">&#39;theta_half_f2&#39;</span><span class="p">:</span> <span class="n">theta_half_f2</span><span class="p">,</span>
<span class="s1">&#39;theta_half_width&#39;</span><span class="p">:</span> <span class="n">theta_half_width</span><span class="p">,</span>
<span class="s1">&#39;theta_half_energy&#39;</span><span class="p">:</span> <span class="n">theta_half_energy</span><span class="p">,</span>
<span class="s1">&#39;stim_freq&#39;</span><span class="p">:</span> <span class="n">stim_freq</span><span class="p">,</span>
<span class="s1">&#39;stim_p_max&#39;</span><span class="p">:</span> <span class="n">stim_p_max</span><span class="p">,</span>
<span class="s1">&#39;stim_half_f1&#39;</span><span class="p">:</span> <span class="n">stim_half_f1</span><span class="p">,</span>
<span class="s1">&#39;stim_half_f2&#39;</span><span class="p">:</span> <span class="n">stim_half_f2</span><span class="p">,</span>
<span class="s1">&#39;stim_half_width&#39;</span><span class="p">:</span> <span class="n">stim_half_width</span><span class="p">,</span>
<span class="s1">&#39;stim_energy&#39;</span><span class="p">:</span> <span class="n">stim_energy</span>
<span class="s1">&#39;stim_bandpower&#39;</span><span class="p">:</span> <span class="n">stim_bandpower</span><span class="p">,</span>
<span class="s1">&#39;stim_relpower&#39;</span><span class="p">:</span> <span class="n">stim_relpower</span><span class="p">,</span>
<span class="s1">&#39;stim_relpeak&#39;</span><span class="p">:</span> <span class="n">stim_relpeak</span><span class="p">,</span>
<span class="p">})</span>
<span class="k">return</span> <span class="n">result</span>
</pre></div>
@ -13486,11 +13558,11 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">results</span> <span class="o">=</span> <span class="n">channel_groups</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span>
<span class="n">channel_groups</span><span class="o">.</span><span class="n">progress_apply</span><span class="p">(</span><span class="n">process</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">),</span>
<span class="n">channel_groups</span><span class="o">.</span><span class="n">progress_apply</span><span class="p">(</span><span class="n">process</span><span class="p">,</span> <span class="n">perform_zscore</span><span class="o">=</span><span class="n">perform_zscore</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">),</span>
<span class="n">left_index</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">right_index</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
@ -13512,13 +13584,13 @@ div#notebook {
<div id="ccdb069e-4b63-4db1-9535-31624e57bc0e"></div>
<div id="1e0ab9a5-d5fa-41a2-82b4-a29075688ad9"></div>
<div class="output_subarea output_widget_view ">
<script type="text/javascript">
var element = $('#ccdb069e-4b63-4db1-9535-31624e57bc0e');
var element = $('#1e0ab9a5-d5fa-41a2-82b4-a29075688ad9');
</script>
<script type="application/vnd.jupyter.widget-view+json">
{"model_id": "e733903bb8d7406ea6c05bf5af093bfc", "version_major": 2, "version_minor": 0}
{"model_id": "0a39fef20b724ef186ce6ce5299b3719", "version_major": 2, "version_minor": 0}
</script>
</div>
@ -13529,8 +13601,19 @@ var element = $('#ccdb069e-4b63-4db1-9535-31624e57bc0e');
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
<div class="output_subarea output_stream output_stderr output_text">
<pre>/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:9: RuntimeWarning: invalid value encountered in greater
if __name__ == &#39;__main__&#39;:
/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:9: RuntimeWarning: invalid value encountered in less
if __name__ == &#39;__main__&#39;:
/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:10: RuntimeWarning: invalid value encountered in greater
# Remove the CWD from sys.path while we load stuff.
/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:10: RuntimeWarning: invalid value encountered in less
# Remove the CWD from sys.path while we load stuff.
/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/numpy/core/fromnumeric.py:3257: RuntimeWarning: Mean of empty slice.
out=out, **kwargs)
/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/numpy/core/_methods.py:161: RuntimeWarning: invalid value encountered in true_divide
ret = ret.dtype.type(ret / rcount)
</pre>
</div>
</div>
@ -13541,112 +13624,17 @@ var element = $('#ccdb069e-4b63-4db1-9535-31624e57bc0e');
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[17]:</div>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">psd</span><span class="p">,</span> <span class="n">freqs</span> <span class="o">=</span> <span class="p">{},</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">tqdm</span><span class="p">(</span><span class="n">channel_groups</span><span class="o">.</span><span class="n">iterrows</span><span class="p">(),</span> <span class="n">total</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">channel_groups</span><span class="p">)):</span>
<span class="n">action_id</span> <span class="o">=</span> <span class="n">row</span><span class="p">[</span><span class="s1">&#39;action&#39;</span><span class="p">]</span>
<span class="n">channel_group</span> <span class="o">=</span> <span class="n">row</span><span class="p">[</span><span class="s1">&#39;channel_group&#39;</span><span class="p">]</span>
<span class="n">action_group</span> <span class="o">=</span> <span class="n">f</span><span class="s1">&#39;</span><span class="si">{action_id}</span><span class="s1">_</span><span class="si">{channel_group}</span><span class="s1">&#39;</span>
<span class="n">lfp</span> <span class="o">=</span> <span class="n">data_loader</span><span class="o">.</span><span class="n">lfp</span><span class="p">(</span><span class="n">action_id</span><span class="p">,</span> <span class="n">channel_group</span><span class="p">)</span>
<span class="n">clean_lfp</span> <span class="o">=</span> <span class="n">select_and_clean</span><span class="p">(</span><span class="n">lfp</span><span class="p">)</span>
<span class="n">snls</span> <span class="o">=</span> <span class="n">signaltonoise</span><span class="p">(</span><span class="n">clean_lfp</span><span class="p">)</span>
<span class="n">best_channel</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">snls</span><span class="p">)</span>
<span class="n">snl</span> <span class="o">=</span> <span class="n">snls</span><span class="p">[</span><span class="n">best_channel</span><span class="p">]</span>
<span class="n">lim</span> <span class="o">=</span> <span class="n">get_lim</span><span class="p">(</span><span class="n">action_id</span><span class="p">)</span>
<span class="n">mask</span> <span class="o">=</span> <span class="n">get_mask</span><span class="p">(</span><span class="n">lfp</span><span class="p">,</span> <span class="n">lim</span><span class="p">)</span>
<span class="n">signal</span> <span class="o">=</span> <span class="n">zscore</span><span class="p">(</span><span class="n">clean_lfp</span><span class="p">[</span><span class="n">mask</span><span class="p">,</span> <span class="n">best_channel</span><span class="p">]</span><span class="o">.</span><span class="n">ravel</span><span class="p">())</span>
<span class="n">p_xx</span><span class="p">,</span> <span class="n">freq</span> <span class="o">=</span> <span class="n">mlab</span><span class="o">.</span><span class="n">psd</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="n">Fs</span><span class="o">=</span><span class="n">lfp</span><span class="o">.</span><span class="n">sampling_rate</span><span class="o">.</span><span class="n">magnitude</span><span class="p">,</span> <span class="n">NFFT</span><span class="o">=</span><span class="n">NFFT</span><span class="p">)</span>
<span class="n">psd</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">action_group</span><span class="p">:</span> <span class="n">p_xx</span><span class="p">})</span>
<span class="n">freqs</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">action_group</span><span class="p">:</span> <span class="n">freq</span><span class="p">})</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">psd_data</span><span class="p">)</span><span class="o">.</span><span class="n">to_feather</span><span class="p">(</span><span class="n">output</span> <span class="o">/</span> <span class="s1">&#39;data&#39;</span> <span class="o">/</span> <span class="s1">&#39;psd.feather&#39;</span><span class="p">)</span>
<span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">freq_data</span><span class="p">)</span><span class="o">.</span><span class="n">to_feather</span><span class="p">(</span><span class="n">output</span> <span class="o">/</span> <span class="s1">&#39;data&#39;</span> <span class="o">/</span> <span class="s1">&#39;freqs.feather&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div id="6a05ea1e-3782-40af-8458-e7713b9c2fb7"></div>
<div class="output_subarea output_widget_view ">
<script type="text/javascript">
var element = $('#6a05ea1e-3782-40af-8458-e7713b9c2fb7');
</script>
<script type="application/vnd.jupyter.widget-view+json">
{"model_id": "0184251a265a40a2a1e9910405f05db8", "version_major": 2, "version_minor": 0}
</script>
</div>
</div>
<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[18]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="p">(</span><span class="n">psd</span><span class="p">)</span><span class="o">.</span><span class="n">to_feather</span><span class="p">(</span><span class="n">output</span> <span class="o">/</span> <span class="s1">&#39;data&#39;</span> <span class="o">/</span> <span class="s1">&#39;psd.feather&#39;</span><span class="p">)</span>
<span class="n">freqs</span><span class="o">.</span><span class="n">to_feather</span><span class="p">(</span><span class="n">output</span> <span class="o">/</span> <span class="s1">&#39;data&#39;</span> <span class="o">/</span> <span class="s1">&#39;freqs.feather&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_text output_error">
<pre>
<span class="ansi-red-fg">---------------------------------------------------------------------------</span>
<span class="ansi-red-fg">AttributeError</span> Traceback (most recent call last)
<span class="ansi-green-fg">&lt;ipython-input-18-7660e1d3307d&gt;</span> in <span class="ansi-cyan-fg">&lt;module&gt;</span>
<span class="ansi-green-fg">----&gt; 1</span><span class="ansi-red-fg"> </span>psd<span class="ansi-blue-fg">.</span>to_feather<span class="ansi-blue-fg">(</span>output <span class="ansi-blue-fg">/</span> <span class="ansi-blue-fg">&#39;data&#39;</span> <span class="ansi-blue-fg">/</span> <span class="ansi-blue-fg">&#39;psd.feather&#39;</span><span class="ansi-blue-fg">)</span>
<span class="ansi-green-intense-fg ansi-bold"> 2</span> freqs<span class="ansi-blue-fg">.</span>to_feather<span class="ansi-blue-fg">(</span>output <span class="ansi-blue-fg">/</span> <span class="ansi-blue-fg">&#39;data&#39;</span> <span class="ansi-blue-fg">/</span> <span class="ansi-blue-fg">&#39;freqs.feather&#39;</span><span class="ansi-blue-fg">)</span>
<span class="ansi-red-fg">AttributeError</span>: &#39;dict&#39; object has no attribute &#39;to_feather&#39;</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
@ -13660,7 +13648,7 @@ var element = $('#6a05ea1e-3782-40af-8458-e7713b9c2fb7');
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">action</span> <span class="o">=</span> <span class="n">project</span><span class="o">.</span><span class="n">require_action</span><span class="p">(</span><span class="s2">&quot;stimulus-lfp-response&quot;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">action</span> <span class="o">=</span> <span class="n">project</span><span class="o">.</span><span class="n">require_action</span><span class="p">(</span><span class="s2">&quot;stimulus-lfp-response&quot;</span> <span class="o">+</span> <span class="n">zscore_str</span><span class="p">)</span>
</pre></div>
</div>
@ -13674,7 +13662,7 @@ var element = $('#6a05ea1e-3782-40af-8458-e7713b9c2fb7');
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">action</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="s1">&#39;parameters&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">&#39;NFFT&#39;</span><span class="p">:</span> <span class="n">NFFT</span><span class="p">,</span>
<span class="s1">&#39;window&#39;</span><span class="p">:</span> <span class="mi">6</span><span class="p">,</span>
<span class="s1">&#39;theta_band_f1&#39;</span><span class="p">:</span> <span class="n">theta_band_f1</span><span class="p">,</span>
<span class="s1">&#39;theta_band_f2&#39;</span><span class="p">:</span> <span class="n">theta_band_f2</span>
<span class="p">}</span>

View File

@ -19,7 +19,9 @@
"name": "stderr",
"output_type": "stream",
"text": [
"10:26:07 [I] klustakwik KlustaKwik2 version 0.2.6\n"
"19:43:24 [I] klustakwik KlustaKwik2 version 0.2.6\n",
"/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:24: TqdmDeprecationWarning: This function will be removed in tqdm==5.0.0\n",
"Please use `tqdm.notebook.*` instead of `tqdm._tqdm_notebook.*`\n"
]
}
],
@ -39,6 +41,8 @@
"import quantities as pq\n",
"import shutil\n",
"from distutils.dir_util import copy_tree\n",
"import scipy\n",
"import scipy.signal as ss\n",
"\n",
"from scipy.signal import find_peaks\n",
"from scipy.interpolate import interp1d\n",
@ -66,8 +70,16 @@
"metadata": {},
"outputs": [],
"source": [
"output = pathlib.Path('output/stimulus-lfp-response')\n",
"(output / 'data').mkdir(parents=True, exist_ok=True)"
"#############################\n",
"\n",
"perform_zscore = True\n",
"\n",
"if not perform_zscore:\n",
" zscore_str = \"-no-zscore\"\n",
"else:\n",
" zscore_str = \"\"\n",
"\n",
"#################################"
]
},
{
@ -75,6 +87,16 @@
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"output = pathlib.Path('output/stimulus-lfp-response' + zscore_str)\n",
"(output / 'data').mkdir(parents=True, exist_ok=True)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"identify_neurons = actions['identify-neurons']\n",
"sessions = pd.read_csv(identify_neurons.data_path('sessions'))"
@ -82,7 +104,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
@ -95,7 +117,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
@ -104,7 +126,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@ -131,7 +153,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
@ -154,7 +176,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
@ -163,17 +185,32 @@
" return np.nan\n",
" mask = (f > f1) & (f < f2)\n",
" df = f[1] - f[0]\n",
" return np.sum(p[mask]) * df"
" return np.sum(p[mask]) * df\n",
"\n",
"\n",
"def compute_band_power(p, f, f1, f2):\n",
" if np.isnan(f1) or np.all(np.isnan(p)):\n",
" return [np.nan] * 2\n",
" from scipy.integrate import simps\n",
" dx = f[1] - f[0]\n",
" mask = (f > f1) & (f < f2)\n",
" # Compute the absolute power by approximating the area under the curve\n",
" band_power = simps(p[mask], dx=dx)\n",
" total_power = simps(p, dx=dx)\n",
" rel_power = band_power / total_power\n",
" return band_power, rel_power"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def find_theta_peak(p, f):\n",
" mask = (f > 6) & (f < 10)\n",
"def find_theta_peak(p, f, f1, f2):\n",
" if np.all(np.isnan(p)):\n",
" return np.nan, np.nan\n",
" mask = (f > f1) & (f < f2)\n",
" p_m = p[mask]\n",
" f_m = f[mask]\n",
" peaks, _ = find_peaks(p_m)\n",
@ -183,49 +220,41 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def compute_half_width(p, f, m_p, m_f):\n",
" if np.isnan(m_p):\n",
" return np.nan, np.nan\n",
"def compute_half_width(power, freq, max_power, max_frequency, band, band_width=1):\n",
" if np.isnan(max_power):\n",
" return [np.nan] * 3\n",
" \n",
" # estimate baseline power\n",
" low_baseline_mask = (freq > band[0] - band_width) & (freq < band[0])\n",
" high_baseline_mask = (freq > band[1]) & (freq < band[1] + band_width)\n",
" baseline = np.mean(np.concatenate([power[low_baseline_mask], power[high_baseline_mask]]))\n",
" p = power - baseline\n",
" m_p = max_power - baseline\n",
" m_f = max_frequency\n",
" f = freq\n",
" \n",
" # estimate half width\n",
" m_p_half = m_p / 2\n",
" half_p = p - m_p_half\n",
" idx_f = np.where(f <= m_f)[0].max()\n",
" idxs_p1, = np.where(np.diff(half_p[:idx_f + 1] > 0) == 1)\n",
" if len(idxs_p1) == 0:\n",
" return np.nan, np.nan\n",
" return [np.nan] * 3\n",
" m1 = idxs_p1.max()\n",
" idxs_p2, = np.where(np.diff(half_p[idx_f:] > 0) == 1)\n",
" if len(idxs_p2) == 0:\n",
" return [np.nan] * 3\n",
" m2 = idxs_p2.min() + idx_f\n",
"# plt.plot(f, p)\n",
"# plt.plot(m_f, m_p, marker='o', ls='none', markersize=10)\n",
"# plt.plot(f[m1], p[m1], marker='x', ls='none', markersize=10, c='r')\n",
"# plt.plot(f[m1+1], p[m1+1], marker='+', ls='none', markersize=10, c='r')\n",
" \n",
"# plt.plot(f[m2], p[m2], marker='x', ls='none', markersize=10, c='k')\n",
"# plt.plot(f[m2+1], p[m2+1], marker='+', ls='none', markersize=10, c='k')\n",
" \n",
"# plt.xlim(4,12)\n",
" assert p[m1] < m_p_half < p[m1+1], (p[m1], m_p_half, p[m1+1])\n",
" assert p[m2] > m_p_half > p[m2+1], (p[m2], m_p_half, p[m2+1])\n",
"# assert p[m1] < m_p_half < p[m1+1], (p[m1], m_p_half, p[m1+1])\n",
"# assert p[m2] > m_p_half > p[m2+1], (p[m2], m_p_half, p[m2+1])\n",
" \n",
" f1 = interp1d([half_p[m1], half_p[m1 + 1]], [f[m1], f[m1 + 1]])(0)\n",
" f2 = interp1d([half_p[m2], half_p[m2 + 1]], [f[m2], f[m2 + 1]])(0)\n",
" return f1, f2"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def compute_stim_peak(p, f, s_f):\n",
" if np.isnan(s_f):\n",
" return np.nan\n",
" return interp1d(f, p)(s_f)"
" return f1, f2, m_p_half + baseline"
]
},
{
@ -234,19 +263,41 @@
"metadata": {},
"outputs": [],
"source": [
"NFFT = 10000\n",
"def compute_stim_peak(p, f, s_f):\n",
" if np.isnan(s_f):\n",
" return np.nan\n",
" return interp1d(f, p)(s_f)\n",
"\n",
"\n",
"def compute_relative_peak(power, freq, max_power, band, band_width=1):\n",
" # estimate baseline power\n",
" low_baseline_mask = (freq > band[0] - band_width) & (freq < band[0])\n",
" high_baseline_mask = (freq > band[1]) & (freq < band[1] + band_width)\n",
" baseline = np.mean(np.concatenate([power[low_baseline_mask], power[high_baseline_mask]]))\n",
" return (max_power - baseline) / abs(baseline)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"theta_band_f1, theta_band_f2 = 6, 10 "
]
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def process(row):\n",
"psd_data, freq_data = {}, {}\n",
"\n",
"def process(row, perform_zscore):\n",
" action_id = row['action']\n",
" channel_group = row['channel_group']\n",
" name = f'{action_id}_{channel_group}'\n",
" lfp = data_loader.lfp(action_id, channel_group)\n",
" clean_lfp = select_and_clean(lfp)\n",
" snls = signaltonoise(clean_lfp)\n",
@ -256,19 +307,30 @@
" lim = get_lim(action_id)\n",
" \n",
" mask = get_mask(lfp, lim)\n",
" if perform_zscore:\n",
" signal = zscore(clean_lfp[mask, best_channel].ravel())\n",
" else:\n",
" signal = clean_lfp[mask, best_channel].ravel()\n",
" \n",
" p_xx, freq = mlab.psd(signal, Fs=lfp.sampling_rate.magnitude, NFFT=NFFT)\n",
" window = 6 * lfp.sampling_rate.magnitude\n",
" \n",
" theta_f, theta_p_max = find_theta_peak(p_xx, freq)\n",
"# p_xx, freq = mlab.psd(signal, Fs=lfp.sampling_rate.magnitude, NFFT=NFFT)\n",
" freq, p_xx = ss.welch(signal, fs=lfp.sampling_rate.magnitude, nperseg=window, nfft=scipy.fftpack.next_fast_len(window))\n",
" p_xx = 10 * np.log10(p_xx)\n",
" \n",
" theta_energy = compute_energy(p_xx, freq, theta_band_f1, theta_band_f2) # theta band 6 - 10 Hz\n",
" theta_f, theta_p_max = find_theta_peak(p_xx, freq, theta_band_f1, theta_band_f2)\n",
" \n",
" theta_half_f1, theta_half_f2 = compute_half_width(p_xx, freq, theta_p_max, theta_f)\n",
" theta_bandpower, theta_relpower = compute_band_power(p_xx, freq, theta_band_f1, theta_band_f2)\n",
" \n",
" theta_relpeak = compute_relative_peak(p_xx, freq, theta_p_max, [theta_band_f1, theta_band_f2])\n",
" \n",
" theta_half_f1, theta_half_f2, theta_half_power = compute_half_width(p_xx, freq, theta_p_max, theta_f, [theta_band_f1, theta_band_f2])\n",
" \n",
" theta_half_width = theta_half_f2 - theta_half_f1\n",
" \n",
" theta_half_energy = compute_energy(p_xx, freq, theta_half_f1, theta_half_f2) # theta band 6 - 10 Hz\n",
" psd_data.update({name: p_xx})\n",
" freq_data.update({name: freq})\n",
"\n",
" \n",
" # stim\n",
" \n",
@ -276,133 +338,89 @@
" \n",
" stim_p_max = compute_stim_peak(p_xx, freq, stim_freq)\n",
" \n",
" stim_half_f1, stim_half_f2 = compute_half_width(p_xx, freq, stim_p_max, stim_freq)\n",
" stim_half_f1, stim_half_f2, stim_half_power = compute_half_width(p_xx, freq, stim_p_max, stim_freq, [stim_freq - 1, stim_freq + 1])\n",
" \n",
" stim_half_width = stim_half_f2 - stim_half_f1\n",
" \n",
" stim_energy = compute_energy(p_xx, freq, stim_half_f1, stim_half_f2)\n",
" stim_bandpower, stim_relpower = compute_band_power(p_xx, freq, stim_freq - 1, stim_freq + 1)\n",
" \n",
" stim_relpeak = compute_relative_peak(p_xx, freq, stim_p_max, [stim_freq - 1, stim_freq + 1])\n",
" \n",
" result = pd.Series({\n",
" 'signal_to_noise': snl,\n",
" 'best_channel': best_channel,\n",
" 'theta_freq': theta_f,\n",
" 'theta_peak': theta_p_max,\n",
" 'theta_energy': theta_energy,\n",
" 'theta_bandpower': theta_bandpower,\n",
" 'theta_relpower': theta_relpower,\n",
" 'theta_relpeak': theta_relpeak,\n",
" 'theta_half_f1': theta_half_f1, \n",
" 'theta_half_f2': theta_half_f2,\n",
" 'theta_half_width': theta_half_width,\n",
" 'theta_half_energy': theta_half_energy,\n",
" 'stim_freq': stim_freq,\n",
" 'stim_p_max': stim_p_max,\n",
" 'stim_half_f1': stim_half_f1, \n",
" 'stim_half_f2': stim_half_f2,\n",
" 'stim_half_width': stim_half_width,\n",
" 'stim_energy': stim_energy\n",
" 'stim_bandpower': stim_bandpower,\n",
" 'stim_relpower': stim_relpower,\n",
" 'stim_relpeak': stim_relpeak,\n",
" })\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e733903bb8d7406ea6c05bf5af093bfc",
"model_id": "0a39fef20b724ef186ce6ce5299b3719",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=704), HTML(value='')))"
"HBox(children=(IntProgress(value=0, max=696), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
"/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:9: RuntimeWarning: invalid value encountered in greater\n",
" if __name__ == '__main__':\n",
"/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:9: RuntimeWarning: invalid value encountered in less\n",
" if __name__ == '__main__':\n",
"/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:10: RuntimeWarning: invalid value encountered in greater\n",
" # Remove the CWD from sys.path while we load stuff.\n",
"/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:10: RuntimeWarning: invalid value encountered in less\n",
" # Remove the CWD from sys.path while we load stuff.\n",
"/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/numpy/core/fromnumeric.py:3257: RuntimeWarning: Mean of empty slice.\n",
" out=out, **kwargs)\n",
"/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/numpy/core/_methods.py:161: RuntimeWarning: invalid value encountered in true_divide\n",
" ret = ret.dtype.type(ret / rcount)\n"
]
}
],
"source": [
"results = channel_groups.merge(\n",
" channel_groups.progress_apply(process, axis=1), \n",
" channel_groups.progress_apply(process, perform_zscore=perform_zscore, axis=1), \n",
" left_index=True, right_index=True)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0184251a265a40a2a1e9910405f05db8",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=704), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"outputs": [],
"source": [
"psd, freqs = {}, {}\n",
"for i, row in tqdm(channel_groups.iterrows(), total=len(channel_groups)):\n",
" action_id = row['action']\n",
" channel_group = row['channel_group']\n",
" action_group = f'{action_id}_{channel_group}'\n",
" lfp = data_loader.lfp(action_id, channel_group)\n",
" clean_lfp = select_and_clean(lfp)\n",
" snls = signaltonoise(clean_lfp)\n",
" best_channel = np.argmax(snls)\n",
" snl = snls[best_channel]\n",
" \n",
" lim = get_lim(action_id)\n",
" \n",
" mask = get_mask(lfp, lim)\n",
" signal = zscore(clean_lfp[mask, best_channel].ravel())\n",
" \n",
" p_xx, freq = mlab.psd(signal, Fs=lfp.sampling_rate.magnitude, NFFT=NFFT)\n",
" psd.update({action_group: p_xx})\n",
" freqs.update({action_group: freq})"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'dict' object has no attribute 'to_feather'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-18-7660e1d3307d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mpsd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto_feather\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutput\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;34m'data'\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;34m'psd.feather'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mfreqs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto_feather\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutput\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;34m'data'\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;34m'freqs.feather'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'dict' object has no attribute 'to_feather'"
]
}
],
"source": [
"(psd).to_feather(output / 'data' / 'psd.feather')\n",
"freqs.to_feather(output / 'data' / 'freqs.feather')"
"pd.DataFrame(psd_data).to_feather(output / 'data' / 'psd.feather')\n",
"pd.DataFrame(freq_data).to_feather(output / 'data' / 'freqs.feather')"
]
},
{
@ -418,7 +436,7 @@
"metadata": {},
"outputs": [],
"source": [
"action = project.require_action(\"stimulus-lfp-response\")"
"action = project.require_action(\"stimulus-lfp-response\" + zscore_str)"
]
},
{
@ -428,7 +446,7 @@
"outputs": [],
"source": [
"action.modules['parameters'] = {\n",
" 'NFFT': NFFT,\n",
" 'window': 6,\n",
" 'theta_band_f1': theta_band_f1,\n",
" 'theta_band_f2': theta_band_f2\n",
"}"
@ -490,5 +508,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,3 @@
NFFT: 10000
window: 6
theta_band_f1: 6
theta_band_f2: 10