From 890b85208d041acf0d22d3226dd0fc9f9288ed60 Mon Sep 17 00:00:00 2001
From: Timothe Jost <timothe.jost@wanadoo.fr>
Date: Wed, 19 Apr 2023 20:47:24 +0200
Subject: [PATCH] Update tutorial.ipynb

---
 my_first_analysis/tutorial.ipynb | 279 ++++++++++++++++++++++++++++++-
 1 file changed, 275 insertions(+), 4 deletions(-)

diff --git a/my_first_analysis/tutorial.ipynb b/my_first_analysis/tutorial.ipynb
index b606adc..732fc74 100644
--- a/my_first_analysis/tutorial.ipynb
+++ b/my_first_analysis/tutorial.ipynb
@@ -1,8 +1,17 @@
 {
  "cells": [
+  {
+   "cell_type": "markdown",
+   "id": "91dea70d-852a-4874-a2d6-3942647ddea0",
+   "metadata": {},
+   "source": [
+    "# Perform imports\n",
+    "This only needs to be done once, each time you open the jupyter lab document"
+   ]
+  },
   {
    "cell_type": "code",
-   "execution_count": 1,
+   "execution_count": 10,
    "id": "d9f870bc-db29-4c14-bacb-d8e5d03e92d0",
    "metadata": {},
    "outputs": [],
@@ -13,9 +22,37 @@
     "from ResearchProjects.adaptation import aliases as alias"
    ]
   },
+  {
+   "cell_type": "markdown",
+   "id": "3fd56db3-da3e-49b0-a691-81cbf9069f34",
+   "metadata": {},
+   "source": [
+    "## Enable logging messages and files\n",
+    "This only needs to be done once, each time you open the jupyter lab document"
+   ]
+  },
   {
    "cell_type": "code",
-   "execution_count": 3,
+   "execution_count": 15,
+   "id": "2512ee5c-3309-4c8f-a170-008ea1e9e592",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "Inflow.logging.enable_logging()"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "3a41c05b-1d8e-40b0-ad3e-80b68e95327d",
+   "metadata": {},
+   "source": [
+    "# Manage data selection\n",
+    "We use one-api to connect to alyx database service (hosted here in the lab)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 16,
    "id": "47b1ad86-ac3d-42cc-a46b-f33e3ae8a251",
    "metadata": {},
    "outputs": [],
@@ -23,15 +60,249 @@
     "connector = one.ONE()"
    ]
   },
+  {
+   "cell_type": "markdown",
+   "id": "3c23ffa5-896e-45cd-a4d4-358ccfbad80f",
+   "metadata": {
+    "tags": []
+   },
+   "source": [
+    "## Search a set of sessions or a specific session\n",
+    "Using the one connector object creaed before, and the search method.\n",
+    "The available options for `search` are : \n",
+    "- dataset : str, list\n",
+    "    List of dataset names. Returns sessions containing all these datasets.\n",
+    "    A dataset matches if it contains the search string e.g. 'wheel.position' matches\n",
+    "    '_ibl_wheel.position.npy'\n",
+    "- date_range : str, list, datetime.datetime, datetime.date, pandas.timestamp\n",
+    "    A single date to search or a list of 2 dates that define the range (inclusive).  To\n",
+    "    define only the upper or lower date bound, set the other element to None.\n",
+    "- lab : str, list\n",
+    "    A str or list of lab names, returns sessions from any of these labs\n",
+    "- number : str, int\n",
+    "    Number of session to be returned, i.e. number in sequence for a given date\n",
+    "- subject : str, list\n",
+    "    A list of subject nicknames, returns sessions for any of these subjects\n",
+    "- task_protocol : str, list\n",
+    "    The task protocol name (can be partial, i.e. any task protocol containing that str\n",
+    "    will be found)\n",
+    "- project(s) : str, list\n",
+    "    The project name (can be partial, i.e. any task protocol containing that str\n",
+    "    will be found)\n",
+    "- performance_lte / performance_gte : float\n",
+    "    Search only for sessions whose performance is less equal or greater equal than a\n",
+    "    pre-defined threshold as a percentage (0-100)\n",
+    "- users : str, list\n",
+    "    A list of users\n",
+    "- location : str, list\n",
+    "    A str or list of lab location (as per Alyx definition) name\n",
+    "    Note: this corresponds to the specific rig, not the lab geographical location per se\n",
+    "- dataset_types : str, list\n",
+    "    One or more of dataset_types\n",
+    "- details : bool\n",
+    "    If true also returns a dict of dataset details\n",
+    "- query_type : str, None\n",
+    "    Query cache ('local') or Alyx database ('remote')\n",
+    "- limit : int\n",
+    "    The number of results to fetch in one go (if pagination enabled on server)"
+   ]
+  },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 17,
    "id": "661207a3-8a5a-4125-b276-e9ba86c195dd",
    "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>subject</th>\n",
+       "      <th>start_time</th>\n",
+       "      <th>number</th>\n",
+       "      <th>lab</th>\n",
+       "      <th>projects</th>\n",
+       "      <th>url</th>\n",
+       "      <th>task_protocol</th>\n",
+       "      <th>date</th>\n",
+       "      <th>json</th>\n",
+       "      <th>extended_qc</th>\n",
+       "      <th>rel_path</th>\n",
+       "      <th>alias_name</th>\n",
+       "      <th>short_path</th>\n",
+       "      <th>path</th>\n",
+       "    </tr>\n",
+       "    <tr>\n",
+       "      <th>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",
+       "    </tr>\n",
+       "  </thead>\n",
+       "  <tbody>\n",
+       "    <tr>\n",
+       "      <th>04f92e4a-da64-4018-aa6c-d9a79a91c831</th>\n",
+       "      <td>wm24</td>\n",
+       "      <td>2022-08-22T15:27:00</td>\n",
+       "      <td>1</td>\n",
+       "      <td>HaissLab</td>\n",
+       "      <td>[Adaptation]</td>\n",
+       "      <td>http://157.99.138.172/sessions/04f92e4a-da64-4...</td>\n",
+       "      <td></td>\n",
+       "      <td>2022-08-22</td>\n",
+       "      <td>{'channels': ['R', 'G'], 'whisker_stims': {'St...</td>\n",
+       "      <td>{'exclude_whisker': ['C1']}</td>\n",
+       "      <td>wm24\\2022-08-22\\001</td>\n",
+       "      <td>wm24_2022_08_22_001</td>\n",
+       "      <td>wm24\\2022-08-22\\001</td>\n",
+       "      <td>D:\\LOCAL_DATA\\wm24\\2022-08-22\\001</td>\n",
+       "    </tr>\n",
+       "  </tbody>\n",
+       "</table>\n",
+       "</div>"
+      ],
+      "text/plain": [
+       "                                     subject           start_time  number  \\\n",
+       "id                                                                          \n",
+       "04f92e4a-da64-4018-aa6c-d9a79a91c831    wm24  2022-08-22T15:27:00       1   \n",
+       "\n",
+       "                                           lab      projects  \\\n",
+       "id                                                             \n",
+       "04f92e4a-da64-4018-aa6c-d9a79a91c831  HaissLab  [Adaptation]   \n",
+       "\n",
+       "                                                                                    url  \\\n",
+       "id                                                                                        \n",
+       "04f92e4a-da64-4018-aa6c-d9a79a91c831  http://157.99.138.172/sessions/04f92e4a-da64-4...   \n",
+       "\n",
+       "                                     task_protocol        date  \\\n",
+       "id                                                               \n",
+       "04f92e4a-da64-4018-aa6c-d9a79a91c831                2022-08-22   \n",
+       "\n",
+       "                                                                                   json  \\\n",
+       "id                                                                                        \n",
+       "04f92e4a-da64-4018-aa6c-d9a79a91c831  {'channels': ['R', 'G'], 'whisker_stims': {'St...   \n",
+       "\n",
+       "                                                      extended_qc  \\\n",
+       "id                                                                  \n",
+       "04f92e4a-da64-4018-aa6c-d9a79a91c831  {'exclude_whisker': ['C1']}   \n",
+       "\n",
+       "                                                 rel_path  \\\n",
+       "id                                                          \n",
+       "04f92e4a-da64-4018-aa6c-d9a79a91c831  wm24\\2022-08-22\\001   \n",
+       "\n",
+       "                                               alias_name  \\\n",
+       "id                                                          \n",
+       "04f92e4a-da64-4018-aa6c-d9a79a91c831  wm24_2022_08_22_001   \n",
+       "\n",
+       "                                               short_path  \\\n",
+       "id                                                          \n",
+       "04f92e4a-da64-4018-aa6c-d9a79a91c831  wm24\\2022-08-22\\001   \n",
+       "\n",
+       "                                                                   path  \n",
+       "id                                                                       \n",
+       "04f92e4a-da64-4018-aa6c-d9a79a91c831  D:\\LOCAL_DATA\\wm24\\2022-08-22\\001  "
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    },
+    {
+     "data": {
+      "text/plain": [
+       "subject                                                       wm24\n",
+       "start_time                                     2022-08-22T15:27:00\n",
+       "number                                                           1\n",
+       "lab                                                       HaissLab\n",
+       "projects                                              [Adaptation]\n",
+       "url              http://157.99.138.172/sessions/04f92e4a-da64-4...\n",
+       "task_protocol                                                     \n",
+       "date                                                    2022-08-22\n",
+       "json             {'channels': ['R', 'G'], 'whisker_stims': {'St...\n",
+       "extended_qc                            {'exclude_whisker': ['C1']}\n",
+       "rel_path                                       wm24\\2022-08-22\\001\n",
+       "alias_name                                     wm24_2022_08_22_001\n",
+       "short_path                                     wm24\\2022-08-22\\001\n",
+       "path                             D:\\LOCAL_DATA\\wm24\\2022-08-22\\001\n",
+       "Name: 04f92e4a-da64-4018-aa6c-d9a79a91c831, dtype: object"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "sessions = connector.search(subject = \"wm24\", date_range = \"2022-08-22\", number = 1, details = True)\n",
+    "my_session = sessions.iloc[0]\n",
+    "display(sessions)\n",
+    "display(my_session)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "62e9a6ec-17ff-4d73-b265-c7f0aad6e810",
+   "metadata": {},
+   "source": [
+    "## Using adaptation pipelines\n",
+    "### for pipelines : \n",
+    "Either :\n",
+    "- `generate_afunction` which tryes o load previously savec results for that function, and if it doesnt find it, generates the results and saves them. You can force regenerating by using ` refresh = True`  in the arguments to the function.\n",
+    "- `get_afunction` which returns already processed results of that function (no make sure to not reprocess and overwrite previously analysed data)\n",
+    "- `afunction` which only creates and returns processed results for that function (does not check if there is processed data, and does not save data)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "9e205f79-ef58-48ea-b523-8f86a48ea6a5",
+   "metadata": {},
+   "source": [
+    "# Getting structured data for analysis"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 31,
+   "id": "c7a5d315-2c7a-4534-b3af-3cdc855efc89",
+   "metadata": {},
    "outputs": [],
    "source": [
-    "connector.search()"
+    "trials_df = adaptation.pipelines.get_trials_df(my_session)"
    ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "aadc5365-d4e8-4f91-a9c7-6bd0ac8934b6",
+   "metadata": {},
+   "outputs": [],
+   "source": []
   }
  ],
  "metadata": {
-- 
GitLab