{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The Unitary Events Analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The executed version of this tutorial is at https://elephant.readthedocs.io/en/latest/tutorials/unitary_event_analysis.html\n", "\n", "The Unitary Events (UE) analysis \\[1\\] tool allows us to reliably detect correlated spiking activity that is not explained by the firing rates of the neurons alone. It was designed to detect coordinated spiking activity that occurs significantly more often than predicted by the firing rates of the neurons. The method allows one to analyze correlations not only between pairs of neurons but also between multiple neurons, by considering the various spike patterns across the neurons. In addition, the method allows one to extract the dynamics of correlation between the neurons by perform-ing the analysis in a time-resolved manner. This enables us to relate the occurrence of spike synchrony to behavior.\n", "\n", "The algorithm:\n", "\n", "1. Align trials, decide on width of analysis window.\n", "2. Decide on allowed coincidence width.\n", "3. Perform a sliding window analysis. In each window:\n", " 1. Detect and count coincidences.\n", " 2. Calculate expected number of coincidences.\n", " 3. Evaluate significance of detected coincidences.\n", " 4. If significant, the window contains Unitary Events.\n", "4. Explore behavioral relevance of UE epochs.\n", "\n", "References:\n", "\n", "1. GrĂ¼n, S., Diesmann, M., Grammont, F., Riehle, A., & Aertsen, A. (1999). Detecting unitary events without discretization of time. Journal of neuroscience methods, 94(1), 67-79." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-07-18T08:56:30.663173Z", "start_time": "2018-07-18T08:56:29.825521Z" } }, "outputs": [], "source": [ "import random\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import quantities as pq\n", "import neo\n", "\n", "import elephant.unitary_event_analysis as ue\n", "\n", "# Fix random seed to guarantee fixed output\n", "random.seed(1224)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we download a data file containing spike train data from multiple trials of two neurons." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-07-18T08:56:32.142189Z", "start_time": "2018-07-18T08:56:31.420462Z" } }, "outputs": [], "source": [ "# Download data\n", "!wget -Nq https://github.com/INM-6/elephant-tutorial-data/raw/master/dataset-1/dataset-1.h5" ] }, { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "# Write a plotting function" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-07-18T08:56:32.920355Z", "start_time": "2018-07-18T08:56:32.611208Z" }, "nbsphinx": "hidden" }, "outputs": [], "source": [ "def plot_UE(data,Js_dict,Js_sig,binsize,winsize,winstep, pat,N,t_winpos,**kwargs):\n", " \"\"\"\n", " Examples:\n", " ---------\n", " dict_args = {'events':{'SO':[100*pq.ms]},\n", " 'save_fig': True,\n", " 'path_filename_format':'UE1.pdf',\n", " 'showfig':True,\n", " 'suptitle':True,\n", " 'figsize':(12,10),\n", " 'unit_ids':[10, 19, 20],\n", " 'ch_ids':[1,3,4],\n", " 'fontsize':15,\n", " 'linewidth':2,\n", " 'set_xticks' :False'}\n", " 'marker_size':8,\n", " \"\"\"\n", " import matplotlib.pylab as plt\n", " t_start = data[0][0].t_start\n", " t_stop = data[0][0].t_stop\n", "\n", "\n", " arg_dict = {'events':{},'figsize':(12,10), 'top':0.9, 'bottom':0.05, 'right':0.95,'left':0.1,\n", " 'hspace':0.5,'wspace':0.5,'fontsize':15,'unit_ids':range(1,N+1,1),\n", " 'ch_real_ids':[],'showfig':False, 'lw':2,'S_ylim':[-3,3],\n", " 'marker_size':8, 'suptitle':False, 'set_xticks':False,\n", " 'save_fig':False,'path_filename_format':'UE.pdf'}\n", " arg_dict.update(kwargs)\n", " \n", " num_tr = len(data)\n", " unit_real_ids = arg_dict['unit_ids']\n", " \n", " num_row = 5\n", " num_col = 1\n", " ls = '-'\n", " alpha = 0.5\n", " plt.figure(1,figsize = arg_dict['figsize'])\n", " if arg_dict['suptitle'] == True:\n", " plt.suptitle(\"Spike Pattern:\"+ str((pat.T)[0]),fontsize = 20)\n", " print('plotting UEs ...')\n", " plt.subplots_adjust(top=arg_dict['top'], right=arg_dict['right'], left=arg_dict['left']\n", " , bottom=arg_dict['bottom'], hspace=arg_dict['hspace'] , wspace=arg_dict['wspace'])\n", " ax = plt.subplot(num_row,1,1)\n", " ax.set_title('Unitary Events',fontsize = arg_dict['fontsize'],color = 'r')\n", " for n in range(N):\n", " for tr,data_tr in enumerate(data):\n", " plt.plot(data_tr[n].rescale('ms').magnitude, np.ones_like(data_tr[n].magnitude)*tr + n*(num_tr + 1) + 1, '.', markersize=0.5,color = 'k')\n", " sig_idx_win = np.where(Js_dict['Js']>= Js_sig)[0]\n", " if len(sig_idx_win)>0:\n", " x = np.unique(Js_dict['indices']['trial'+str(tr)])\n", " if len(x)>0:\n", " xx = []\n", " for j in sig_idx_win:\n", " xx =np.append(xx,x[np.where((x*binsize>=t_winpos[j]) &(x*binsize