{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "DeudRk4Y1sJJ" }, "source": [ "# Database Example " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2023-07-17T19:52:35.434872Z", "start_time": "2023-07-17T19:52:33.950791Z" }, "colab": { "base_uri": "https://localhost:8080/", "height": 350 }, "execution": { "iopub.execute_input": "2024-07-10T21:22:25.849177Z", "iopub.status.busy": "2024-07-10T21:22:25.848235Z", "iopub.status.idle": "2024-07-10T21:22:29.729455Z", "shell.execute_reply": "2024-07-10T21:22:29.728253Z", "shell.execute_reply.started": "2024-07-10T21:22:25.849115Z" }, "executionInfo": { "elapsed": 420, "status": "error", "timestamp": 1615494076728, "user": { "displayName": "Leo Scholl", "photoUrl": "", "userId": "08377480612982390884" }, "user_tz": 480 }, "id": "8CzeKK8Hg-bI", "outputId": "c5c454f6-5cf0-4f77-c822-1602cd207ed8", "tags": [] }, "outputs": [], "source": [ "# Import libraries\n", "import aopy\n", "from aopy.data import db\n", "from ipywidgets import interactive, widgets\n", "from datetime import date, datetime, timedelta\n", "import re\n", "from tqdm.notebook import tqdm\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2023-07-17T19:52:35.505436Z", "start_time": "2023-07-17T19:52:35.500179Z" }, "execution": { "iopub.execute_input": "2024-07-10T21:22:30.893900Z", "iopub.status.busy": "2024-07-10T21:22:30.892630Z", "iopub.status.idle": "2024-07-10T21:22:30.902121Z", "shell.execute_reply": "2024-07-10T21:22:30.900195Z", "shell.execute_reply.started": "2024-07-10T21:22:30.893834Z" }, "tags": [] }, "outputs": [], "source": [ "# set up constants\n", "preproc_dir = '/data/preprocessed'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Finding a single recording" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2023-07-17T19:52:37.746216Z", "start_time": "2023-07-17T19:52:37.683471Z" }, "execution": { "iopub.execute_input": "2024-07-10T21:17:51.454392Z", "iopub.status.busy": "2024-07-10T21:17:51.452733Z", "iopub.status.idle": "2024-07-10T21:17:51.551193Z", "shell.execute_reply": "2024-07-10T21:17:51.549087Z", "shell.execute_reply.started": "2024-07-10T21:17:51.454320Z" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2022-06-11 09:55:10.450000: beignet on manual control task, id=5624\n", "baseline flash\n" ] } ], "source": [ "subject = 'beignet'\n", "task = 'manual control'\n", "project = 'baseline flash'\n", "day = date(2022, 6, 11) # June 11, 2022\n", "\n", "sessions = db.lookup_sessions(subject=subject, task_name=task, date=day, project=project)\n", "e = sessions[-1]\n", "print(e)\n", "print(e.project)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2023-07-17T19:52:39.155712Z", "start_time": "2023-07-17T19:52:38.916720Z" }, "execution": { "iopub.execute_input": "2024-07-10T21:17:52.268969Z", "iopub.status.busy": "2024-07-10T21:17:52.267162Z", "iopub.status.idle": "2024-07-10T21:17:52.942184Z", "shell.execute_reply": "2024-07-10T21:17:52.940941Z", "shell.execute_reply.started": "2024-07-10T21:17:52.268886Z" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "412 sessions found with ecog data\n" ] } ], "source": [ "# Filter on recordings with neural data\n", "filter_fn = db.filter_has_neural_data('ecog')\n", "sessions = db.lookup_sessions(subject='affi', task_desc='resting data', experimenter='leo', filter_fn=filter_fn)\n", "print(f\"{len(sessions)} sessions found with ecog data\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2023-07-17T19:52:42.363253Z", "start_time": "2023-07-17T19:52:39.855627Z" }, "execution": { "iopub.execute_input": "2024-07-10T21:17:53.339014Z", "iopub.status.busy": "2024-07-10T21:17:53.337488Z", "iopub.status.idle": "2024-07-10T21:18:00.979077Z", "shell.execute_reply": "2024-07-10T21:18:00.977717Z", "shell.execute_reply.started": "2024-07-10T21:17:53.338940Z" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "197 sessions found with 250ms rewards\n" ] } ], "source": [ "# Filter specific parameters\n", "filter_fn = lambda x: x.get_task_param('reward_time') == 0.25\n", "sessions = db.lookup_sessions(subject='affi', filter_fn=filter_fn)\n", "print(f\"{len(sessions)} sessions found with 250ms rewards\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2023-07-17T19:52:42.831884Z", "start_time": "2023-07-17T19:52:42.781519Z" }, "execution": { "iopub.execute_input": "2024-07-10T21:18:00.980964Z", "iopub.status.busy": "2024-07-10T21:18:00.980734Z", "iopub.status.idle": "2024-07-10T21:18:01.020791Z", "shell.execute_reply": "2024-07-10T21:18:01.019744Z", "shell.execute_reply.started": "2024-07-10T21:18:00.980941Z" }, "scrolled": true, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('beignet', 'beignet', 'beignet', 'beignet') (9609, 9631, 9638, 9667) (datetime.date(2023, 6, 5), datetime.date(2023, 6, 6), datetime.date(2023, 6, 8), datetime.date(2023, 6, 9))\n" ] } ], "source": [ "# Or use a shortcut\n", "sessions = db.lookup_mc_sessions(subject=subject, date=('2023-06-05', '2023-06-10'))\n", "subjects, ids, dates = db.list_entry_details(sessions)\n", "print(subjects, ids, dates)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Finding a range of dates" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2023-07-17T19:52:44.496258Z", "start_time": "2023-07-17T19:52:44.470187Z" }, "execution": { "iopub.execute_input": "2024-07-10T21:18:02.314040Z", "iopub.status.busy": "2024-07-10T21:18:02.312248Z", "iopub.status.idle": "2024-07-10T21:18:02.342179Z", "shell.execute_reply": "2024-07-10T21:18:02.341070Z", "shell.execute_reply.started": "2024-07-10T21:18:02.313961Z" }, "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8692f73cc35645f5ab31cc31bdecb175", "version_major": 2, "version_minor": 0 }, "text/plain": [ "DatePicker(value=None, description='Start date', step=1)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "e5c60185a8ca47a0b3ebc91b60004671", "version_major": 2, "version_minor": 0 }, "text/plain": [ "DatePicker(value=None, description='End date', step=1)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "start = widgets.DatePicker(\n", " description='Start date',\n", " disabled=False\n", ")\n", "end = widgets.DatePicker(\n", " description='End date',\n", " disabled=False\n", ")\n", "display(start)\n", "display(end)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2023-07-17T19:53:01.858299Z", "start_time": "2023-07-17T19:53:01.745774Z" }, "execution": { "iopub.execute_input": "2024-07-10T21:18:34.589073Z", "iopub.status.busy": "2024-07-10T21:18:34.587359Z", "iopub.status.idle": "2024-07-10T21:18:34.751955Z", "shell.execute_reply": "2024-07-10T21:18:34.750813Z", "shell.execute_reply.started": "2024-07-10T21:18:34.588996Z" }, "scrolled": true, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "101 matching recordings\n" ] } ], "source": [ "# Might want to do some additional filtering by date\n", "subject = 'beignet'\n", "task = 'bmi control'\n", "project = 'linear map'\n", "entries = db.lookup_sessions(subject=subject, task_name=task, date=(start.value, end.value), project=project)\n", "print(f\"{len(entries)} matching recordings\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Select from a dropdown menu" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2023-07-17T19:53:04.033335Z", "start_time": "2023-07-17T19:53:03.921534Z" }, "execution": { "iopub.execute_input": "2024-07-10T21:18:37.645625Z", "iopub.status.busy": "2024-07-10T21:18:37.643616Z", "iopub.status.idle": "2024-07-10T21:18:37.813796Z", "shell.execute_reply": "2024-07-10T21:18:37.812629Z", "shell.execute_reply.started": "2024-07-10T21:18:37.645549Z" }, "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "80ad680daa424049a31054552bf5c755", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Dropdown(options=(2022-06-06 13:44:42.434000: beignet on bmi control task, id=5512, 2022-06-06 13:45:31.687000…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "entry_drop = widgets.Dropdown(options=entries)\n", "display(entry_drop)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2023-07-17T19:53:06.309237Z", "start_time": "2023-07-17T19:53:06.302092Z" }, "execution": { "iopub.execute_input": "2024-07-10T21:18:38.755685Z", "iopub.status.busy": "2024-07-10T21:18:38.754400Z", "iopub.status.idle": "2024-07-10T21:18:38.763185Z", "shell.execute_reply": "2024-07-10T21:18:38.761629Z", "shell.execute_reply.started": "2024-07-10T21:18:38.755613Z" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2022-06-06\n", "2022-06-06 13:44:42.434000\n" ] } ], "source": [ "e = entry_drop.value\n", "print(e.date)\n", "print(e.datetime)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Select anything" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2023-07-17T19:53:11.922516Z", "start_time": "2023-07-17T19:53:11.316184Z" }, "execution": { "iopub.execute_input": "2024-07-10T21:18:42.734010Z", "iopub.status.busy": "2024-07-10T21:18:42.732294Z", "iopub.status.idle": "2024-07-10T21:18:43.225659Z", "shell.execute_reply": "2024-07-10T21:18:43.224876Z", "shell.execute_reply.started": "2024-07-10T21:18:42.733936Z" }, "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "812a1689d5af4f20b922e4e0a344bb8d", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Dropdown(description='subject', options=('affi', 'churro', 'test'), value='affi'), Dropd…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "start_date = widgets.DatePicker(value = datetime.today() - timedelta(days=30))\n", "end_date = widgets.DatePicker(value = datetime.today())\n", "\n", "# Filter by task and subject\n", "entries = db.lookup_sessions(date=(start_date.value, end_date.value))\n", "subjects = np.unique([e.subject for e in entries])\n", "tasks = np.unique([e.task_name for e in entries])\n", "\n", "subj_drop = widgets.Dropdown(options=subjects)\n", "task_drop = widgets.Dropdown(options=tasks) \n", "entry_select = widgets.SelectMultiple(\n", " options=[],\n", " description='entries',\n", " disabled=False\n", ")\n", "entry_select.layout.width = '600px'\n", "\n", "def update_entries(*args):\n", " entries = db.lookup_sessions(\n", " subject=subj_drop.value, \n", " task_name=task_drop.value,\n", " date=(start_date.value,end_date.value)\n", " )\n", " entry_select.options = [\"{} - {}: {}\".format(e.id, e.task_desc, e.date) for i, e in enumerate(entries)]\n", " \n", "start_date.observe(update_entries)\n", "end_date.observe(update_entries)\n", "subj_drop.observe(update_entries)\n", "task_drop.observe(update_entries)\n", "\n", "def select_entry(subject, task, start_date, end_date, entries):\n", " ids = [int(re.search(r'\\d+', e).group()) for e in entries]\n", " return db.lookup_sessions(id=ids)\n", "\n", "\n", "selection = interactive(select_entry,\n", " subject=subj_drop,\n", " task=task_drop,\n", " start_date=start_date,\n", " end_date=end_date,\n", " entries=entry_select)\n", "\n", "display(selection)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2023-07-17T19:53:15.618678Z", "start_time": "2023-07-17T19:53:15.610971Z" }, "execution": { "iopub.execute_input": "2024-07-10T21:18:51.644850Z", "iopub.status.busy": "2024-07-10T21:18:51.643187Z", "iopub.status.idle": "2024-07-10T21:18:51.653220Z", "shell.execute_reply": "2024-07-10T21:18:51.651720Z", "shell.execute_reply.started": "2024-07-10T21:18:51.644777Z" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 selected\n" ] } ], "source": [ "entries = selection.result\n", "print(f\"{len(entries)} selected\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loading the data" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "ExecuteTime": { "end_time": "2023-07-17T19:53:21.093240Z", "start_time": "2023-07-17T19:53:20.942158Z" }, "execution": { "iopub.execute_input": "2024-07-10T21:18:53.725353Z", "iopub.status.busy": "2024-07-10T21:18:53.723661Z", "iopub.status.idle": "2024-07-10T21:18:53.850470Z", "shell.execute_reply": "2024-07-10T21:18:53.849215Z", "shell.execute_reply.started": "2024-07-10T21:18:53.725279Z" }, "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "89f3c1330bd544afa79337e8a180ec05", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/1 [00:00