Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

Browse Source

Subir archivos a 'Code'

Here I add the notebooks for the obtention of power spectra ("get_power_spectra") and the prediction of task engagement states from the spectra ("Spectral_Analysis").
Pau Boncompte Carré 3 months ago
parent
commit
eb5bcc9543
2 changed files with 1560 additions and 0 deletions
  1. 1296 0
      Code/Spectral_Analysis.ipynb
  2. 264 0
      Code/get_power_spectra.ipynb

File diff suppressed because it is too large
+ 1296 - 0
Code/Spectral_Analysis.ipynb


+ 264 - 0
Code/get_power_spectra.ipynb

@@ -0,0 +1,264 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "## GET POWER SPECTRA"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "<module 'LFP_functions' from 'e:\\\\BT_Code\\\\LFP_functions.py'>"
+      ]
+     },
+     "execution_count": 1,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt\n",
+    "import pickle\n",
+    "import xarray as xr\n",
+    "import pandas as pd\n",
+    "import sys, os\n",
+    "\n",
+    "import importlib\n",
+    "import LFP_functions\n",
+    "importlib.reload(LFP_functions)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# Disable\n",
+    "def blockPrint():\n",
+    "    sys.stdout = open(os.devnull, 'w')\n",
+    "\n",
+    "# Restore\n",
+    "def enablePrint():\n",
+    "    sys.stdout = sys.__stdout__"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "We import the lfps for all sessions and save them on the same dictionaries"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Loaded data for session 1\n",
+      "Loaded data for session 2\n",
+      "Loaded data for session 3\n",
+      "Loaded data for session 4\n",
+      "Loaded data for session 5\n",
+      "Loaded data for session 6\n",
+      "Loaded data for session 7\n",
+      "Loaded data for session 8\n",
+      "Loaded data for session 9\n",
+      "Loaded data for session 10\n",
+      "Loaded data for session 11\n",
+      "Loaded data for session 12\n",
+      "Loaded data for session 13\n",
+      "Loaded data for session 14\n",
+      "Loaded data for session 15\n",
+      "Loaded data for session 16\n"
+     ]
+    }
+   ],
+   "source": [
+    "import pickle\n",
+    "\n",
+    "# Initialize the dictionaries to store the loaded data\n",
+    "lfps_act = {}\n",
+    "lfps_pas = {}\n",
+    "chans = {}\n",
+    "\n",
+    "\n",
+    "for i in range(1,17):\n",
+    "    # Format the file names with the current number\n",
+    "    act_file = f'aligned_LFP_area/aligned_lfps_act_en_V1_{i}.pickle'\n",
+    "    pas_file = f'aligned_LFP_area/aligned_lfps_pas_en_V1_{i}.pickle'\n",
+    "    chan_file = f'channels/chans_V1_{i}.pickle'\n",
+    "    \n",
+    "    # Open and load the data from the files\n",
+    "    with open(act_file, 'rb') as f:\n",
+    "        lfps_act[i] = pickle.load(f)\n",
+    "    with open(pas_file, 'rb') as f:\n",
+    "        lfps_pas[i] = pickle.load(f)\n",
+    "    with open(chan_file, 'rb') as f:\n",
+    "        chans[i] = pickle.load(f)\n",
+    "    print(f'Loaded data for session {i}')\n",
+    "\n",
+    "#open the images file\n",
+    "with open('images.pickle', 'rb') as f:\n",
+    "    images = pickle.load(f)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "from multitaper import mtspec\n",
+    "\n",
+    "def get_spectra(lfps,pres):\n",
+    "    '''\n",
+    "    given a dictionary of lfps, return the power spectra in the same format,\n",
+    "    where we first have the sessions, then the images, and for each combination there's a 3d xarray\n",
+    "    containing the power spectra for each channel and presentation id\n",
+    "\n",
+    "    INPUTS:\n",
+    "        lfps: a dictionary of lfps, where the first key is the session, the second key is the image, and the value is an xarray\n",
+    "        containing the lfp data for that session and image\n",
+    "        pres: the image onset for the novel image (1 if it is the first onset, 2 if it is the second, etc)\n",
+    "    \n",
+    "    OUTPUTS:\n",
+    "        lfps_spectra: the dictionary of lfps\n",
+    "    '''\n",
+    "    blockPrint()\n",
+    "    lfps_spectra = {}\n",
+    "    for i in lfps.keys():\n",
+    "        lfps_spectra[i] = {}\n",
+    "        for j in lfps[i].keys():\n",
+    "            # we are now inside the aligned lfp of a session i and image j\n",
+    "            # we will now create another xarray to store the spectrogram\n",
+    "            lfp = lfps[i][j]\n",
+    "            lfps_spectra[i][j] = get_powers(lfp,pres)\n",
+    "    enablePrint()\n",
+    "    return lfps_spectra\n",
+    "             \n",
+    "\n",
+    "def get_powers(lfp,pres):\n",
+    "    '''\n",
+    "    given a lfp for a specific image and session, return the power spectrum for each channel\n",
+    "    and presentation id in the form of an xarray\n",
+    "\n",
+    "    INPUTS:\n",
+    "        lfp: an xarray containing the lfp data for a specific image and session\n",
+    "        pres: the image onset for the novel image (1 if it is the first onset, 2 if it is the second, etc)\n",
+    "    '''\n",
+    "\n",
+    "    #set start and end times\n",
+    "    ti, tf = LFP_functions.pres_times(pres)\n",
+    "    lfp1 = lfp.sel(time_from_presentation_onset=slice(ti,tf))\n",
+    "    #energy normalization for each presentation\n",
+    "    for pres in lfp1.presentation_id.values:\n",
+    "        lfp1.loc[{'presentation_id':pres}] = LFP_functions.energy_normalization(lfp1.sel(presentation_id=pres))\n",
+    "    presentation_ids = lfp1.presentation_id.values.tolist()\n",
+    "    channels = lfp1.channel.values.tolist()\n",
+    "\n",
+    "\n",
+    "    for i,pres in enumerate(presentation_ids): #for every presentation ID\n",
+    "        for j,c in enumerate(channels): #for every channel\n",
+    "            #obtain the spectrogram for the current channel and presentation id\n",
+    "            _,sigspec,lfpspecs,_=mtspec.spectrogram(lfp_res.isel(channel=j,presentation_id=i).values, 1/500, (tf-ti)-1/500, olap=0, nw=1, kspec=2, fmin=0, fmax=100, iadapt=0)\n",
+    "            if i==0 and j==0:\n",
+    "                #create the 3d numpy array to store the spectrogram\n",
+    "                data = np.zeros((len(channels),len(presentation_ids),sigspec.shape[0]))\n",
+    "                dims = ['channel','presentation_id','frequency']\n",
+    "            data[j,i,:] = lfpspecs[:,0]\n",
+    "\n",
+    "    #create the xarray\n",
+    "    da = xr.DataArray(data,\n",
+    "        coords=[channels,presentation_ids,sigspec[:,0]],\n",
+    "        dims=['channel','presentation_id','frequency'])\n",
+    "    return da"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "When specifying pres=1, we refer to the first presentation of the images (0-250ms from the onset), whilst with pres=0, we obtain the power spectra of the baseline (250-0ms before the onset)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "spectra_act_1 = get_spectra(lfps_act,1)\n",
+    "spectra_act_0 = get_spectra(lfps_act,0)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "with open('power_spectra/spectra_act_1.pickle', 'wb') as f:\n",
+    "    pickle.dump(spectra_act_1, f)\n",
+    "with open('power_spectra/spectra_act_0.pickle', 'wb') as f:\n",
+    "    pickle.dump(spectra_act_0, f)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 8,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "spectra_pas_1 = get_spectra(lfps_pas,1)\n",
+    "spectra_pas_0 = get_spectra(lfps_pas,0)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "#save the power spectra\n",
+    "with open('power_spectra/spectra_pas_1.pickle', 'wb') as f:\n",
+    "    pickle.dump(spectra_pas_1, f)\n",
+    "with open('power_spectra/spectra_pas_0.pickle', 'wb') as f:\n",
+    "    pickle.dump(spectra_pas_0, f)\n"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "allensdk",
+   "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.18"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}