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

354 lines
19 KiB
Plaintext
Raw Normal View History

2019-10-10 09:44:14 +00:00
{
"cells": [
{
"cell_type": "code",
2019-10-17 17:41:18 +00:00
"execution_count": 15,
2019-10-10 09:44:14 +00:00
"metadata": {},
2019-10-17 17:41:18 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The autoreload extension is already loaded. To reload it, use:\n",
" %reload_ext autoreload\n"
]
}
],
2019-10-10 09:44:14 +00:00
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
2019-10-17 17:41:18 +00:00
"execution_count": 16,
2019-10-10 09:44:14 +00:00
"metadata": {},
2019-10-17 17:41:18 +00:00
"outputs": [],
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-10-17 17:41:18 +00:00
"execution_count": 17,
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-10-17 17:41:18 +00:00
"execution_count": 18,
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-10-17 17:41:18 +00:00
"execution_count": null,
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-10-17 17:41:18 +00:00
"execution_count": 20,
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+S7pT0tII/81ZJry7yM7vN7E4HYGblFxEv6HQM/cgJPidJS4FPAP8OHAT8J/CGiBitsc8IcDnwJ8As4FMRsabO56wFJoARYD6wAXh7RGyvdjxJg8AngWOAecAAcE5EbJpy7MuAI4B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IuI0QnerEHTLED/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",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2ee10927bbdf456c9178b9fd5448be23",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=1298), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"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",
" times, spikes, kernel, p_e, p_i = stimulus_response_latency(\n",
" spike_times, stim_times, window_size, std_gaussian_kde)\n",
" \n",
" if all(np.isnan([p_e, p_i])):\n",
" continue\n",
" \n",
" psth.update({name: kernel(times)})\n",
" time.update({name: times})"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"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": null,
"metadata": {},
"outputs": [],
"source": [
"copy_tree(output, str(action.data_path()))"
]
},
{
"cell_type": "code",
"execution_count": null,
"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
}