{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os\n",
    "from matplotlib import pyplot as plt\n",
    "import vbn_utils"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Paths to all of the useful supplemental tables and tensors\n",
    "sessions_table_file = \"/Volumes/programs/mindscope/workgroups/np-behavior/vbn_data_release/supplemental_tables/master_sessions_table.csv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sessions_table = pd.read_csv(sessions_table_file)\n",
    "good_sessions = sessions_table[sessions_table['abnormal_activity'].isnull() & sessions_table['abnormal_histology'].isnull()]['ecephys_session_id'].values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Decoding through the omission"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generated on HPC by 'run_image_decoding_sliding_window.py'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_session = []\n",
    "cluster = 'all'\n",
    "unitsamplesize =80\n",
    "for session_id in good_sessions:\n",
    "    try:\n",
    "        sess_data = np.load(os.path.join(\"/Volumes/programs/mindscope/workgroups/np-behavior/vbn_data_release/image_decoding_sliding_window\", str(session_id) + f\"_throughomissionRS_{cluster}.npy\"), allow_pickle=True).item()\n",
    "        if len(sess_data['VISall'][unitsamplesize]['imagewise_recall'])>0:\n",
    "            all_session.append(np.array(sess_data['VISall'][unitsamplesize]['imagewise_recall']).mean(axis=1))\n",
    "    except Exception as e:\n",
    "        print(f\"Error processing session {session_id}: {e}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib.patches import Rectangle\n",
    "plt.rcParams['figure.dpi'] = 300\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "fig.suptitle('VIS all, pop size: 80', fontsize=12)\n",
    "fig.set_size_inches([8, 4])\n",
    "vbn_utils.mean_sem_plot(np.array(all_session), ax,  x=sess_data['decodeWindows'], color='k')\n",
    "ax.axhline(0.125, color='k', linestyle='--', linewidth=0.5)\n",
    "ax.set_xlabel('Time from pre-omission image onset (ms)')\n",
    "ax.set_ylabel('Decoder accuracy')\n",
    "\n",
    "filled_rect = Rectangle((0, ax.get_ylim()[1]), 250, 0.05, color='gray', alpha=1, clip_on=False)\n",
    "ax.add_patch(filled_rect)\n",
    "\n",
    "open_rect = Rectangle((750, ax.get_ylim()[1]), 250, 0.05, edgecolor='gray', facecolor='none', linewidth=2, clip_on=False)\n",
    "ax.add_patch(open_rect)\n",
    "\n",
    "\n",
    "vbn_utils.formatFigure(fig, ax, fontsize=16)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "allensdk_38",
   "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.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
