{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42910e2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import h5py\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "from matplotlib import pyplot as plt\n",
    "import vbn_utils as vbn\n",
    "from vbn_utils import formatFigure\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fc2b3fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "from notebook_utils import get_opto_responses_for_units, plot_raster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1878f051",
   "metadata": {},
   "outputs": [],
   "source": [
    "high_res = True\n",
    "if high_res:\n",
    "    plt.rcParams['figure.dpi'] = 150\n",
    "    plt.rcParams['savefig.dpi'] = 300\n",
    "    plt.rcParams['font.size'] = 12\n",
    "    plt.rcParams['pdf.fonttype'] = 42\n",
    "\n",
    "    plt.rcParams['figure.facecolor'] = 'white'\n",
    "    plt.rcParams['axes.facecolor'] = 'white'\n",
    "    plt.rcParams['savefig.facecolor'] = 'white'  # affects clipboard copy too\n",
    "    plt.rcParams['savefig.transparent'] = False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6653fdc5",
   "metadata": {},
   "source": [
    "## Data loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "935c74e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Paths to all of the useful supplemental tables and tensors\n",
    "unit_table_file = \"/Volumes/programs/mindscope/workgroups/np-behavior/vbn_data_release/supplemental_tables/master_units_with_responsiveness.csv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e15dbf6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "units = pd.read_csv(unit_table_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9591160",
   "metadata": {},
   "source": [
    "## Example session raster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acb5bbf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from allensdk.brain_observatory.behavior.behavior_project_cache.\\\n",
    "    behavior_neuropixels_project_cache \\\n",
    "    import VisualBehaviorNeuropixelsProjectCache\n",
    "\n",
    "\n",
    "cache_dir = \"/Volumes/programs/mindscope/workgroups/np-behavior/vbn_data_release/vbn_s3_cache\"\n",
    "\n",
    "\n",
    "cache = VisualBehaviorNeuropixelsProjectCache.from_s3_cache(\n",
    "            cache_dir=cache_dir)\n",
    "cache.load_manifest('visual-behavior-neuropixels_project_manifest_v0.5.0.json')\n",
    "\n",
    "session_table = cache.get_ecephys_session_table()\n",
    "sst_sessions = session_table[session_table['genotype'].str.contains('Sst')]\n",
    "session_id = sst_sessions.index.values[0]\n",
    "session = cache.get_ecephys_session(\n",
    "           ecephys_session_id=session_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e2685fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "session_sst_units = units[(units['ecephys_session_id'] == session_id)&(units['SST'])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8361d15f",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.size'] = 16\n",
    "opto_table = session.optotagging_table\n",
    "long_pulse = opto_table.loc[(opto_table['duration']==opto_table['duration'].max())&\n",
    "                            (opto_table['level']==opto_table['level'].max())]['start_time'].values\n",
    "\n",
    "short_pulse = opto_table.loc[(opto_table['duration']==opto_table['duration'].min())&\n",
    "                            (opto_table['level']==opto_table['level'].max())]['start_time'].values\n",
    "\n",
    "example_cells = [1179688501,]\n",
    "for ssu in example_cells:\n",
    "    spikes = session.spike_times[ssu]\n",
    "    fig, ax = plt.subplots(1,2)\n",
    "    fig.suptitle(str(ssu))\n",
    "    plot_raster(ax[0], spikes, short_pulse, time_before=0.01, time_after = 0.02)\n",
    "    plot_raster(ax[1], spikes, long_pulse, time_before=0.1, time_after = 1.1)\n",
    "    formatFigure(fig, ax[0], xLabel='Time from laser onset (s)', yLabel='Trial')\n",
    "    formatFigure(fig, ax[1],xLabel='Time from laser onset (s)', yLabel='Trial')\n",
    "    plt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "baf2d082",
   "metadata": {},
   "source": [
    "## Waveform duration distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b77b28bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "vis_units = vbn.get_unit_ids(units, 'VISall')\n",
    "vis_units = units[units['unit_id'].isin(vis_units)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7753068d",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ax.hist(vis_units['waveform_duration'], bins=np.arange(0, 1, 0.04), color='k', edgecolor='w')\n",
    "formatFigure(fig, ax, xLabel='Spike width (ms)', yLabel='Unit count')\n",
    "ax.axvline(0.4, color='gray', linestyle='--')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8e20650",
   "metadata": {},
   "source": [
    "## Cell type classification criteria"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe552524",
   "metadata": {},
   "source": [
    "| **Cell Type** | **Criteria** |\n",
    "|--------------|-------------------------|\n",
    "| **SST** | • Recorded in **Sst-IRES-Cre;Ai32** mouse <br>• Z-scored pulse-evoked firing rate is **greater than 2**<br>• First-spike latency during the pulse stimulus is **less than 8 ms**<br>• First-spike jitter during the pulse stimulus is **less than 2 ms**<br>• Responsive for **at least 30%** of cosine stimulus duration |\n",
    "| **VIP** | • Recorded in **Vip-IRES-Cre;Ai32** mouse <br>• Z-scored pulse-evoked firing rate is **greater than 2**<br>• First-spike latency during the pulse stimulus is **less than 8 ms**<br>• First-spike jitter during the pulse stimulus is **less than 2 ms**<br>• Responsive for **at least 30%** of cosine stimulus duration |\n",
    "| **RS (Regular Spiking)** | • Waveform duration is **greater than 0.4 ms**<br>• **Does not** meet SST criteria<br>• **Does not** meet VIP criteria |\n",
    "| **FS (Fast Spiking)** | • Waveform duration is **less than 0.4 ms**<br>• **Does not** meet SST criteria<br>• **Does not** meet VIP criteria |"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "vbn_manuscript",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
