{ "cells": [ { "cell_type": "markdown", "id": "2f5bab82", "metadata": {}, "source": [ "# Saving kinematics and neural data segments" ] }, { "cell_type": "code", "execution_count": 1, "id": "4c3a7350-83d2-4a59-9ac9-54f9c342e61c", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/aolab/miniconda3/envs/leo-analysis/lib/python3.9/site-packages/statsmodels/tools/_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.\n", " import pandas.util.testing as tm\n" ] } ], "source": [ "import aopy\n", "from aopy.data import db\n", "import seaborn as sns\n", "import pickle" ] }, { "cell_type": "code", "execution_count": 2, "id": "8f74ba62-93f7-4b0a-9cb1-7bb601054b29", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-e git+https://github.com/aolabNeuro/analyze.git@7e7db7fccb1639273553b9eb907cac36eff6a466#egg=aolab_aopy\n", "aopy==0.6.0\n" ] } ], "source": [ "# Version of aopy used: \n", "!pip freeze | grep aopy" ] }, { "cell_type": "markdown", "id": "c88fb06d", "metadata": {}, "source": [ "## Define what to save" ] }, { "cell_type": "code", "execution_count": 8, "id": "b0bd4c2c-92bf-4447-9c3c-985fccc34dc4", "metadata": {}, "outputs": [], "source": [ "def save_kinematics_and_lfp(preproc_dir, subject, date_range):\n", " entries = db.lookup_mc_sessions(subject=subject, date=date_range)\n", " subjects, ids, dates = db.list_entry_details(entries)\n", " df = aopy.data.tabulate_behavior_data_center_out(preproc_dir, subjects, ids, dates)\n", " \n", " # Choose just the trials with completed reach segments\n", " df_subset = df[df['reach_completed']].reset_index()\n", " \n", " # Get the reach segments kinematics\n", " kin_segments = aopy.data.bmi3d.tabulate_kinematic_data(\n", " preproc_dir, df_subset['subject'], df_subset['te_id'], df_subset['date'], \n", " df_subset['go_cue_time'], df_subset['reach_end_time'], datatype='cursor'\n", " )\n", " \n", " target_idx = df_subset['target_idx'].to_numpy()\n", " aopy.visualization.color_trajectories(kin_segments, target_idx, sns.color_palette(n_colors=9))\n", " \n", " # Choose some data around go cue\n", " lfp_segments, samplerate = aopy.data.tabulate_ts_segments(\n", " preproc_dir, df_subset['subject'], df_subset['te_id'], df_subset['date'], \n", " df_subset['go_cue_time'], df_subset['reach_end_time'], datatype='lfp'\n", " )\n", " \n", " _, acq_ch, _ = aopy.data.load_chmap()\n", " \n", " # Save as pickle file for each subject:\n", " # kin_segments - list of (nt, 2) x, y cursor positions\n", " # lfp_segments - list of (nt, 240) ecog data from 240 array over motor cortex\n", " # samplerate - sampling rate of both data\n", " filename = f'{subject}_{date_range[0]}_{date_range[1]}_kinematics_and_lfp.pkl'\n", " write_dir = './'\n", " data_dict = {\n", " 'kin_segments': kin_segments, \n", " 'lfp_segments': [seg[:,acq_ch-1] for seg in lfp_segments], \n", " 'samplerate': samplerate\n", " }\n", " aopy.data.pkl_write(filename, data_dict, write_dir)" ] }, { "cell_type": "code", "execution_count": 9, "id": "f8280038-6f17-46b0-abfd-ccdcba574d26", "metadata": {}, "outputs": [], "source": [ "# 'beignet': ex_date_list = [(2022,6,28)]\n", "preproc_dir = '/data/preprocessed'\n", "subject = 'beignet'\n", "date_range = ('2022-06-28', '2022-06-29')\n" ] }, { "cell_type": "markdown", "id": "a20c9ade", "metadata": {}, "source": [ "## Save data" ] }, { "cell_type": "code", "execution_count": 10, "id": "8c1c7584-2da9-4a44-a187-85915ddbc490", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "1e7539910e3a4426a8a056ca6d4794e8", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/1 [00:00" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "save_kinematics_and_lfp(preproc_dir, subject, date_range)" ] }, { "cell_type": "code", "execution_count": 48, "id": "679ff276-9657-4590-bfab-89013dd810ec", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "32fb9d890a6541c78367448d0966fe21", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/17 [00:00" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "# For Affi: ex_date_list = [(2024,1,2),(2024,1,3)]\n", "subject = 'affi'\n", "date_range = ('2024-01-02', '2024-01-04')\n", "\n", "save_kinematics_and_lfp(preproc_dir, subject, date_range)" ] }, { "cell_type": "markdown", "id": "4e31dfe4", "metadata": {}, "source": [ "## Load data" ] }, { "cell_type": "code", "execution_count": null, "id": "a2de697b-ff6a-4ae5-90e3-62df95d3e0da", "metadata": {}, "outputs": [], "source": [ "# Saved as pickle file for each subject:\n", "# kin_segments - list of (nt, 2) x, y cursor positions\n", "# lfp_segments - list of (nt, 240) ecog data from 240 array over motor cortex\n", "# samplerate - sampling rate of both data" ] }, { "cell_type": "code", "execution_count": 54, "id": "2a93277c-7c0f-4851-b293-6555502e10a6", "metadata": {}, "outputs": [], "source": [ "# How to load data:\n", "file = './beignet_2022-06-28_2022-06-29_kinematics_and_lfp.pkl'\n", "with open(file, \"rb\") as f:\n", " data_dict = pickle.load(f)" ] }, { "cell_type": "code", "execution_count": 55, "id": "4d0748ee-4d80-4e08-a475-a8270e243b33", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "823\n" ] } ], "source": [ "print(len(data_dict['kin_segments']))" ] }, { "cell_type": "code", "execution_count": 56, "id": "19f0ebd1-309e-4b3d-b513-b8bf55d12e6a", "metadata": {}, "outputs": [], "source": [ "file = './affi_2024-01-02_2024-01-04_kinematics_and_lfp.pkl'\n", "with open(file, \"rb\") as f:\n", " data_dict = pickle.load(f)" ] }, { "cell_type": "code", "execution_count": 57, "id": "4da6cb55-7df0-4224-ad86-91dbbc860bf7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1739\n" ] } ], "source": [ "print(len(data_dict['kin_segments']))" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:leo-analysis]", "language": "python", "name": "conda-env-leo-analysis-py" }, "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }