actions/stimulus-spike-lfp-response/

This commit is contained in:
Mikkel Elle Lepperød 2021-03-10 13:27:53 +01:00
parent e32c5b37bc
commit 788d46e292
251 changed files with 44319 additions and 3949 deletions

View File

@ -13115,7 +13115,7 @@ div#notebook {
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</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="o">%</span><span class="k">load_ext</span> autoreload
@ -13129,7 +13129,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[2]:</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="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
@ -13140,6 +13140,8 @@ div#notebook {
<span class="kn">import</span> <span class="nn">expipe</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">pathlib</span>
<span class="kn">import</span> <span class="nn">scipy</span>
<span class="kn">import</span> <span class="nn">scipy.signal</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">exdir</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
@ -13162,28 +13164,10 @@ div#notebook {
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stderr output_text">
<pre>10:25:26 [I] klustakwik KlustaKwik2 version 0.2.6
</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;[3]:</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">data_loader</span> <span class="o">=</span> <span class="n">dp</span><span class="o">.</span><span class="n">Data</span><span class="p">()</span>
@ -13198,7 +13182,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[4]:</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">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-spike-lfp-response&#39;</span><span class="p">)</span>
@ -13212,7 +13196,7 @@ div#notebook {
</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;[&nbsp;]:</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>
@ -13227,7 +13211,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;[&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">get_lim</span><span class="p">(</span><span class="n">action_id</span><span class="p">):</span>
@ -13258,7 +13242,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;[&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">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>
@ -13266,16 +13250,6 @@ div#notebook {
<span class="n">m</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">axis</span><span class="p">)</span>
<span class="n">sd</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="n">axis</span><span class="p">,</span> <span class="n">ddof</span><span class="o">=</span><span class="n">ddof</span><span class="p">)</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">sd</span> <span class="o">==</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">m</span> <span class="o">/</span> <span class="n">sd</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">compute_clean_lfp</span><span class="p">(</span><span class="n">anas</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">500</span><span class="p">,</span> <span class="n">threshold</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span>
<span class="n">anas</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">anas</span><span class="p">)</span>
<span class="k">for</span> <span class="n">ch</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">anas</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
<span class="n">idxs</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="nb">abs</span><span class="p">(</span><span class="n">anas</span><span class="p">[:,</span> <span class="n">ch</span><span class="p">])</span> <span class="o">&gt;</span> <span class="n">threshold</span><span class="p">)</span>
<span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="n">idxs</span><span class="p">:</span>
<span class="n">anas</span><span class="p">[</span><span class="n">idx</span><span class="o">-</span><span class="n">width</span><span class="p">:</span><span class="n">idx</span><span class="o">+</span><span class="n">width</span><span class="p">,</span> <span class="n">ch</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># TODO AR model prediction</span>
<span class="k">return</span> <span class="n">anas</span>
</pre></div>
</div>
@ -13285,7 +13259,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;[&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">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>
@ -13303,7 +13277,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;[&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">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>
@ -13324,7 +13298,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;[&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">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>
@ -13354,7 +13328,36 @@ 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;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># p = np.load(&#39;debug_p.npy&#39;)</span>
<span class="c1"># f = np.load(&#39;debug_f.npy&#39;)</span>
<span class="c1"># compute_half_width(p, f, 0.01038941, 30.30187709636872)</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;[12]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># plt.plot(f, p)</span>
<span class="c1"># plt.xlim(29.9,30.6)</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>
@ -13367,81 +13370,16 @@ div#notebook {
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<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">compute_spike_lfp</span><span class="p">(</span><span class="n">anas</span><span class="p">,</span> <span class="n">sptr</span><span class="p">,</span> <span class="n">t_start</span><span class="p">,</span> <span class="n">t_stop</span><span class="p">,</span> <span class="n">NFFT</span><span class="p">):</span>
<span class="n">t_start</span> <span class="o">=</span> <span class="n">t_start</span> <span class="o">*</span> <span class="n">pq</span><span class="o">.</span><span class="n">s</span> <span class="k">if</span> <span class="n">t_start</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="mi">0</span> <span class="o">*</span> <span class="n">pq</span><span class="o">.</span><span class="n">s</span>
<span class="n">sampling_rate</span> <span class="o">=</span> <span class="n">anas</span><span class="o">.</span><span class="n">sampling_rate</span>
<span class="n">units</span> <span class="o">=</span> <span class="n">anas</span><span class="o">.</span><span class="n">units</span>
<span class="k">if</span> <span class="n">t_start</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">t_stop</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">t_stop</span> <span class="o">=</span> <span class="n">t_stop</span> <span class="o">*</span> <span class="n">pq</span><span class="o">.</span><span class="n">s</span>
<span class="n">mask</span> <span class="o">=</span> <span class="p">(</span><span class="n">anas</span><span class="o">.</span><span class="n">times</span> <span class="o">&gt;</span> <span class="n">t_start</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">anas</span><span class="o">.</span><span class="n">times</span> <span class="o">&lt;</span> <span class="n">t_stop</span><span class="p">)</span>
<span class="n">anas</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">anas</span><span class="p">)[</span><span class="n">mask</span><span class="p">,:]</span>
<span class="n">cleaned_anas</span> <span class="o">=</span> <span class="n">zscore</span><span class="p">(</span><span class="n">compute_clean_lfp</span><span class="p">(</span><span class="n">anas</span><span class="p">))</span>
<span class="n">cleaned_anas</span> <span class="o">=</span> <span class="n">neo</span><span class="o">.</span><span class="n">AnalogSignal</span><span class="p">(</span>
<span class="n">signal</span><span class="o">=</span><span class="n">cleaned_anas</span> <span class="o">*</span> <span class="n">units</span><span class="p">,</span> <span class="n">sampling_rate</span><span class="o">=</span><span class="n">sampling_rate</span><span class="p">,</span> <span class="n">t_start</span><span class="o">=</span><span class="n">t_start</span>
<span class="p">)</span>
<span class="n">sptr</span> <span class="o">=</span> <span class="n">neo</span><span class="o">.</span><span class="n">SpikeTrain</span><span class="p">(</span>
<span class="n">sptr</span><span class="o">.</span><span class="n">times</span><span class="p">[(</span><span class="n">sptr</span><span class="o">.</span><span class="n">times</span> <span class="o">&gt;</span> <span class="n">t_start</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">sptr</span><span class="o">.</span><span class="n">times</span> <span class="o">&lt;</span> <span class="n">cleaned_anas</span><span class="o">.</span><span class="n">times</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])],</span>
<span class="n">t_start</span><span class="o">=</span><span class="n">t_start</span><span class="p">,</span> <span class="n">t_stop</span><span class="o">=</span><span class="n">cleaned_anas</span><span class="o">.</span><span class="n">times</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="p">)</span>
<span class="n">sigs</span><span class="p">,</span> <span class="n">freqs</span> <span class="o">=</span> <span class="n">el</span><span class="o">.</span><span class="n">sta</span><span class="o">.</span><span class="n">spike_field_coherence</span><span class="p">(</span><span class="n">cleaned_anas</span><span class="p">,</span> <span class="n">sptr</span><span class="p">,</span> <span class="o">**</span><span class="p">{</span><span class="s1">&#39;nperseg&#39;</span><span class="p">:</span> <span class="n">NFFT</span><span class="p">})</span>
<span class="k">return</span> <span class="n">sigs</span><span class="p">,</span> <span class="n">freqs</span><span class="p">,</span> <span class="n">cleaned_anas</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="c1"># action_id_0 = &#39;1833-200619-1&#39;</span>
<span class="c1"># action_id_1 = &#39;1833-200619-2&#39;</span>
<span class="c1"># lfp_0 = data_loader.lfp(action_id_0, 6)</span>
<span class="c1"># lfp_1 = data_loader.lfp(action_id_1, 6)</span>
<span class="c1"># lim_0 = get_lim(action_id_0)</span>
<span class="c1"># lim_1 = get_lim(action_id_1)</span>
<span class="c1"># sptrs_0 = data_loader.spike_trains(action_id_0, 6)</span>
<span class="c1"># sptrs_1 = data_loader.spike_trains(action_id_1, 6)</span>
<span class="c1"># coher_0, freq_0, clean_lfp_0 = compute_spike_lfp(lfp_0, sptrs_0[163], *lim_0, 4096)</span>
<span class="c1"># coher_1, freq_1, clean_lfp_1 = compute_spike_lfp(lfp_1, sptrs_1[28], *lim_1, 4096)</span>
<span class="c1"># best_channel_0 = np.argmax(signaltonoise(clean_lfp_0))</span>
<span class="c1"># best_channel_1 = np.argmax(signaltonoise(clean_lfp_1))</span>
<span class="c1"># plt.plot(freq_0, coher_0[:,best_channel_0])</span>
<span class="c1"># plt.plot(freq_1, coher_1[:,best_channel_1])</span>
<span class="c1"># plt.xlim(0,20)</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;[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">4096</span><span class="o">*</span><span class="mi">2</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_spike_lfp_coherence</span><span class="p">(</span><span class="n">anas</span><span class="p">,</span> <span class="n">sptr</span><span class="p">,</span> <span class="n">NFFT</span><span class="p">):</span>
<span class="n">sigs</span><span class="p">,</span> <span class="n">freqs</span> <span class="o">=</span> <span class="n">el</span><span class="o">.</span><span class="n">sta</span><span class="o">.</span><span class="n">spike_field_coherence</span><span class="p">(</span><span class="n">anas</span><span class="p">,</span> <span class="n">sptr</span><span class="p">,</span> <span class="o">**</span><span class="p">{</span><span class="s1">&#39;nperseg&#39;</span><span class="p">:</span> <span class="n">NFFT</span><span class="p">})</span>
<span class="k">return</span> <span class="n">sigs</span><span class="p">,</span> <span class="n">freqs</span>
</pre></div>
</div>
@ -13454,23 +13392,327 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[15]:</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="k">def</span> <span class="nf">butter_bandpass</span><span class="p">(</span><span class="n">lowcut</span><span class="p">,</span> <span class="n">highcut</span><span class="p">,</span> <span class="n">fs</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">5</span><span class="p">):</span>
<span class="n">nyq</span> <span class="o">=</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">fs</span>
<span class="n">low</span> <span class="o">=</span> <span class="n">lowcut</span> <span class="o">/</span> <span class="n">nyq</span>
<span class="n">high</span> <span class="o">=</span> <span class="n">highcut</span> <span class="o">/</span> <span class="n">nyq</span>
<span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">=</span> <span class="n">scipy</span><span class="o">.</span><span class="n">signal</span><span class="o">.</span><span class="n">butter</span><span class="p">(</span><span class="n">order</span><span class="p">,</span> <span class="p">[</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">],</span> <span class="n">btype</span><span class="o">=</span><span class="s1">&#39;band&#39;</span><span class="p">)</span>
<span class="k">return</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span>
<span class="k">def</span> <span class="nf">butter_bandpass_filter</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">lowcut</span><span class="p">,</span> <span class="n">highcut</span><span class="p">,</span> <span class="n">fs</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">5</span><span class="p">):</span>
<span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">=</span> <span class="n">butter_bandpass</span><span class="p">(</span><span class="n">lowcut</span><span class="p">,</span> <span class="n">highcut</span><span class="p">,</span> <span class="n">fs</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="n">order</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">scipy</span><span class="o">.</span><span class="n">signal</span><span class="o">.</span><span class="n">filtfilt</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
<span class="k">return</span> <span class="n">y</span>
<span class="k">def</span> <span class="nf">compute_spike_phase_func</span><span class="p">(</span><span class="n">lfp</span><span class="p">,</span> <span class="n">times</span><span class="p">,</span> <span class="n">return_degrees</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="n">x_a</span> <span class="o">=</span> <span class="n">hilbert</span><span class="p">(</span><span class="n">lfp</span><span class="p">)</span>
<span class="n">x_phase</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">angle</span><span class="p">(</span><span class="n">x_a</span><span class="p">)</span>
<span class="k">if</span> <span class="n">return_degrees</span><span class="p">:</span>
<span class="n">x_phase</span> <span class="o">=</span> <span class="n">x_phase</span> <span class="o">*</span> <span class="mi">180</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span>
<span class="k">return</span> <span class="n">interp1d</span><span class="p">(</span><span class="n">times</span><span class="p">,</span> <span class="n">x_phase</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">vonmises_kde</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">kappa</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">n_bins</span><span class="o">=</span><span class="mi">100</span><span class="p">):</span>
<span class="kn">from</span> <span class="nn">scipy.special</span> <span class="k">import</span> <span class="n">i0</span>
<span class="n">bins</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">,</span> <span class="n">n_bins</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">,</span> <span class="n">n_bins</span><span class="p">)</span>
<span class="c1"># integrate vonmises kernels</span>
<span class="n">kde</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">kappa</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">]</span> <span class="o">-</span> <span class="n">data</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:]))</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">i0</span><span class="p">(</span><span class="n">kappa</span><span class="p">))</span>
<span class="n">kde</span> <span class="o">/=</span> <span class="n">np</span><span class="o">.</span><span class="n">trapz</span><span class="p">(</span><span class="n">kde</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="n">bins</span><span class="p">)</span>
<span class="k">return</span> <span class="n">bins</span><span class="p">,</span> <span class="n">kde</span>
<span class="k">def</span> <span class="nf">spike_phase_score</span><span class="p">(</span><span class="n">phase_bins</span><span class="p">,</span> <span class="n">density</span><span class="p">):</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">pycircstat</span> <span class="k">as</span> <span class="nn">pc</span>
<span class="n">ang</span> <span class="o">=</span> <span class="n">pc</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">phase_bins</span><span class="p">,</span> <span class="n">w</span><span class="o">=</span><span class="n">density</span><span class="p">)</span>
<span class="n">vec_len</span> <span class="o">=</span> <span class="n">pc</span><span class="o">.</span><span class="n">resultant_vector_length</span><span class="p">(</span><span class="n">phase_bins</span><span class="p">,</span> <span class="n">w</span><span class="o">=</span><span class="n">density</span><span class="p">)</span>
<span class="c1"># ci_lim = pc.mean_ci_limits(head_angle_bins, w=rate)</span>
<span class="k">return</span> <span class="n">ang</span><span class="p">,</span> <span class="n">vec_len</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;[16]:</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_clean_lfp</span><span class="p">(</span><span class="n">anas</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">500</span><span class="p">,</span> <span class="n">threshold</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span>
<span class="n">anas</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">anas</span><span class="p">)</span>
<span class="n">idxs</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="nb">abs</span><span class="p">(</span><span class="n">anas</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">threshold</span><span class="p">)</span>
<span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="n">idxs</span><span class="p">:</span>
<span class="n">anas</span><span class="p">[</span><span class="n">idx</span><span class="o">-</span><span class="n">width</span><span class="p">:</span><span class="n">idx</span><span class="o">+</span><span class="n">width</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># TODO AR model prediction</span>
<span class="k">return</span> <span class="n">anas</span><span class="p">,</span> <span class="n">idxs</span>
<span class="k">def</span> <span class="nf">compute_clean_spikes</span><span class="p">(</span><span class="n">spikes</span><span class="p">,</span> <span class="n">idxs</span><span class="p">,</span> <span class="n">times</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">500</span><span class="p">):</span>
<span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="n">idxs</span><span class="p">:</span>
<span class="n">t0</span> <span class="o">=</span> <span class="n">times</span><span class="p">[</span><span class="n">idx</span><span class="o">-</span><span class="n">width</span><span class="p">]</span>
<span class="n">stop</span> <span class="o">=</span> <span class="n">idx</span> <span class="o">+</span> <span class="n">width</span>
<span class="k">if</span> <span class="n">stop</span> <span class="o">&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">times</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">stop</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">times</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
<span class="n">t1</span> <span class="o">=</span> <span class="n">times</span><span class="p">[</span><span class="n">stop</span><span class="p">]</span>
<span class="n">mask</span> <span class="o">=</span> <span class="p">(</span><span class="n">spikes</span> <span class="o">&gt;</span> <span class="n">t0</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">spikes</span> <span class="o">&lt;</span> <span class="n">t1</span><span class="p">)</span>
<span class="n">spikes</span> <span class="o">=</span> <span class="n">spikes</span><span class="p">[</span><span class="o">~</span><span class="n">mask</span><span class="p">]</span>
<span class="n">spikes</span> <span class="o">=</span> <span class="n">spikes</span><span class="p">[</span><span class="n">spikes</span> <span class="o">&lt;=</span> <span class="n">times</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
<span class="k">return</span> <span class="n">spikes</span>
<span class="k">def</span> <span class="nf">prepare_spike_lfp</span><span class="p">(</span><span class="n">anas</span><span class="p">,</span> <span class="n">sptr</span><span class="p">,</span> <span class="n">t_start</span><span class="p">,</span> <span class="n">t_stop</span><span class="p">):</span>
<span class="n">t_start</span> <span class="o">=</span> <span class="n">t_start</span> <span class="o">*</span> <span class="n">pq</span><span class="o">.</span><span class="n">s</span> <span class="k">if</span> <span class="n">t_start</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="mi">0</span> <span class="o">*</span> <span class="n">pq</span><span class="o">.</span><span class="n">s</span>
<span class="n">sampling_rate</span> <span class="o">=</span> <span class="n">anas</span><span class="o">.</span><span class="n">sampling_rate</span>
<span class="n">units</span> <span class="o">=</span> <span class="n">anas</span><span class="o">.</span><span class="n">units</span>
<span class="n">times</span> <span class="o">=</span> <span class="n">anas</span><span class="o">.</span><span class="n">times</span>
<span class="k">if</span> <span class="n">t_start</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">t_stop</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">t_stop</span> <span class="o">=</span> <span class="n">t_stop</span> <span class="o">*</span> <span class="n">pq</span><span class="o">.</span><span class="n">s</span>
<span class="n">mask</span> <span class="o">=</span> <span class="p">(</span><span class="n">times</span> <span class="o">&gt;</span> <span class="n">t_start</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">times</span> <span class="o">&lt;</span> <span class="n">t_stop</span><span class="p">)</span>
<span class="n">anas</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">anas</span><span class="p">)[</span><span class="n">mask</span><span class="p">,:]</span>
<span class="n">times</span> <span class="o">=</span> <span class="n">times</span><span class="p">[</span><span class="n">mask</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">signaltonoise</span><span class="p">(</span><span class="n">anas</span><span class="p">))</span>
<span class="c1"># best_channel = np.random.choice(anas.shape[1])</span>
<span class="n">cleaned_anas</span><span class="p">,</span> <span class="n">idxs</span> <span class="o">=</span> <span class="n">compute_clean_lfp</span><span class="p">(</span><span class="n">anas</span><span class="p">[:,</span> <span class="n">best_channel</span><span class="p">])</span>
<span class="n">cleaned_anas</span> <span class="o">=</span> <span class="n">neo</span><span class="o">.</span><span class="n">AnalogSignal</span><span class="p">(</span>
<span class="n">signal</span><span class="o">=</span><span class="n">cleaned_anas</span> <span class="o">*</span> <span class="n">units</span><span class="p">,</span> <span class="n">sampling_rate</span><span class="o">=</span><span class="n">sampling_rate</span><span class="p">,</span> <span class="n">t_start</span><span class="o">=</span><span class="n">t_start</span>
<span class="p">)</span>
<span class="n">spike_units</span> <span class="o">=</span> <span class="n">sptr</span><span class="o">.</span><span class="n">units</span>
<span class="n">spike_times</span> <span class="o">=</span> <span class="n">sptr</span><span class="o">.</span><span class="n">times</span>
<span class="n">spike_times</span> <span class="o">=</span> <span class="n">compute_clean_spikes</span><span class="p">(</span><span class="n">spike_times</span><span class="p">,</span> <span class="n">idxs</span><span class="p">,</span> <span class="n">times</span><span class="p">)</span>
<span class="n">sptr</span> <span class="o">=</span> <span class="n">neo</span><span class="o">.</span><span class="n">SpikeTrain</span><span class="p">(</span>
<span class="n">spike_times</span><span class="p">[(</span><span class="n">spike_times</span> <span class="o">&gt;</span> <span class="n">t_start</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">spike_times</span> <span class="o">&lt;</span> <span class="n">times</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])],</span> <span class="n">units</span><span class="o">=</span><span class="n">spike_units</span><span class="p">,</span>
<span class="n">t_start</span><span class="o">=</span><span class="n">t_start</span><span class="p">,</span> <span class="n">t_stop</span><span class="o">=</span><span class="n">times</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="p">)</span>
<span class="k">return</span> <span class="n">cleaned_anas</span><span class="p">,</span> <span class="n">sptr</span><span class="p">,</span> <span class="n">best_channel</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;[17]:</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_spike_phase_func</span><span class="p">(</span><span class="n">lfp</span><span class="p">,</span> <span class="n">times</span><span class="p">,</span> <span class="n">return_degrees</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="kn">from</span> <span class="nn">scipy.fftpack</span> <span class="k">import</span> <span class="n">next_fast_len</span>
<span class="n">x_a</span> <span class="o">=</span> <span class="n">scipy</span><span class="o">.</span><span class="n">signal</span><span class="o">.</span><span class="n">hilbert</span><span class="p">(</span>
<span class="n">lfp</span><span class="p">,</span> <span class="n">next_fast_len</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">lfp</span><span class="p">)))[:</span><span class="nb">len</span><span class="p">(</span><span class="n">lfp</span><span class="p">)]</span>
<span class="c1"># x_a = hilbert(lfp)</span>
<span class="n">x_phase</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">angle</span><span class="p">(</span><span class="n">x_a</span><span class="p">,</span> <span class="n">deg</span><span class="o">=</span><span class="n">return_degrees</span><span class="p">)</span>
<span class="k">return</span> <span class="n">interp1d</span><span class="p">(</span><span class="n">times</span><span class="p">,</span> <span class="n">x_phase</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">compute_spike_phase</span><span class="p">(</span><span class="n">lfp</span><span class="p">,</span> <span class="n">spikes</span><span class="p">,</span> <span class="n">flim</span><span class="o">=</span><span class="p">[</span><span class="mi">6</span><span class="p">,</span><span class="mi">10</span><span class="p">]):</span>
<span class="n">sample_rate</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="c1"># sometimes the position is recorded after LFP recording is ended</span>
<span class="n">times</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">lfp</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">/</span> <span class="n">sample_rate</span>
<span class="n">spikes</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">spikes</span><span class="p">)</span>
<span class="n">spikes</span> <span class="o">=</span> <span class="n">spikes</span><span class="p">[(</span><span class="n">spikes</span> <span class="o">&gt;</span> <span class="n">times</span><span class="o">.</span><span class="n">min</span><span class="p">())</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">spikes</span> <span class="o">&lt;</span> <span class="n">times</span><span class="o">.</span><span class="n">max</span><span class="p">())]</span>
<span class="n">filtered_lfp</span> <span class="o">=</span> <span class="n">butter_bandpass_filter</span><span class="p">(</span>
<span class="n">lfp</span><span class="o">.</span><span class="n">magnitude</span><span class="o">.</span><span class="n">ravel</span><span class="p">(),</span> <span class="o">*</span><span class="n">flim</span><span class="p">,</span> <span class="n">fs</span><span class="o">=</span><span class="n">sample_rate</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="n">spike_phase_func</span> <span class="o">=</span> <span class="n">compute_spike_phase_func</span><span class="p">(</span><span class="n">filtered_lfp</span><span class="p">,</span> <span class="n">times</span><span class="p">)</span>
<span class="k">return</span> <span class="n">spike_phase_func</span><span class="p">(</span><span class="n">spikes</span><span class="p">),</span> <span class="n">filtered_lfp</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;[18]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># plt.figure(figsize=(16,9))</span>
<span class="c1"># # lfp = data_loader.lfp(&#39;1833-200619-2&#39;, 6)</span>
<span class="c1"># lfp = data_loader.lfp(&#39;1834-220319-3&#39;, 6)</span>
<span class="c1"># # lfp = data_loader.lfp(&#39;1849-010319-4&#39;, 6)</span>
<span class="c1"># times = np.arange(lfp.shape[0]) / lfp.sampling_rate.magnitude</span>
<span class="c1"># clean_lfp, _ = compute_clean_lfp(lfp.magnitude[:, 0], threshold=2)</span>
<span class="c1"># plt.plot(times,lfp[:,0])</span>
<span class="c1"># plt.plot(times,clean_lfp)</span>
<span class="c1"># plt.figure(figsize=(16,9))</span>
<span class="c1"># plt.psd(lfp[:,0].ravel(), Fs=1000, NFFT=10000)</span>
<span class="c1"># plt.psd(clean_lfp, Fs=1000, NFFT=10000)</span>
<span class="c1"># plt.xlim(0,100)</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;[19]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># plt.figure(figsize=(16,9))</span>
<span class="c1"># plt.plot(times,lfp[:,0])</span>
<span class="c1"># # plt.plot(clean_lfp*100)</span>
<span class="c1"># plt.plot(times[:-1], np.diff(lfp[:,0].magnitude.ravel()))</span>
<span class="c1"># plt.xlim(64.5,65.5)</span>
<span class="c1"># # plt.ylim(-250,250)</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;[20]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># # action_id_0, channel_0, unit_0 = &#39;1833-200619-1&#39;, 6, 163</span>
<span class="c1"># # action_id_1, channel_1, unit_1 = &#39;1833-200619-2&#39;, 6, 28</span>
<span class="c1"># action_id_0, channel_0, unit_0 = &#39;1834-220319-3&#39;, 2, 46</span>
<span class="c1"># action_id_1, channel_1, unit_1 = &#39;1834-220319-4&#39;, 2, 60</span>
<span class="c1"># lfp_0 = data_loader.lfp(action_id_0, channel_0)</span>
<span class="c1"># lfp_1 = data_loader.lfp(action_id_1, channel_1)</span>
<span class="c1"># sample_rate_0 = lfp_0.sampling_rate</span>
<span class="c1"># sample_rate_1 = lfp_1.sampling_rate</span>
<span class="c1"># lim_0 = get_lim(action_id_0)</span>
<span class="c1"># lim_1 = get_lim(action_id_1)</span>
<span class="c1"># sptrs_0 = data_loader.spike_trains(action_id_0, channel_0)</span>
<span class="c1"># sptrs_1 = data_loader.spike_trains(action_id_1, channel_1)</span>
<span class="c1"># cleaned_lfps_0, sptr_0, best_channel_0 = prepare_spike_lfp(lfp_0, sptrs_0[unit_0], *lim_0)</span>
<span class="c1"># cleaned_lfps_1, sptr_1, best_channel_1 = prepare_spike_lfp(lfp_1, sptrs_1[unit_1], *lim_1)</span>
<span class="c1"># coher_0, freq_0 = compute_spike_lfp_coherence(cleaned_lfps_0, sptr_0, 4096)</span>
<span class="c1"># coher_1, freq_1 = compute_spike_lfp_coherence(cleaned_lfps_1, sptr_1, 4096)</span>
<span class="c1"># spike_phase_0, filtered_lfp_0 = compute_spike_phase(cleaned_lfps_0, sptrs_0[unit_0], flim=[6,10])</span>
<span class="c1"># spike_phase_1, filtered_lfp_1 = compute_spike_phase(cleaned_lfps_1, sptrs_1[unit_1], flim=[6,10])</span>
<span class="c1"># spike_phase_1_stim, filtered_lfp_1_stim = compute_spike_phase(cleaned_lfps_1, sptrs_1[unit_1], flim=[10.5,11.5])</span>
<span class="c1"># plt.figure()</span>
<span class="c1"># plt.plot(freq_0, coher_0.ravel())</span>
<span class="c1"># plt.plot(freq_1, coher_1.ravel())</span>
<span class="c1"># plt.xlim(0,20)</span>
<span class="c1"># plt.figure()</span>
<span class="c1"># bins_0, kde_0 = vonmises_kde(spike_phase_0, 100)</span>
<span class="c1"># ang_0, vec_len_0 = spike_phase_score(bins_0, kde_0)</span>
<span class="c1"># plt.polar(bins_0, kde_0, color=&#39;b&#39;)</span>
<span class="c1"># plt.polar([ang_0, ang_0], [0, vec_len_0], color=&#39;b&#39;)</span>
<span class="c1"># bins_1, kde_1 = vonmises_kde(spike_phase_1, 100)</span>
<span class="c1"># ang_1, vec_len_1 = spike_phase_score(bins_1, kde_1)</span>
<span class="c1"># plt.polar(bins_1, kde_1, color=&#39;r&#39;)</span>
<span class="c1"># plt.polar([ang_1, ang_1], [0, vec_len_1], color=&#39;r&#39;)</span>
<span class="c1"># bins_1_stim, kde_1_stim = vonmises_kde(spike_phase_1_stim, 100)</span>
<span class="c1"># ang_1_stim, vec_len_1_stim = spike_phase_score(bins_1_stim, kde_1_stim)</span>
<span class="c1"># plt.polar(bins_1_stim, kde_1_stim, color=&#39;k&#39;)</span>
<span class="c1"># plt.polar([ang_1_stim, ang_1_stim], [0, vec_len_1_stim], color=&#39;k&#39;)</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>
</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;[21]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># TODO fix artefact stuff from phase precession</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;[22]:</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">8192</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;[23]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">coherence_data</span><span class="p">,</span> <span class="n">freqency_data</span> <span class="o">=</span> <span class="p">{},</span> <span class="p">{}</span>
<span class="n">theta_kde_data</span><span class="p">,</span> <span class="n">theta_bins_data</span> <span class="o">=</span> <span class="p">{},</span> <span class="p">{}</span>
<span class="n">stim_kde_data</span><span class="p">,</span> <span class="n">stim_bins_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">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">unit_name</span> <span class="o">=</span> <span class="n">row</span><span class="p">[</span><span class="s1">&#39;unit_name&#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">_</span><span class="si">{unit_name}</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="c1"># TODO consider choosing strongest stim response</span>
<span class="n">sptr</span> <span class="o">=</span> <span class="n">data_loader</span><span class="o">.</span><span class="n">spike_train</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">unit_name</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">p_xys</span><span class="p">,</span> <span class="n">freq</span><span class="p">,</span> <span class="n">clean_lfp</span> <span class="o">=</span> <span class="n">compute_spike_lfp</span><span class="p">(</span><span class="n">lfp</span><span class="p">,</span> <span class="n">sptr</span><span class="p">,</span> <span class="o">*</span><span class="n">lim</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">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">p_xy</span> <span class="o">=</span> <span class="n">p_xys</span><span class="p">[:,</span><span class="n">best_channel</span><span class="p">]</span><span class="o">.</span><span class="n">magnitude</span>
<span class="n">cleaned_lfp</span><span class="p">,</span> <span class="n">sptr</span><span class="p">,</span> <span class="n">best_channel</span> <span class="o">=</span> <span class="n">prepare_spike_lfp</span><span class="p">(</span><span class="n">lfp</span><span class="p">,</span> <span class="n">sptr</span><span class="p">,</span> <span class="o">*</span><span class="n">lim</span><span class="p">)</span>
<span class="n">p_xys</span><span class="p">,</span> <span class="n">freq</span> <span class="o">=</span> <span class="n">compute_spike_lfp_coherence</span><span class="p">(</span><span class="n">cleaned_lfp</span><span class="p">,</span> <span class="n">sptr</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">p_xy</span> <span class="o">=</span> <span class="n">p_xys</span><span class="o">.</span><span class="n">magnitude</span><span class="o">.</span><span class="n">ravel</span><span class="p">()</span>
<span class="n">freq</span> <span class="o">=</span> <span class="n">freq</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_xy</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>
@ -13483,6 +13725,12 @@ div#notebook {
<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_xy</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">theta_spike_phase</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">compute_spike_phase</span><span class="p">(</span><span class="n">cleaned_lfp</span><span class="p">,</span> <span class="n">sptr</span><span class="p">,</span> <span class="n">flim</span><span class="o">=</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_bins</span><span class="p">,</span> <span class="n">theta_kde</span> <span class="o">=</span> <span class="n">vonmises_kde</span><span class="p">(</span><span class="n">theta_spike_phase</span><span class="p">)</span>
<span class="n">theta_ang</span><span class="p">,</span> <span class="n">theta_vec_len</span> <span class="o">=</span> <span class="n">spike_phase_score</span><span class="p">(</span><span class="n">theta_bins</span><span class="p">,</span> <span class="n">theta_kde</span><span class="p">)</span>
<span class="n">theta_kde_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">theta_kde</span><span class="p">})</span>
<span class="n">theta_bins_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">theta_bins</span><span class="p">})</span>
<span class="c1"># stim</span>
<span class="n">stim_freq</span> <span class="o">=</span> <span class="n">compute_stim_freq</span><span class="p">(</span><span class="n">action_id</span><span class="p">)</span>
@ -13494,8 +13742,19 @@ div#notebook {
<span class="n">stim_energy</span> <span class="o">=</span> <span class="n">compute_energy</span><span class="p">(</span><span class="n">p_xy</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="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">stim_freq</span><span class="p">):</span>
<span class="n">stim_spike_phase</span><span class="p">,</span> <span class="n">stim_bins</span><span class="p">,</span> <span class="n">stim_kde</span><span class="p">,</span> <span class="n">stim_ang</span><span class="p">,</span> <span class="n">stim_vec_len</span> <span class="o">=</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">5</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">stim_spike_phase</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">compute_spike_phase</span><span class="p">(</span><span class="n">cleaned_lfp</span><span class="p">,</span> <span class="n">sptr</span><span class="p">,</span> <span class="n">flim</span><span class="o">=</span><span class="p">[</span><span class="n">stim_freq</span> <span class="o">-</span> <span class="o">.</span><span class="mi">5</span><span class="p">,</span> <span class="n">stim_freq</span> <span class="o">+</span> <span class="o">.</span><span class="mi">5</span><span class="p">])</span>
<span class="n">stim_bins</span><span class="p">,</span> <span class="n">stim_kde</span> <span class="o">=</span> <span class="n">vonmises_kde</span><span class="p">(</span><span class="n">stim_spike_phase</span><span class="p">)</span>
<span class="n">stim_ang</span><span class="p">,</span> <span class="n">stim_vec_len</span> <span class="o">=</span> <span class="n">spike_phase_score</span><span class="p">(</span><span class="n">stim_bins</span><span class="p">,</span> <span class="n">stim_kde</span><span class="p">)</span>
<span class="n">stim_kde_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">stim_kde</span><span class="p">})</span>
<span class="n">stim_bins_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">stim_bins</span><span class="p">})</span>
<span class="n">coherence_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_xy</span><span class="p">})</span>
<span class="n">freqency_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="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>
@ -13504,12 +13763,16 @@ div#notebook {
<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;theta_ang&#39;</span><span class="p">:</span> <span class="n">theta_ang</span><span class="p">,</span>
<span class="s1">&#39;theta_vec_len&#39;</span><span class="p">:</span> <span class="n">theta_vec_len</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_energy&#39;</span><span class="p">:</span> <span class="n">stim_energy</span><span class="p">,</span>
<span class="s1">&#39;stim_ang&#39;</span><span class="p">:</span> <span class="n">stim_ang</span><span class="p">,</span>
<span class="s1">&#39;stim_vec_len&#39;</span><span class="p">:</span> <span class="n">stim_vec_len</span>
<span class="p">})</span>
<span class="k">return</span> <span class="n">result</span>
</pre></div>
@ -13521,7 +13784,7 @@ 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;[24]:</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">units</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span>
@ -13547,13 +13810,13 @@ div#notebook {
<div id="fdf01411-110c-4f09-8a74-86966ed09bad"></div>
<div id="a0803bf8-bad9-4783-b06a-316f09ff8583"></div>
<div class="output_subarea output_widget_view ">
<script type="text/javascript">
var element = $('#fdf01411-110c-4f09-8a74-86966ed09bad');
var element = $('#a0803bf8-bad9-4783-b06a-316f09ff8583');
</script>
<script type="application/vnd.jupyter.widget-view+json">
{"model_id": "564d1ef5339f46eebee42ec41cfcfe62", "version_major": 2, "version_minor": 0}
{"model_id": "199c002aa9604451a318e3f471c9d892", "version_major": 2, "version_minor": 0}
</script>
</div>
@ -13565,8 +13828,9 @@ var element = $('#fdf01411-110c-4f09-8a74-86966ed09bad');
<div class="output_subarea output_stream output_stderr output_text">
<pre>/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/scipy/signal/spectral.py:1578: RuntimeWarning: invalid value encountered in true_divide
<pre>/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/scipy/signal/spectral.py:1577: RuntimeWarning: invalid value encountered in true_divide
Cxy = np.abs(Pxy)**2 / Pxx / Pyy
/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:28: RuntimeWarning: invalid value encountered in true_divide
</pre>
</div>
</div>
@ -13585,97 +13849,54 @@ var element = $('#fdf01411-110c-4f09-8a74-86966ed09bad');
</div>