septum-mec/actions/stimulus-response/data/10-calculate-stimulus-respo...

384 lines
20 KiB
Plaintext
Raw Permalink Normal View History

2019-10-10 09:44:14 +00:00
{
"cells": [
{
"cell_type": "code",
2019-12-13 10:43:57 +00:00
"execution_count": 1,
2019-10-10 09:44:14 +00:00
"metadata": {},
2019-12-13 10:43:57 +00:00
"outputs": [],
2019-10-10 09:44:14 +00:00
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
2019-12-13 10:43:57 +00:00
"execution_count": 2,
2019-10-10 09:44:14 +00:00
"metadata": {},
2019-12-13 10:43:57 +00:00
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"07:55:15 [I] klustakwik KlustaKwik2 version 0.2.6\n",
"/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:20: TqdmDeprecationWarning: This function will be removed in tqdm==5.0.0\n",
"Please use `tqdm.notebook.*` instead of `tqdm._tqdm_notebook.*`\n"
]
}
],
2019-10-10 09:44:14 +00:00
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"import spatial_maps as sp\n",
"import septum_mec.analysis.data_processing as dp\n",
"import septum_mec.analysis.registration\n",
"import expipe\n",
"import os\n",
"import pathlib\n",
"import numpy as np\n",
"import exdir\n",
"import pandas as pd\n",
"import optogenetics as og\n",
"import quantities as pq\n",
"import shutil\n",
"from distutils.dir_util import copy_tree\n",
"\n",
"from septum_mec.analysis.stimulus_response import stimulus_response_latency, compute_response\n",
"\n",
"from tqdm import tqdm_notebook as tqdm\n",
"from tqdm._tqdm_notebook import tqdm_notebook\n",
"tqdm_notebook.pandas()"
]
},
{
"cell_type": "code",
2019-12-13 10:43:57 +00:00
"execution_count": 3,
2019-10-10 09:44:14 +00:00
"metadata": {},
"outputs": [],
"source": [
"std_gaussian_kde = 0.04\n",
"window_size = 0.03"
]
},
{
"cell_type": "code",
2019-12-13 10:43:57 +00:00
"execution_count": 4,
2019-10-10 09:44:14 +00:00
"metadata": {},
"outputs": [],
"source": [
"data_loader = dp.Data()\n",
"actions = data_loader.actions\n",
"project = data_loader.project"
]
},
{
"cell_type": "code",
2019-12-13 10:43:57 +00:00
"execution_count": 5,
2019-10-10 09:44:14 +00:00
"metadata": {},
"outputs": [],
"source": [
"output = pathlib.Path('output/stimulus-response')\n",
2019-10-17 17:41:18 +00:00
"(output / 'data').mkdir(parents=True, exist_ok=True)"
2019-10-10 09:44:14 +00:00
]
},
{
"cell_type": "code",
2019-12-13 10:43:57 +00:00
"execution_count": 6,
2019-10-10 09:44:14 +00:00
"metadata": {},
"outputs": [],
"source": [
"identify_neurons = actions['identify-neurons']\n",
"units = pd.read_csv(identify_neurons.data_path('units'))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"def process(row):\n",
" \n",
" action_id = row['action']\n",
" channel_id = int(row['channel_group'])\n",
" unit_id = int(row['unit_name']) \n",
" \n",
" spike_times = data_loader.spike_train(action_id, channel_id, unit_id)\n",
" \n",
" spike_times = np.array(spike_times)\n",
" \n",
" stim_times = data_loader.stim_times(action_id)\n",
" \n",
" nan_series = pd.Series({\n",
" 't_e_peak': np.nan,\n",
" 'p_e_peak': np.nan,\n",
" 't_i_peak': np.nan,\n",
" 'p_i_peak': np.nan\n",
" })\n",
" \n",
" if stim_times is None:\n",
" return nan_series\n",
" \n",
" stim_times = np.array(stim_times)\n",
" \n",
" times, spikes, kernel, p_e, p_i = stimulus_response_latency(\n",
" spike_times, stim_times, window_size, std_gaussian_kde)\n",
" \n",
" # if no spikes detected after stimulus nan is returned\n",
" if all(np.isnan([p_e, p_i])):\n",
" return nan_series\n",
" \n",
" t_e_peak, p_e_peak, t_i_peak, p_i_peak = compute_response(\n",
" spike_times, stim_times, times, kernel, p_e, p_i)\n",
"\n",
" return pd.Series({\n",
" 't_e_peak': t_e_peak,\n",
" 'p_e_peak': p_e_peak,\n",
" 't_i_peak': t_i_peak,\n",
" 'p_i_peak': p_i_peak\n",
" })\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
2019-10-17 17:41:18 +00:00
"execution_count": 8,
2019-10-10 09:44:14 +00:00
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "476c31da67274b2396ed3f2ec54a8344",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=1298), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/mikkel/apps/expipe-project/septum-mec/septum_mec/analysis/stimulus_response.py:33: RuntimeWarning: invalid value encountered in less\n",
" if any(times[idxs_i] < te_peak):\n"
]
2019-10-17 17:41:18 +00:00
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
2019-10-10 09:44:14 +00:00
}
],
"source": [
"results = units.merge(\n",
" units.progress_apply(process, axis=1), \n",
" left_index=True, right_index=True)"
]
},
{
"cell_type": "code",
2019-10-17 17:41:18 +00:00
"execution_count": 9,
2019-10-10 09:44:14 +00:00
"metadata": {
"scrolled": false
},
2019-10-17 17:41:18 +00:00
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXgAAAEFCAYAAADpIfy5AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAHZlJREFUeJzt3XuUXGWZ7/Fvd5JOgwaUMQMSwcjtZ89REFCDB2PiGiBcjgNHPcIgeDuIc8yoLG9kHJB4QUUDDk4QHDBGGVl6hOCoTCRzUDgxojkqqMH2YUABZ1wuCSMQZdpOQp8/3t2k0nTddlftqtr1+6yVlapdtXc9u/rdT7/97vcyMDExgZmZlc9gpwMwM7P2cII3MyspJ3gzs5JygjczKykneDOzknKCNzMrKSf4PiNpraR3dzoO6y+S7pT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+5IgGbTcII3a8zkAvRnS9oHuBP4IHBZRFza2dDMpucEb9aYagvQS9KppFr8eRGxrUPxNczrJvQPJ3izBlRZgH4ucE1E/FDS3wIXAe+udZxqC81DcesJ5103YXR0tGcWPu+FOIuI0QnerEHTLED/tIiYXFz+RuDv6x2jHQvNF2VkZKRnFj7vhTiLiNG9aMwaUGUB+pslvTh7/OfADzsSnFkVrsGbNWa6BejfCXxS0nbgN8C5nQrObDpO8GYNqLEA/bFFx2LWKDfRmJmVlBO8mVlJOcGbmZWUE7yZWUk5wZtZXWPbdwLND8aa3M86w71ozKyuPKNfIY2Atc7p+xp8nhqGayVm1gv6vgafd14OM6vPE5t1Vt8neDNrH1egOqvvm2jMzMrKCd7
"text/plain": [
"<Figure size 432x288 with 4 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
2019-10-10 09:44:14 +00:00
"source": [
"results.loc[:, ['t_e_peak', 't_i_peak', 'p_e_peak', 'p_i_peak']].hist()\n",
"plt.gcf().savefig(output / 'figures' / 'summary_histogram.png')"
]
},
2019-10-17 17:41:18 +00:00
{
"cell_type": "code",
2019-12-13 10:43:57 +00:00
"execution_count": 8,
2019-10-17 17:41:18 +00:00
"metadata": {},
"outputs": [
2019-12-13 10:43:57 +00:00
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:2: TqdmDeprecationWarning: This function will be removed in tqdm==5.0.0\n",
"Please use `tqdm.notebook.tqdm` instead of `tqdm.tqdm_notebook`\n",
" \n"
]
},
2019-10-17 17:41:18 +00:00
{
"data": {
"application/vnd.jupyter.widget-view+json": {
2019-12-13 10:43:57 +00:00
"model_id": "2828a0134cc2449ba15a30119991324d",
2019-10-17 17:41:18 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
2019-12-13 10:43:57 +00:00
"HBox(children=(IntProgress(value=0, max=1284), HTML(value='')))"
2019-10-17 17:41:18 +00:00
]
},
"metadata": {},
"output_type": "display_data"
2019-12-13 10:43:57 +00:00
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
2019-10-17 17:41:18 +00:00
}
],
"source": [
"psth, time = {}, {}\n",
"for i, row in tqdm(units.iterrows(), total=len(units)):\n",
" action_id = row['action']\n",
" channel_group = row['channel_group']\n",
" unit_name = row['unit_name']\n",
" name = f'{action_id}_{channel_group}_{unit_name}'\n",
" spike_times = data_loader.spike_train(action_id, channel_group, unit_name)\n",
" \n",
" spike_times = np.array(spike_times)\n",
" \n",
" stim_times = data_loader.stim_times(action_id)\n",
" \n",
" if stim_times is None:\n",
" continue\n",
" \n",
" stim_times = np.array(stim_times)\n",
" \n",
2019-12-13 10:43:57 +00:00
" _, spikes, kernel, p_e, p_i = stimulus_response_latency(\n",
2019-10-17 17:41:18 +00:00
" spike_times, stim_times, window_size, std_gaussian_kde)\n",
" \n",
" if all(np.isnan([p_e, p_i])):\n",
" continue\n",
2019-12-13 10:43:57 +00:00
" times = np.arange(-0.005, window_size, 1e-4)\n",
2019-10-17 17:41:18 +00:00
" psth.update({name: kernel(times)})\n",
" time.update({name: times})"
]
},
{
"cell_type": "code",
2019-12-13 10:43:57 +00:00
"execution_count": 9,
2019-10-17 17:41:18 +00:00
"metadata": {},
"outputs": [],
"source": [
"pd.DataFrame(psth).to_feather(output / 'data' / 'psth.feather')\n",
"pd.DataFrame(time).to_feather(output / 'data' / 'times.feather')"
]
},
2019-10-10 09:44:14 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Save to expipe"
]
},
{
"cell_type": "code",
"execution_count": 27,
2019-10-10 09:44:14 +00:00
"metadata": {},
"outputs": [],
"source": [
"action = project.require_action(\"stimulus-response\")"
]
},
{
"cell_type": "code",
2019-10-17 17:41:18 +00:00
"execution_count": 11,
2019-10-10 09:44:14 +00:00
"metadata": {},
"outputs": [],
"source": [
"action.modules['parameters'] = {\n",
" 'window_size': window_size,\n",
" 'std_gaussian_kde': std_gaussian_kde\n",
"}"
]
},
{
"cell_type": "code",
2019-10-17 17:41:18 +00:00
"execution_count": 12,
2019-10-10 09:44:14 +00:00
"metadata": {},
"outputs": [],
"source": [
"action.data['results'] = 'results.csv'\n",
"results.to_csv(action.data_path('results'), index=False)"
]
},
{
"cell_type": "code",
"execution_count": 28,
2019-10-10 09:44:14 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['/media/storage/expipe/septum-mec/actions/stimulus-response/data/data/times.feather',\n",
" '/media/storage/expipe/septum-mec/actions/stimulus-response/data/data/psth.feather',\n",
" '/media/storage/expipe/septum-mec/actions/stimulus-response/data/figures/summary_histogram.png']"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
2019-10-10 09:44:14 +00:00
"source": [
"copy_tree(output, str(action.data_path()))"
]
},
{
"cell_type": "code",
"execution_count": 29,
2019-10-10 09:44:14 +00:00
"metadata": {},
"outputs": [],
"source": [
"septum_mec.analysis.registration.store_notebook(action, \"10-calculate-stimulus-response.ipynb\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}