Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matchings/846e7633d57501b5af55f4daae22a4d9 to your computer and use it in GitHub Desktop.
Save matchings/846e7633d57501b5af55f4daae22a4d9 to your computer and use it in GitHub Desktop.
Visual Behavior 2P - adding rewards to stimulus presentations
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get the Visual Behavior dataset cache"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\marinag\\Anaconda3\\envs\\visual_behavior_sdk\\lib\\site-packages\\requests\\__init__.py:91: RequestsDependencyWarning: urllib3 (1.26.4) or chardet (3.0.4) doesn't match a supported version!\n",
" RequestsDependencyWarning)\n"
]
}
],
"source": [
"from allensdk.brain_observatory.behavior.behavior_project_cache import VisualBehaviorOphysProjectCache"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"cache_dir = r'\\\\allen\\programs\\braintv\\workgroups\\nc-ophys\\visual_behavior\\platform_paper_cache'\n",
"\n",
"cache = VisualBehaviorOphysProjectCache.from_s3_cache(cache_dir=cache_dir)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### pick a random experiment and load the dataset "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"experiments_table = cache.get_ophys_experiment_table()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"ophys_experiment_id = experiments_table.index[0]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"dataset = cache.get_behavior_ophys_experiment(ophys_experiment_id)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get stimulus presentations and reward times"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"stimulus_presentations = dataset.stimulus_presentations.copy()\n",
"rewards = dataset.rewards.copy()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"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>duration</th>\n",
" <th>end_frame</th>\n",
" <th>image_index</th>\n",
" <th>image_name</th>\n",
" <th>image_set</th>\n",
" <th>index</th>\n",
" <th>omitted</th>\n",
" <th>start_frame</th>\n",
" <th>start_time</th>\n",
" <th>stop_time</th>\n",
" <th>is_change</th>\n",
" </tr>\n",
" <tr>\n",
" <th>stimulus_presentations_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></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",
" <td>0</td>\n",
" <td>0.25020</td>\n",
" <td>18001.0</td>\n",
" <td>0</td>\n",
" <td>im065</td>\n",
" <td>Natural_Images_Lum_Matched_set_training_2017.0...</td>\n",
" <td>0</td>\n",
" <td>False</td>\n",
" <td>17986</td>\n",
" <td>309.27537</td>\n",
" <td>309.52557</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>0.25021</td>\n",
" <td>18046.0</td>\n",
" <td>0</td>\n",
" <td>im065</td>\n",
" <td>Natural_Images_Lum_Matched_set_training_2017.0...</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" <td>18031</td>\n",
" <td>310.02598</td>\n",
" <td>310.27619</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.25020</td>\n",
" <td>18091.0</td>\n",
" <td>0</td>\n",
" <td>im065</td>\n",
" <td>Natural_Images_Lum_Matched_set_training_2017.0...</td>\n",
" <td>2</td>\n",
" <td>False</td>\n",
" <td>18076</td>\n",
" <td>310.77660</td>\n",
" <td>311.02680</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.25019</td>\n",
" <td>18136.0</td>\n",
" <td>0</td>\n",
" <td>im065</td>\n",
" <td>Natural_Images_Lum_Matched_set_training_2017.0...</td>\n",
" <td>3</td>\n",
" <td>False</td>\n",
" <td>18121</td>\n",
" <td>311.52721</td>\n",
" <td>311.77740</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>0.25024</td>\n",
" <td>18181.0</td>\n",
" <td>0</td>\n",
" <td>im065</td>\n",
" <td>Natural_Images_Lum_Matched_set_training_2017.0...</td>\n",
" <td>4</td>\n",
" <td>False</td>\n",
" <td>18166</td>\n",
" <td>312.27782</td>\n",
" <td>312.52806</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" duration end_frame image_index image_name \\\n",
"stimulus_presentations_id \n",
"0 0.25020 18001.0 0 im065 \n",
"1 0.25021 18046.0 0 im065 \n",
"2 0.25020 18091.0 0 im065 \n",
"3 0.25019 18136.0 0 im065 \n",
"4 0.25024 18181.0 0 im065 \n",
"\n",
" image_set \\\n",
"stimulus_presentations_id \n",
"0 Natural_Images_Lum_Matched_set_training_2017.0... \n",
"1 Natural_Images_Lum_Matched_set_training_2017.0... \n",
"2 Natural_Images_Lum_Matched_set_training_2017.0... \n",
"3 Natural_Images_Lum_Matched_set_training_2017.0... \n",
"4 Natural_Images_Lum_Matched_set_training_2017.0... \n",
"\n",
" index omitted start_frame start_time stop_time \\\n",
"stimulus_presentations_id \n",
"0 0 False 17986 309.27537 309.52557 \n",
"1 1 False 18031 310.02598 310.27619 \n",
"2 2 False 18076 310.77660 311.02680 \n",
"3 3 False 18121 311.52721 311.77740 \n",
"4 4 False 18166 312.27782 312.52806 \n",
"\n",
" is_change \n",
"stimulus_presentations_id \n",
"0 False \n",
"1 False \n",
"2 False \n",
"3 False \n",
"4 False "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# stimulus_presentations has start and end times of each stimulus during the session\n",
"stimulus_presentations.head()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"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>volume</th>\n",
" <th>timestamps</th>\n",
" <th>autorewarded</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>0</td>\n",
" <td>0.005</td>\n",
" <td>322.91986</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>0.005</td>\n",
" <td>333.44514</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.005</td>\n",
" <td>364.23697</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.005</td>\n",
" <td>388.25660</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>0.005</td>\n",
" <td>455.04450</td>\n",
" <td>True</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" volume timestamps autorewarded\n",
"0 0.005 322.91986 True\n",
"1 0.005 333.44514 True\n",
"2 0.005 364.23697 True\n",
"3 0.005 388.25660 True\n",
"4 0.005 455.04450 True"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# rewards has the time of each reward that happened during the session \n",
"rewards.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### method for associating reward times with stimulus presentation times"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def rewards_each_flash(stimulus_presentations_df, rewards_df,\n",
" range_relative_to_stimulus_start=[0, 0.75]):\n",
" '''\n",
" Append a column to stimulus_presentations which contains the timestamps of rewards that occur\n",
" in a range relative to the onset of the stimulus.\n",
"\n",
" Args:\n",
" stimulus_presentations_df (pd.DataFrame): dataframe of stimulus presentations.\n",
" Must contain: 'start_time'\n",
" rewards_df (pd.DataFrame): rewards dataframe. Must contain 'timestamps'\n",
" range_relative_to_stimulus_start (list with 2 elements): start and end of the range\n",
" relative to the start of each stimulus to average the running speed.\n",
" Returns:\n",
" rewards_each_flash (pd.Series): reward times that fell within the window relative to each stim time\n",
" '''\n",
"\n",
" reward_times = rewards_df['timestamps'].values\n",
" rewards_each_flash = stimulus_presentations_df.apply(\n",
" lambda row: reward_times[\n",
" ((reward_times > row[\"start_time\"] + range_relative_to_stimulus_start[0]) & \n",
" (reward_times < row[\"start_time\"] + range_relative_to_stimulus_start[1]))],\n",
" axis=1,)\n",
" return rewards_each_flash\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"stimulus_presentations['rewards'] = rewards_each_flash(stimulus_presentations, rewards)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"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>duration</th>\n",
" <th>end_frame</th>\n",
" <th>image_index</th>\n",
" <th>image_name</th>\n",
" <th>image_set</th>\n",
" <th>index</th>\n",
" <th>omitted</th>\n",
" <th>start_frame</th>\n",
" <th>start_time</th>\n",
" <th>stop_time</th>\n",
" <th>is_change</th>\n",
" <th>rewards</th>\n",
" </tr>\n",
" <tr>\n",
" <th>stimulus_presentations_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></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",
" <td>0</td>\n",
" <td>0.25020</td>\n",
" <td>18001.0</td>\n",
" <td>0</td>\n",
" <td>im065</td>\n",
" <td>Natural_Images_Lum_Matched_set_training_2017.0...</td>\n",
" <td>0</td>\n",
" <td>False</td>\n",
" <td>17986</td>\n",
" <td>309.27537</td>\n",
" <td>309.52557</td>\n",
" <td>False</td>\n",
" <td>[]</td>\n",
" </tr>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>0.25021</td>\n",
" <td>18046.0</td>\n",
" <td>0</td>\n",
" <td>im065</td>\n",
" <td>Natural_Images_Lum_Matched_set_training_2017.0...</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" <td>18031</td>\n",
" <td>310.02598</td>\n",
" <td>310.27619</td>\n",
" <td>False</td>\n",
" <td>[]</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.25020</td>\n",
" <td>18091.0</td>\n",
" <td>0</td>\n",
" <td>im065</td>\n",
" <td>Natural_Images_Lum_Matched_set_training_2017.0...</td>\n",
" <td>2</td>\n",
" <td>False</td>\n",
" <td>18076</td>\n",
" <td>310.77660</td>\n",
" <td>311.02680</td>\n",
" <td>False</td>\n",
" <td>[]</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.25019</td>\n",
" <td>18136.0</td>\n",
" <td>0</td>\n",
" <td>im065</td>\n",
" <td>Natural_Images_Lum_Matched_set_training_2017.0...</td>\n",
" <td>3</td>\n",
" <td>False</td>\n",
" <td>18121</td>\n",
" <td>311.52721</td>\n",
" <td>311.77740</td>\n",
" <td>False</td>\n",
" <td>[]</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>0.25024</td>\n",
" <td>18181.0</td>\n",
" <td>0</td>\n",
" <td>im065</td>\n",
" <td>Natural_Images_Lum_Matched_set_training_2017.0...</td>\n",
" <td>4</td>\n",
" <td>False</td>\n",
" <td>18166</td>\n",
" <td>312.27782</td>\n",
" <td>312.52806</td>\n",
" <td>False</td>\n",
" <td>[]</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" duration end_frame image_index image_name \\\n",
"stimulus_presentations_id \n",
"0 0.25020 18001.0 0 im065 \n",
"1 0.25021 18046.0 0 im065 \n",
"2 0.25020 18091.0 0 im065 \n",
"3 0.25019 18136.0 0 im065 \n",
"4 0.25024 18181.0 0 im065 \n",
"\n",
" image_set \\\n",
"stimulus_presentations_id \n",
"0 Natural_Images_Lum_Matched_set_training_2017.0... \n",
"1 Natural_Images_Lum_Matched_set_training_2017.0... \n",
"2 Natural_Images_Lum_Matched_set_training_2017.0... \n",
"3 Natural_Images_Lum_Matched_set_training_2017.0... \n",
"4 Natural_Images_Lum_Matched_set_training_2017.0... \n",
"\n",
" index omitted start_frame start_time stop_time \\\n",
"stimulus_presentations_id \n",
"0 0 False 17986 309.27537 309.52557 \n",
"1 1 False 18031 310.02598 310.27619 \n",
"2 2 False 18076 310.77660 311.02680 \n",
"3 3 False 18121 311.52721 311.77740 \n",
"4 4 False 18166 312.27782 312.52806 \n",
"\n",
" is_change rewards \n",
"stimulus_presentations_id \n",
"0 False [] \n",
"1 False [] \n",
"2 False [] \n",
"3 False [] \n",
"4 False [] "
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stimulus_presentations.head()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"stimulus_presentations_id\n",
"0 []\n",
"1 []\n",
"2 []\n",
"3 []\n",
"4 []\n",
" ..\n",
"4796 []\n",
"4797 []\n",
"4798 []\n",
"4799 []\n",
"4800 []\n",
"Name: rewards, Length: 4801, dtype: object"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# look at values for rewards column\n",
"stimulus_presentations.rewards"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"stimulus_presentations_id\n",
"18 [322.91986]\n",
"32 [333.44514]\n",
"73 [364.23697]\n",
"105 [388.2566]\n",
"194 [455.0445]\n",
" ... \n",
"4747 []\n",
"4757 []\n",
"4770 []\n",
"4784 []\n",
"4795 []\n",
"Name: rewards, Length: 186, dtype: object"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# let's check the changes specifically\n",
"stimulus_presentations[stimulus_presentations.is_change==True].rewards"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# turn it in to a Boolean to get a True False for whether a stimulus was rewarded or not\n",
"stimulus_presentations['rewarded'] = [True if len(rewards)>0 else False for rewards in stimulus_presentations.rewards.values]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"scrolled": true
},
"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>is_change</th>\n",
" <th>rewards</th>\n",
" <th>rewarded</th>\n",
" </tr>\n",
" <tr>\n",
" <th>stimulus_presentations_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>18</td>\n",
" <td>True</td>\n",
" <td>[322.91986]</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <td>32</td>\n",
" <td>True</td>\n",
" <td>[333.44514]</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <td>73</td>\n",
" <td>True</td>\n",
" <td>[364.23697]</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <td>105</td>\n",
" <td>True</td>\n",
" <td>[388.2566]</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <td>194</td>\n",
" <td>True</td>\n",
" <td>[455.0445]</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4747</td>\n",
" <td>True</td>\n",
" <td>[]</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4757</td>\n",
" <td>True</td>\n",
" <td>[]</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4770</td>\n",
" <td>True</td>\n",
" <td>[]</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4784</td>\n",
" <td>True</td>\n",
" <td>[]</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4795</td>\n",
" <td>True</td>\n",
" <td>[]</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>186 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" is_change rewards rewarded\n",
"stimulus_presentations_id \n",
"18 True [322.91986] True\n",
"32 True [333.44514] True\n",
"73 True [364.23697] True\n",
"105 True [388.2566] True\n",
"194 True [455.0445] True\n",
"... ... ... ...\n",
"4747 True [] False\n",
"4757 True [] False\n",
"4770 True [] False\n",
"4784 True [] False\n",
"4795 True [] False\n",
"\n",
"[186 rows x 3 columns]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# let's check the reward times and rewarded Boolean column for the changes\n",
"stimulus_presentations[stimulus_presentations.is_change][['is_change', 'rewards', 'rewarded']]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Another way to get reward and behavioral trial information, including reward times"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `trials` attribute contains information just for image changes and sham changes (catch trials) and is useful for analysis of behavioral performance when you do not need information about each individual stimulus presentation in between the changes"
]
},
{
"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>start_time</th>\n",
" <th>stop_time</th>\n",
" <th>lick_times</th>\n",
" <th>reward_time</th>\n",
" <th>reward_volume</th>\n",
" <th>hit</th>\n",
" <th>false_alarm</th>\n",
" <th>miss</th>\n",
" <th>stimulus_change</th>\n",
" <th>aborted</th>\n",
" <th>...</th>\n",
" <th>catch</th>\n",
" <th>auto_rewarded</th>\n",
" <th>correct_reject</th>\n",
" <th>trial_length</th>\n",
" <th>response_time</th>\n",
" <th>change_frame</th>\n",
" <th>change_time</th>\n",
" <th>response_latency</th>\n",
" <th>initial_image_name</th>\n",
" <th>change_image_name</th>\n",
" </tr>\n",
" <tr>\n",
" <th>trials_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></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",
" <td>0</td>\n",
" <td>309.25866</td>\n",
" <td>311.72736</td>\n",
" <td>[311.41045]</td>\n",
" <td>NaN</td>\n",
" <td>0.000</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>2.46870</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>im065</td>\n",
" <td>im065</td>\n",
" </tr>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>312.26114</td>\n",
" <td>314.16270</td>\n",
" <td>[313.84577]</td>\n",
" <td>NaN</td>\n",
" <td>0.000</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>1.90156</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>im065</td>\n",
" <td>im065</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>314.51298</td>\n",
" <td>316.64807</td>\n",
" <td>[316.33114]</td>\n",
" <td>NaN</td>\n",
" <td>0.000</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>2.13509</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>im065</td>\n",
" <td>im065</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>316.76480</td>\n",
" <td>319.61716</td>\n",
" <td>[319.30022]</td>\n",
" <td>NaN</td>\n",
" <td>0.000</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>2.85236</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>im065</td>\n",
" <td>im065</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>319.76729</td>\n",
" <td>327.03990</td>\n",
" <td>[323.22008, 323.38691, 323.75386, 323.90398, 3...</td>\n",
" <td>322.91986</td>\n",
" <td>0.005</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" <td>7.27261</td>\n",
" <td>323.22008</td>\n",
" <td>18795.0</td>\n",
" <td>322.822945</td>\n",
" <td>0.397135</td>\n",
" <td>im065</td>\n",
" <td>im069</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 21 columns</p>\n",
"</div>"
],
"text/plain": [
" start_time stop_time \\\n",
"trials_id \n",
"0 309.25866 311.72736 \n",
"1 312.26114 314.16270 \n",
"2 314.51298 316.64807 \n",
"3 316.76480 319.61716 \n",
"4 319.76729 327.03990 \n",
"\n",
" lick_times reward_time \\\n",
"trials_id \n",
"0 [311.41045] NaN \n",
"1 [313.84577] NaN \n",
"2 [316.33114] NaN \n",
"3 [319.30022] NaN \n",
"4 [323.22008, 323.38691, 323.75386, 323.90398, 3... 322.91986 \n",
"\n",
" reward_volume hit false_alarm miss stimulus_change aborted \\\n",
"trials_id \n",
"0 0.000 False False False False True \n",
"1 0.000 False False False False True \n",
"2 0.000 False False False False True \n",
"3 0.000 False False False False True \n",
"4 0.005 False False False True False \n",
"\n",
" ... catch auto_rewarded correct_reject trial_length \\\n",
"trials_id ... \n",
"0 ... False False False 2.46870 \n",
"1 ... False False False 1.90156 \n",
"2 ... False False False 2.13509 \n",
"3 ... False False False 2.85236 \n",
"4 ... False True False 7.27261 \n",
"\n",
" response_time change_frame change_time response_latency \\\n",
"trials_id \n",
"0 NaN NaN NaN NaN \n",
"1 NaN NaN NaN NaN \n",
"2 NaN NaN NaN NaN \n",
"3 NaN NaN NaN NaN \n",
"4 323.22008 18795.0 322.822945 0.397135 \n",
"\n",
" initial_image_name change_image_name \n",
"trials_id \n",
"0 im065 im065 \n",
"1 im065 im065 \n",
"2 im065 im065 \n",
"3 im065 im065 \n",
"4 im065 im069 \n",
"\n",
"[5 rows x 21 columns]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"trials = dataset.trials.copy()\n",
"trials.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Other useful functions"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"\n",
"def licks_each_flash(stimulus_presentations_df, licks_df,\n",
" range_relative_to_stimulus_start=[0, 0.75]):\n",
" '''\n",
" Append a column to stimulus_presentations which contains the timestamps of licks that occur\n",
" in a range relative to the onset of the stimulus.\n",
"\n",
" Args:\n",
" stimulus_presentations_df (pd.DataFrame): dataframe of stimulus presentations.\n",
" Must contain: 'start_time'\n",
" licks_df (pd.DataFrame): lick dataframe. Must contain 'timestamps'\n",
" range_relative_to_stimulus_start (list with 2 elements): start and end of the range\n",
" relative to the start of each stimulus to average the running speed.\n",
" Returns:\n",
" licks_each_flash (pd.Series): lick times that fell within the window relative to each stim time\n",
" '''\n",
"\n",
" lick_times = licks_df['timestamps'].values\n",
" licks_each_flash = stimulus_presentations_df.apply(\n",
" lambda row: lick_times[\n",
" ((lick_times > row[\"start_time\"] + range_relative_to_stimulus_start[0]) & \n",
" (lick_times < row[\"start_time\"] + range_relative_to_stimulus_start[1]))],\n",
" axis=1,)\n",
" return licks_each_flash\n",
"\n",
"\n",
"def mean_running_speed(stimulus_presentations_df, running_speed_df,\n",
" range_relative_to_stimulus_start=[0, 0.25]):\n",
" '''\n",
" Append a column to stimulus_presentations which contains the mean running speed in a range relative to\n",
" the stimulus start time.\n",
"\n",
" Args:\n",
" stimulus_presentations_df (pd.DataFrame): dataframe of stimulus presentations.\n",
" Must contain: 'start_time'\n",
" running_speed_df (pd.DataFrame): dataframe of running speed.\n",
" Must contain: 'speed', 'timestamps'\n",
" range_relative_to_stimulus_start (list with 2 elements): start and end of the range\n",
" relative to the start of each stimulus to average the running speed.\n",
" Returns:\n",
" flash_running_speed (pd.Series): mean running speed for each stimulus presentation.\n",
" '''\n",
" flash_running_speed = stimulus_presentations_df.apply(\n",
" lambda row: trace_average(\n",
" running_speed_df['speed'].values,\n",
" running_speed_df['timestamps'].values,\n",
" row[\"start_time\"] + range_relative_to_stimulus_start[0],\n",
" row[\"start_time\"] + range_relative_to_stimulus_start[1],\n",
" ),\n",
" axis=1,\n",
" )\n",
" return flash_running_speed\n",
"\n",
"\n",
"def mean_pupil_area(stimulus_presentations_df, eye_tracking,\n",
" range_relative_to_stimulus_start=[0, 0.25]):\n",
" '''\n",
" Append a column to stimulus_presentations which contains the mean pupil area in a range relative to\n",
" the stimulus start time.\n",
"\n",
" Args:\n",
" stimulus_presentations_df (pd.DataFrame): dataframe of stimulus presentations.\n",
" Must contain: 'start_time'\n",
" eye_tracking (pd.DataFrame): dataframe of eye tracking data.\n",
" Must contain: 'pupil_area', 'timestamps'\n",
" range_relative_to_stimulus_start (list with 2 elements): start and end of the range\n",
" relative to the start of each stimulus to average the pupil area.\n",
" Returns:\n",
" flash_running_speed (pd.Series): mean running speed for each stimulus presentation.\n",
" '''\n",
" flash_pupil_area = stimulus_presentations_df.apply(\n",
" lambda row: trace_average(\n",
" eye_tracking['pupil_area'].values,\n",
" eye_tracking['timestamps'].values,\n",
" row[\"start_time\"] + range_relative_to_stimulus_start[0],\n",
" row[\"start_time\"] + range_relative_to_stimulus_start[1],\n",
" ),\n",
" axis=1,\n",
" )\n",
" return flash_pupil_area\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "visual_behavior_sdk",
"language": "python",
"name": "visual_behavior_sdk"
},
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment