{
"cells": [
{
"cell_type": "markdown",
"id": "6f6aa857",
"metadata": {},
"source": [
"# Exercise: For each patcher, compute the average number of days they waited between experiments\n",
"\n",
"Here is how to proceed\n",
"1. Use a window function to compute the number of days that elapse between experiment (i.e., the distance between `date`), for each `patcher`. Add that as a new column, `'days from prev'`\n",
"2. Compute the average `'days from prev'` per patcher\n",
"\n",
"With your new awesome vectorization skills, it should take two lines!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8f9bc8b1",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"# Set some Pandas options: maximum number of rows/columns it's going to display\n",
"pd.set_option('display.max_rows', 1000)\n",
"pd.set_option('display.max_columns', 100)"
]
},
{
"cell_type": "markdown",
"id": "1be11d54",
"metadata": {},
"source": [
"# Load the neural data"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "8dfc3020",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('shuffled_QC_passed_2024-07-04_collected_v1.csv', parse_dates=['date'])"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "0430951e",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" OP | \n",
" patcher | \n",
" date | \n",
" slice | \n",
" cell_ch | \n",
" cell_ID | \n",
" day | \n",
" treatment | \n",
" hrs_incubation | \n",
" repatch | \n",
" hrs_after_OP | \n",
" Rs | \n",
" Rin | \n",
" resting_potential | \n",
" max_spikes | \n",
" Rheobase | \n",
" AP_heigth | \n",
" TH | \n",
" max_depol | \n",
" max_repol | \n",
" membra_time_constant_tau | \n",
" capacitance | \n",
" comments | \n",
" rheo_ramp | \n",
" AP_halfwidth | \n",
" Rheobse_ramp | \n",
" Unnamed: 27 | \n",
" rheos_ramp | \n",
" comment | \n",
" | \n",
" high K concentration | \n",
" RMP_from_char | \n",
" tissue_source | \n",
" area | \n",
" patient_age | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" OP211209 | \n",
" Verji | \n",
" 2024-03-13 | \n",
" S2 | \n",
" 8 | \n",
" 21d10S2c8 | \n",
" D1 | \n",
" Ctrl | \n",
" 0.0 | \n",
" no | \n",
" 13.298889 | \n",
" 14.470281 | \n",
" 166.878916 | \n",
" -67.962646 | \n",
" 34 | \n",
" 50.0 | \n",
" 83.190918 | \n",
" -36.132812 | \n",
" 302.124023 | \n",
" -72.631836 | \n",
" 20.75 | \n",
" 152.623120 | \n",
" NaN | \n",
" NaN | \n",
" 0.966102 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" 15 mM | \n",
" -59.101382 | \n",
" Bielefeld | \n",
" temporal | \n",
" 27.0 | \n",
"
\n",
" \n",
" 1 | \n",
" OP221024 | \n",
" Verji | \n",
" 2024-06-16 | \n",
" S3 | \n",
" 4 | \n",
" 22o24S3c4 | \n",
" D1 | \n",
" Ctrl | \n",
" 0.0 | \n",
" no | \n",
" 23.964167 | \n",
" 11.521243 | \n",
" 137.820797 | \n",
" -71.789551 | \n",
" 41 | \n",
" 50.0 | \n",
" 93.322754 | \n",
" -42.968750 | \n",
" 465.820312 | \n",
" -83.740234 | \n",
" 14.85 | \n",
" 124.324170 | \n",
" 17 | \n",
" NaN | \n",
" 0.959995 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" 8 mM | \n",
" -62.265689 | \n",
" Bielefeld | \n",
" temporal | \n",
" 42.0 | \n",
"
\n",
" \n",
" 2 | \n",
" OP230810 | \n",
" Verji | \n",
" 2024-05-14 | \n",
" S2 | \n",
" 5 | \n",
" 23810S2c5 | \n",
" D1 | \n",
" TTX | \n",
" 0.0 | \n",
" no | \n",
" 7.043056 | \n",
" 10.120637 | \n",
" 67.739416 | \n",
" -70.629883 | \n",
" 47 | \n",
" 100.0 | \n",
" 91.973877 | \n",
" -37.817383 | \n",
" 415.771484 | \n",
" -107.666016 | \n",
" 13.00 | \n",
" 228.654858 | \n",
" 10 | \n",
" 402.013400 | \n",
" 0.760052 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" 8 mM | \n",
" -61.329228 | \n",
" Mitte | \n",
" temporal | \n",
" 63.0 | \n",
"
\n",
" \n",
" 3 | \n",
" OP230209 | \n",
" Verji | \n",
" 2024-04-27 | \n",
" S2_D2 | \n",
" 3 | \n",
" 23209S2_D2c3 | \n",
" D2 | \n",
" high K | \n",
" 25.0 | \n",
" no | \n",
" 21.848333 | \n",
" 7.745503 | \n",
" 43.009610 | \n",
" -68.371582 | \n",
" 31 | \n",
" 500.0 | \n",
" 67.163086 | \n",
" -29.284668 | \n",
" 212.036133 | \n",
" -61.645508 | \n",
" 11.05 | \n",
" 215.784505 | \n",
" 30 | \n",
" 672.202407 | \n",
" 0.958735 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" 8 mM | \n",
" -62.577472 | \n",
" Bielefeld | \n",
" temporal | \n",
" 63.0 | \n",
"
\n",
" \n",
" 4 | \n",
" OP240321 | \n",
" Verji | \n",
" 2024-04-11 | \n",
" S2 | \n",
" 4 | \n",
" 24321S2c4 | \n",
" D1 | \n",
" Ctrl | \n",
" 0.0 | \n",
" no | \n",
" 11.530278 | \n",
" 7.632941 | \n",
" 32.884808 | \n",
" -52.453613 | \n",
" 21 | \n",
" 200.0 | \n",
" 84.008789 | \n",
" -36.785889 | \n",
" 403.442383 | \n",
" -71.899414 | \n",
" 14.80 | \n",
" 695.791105 | \n",
" 8 | \n",
" NaN | \n",
" 1.063838 | \n",
" 324.520817 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" 8 mM | \n",
" -63.149769 | \n",
" Bielefeld | \n",
" temporal | \n",
" 31.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" OP patcher date slice cell_ch cell_ID day treatment \\\n",
"0 OP211209 Verji 2024-03-13 S2 8 21d10S2c8 D1 Ctrl \n",
"1 OP221024 Verji 2024-06-16 S3 4 22o24S3c4 D1 Ctrl \n",
"2 OP230810 Verji 2024-05-14 S2 5 23810S2c5 D1 TTX \n",
"3 OP230209 Verji 2024-04-27 S2_D2 3 23209S2_D2c3 D2 high K \n",
"4 OP240321 Verji 2024-04-11 S2 4 24321S2c4 D1 Ctrl \n",
"\n",
" hrs_incubation repatch hrs_after_OP Rs Rin \\\n",
"0 0.0 no 13.298889 14.470281 166.878916 \n",
"1 0.0 no 23.964167 11.521243 137.820797 \n",
"2 0.0 no 7.043056 10.120637 67.739416 \n",
"3 25.0 no 21.848333 7.745503 43.009610 \n",
"4 0.0 no 11.530278 7.632941 32.884808 \n",
"\n",
" resting_potential max_spikes Rheobase AP_heigth TH max_depol \\\n",
"0 -67.962646 34 50.0 83.190918 -36.132812 302.124023 \n",
"1 -71.789551 41 50.0 93.322754 -42.968750 465.820312 \n",
"2 -70.629883 47 100.0 91.973877 -37.817383 415.771484 \n",
"3 -68.371582 31 500.0 67.163086 -29.284668 212.036133 \n",
"4 -52.453613 21 200.0 84.008789 -36.785889 403.442383 \n",
"\n",
" max_repol membra_time_constant_tau capacitance comments rheo_ramp \\\n",
"0 -72.631836 20.75 152.623120 NaN NaN \n",
"1 -83.740234 14.85 124.324170 17 NaN \n",
"2 -107.666016 13.00 228.654858 10 402.013400 \n",
"3 -61.645508 11.05 215.784505 30 672.202407 \n",
"4 -71.899414 14.80 695.791105 8 NaN \n",
"\n",
" AP_halfwidth Rheobse_ramp Unnamed: 27 rheos_ramp comment \\\n",
"0 0.966102 NaN NaN NaN NaN NaN \n",
"1 0.959995 NaN NaN NaN NaN NaN \n",
"2 0.760052 NaN NaN NaN NaN NaN \n",
"3 0.958735 NaN NaN NaN NaN NaN \n",
"4 1.063838 324.520817 NaN NaN NaN NaN \n",
"\n",
" high K concentration RMP_from_char tissue_source area patient_age \n",
"0 15 mM -59.101382 Bielefeld temporal 27.0 \n",
"1 8 mM -62.265689 Bielefeld temporal 42.0 \n",
"2 8 mM -61.329228 Mitte temporal 63.0 \n",
"3 8 mM -62.577472 Bielefeld temporal 63.0 \n",
"4 8 mM -63.149769 Bielefeld temporal 31.0 "
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "685a6b77",
"metadata": {},
"outputs": [],
"source": [
"df['days from prev'] = df['date'] - df.sort_values('date').groupby('patcher')['date'].shift()"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "75a262c6",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" patcher | \n",
" date | \n",
" days from prev | \n",
"
\n",
" \n",
" \n",
" \n",
" 251 | \n",
" Rosie | \n",
" 2024-01-01 | \n",
" NaT | \n",
"
\n",
" \n",
" 102 | \n",
" Rosie | \n",
" 2024-01-04 | \n",
" 3 days | \n",
"
\n",
" \n",
" 355 | \n",
" Rosie | \n",
" 2024-01-04 | \n",
" 0 days | \n",
"
\n",
" \n",
" 47 | \n",
" Rosie | \n",
" 2024-01-05 | \n",
" 1 days | \n",
"
\n",
" \n",
" 477 | \n",
" Rosie | \n",
" 2024-01-05 | \n",
" 0 days | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" patcher date days from prev\n",
"251 Rosie 2024-01-01 NaT\n",
"102 Rosie 2024-01-04 3 days\n",
"355 Rosie 2024-01-04 0 days\n",
"47 Rosie 2024-01-05 1 days\n",
"477 Rosie 2024-01-05 0 days"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.sort_values(['patcher', 'date'])[['patcher', 'date', 'days from prev']].head()"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "dc9a1853",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"patcher\n",
"Rosie 1 days 05:45:15.789473684\n",
"Verji 0 days 10:53:42.269807280\n",
"Name: days from prev, dtype: timedelta64[ns]"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.groupby('patcher')['days from prev'].mean()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de6ee1a8",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "61a39610",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "d63a56b1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}