{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## About this resource" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All of the tutorial notebooks as well as information about the dependent package (`nma-ibl`) can be found at [nma-ibl GitHub repository](https://github.com/int-brain-lab/nma-ibl)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setting up the environment (particularly for Colab users)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please execute the cells below to install the necessary dependencies and prepare the environment." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# install IBL pipeline package to access and navigate the pipeline\n", "!pip install --quiet nma-ibl" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotting Psychometric curves from IBL data pipeline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Welcome back! In this short notebook, we demonstrate how to plot the psychometric curve of the data in a session from the IBL DataJoint pipeline.\n", "\n", "If you haven't done so, please step through the notebook `01-Explore IBL behavior data pipeline` first." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As usual, we start by importing and configuring `datajoint`, and then import schemas from IBL pipeline from `nma_ibl` package.\n", "\n", "Be sure to replace `{PASSWORD}` with the password you received in your email for IBL data access!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import datajoint as dj\n", "\n", "dj.config['database.host'] = 'datajoint-public.internationalbrainlab.org'\n", "dj.config['database.user'] = 'ibl-public'\n", "dj.config['database.password'] = 'ibl-public'\n", "dj.conn() # explicitly checks if database connection can be established" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# behavior data\n", "from nma_ibl import behavior\n", "\n", "# analysis result on behavioral data\n", "from nma_ibl import behavior_analyses\n", "\n", "# meta information about subjects and sessions\n", "from nma_ibl import subject, acquisition\n", "\n", "# A utility function to perform the model fits of the psychometric function\n", "from nma_ibl import psychofit as psy\n", "\n", "# some standard packages\n", "import numpy as np\n", "import datetime\n", "import seaborn as sns \n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the IBL data pipeline, the psychometric curves have already been computed are saved in the table `behavior_analyses.PsychResultsBlock`. We can browse entries in this table for a particular subject, and the `prob_left_block` marks which prior probability $p(\\text{left})$ has been used in the block.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get psychemtric curves computed for behavioral sessions done by subject with nickname \"CSHL_015\"\n", "q = behavior_analyses.PsychResultsBlock & (subject.Subject & 'subject_nickname=\"CSHL_015\"')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's check the results of sessions collected after 2019-09-15, using restriction to further filter the results:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "q & 'session_start_time > \"2019-09-15\"'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's now plot the psychometric curve of the last session on 2019-09-16, containing three blocks with `prob_left` 0.2, 0.5 and 0.8" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "psych_results = q & {'session_start_time': datetime.datetime(2019, 9, 16, 13, 44, 46)}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "psych_results # there are three fits corresponding to three different `prob_left` values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's fetch the fit parameters and behavioral statistics from the table as a list of dictionaries." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dict_results = psych_results.fetch(\n", " 'signed_contrasts', 'prob_choose_right', 'n_trials_stim', 'n_trials_stim_right',\n", " 'threshold', 'bias', 'lapse_low', 'lapse_high', as_dict=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, we will loop through the blocks and plot the psychometric curves:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "colors = [[1., 0., 0.], [0., 0., 0.], [0., 0., 1.]]\n", "fig, ax = plt.subplots(1, 1, dpi=200)\n", "\n", "for result, color in zip(dict_results, colors):\n", " pars = [result['bias'], result['threshold'], result['lapse_low'], result['lapse_high']]\n", " contrasts = result['signed_contrasts'] * 100\n", " contrasts_fit = np.arange(-100, 100)\n", " prob_right_fit = psy.erf_psycho_2gammas(pars, contrasts_fit) *100\n", " sns.lineplot(contrasts_fit, prob_right_fit, color=color, ax=ax)\n", " sns.lineplot(x=contrasts, y=result['prob_choose_right']*100, err_style=\"bars\", linewidth=0, linestyle='None', mew=0.5,\n", " marker='.', ci=68, color=color, ax=ax)\n", "\n", "# add axis labels\n", "ax.set_xlabel('Signed Contrast (%)')\n", "ax.set_ylabel('Rightward Choice (%)')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And that's it! Storing the results of the psychometric curve fits on the behavior data in the DataJoint pipeline allows one to quickly retrieve these values and regenerate the psychometric curves, and to perform any statistical assessment on the fitted parameters. In the next and the last notebook in the series, we will visit how to reproduce a full study figure using data from IBL data pipeline." ] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }