septum-mec/actions/longitudinal-comparisons/data/10_longitudinal_comparisons...

1085 lines
46 KiB
Plaintext
Raw Normal View History

2021-03-10 12:33:42 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/mikkel/.virtualenvs/expipe/lib/python3.6/site-packages/ipykernel_launcher.py:26: TqdmDeprecationWarning: This function will be removed in tqdm==5.0.0\n",
"Please use `tqdm.notebook.*` instead of `tqdm._tqdm_notebook.*`\n"
]
}
],
"source": [
"import os\n",
"import pathlib\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib import colors\n",
"import seaborn as sns\n",
"import re\n",
"import shutil\n",
"import pandas as pd\n",
"import scipy.stats\n",
"import scipy\n",
"from functools import reduce\n",
"import exdir\n",
"import expipe\n",
"from distutils.dir_util import copy_tree\n",
"import septum_mec\n",
"import spatial_maps as sp\n",
"import head_direction.head as head\n",
"import septum_mec.analysis.data_processing as dp\n",
"import septum_mec.analysis.registration\n",
"from septum_mec.analysis.plotting import despine, savefig, plot_bootstrap_timeseries\n",
"from spatial_maps.fields import find_peaks, calculate_field_centers, separate_fields_by_laplace\n",
"from spike_statistics.core import permutation_resampling_test, block_bootstrap\n",
"import speed_cells.speed as spd\n",
"from tqdm import tqdm_notebook as tqdm\n",
"from tqdm._tqdm_notebook import tqdm_notebook\n",
"tqdm_notebook.pandas()\n",
"\n",
"from septum_mec.analysis.statistics import load_data_frames, make_paired_tables, make_statistics_table"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"project_path = dp.project_path()\n",
"project = expipe.get_project(project_path)\n",
"actions = project.actions\n",
"\n",
"output_path = pathlib.Path(\"output\") / \"longitudinal-comparisons\"\n",
"(output_path / \"statistics\").mkdir(exist_ok=True, parents=True)\n",
"(output_path / \"figures\").mkdir(exist_ok=True, parents=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Load cell statistics and shuffling quantiles"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of sessions above threshold 194\n",
"Number of animals 4\n",
"Number of individual gridcells 139\n",
"Number of gridcell recordings 230\n"
]
}
],
"source": [
"data, labels, colors, queries = load_data_frames()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Analysis"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"max_speed = .5 # m/s only used for speed score\n",
"min_speed = 0.02 # m/s only used for speed score\n",
"position_sampling_rate = 100 # for interpolation\n",
"position_low_pass_frequency = 6 # for low pass filtering of position\n",
"\n",
"box_size = [1.0, 1.0]\n",
"bin_size = 0.02\n",
"smoothing_low = 0.03\n",
"smoothing_high = 0.06\n",
"\n",
"speed_binsize = 0.02\n",
"\n",
"stim_mask = True\n",
"# baseline_duration = 600\n",
"baseline_duration = None"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"data_loader = dp.Data(\n",
" position_sampling_rate=position_sampling_rate, \n",
" position_low_pass_frequency=position_low_pass_frequency,\n",
" box_size=box_size, bin_size=bin_size, \n",
" stim_mask=stim_mask, baseline_duration=baseline_duration\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def fftcorrelate2d(arr1, arr2, normalize=False, **kwargs):\n",
" from copy import copy\n",
" arr1 = copy(arr1)\n",
" arr2 = copy(arr2)\n",
" from astropy.convolution import convolve_fft\n",
" if normalize:\n",
" # https://stackoverflow.com/questions/53436231/normalized-cross-correlation-in-python\n",
" a_ = arr1.ravel()\n",
" v_ = arr2.ravel()\n",
" arr1 = (arr1 - np.mean(a_)) / (np.std(a_) * len(a_))\n",
" arr2 = (arr2 - np.mean(v_)) / np.std(v_)\n",
" corr = convolve_fft(arr1, np.fliplr(np.flipud(arr2)), normalize_kernel=False, **kwargs)\n",
" return corr\n",
"\n",
"\n",
"def cross_correlation_distance(r1, r2):\n",
" r12 = fftcorrelate2d(r1, r2)\n",
" labels = separate_fields_by_laplace(r12, threshold=0)\n",
" peaks = calculate_field_centers(r12, labels)\n",
" centered_peaks = peaks - np.array(r1.shape) / 2\n",
" offset = np.linalg.norm(centered_peaks, axis=1)\n",
" distance_idx = np.argmin(offset)\n",
" distance = offset[distance_idx]\n",
" angle = np.arctan2(*centered_peaks[distance_idx])\n",
" \n",
" return distance, angle\n",
"\n",
"\n",
"def cross_correlation_centre_of_mass(r1, r2):\n",
" from scipy import ndimage\n",
" r12 = fftcorrelate2d(r1, r2)\n",
" cntr = ndimage.center_of_mass(r12)\n",
" \n",
" centered_cntr = cntr - np.array(r1.shape) / 2\n",
" distance = np.linalg.norm(centered_cntr)\n",
" angle = np.arctan2(*centered_cntr)\n",
" \n",
" return distance, angle"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"colors = ['#1b9e77','#d95f02','#7570b3','#e7298a']\n",
"labels = [\n",
" 'Baseline Ia', \n",
" 'Baseline Ib', \n",
" 'Baseline I', \n",
" '11 Hz', \n",
" 'Baseline IIa',\n",
" 'Baseline IIb',\n",
" 'Baseline II', \n",
" '30 Hz'\n",
"]\n",
"queries = [\n",
" 'baseline and i and Hz11',\n",
" 'baseline and i and Hz11',\n",
" 'baseline and i and Hz11',\n",
" 'frequency==11 and stim_location==\"ms\"', \n",
" 'baseline and ii and Hz30',\n",
" 'baseline and ii and Hz30',\n",
" 'baseline and ii and Hz30',\n",
" 'frequency==30 and stim_location==\"ms\"']"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"keys = [\n",
" 'gridness',\n",
" 'speed_score',\n",
" 'max_rate',\n",
" 'average_rate',\n",
" 'action',\n",
" 'unit_name'\n",
"]\n",
"\n",
"cell_types = [\n",
" 'gridcell',\n",
" 'ns_inhibited', \n",
" 'ns_not_inhibited',\n",
" 'bs',\n",
" 'bs_not_gridcell'\n",
"]\n",
"\n",
"results, labels = make_paired_tables(data, keys, cell_types=cell_types)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"results['gridcell']['xcorr_cntr_mass'] = results['gridcell']['action'].copy()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>unit_idnum</th>\n",
" <th>channel_group</th>\n",
" <th>date</th>\n",
" <th>Baseline I</th>\n",
" <th>11 Hz</th>\n",
" <th>Baseline II</th>\n",
" <th>30 Hz</th>\n",
" </tr>\n",
" <tr>\n",
" <th>entity</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1833</th>\n",
" <td>94</td>\n",
" <td>94</td>\n",
" <td>94</td>\n",
" <td>43</td>\n",
" <td>38</td>\n",
" <td>36</td>\n",
" <td>24</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1834</th>\n",
" <td>15</td>\n",
" <td>15</td>\n",
" <td>15</td>\n",
" <td>8</td>\n",
" <td>8</td>\n",
" <td>4</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1839</th>\n",
" <td>20</td>\n",
" <td>20</td>\n",
" <td>20</td>\n",
" <td>9</td>\n",
" <td>6</td>\n",
" <td>5</td>\n",
" <td>6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1849</th>\n",
" <td>8</td>\n",
" <td>8</td>\n",
" <td>8</td>\n",
" <td>3</td>\n",
" <td>4</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" unit_idnum channel_group date Baseline I 11 Hz Baseline II 30 Hz\n",
"entity \n",
"1833 94 94 94 43 38 36 24\n",
"1834 15 15 15 8 8 4 3\n",
"1839 20 20 20 9 6 5 6\n",
"1849 8 8 8 3 4 1 2"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results['gridcell']['action'].groupby('entity').count()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"diff_labels = [\n",
" ['Baseline Ia', 'Baseline Ib'], \n",
" ['Baseline I', '11 Hz'], \n",
" ['Baseline IIa', 'Baseline IIb'], \n",
" ['Baseline II', '30 Hz'],\n",
" ['Baseline I', 'Baseline II']\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## compute baseline values"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"def compute_baseline(column):\n",
" total_itr = sum([results[cell_type]['action'][column].count() for cell_type in cell_types])\n",
" pbar = tqdm(total=total_itr)\n",
"\n",
" for cell_type in cell_types:\n",
"\n",
" for idx, action_id in results[cell_type]['action'].loc[:,column].dropna().items():\n",
" unit_name, channel_group = results[cell_type]['unit_name'].loc[idx, [column, 'channel_group']]\n",
" rate_map_1, rate_map_2 = data_loader.rate_map_split(\n",
" action_id, channel_group, unit_name, smoothing_low)\n",
"\n",
" spikes_1, spikes_2, t_split = data_loader.spike_train_split(\n",
" action_id, channel_group, unit_name)\n",
"\n",
" t1, v1, t2, v2 = map(data_loader.tracking_split(action_id).get, ['t1', 'v1', 't2', 'v2'])\n",
"\n",
" pbar.update()\n",
" for spikes, rate_map, speed, times, label in zip([spikes_1, spikes_2], [rate_map_1, rate_map_2], [v1, v2], [t1, t2], [column + 'a', column + 'b']):\n",
" results[cell_type]['gridness'].loc[idx, label] = sp.gridness(rate_map)\n",
"\n",
" results[cell_type]['max_rate'].loc[idx, label] = rate_map.max()\n",
"\n",
" results[cell_type]['average_rate'].loc[idx, label] = len(spikes) / t_split\n",
"\n",
" results[cell_type]['speed_score'].loc[idx, label] = spd.speed_correlation(\n",
" speed, times, spikes, return_data=False)\n",
" pbar.close()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "03628a3788ba4562bdafd2d764f2a579",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=586), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"compute_baseline('Baseline I')"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7f0cb14f1b9a4df7891695043d0ef517",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=438), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"compute_baseline('Baseline II')"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"def compute_changing_statistics(results, labels, key):\n",
" df = results[key]\n",
" if key == 'gridness':\n",
" values = df[labels[0]] - df[labels[1]]\n",
" elif key == 'speed_score':\n",
" values = df[labels[0]] - df[labels[1]]\n",
" elif key == 'max_rate':\n",
" values = (df[labels[0]] - df[labels[1]]) / df[labels[0]]\n",
" elif key == 'average_rate':\n",
" values = (df[labels[0]] - df[labels[1]]) / df[labels[0]]\n",
" elif key == 'xcorr_cntr_mass':\n",
" values = pd.Series(index=df.index)\n",
" for idx, row in results['action'].loc[:, labels].dropna().iterrows():\n",
" action_id_1, action_id_2 = row.loc[labels]\n",
" unit_name_1, unit_name_2, channel_group = results['unit_name'].loc[idx, labels + ['channel_group']]\n",
"\n",
" rate_map_1 = data_loader.rate_map(\n",
" action_id_1, channel_group, int(unit_name_1), smoothing_low)\n",
" \n",
" rate_map_2 = data_loader.rate_map(\n",
" action_id_2, channel_group, int(unit_name_2), smoothing_low)\n",
" distance, angle = cross_correlation_centre_of_mass(rate_map_1, rate_map_2)\n",
" values[idx] = distance * bin_size\n",
" else:\n",
" raise ValueError(key)\n",
" \n",
" return values"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"results_change = {}\n",
"for cell_type in tqdm(cell_types):\n",
" results_change[cell_type] = {}\n",
" for key in tqdm(keys, desc=cell_type):\n",
" if key in ['action', 'unit_name']:\n",
" continue\n",
" results_change[cell_type][key] = pd.DataFrame()\n",
" for diff_label in diff_labels:\n",
" results_change[cell_type][key][' - '.join(diff_label)] = compute_changing_statistics(results[cell_type], diff_label, key)\n",
" results_change[cell_type][key].loc[:, 'entity'] = results[cell_type][key].loc[:, 'entity']\n",
" results_change[cell_type][key].loc[:, 'date'] = results[cell_type][key].loc[:, 'date']\n",
" results_change[cell_type][key].loc[:, 'unit_idnum'] = results[cell_type][key].loc[:, 'unit_idnum']"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Baseline Ia - Baseline Ib</th>\n",
" <th>entity</th>\n",
" <th>date</th>\n",
" <th>Baseline I - 11 Hz</th>\n",
" <th>Baseline IIa - Baseline IIb</th>\n",
" <th>Baseline II - 30 Hz</th>\n",
" <th>Baseline I - Baseline II</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>51</th>\n",
" <td>0.408873</td>\n",
" <td>1833</td>\n",
" <td>20719</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>85</th>\n",
" <td>NaN</td>\n",
" <td>1833</td>\n",
" <td>20719</td>\n",
" <td>NaN</td>\n",
" <td>0.076184</td>\n",
" <td>0.722833</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>86</th>\n",
" <td>NaN</td>\n",
" <td>1833</td>\n",
" <td>20719</td>\n",
" <td>NaN</td>\n",
" <td>0.132470</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>58</th>\n",
" <td>0.284086</td>\n",
" <td>1833</td>\n",
" <td>200619</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>127</th>\n",
" <td>NaN</td>\n",
" <td>1833</td>\n",
" <td>200619</td>\n",
" <td>NaN</td>\n",
" <td>0.077828</td>\n",
" <td>0.301516</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>139</th>\n",
" <td>NaN</td>\n",
" <td>1849</td>\n",
" <td>150319</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>43</th>\n",
" <td>0.147306</td>\n",
" <td>1849</td>\n",
" <td>60319</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>65</th>\n",
" <td>0.228274</td>\n",
" <td>1849</td>\n",
" <td>280219</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>74</th>\n",
" <td>NaN</td>\n",
" <td>1849</td>\n",
" <td>280219</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>105</th>\n",
" <td>NaN</td>\n",
" <td>1849</td>\n",
" <td>280219</td>\n",
" <td>NaN</td>\n",
" <td>0.979388</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>137 rows × 7 columns</p>\n",
"</div>"
],
"text/plain": [
" Baseline Ia - Baseline Ib entity date Baseline I - 11 Hz \\\n",
"51 0.408873 1833 20719 NaN \n",
"85 NaN 1833 20719 NaN \n",
"86 NaN 1833 20719 NaN \n",
"58 0.284086 1833 200619 NaN \n",
"127 NaN 1833 200619 NaN \n",
".. ... ... ... ... \n",
"139 NaN 1849 150319 NaN \n",
"43 0.147306 1849 60319 NaN \n",
"65 0.228274 1849 280219 NaN \n",
"74 NaN 1849 280219 NaN \n",
"105 NaN 1849 280219 NaN \n",
"\n",
" Baseline IIa - Baseline IIb Baseline II - 30 Hz \\\n",
"51 NaN NaN \n",
"85 0.076184 0.722833 \n",
"86 0.132470 NaN \n",
"58 NaN NaN \n",
"127 0.077828 0.301516 \n",
".. ... ... \n",
"139 NaN NaN \n",
"43 NaN NaN \n",
"65 NaN NaN \n",
"74 NaN NaN \n",
"105 0.979388 NaN \n",
"\n",
" Baseline I - Baseline II \n",
"51 NaN \n",
"85 NaN \n",
"86 NaN \n",
"58 NaN \n",
"127 NaN \n",
".. ... \n",
"139 NaN \n",
"43 NaN \n",
"65 NaN \n",
"74 NaN \n",
"105 NaN \n",
"\n",
"[137 rows x 7 columns]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results_change['gridcell']['gridness']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# save to output folder"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"for cell_type, value_holder in results_change.items():\n",
" for key, df in value_holder.items():\n",
" path = output_path / \"data\" / \"results_change\" / cell_type\n",
" path.mkdir(exist_ok=True, parents=True)\n",
" df.to_csv(path / (key + '.csv'))\n",
"\n",
" \n",
"for cell_type, value_holder in results.items():\n",
" for key, df in value_holder.items():\n",
" path = output_path / \"data\" / \"results_unique_cells\" / cell_type\n",
" path.mkdir(exist_ok=True, parents=True)\n",
" df.to_csv(path / (key + '.csv'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotting raw data"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"plt.rc('axes', titlesize=12)\n",
"plt.rcParams.update({\n",
" 'font.size': 12, \n",
" 'figure.figsize': (6, 4), \n",
" 'figure.dpi': 150\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"def make_plot(what, axs, action_ids, unit_names, channel_group):\n",
" if what == 'rate_map':\n",
" sns.despine(left=True, bottom=True)\n",
" for ax, action_id, unit_name in zip(axs, action_ids, unit_names):\n",
" if np.isnan(unit_name):\n",
" continue\n",
" vmax = None\n",
" rate_map = data_loader.rate_map(action_id, channel_group, int(unit_name), smoothing_low)\n",
" if vmax is None:\n",
" vmax = rate_map.max()\n",
" ax.imshow(rate_map, origin='lower', vmax=vmax)\n",
" # ax.set_title(f'{row.gridness:.2f} {row.max_rate:.2f} {row.average_rate:.2f}')\n",
" ax.set_yticklabels([])\n",
" ax.set_xticklabels([])\n",
" if what == 'spike_map':\n",
" from scipy.interpolate import interp1d\n",
" for ax, action_id, unit_name in zip(axs, action_ids, unit_names):\n",
" if np.isnan(unit_name):\n",
" continue\n",
" x, y, t, speed = map(data_loader.tracking(action_id).get, ['x', 'y', 't', 'v'])\n",
" ax.plot(x, y, 'k', alpha=0.3)\n",
" spike_times = data_loader.spike_train(action_id, channel_group, unit_name)\n",
" spike_times = spike_times[(spike_times > min(t)) & (spike_times < max(t))]\n",
" x_spike = interp1d(t, x)(spike_times)\n",
" y_spike = interp1d(t, y)(spike_times)\n",
" ax.set_xticks([])\n",
" ax.set_yticks([])\n",
" ax.scatter(x_spike, y_spike, marker='.', color=(0.7, 0.2, 0.2), s=1.5)\n",
" ax.set_yticklabels([])\n",
" ax.set_xticklabels([])\n",
" ax.set_aspect(1)\n",
" if what == 'speed':\n",
" colors = ['#1b9e77','#d95f02','#7570b3', '#e7298a']\n",
" for ax, action_id, unit_name, color in zip(axs, action_ids, unit_names, colors):\n",
" if np.isnan(unit_name):\n",
" continue\n",
" x, y, t, speed = map(data_loader.tracking(action_id).get, ['x', 'y', 't', 'v'])\n",
"\n",
" spike_times = data_loader.spike_train(action_id, channel_group, unit_name)\n",
" spike_times = spike_times[(spike_times > min(t)) & (spike_times < max(t))]\n",
" \n",
" speed_score, inst_speed, rate, times = spd.speed_correlation(\n",
" speed, t, spike_times, return_data=True)\n",
"\n",
" speed_bins = np.arange(min_speed, max_speed + speed_binsize, speed_binsize)\n",
" ia = np.digitize(inst_speed, bins=speed_bins, right=True)\n",
" rates = []\n",
"\n",
" for i in range(len(speed_bins)):\n",
" rates.append(rate[ia==i])\n",
"\n",
" ax.set_title(f'{speed_score:.3f}')\n",
" plot_bootstrap_timeseries(speed_bins, rates, ax=ax, normalize_values=True, color=color)\n",
"# rr = [rr for r in rates for rr in r]\n",
"# aspect = (max_speed - min_speed) / (np.nanmax(rr) - np.nanmin(rr))\n",
" ax.set_aspect('auto')"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"pure_labels = [\n",
" 'Baseline I', \n",
" '11 Hz', \n",
" 'Baseline II', \n",
" '30 Hz'\n",
"]\n",
"\n",
"def plot(what, cell_type):\n",
"\n",
" n_neurons = len(results[cell_type]['action'])\n",
" fig, axss = plt.subplots(n_neurons, 4, sharey=True, sharex=True, figsize=(8, 2*n_neurons))\n",
"\n",
" for (idx, row), axs in zip(results[cell_type]['action'].iterrows(), axss):\n",
" action_ids = row.loc[pure_labels].values\n",
" unit_names = results[cell_type]['unit_name'].loc[idx, pure_labels].values\n",
" channel_group, unit_idnum, entity, date = row.loc[['channel_group', 'unit_idnum', 'entity', 'date']]\n",
" axs[0].set_ylabel(f'{unit_idnum} {entity}-{date}')\n",
" make_plot(what, axs, action_ids, unit_names, channel_group)\n",
" plt.tight_layout()\n",
"\n",
" # fig.savefig(output_path / 'figures' / f'neuron_{id_num}_rate_map.png', bbox_inches='tight')\n",
" # savefig(output_path / 'figures' / f'{what}-{cell_type}')\n",
" fig.savefig(output_path / 'figures' / f'{what}-{cell_type}.svg', bbox_inches='tight')"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"%%capture\n",
"plot(cell_type='gridcell', what='rate_map')\n",
"plot(cell_type='gridcell', what='speed')\n",
"plot(cell_type='ns_inhibited', what='speed')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Save to expipe"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"action = project.require_action(\"longitudinal-comparisons\")"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/gridcell/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/gridcell/action.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/gridcell/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/gridcell/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/gridcell/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/gridcell/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/gridcell/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/gridcell/unit_name.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_inhibited/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_inhibited/action.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_inhibited/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_inhibited/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_inhibited/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_inhibited/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_inhibited/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_inhibited/unit_name.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs/action.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs/unit_name.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_not_inhibited/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_not_inhibited/action.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_not_inhibited/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_not_inhibited/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_not_inhibited/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_not_inhibited/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_not_inhibited/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/ns_not_inhibited/unit_name.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs_not_gridcell/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs_not_gridcell/action.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs_not_gridcell/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs_not_gridcell/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs_not_gridcell/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs_not_gridcell/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs_not_gridcell/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_unique_cells/bs_not_gridcell/unit_name.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_speed/average_speed.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_speed/action.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/gridcell/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/gridcell/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/gridcell/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/gridcell/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/gridcell/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/gridcell/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/ns_inhibited/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/ns_inhibited/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/ns_inhibited/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/ns_inhibited/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/ns_inhibited/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/ns_inhibited/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/bs/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/bs/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/bs/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/bs/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/bs/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/bs/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/ns_not_inhibited/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/ns_not_inhibited/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/ns_not_inhibited/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/ns_not_inhibited/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/ns_not_inhibited/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/ns_not_inhibited/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/bs_not_gridcell/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/bs_not_gridcell/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/bs_not_gridcell/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/bs_not_gridcell/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/bs_not_gridcell/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_change/bs_not_gridcell/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/gridcell/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/gridcell/action.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/gridcell/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/gridcell/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/gridcell/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/gridcell/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/gridcell/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/gridcell/unit_name.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_inhibited/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_inhibited/action.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_inhibited/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_inhibited/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_inhibited/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_inhibited/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_inhibited/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_inhibited/unit_name.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs/action.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs/unit_name.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_not_inhibited/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_not_inhibited/action.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_not_inhibited/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_not_inhibited/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_not_inhibited/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_not_inhibited/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_not_inhibited/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/ns_not_inhibited/unit_name.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs_not_gridcell/gridness.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs_not_gridcell/action.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs_not_gridcell/max_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs_not_gridcell/xcorr_displacement.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs_not_gridcell/xcorr_cntr_mass.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs_not_gridcell/speed_score.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs_not_gridcell/average_rate.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/data/results_all_cells/bs_not_gridcell/unit_name.csv',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/figures/speed-gridcell.svg',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/figures/speed-ns_inhibited.svg',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/figures/rate_map-gridcell.svg',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/figures/.ipynb_checkpoints/speed-gridcell-checkpoint.svg',\n",
" '/media/storage/expipe/septum-mec/actions/longitudinal-comparisons/data/figures/.ipynb_checkpoints/rate_map-gridcell-checkpoint.svg']"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"copy_tree(output_path, str(action.data_path()))"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"septum_mec.analysis.registration.store_notebook(action, \"10_longitudinal_comparisons.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": 4
}