stim lfp response

This commit is contained in:
Mikkel Elle Lepperød 2019-10-17 19:50:46 +02:00
parent a11ec2ee87
commit e66e5e8810
25 changed files with 14748 additions and 28 deletions

View File

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

View File

@ -13171,7 +13171,7 @@ div#notebook {
<div class="output_subarea output_stream output_stderr output_text">
<pre>15:15:05 [I] klustakwik KlustaKwik2 version 0.2.6
<pre>10:26:07 [I] klustakwik KlustaKwik2 version 0.2.6
</pre>
</div>
</div>
@ -13201,7 +13201,7 @@ div#notebook {
<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;figures&#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>
<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>
@ -13255,7 +13255,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</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="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 +13286,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</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">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 +13313,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</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">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>
@ -13331,7 +13331,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</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">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>
@ -13350,7 +13350,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</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">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>
@ -13389,7 +13389,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</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_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>
@ -13405,7 +13405,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<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>
@ -13419,7 +13419,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<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>
@ -13486,7 +13486,7 @@ div#notebook {
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<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="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>
@ -13512,18 +13512,138 @@ div#notebook {
<div id="fcbe1331-76c0-47d8-a4f1-70988217251b"></div>
<div id="ccdb069e-4b63-4db1-9535-31624e57bc0e"></div>
<div class="output_subarea output_widget_view ">
<script type="text/javascript">
var element = $('#fcbe1331-76c0-47d8-a4f1-70988217251b');
var element = $('#ccdb069e-4b63-4db1-9535-31624e57bc0e');
</script>
<script type="application/vnd.jupyter.widget-view+json">
{"model_id": "81d917d31c0e43cf80ec3398d46b2c01", "version_major": 2, "version_minor": 0}
{"model_id": "e733903bb8d7406ea6c05bf5af093bfc", "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;[17]:</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>
</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>

View File

@ -19,7 +19,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"15:15:05 [I] klustakwik KlustaKwik2 version 0.2.6\n"
"10:26:07 [I] klustakwik KlustaKwik2 version 0.2.6\n"
]
}
],
@ -67,7 +67,7 @@
"outputs": [],
"source": [
"output = pathlib.Path('output/stimulus-lfp-response')\n",
"(output / 'figures').mkdir(parents=True, exist_ok=True)"
"(output / 'data').mkdir(parents=True, exist_ok=True)"
]
},
{
@ -104,7 +104,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
@ -131,7 +131,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@ -154,7 +154,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
@ -168,7 +168,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
@ -183,7 +183,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
@ -218,7 +218,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
@ -230,7 +230,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
@ -240,7 +240,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
@ -303,13 +303,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "81d917d31c0e43cf80ec3398d46b2c01",
"model_id": "e733903bb8d7406ea6c05bf5af093bfc",
"version_major": 2,
"version_minor": 0
},
@ -319,6 +319,13 @@
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
@ -327,6 +334,77 @@
" left_index=True, right_index=True)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"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"
]
}
],
"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')"
]
},
{
"cell_type": "markdown",
"metadata": {},

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

BIN
actions/stimulus-lfp-response/data/figures/lfp-psd.svg (Stored with Git LFS) Normal file

Binary file not shown.