= Spike Sorting in Brainstorm = ''Author: Konstantinos Nasiotis'' <> {{attachment:spikesortingIntro.png||width="700"}} Spike sorting is an essential step in electrophysiology that provides information on the selectivity of individual neurons. The common practice that most spike sorters use, is to apply a threshold on a band-passed version of the raw signals, collect a few samples of the data around that threshold crossing, and then cluster those waveforms based on their shape. Multiple clustered shapes picked up from the same electrode would signify the presence of multiple neurons. This automatic clustering (unsupervised), although powerful, can often be inaccurate. Therefore, a manual inspection of the clusters is required. After the unsupervised step, users normally manually approve or modify the clusters created (Supervised step). For more general information, users can consult [[http://www.scholarpedia.org/article/Spike_sorting|this non-technical article on Spike Sorting]].Our goal was to allow users to perform both steps (unsupervised-supervised) within Brainstorm. There are currently several solutions for spike sorting and since we cannot accommodate all of them, we have embedded 3 widely used algorithms within Brainstorm – Waveclus, UltraMegaSort2000 and Kilosort.We understand that some users might not feel comfortable learning how to use a new spike sorter from scratch, therefore we created this page, so they can still use their spike sorter of preference (outside of Brainstorm), and later import the spiking events in Brainstorm using a compatible format.The next section shows how the users can perform the Unsupervised and Supervised steps of spike sorting, with the algorithms embedded within Brainstorm. == Unsupervised Spike Sorting == {{attachment:ssUnsupervised.png||width="700"}} During the unsupervised spike-sorting step, the spike sorter assigns the spiking activity to individual neurons. When the unsupervised spike-sorting is completed, new events will be created and linked to the raw file taken from the acquisition system. These events will be concatenated to the existing ones (button presses, trial onset etc.), or substitute __the spiking-events__ that the acquisition system automatically assigned. There is a collection of parameters that each spike sorter uses. In case a user need to modify them, we have enabled this option on the window that pops-up before they are used. {{attachment:ssWindow.png}} Once the unsupervised spike-sorting step is complete, a new set of events will be concatenated to the existing ones. Neuronal activity is assigned on each electrode based on the clustering that the spike-sorter performed. ||'''The following convention for the spiking events will be used throughout this toolbox:''' “Spikes Channel ChannelLabel”, where ChannelLabel is the Label of the electrode assigned by the acquisition system. If the acquisition system assigned the spikes’ waveforms into more than one cluster/neuron during this automatic clustering, the neuronal events would be (e.g. for an electrode that picked up spikes from 3 neurons): “Spikes Channel ChannelLabel |1|”, “Spikes Channel ChannelLabel |2|”, “Spikes Channel ChannelLabel |3|” || The following image shows the outcome of using an unsupervised spike-sorting process. 1. A new icon (SpikeSorterNameSpike Sorting) will appear in the ytu288c-01_converted folder (WaveClus in this example). This icon is a link to the unsupervised spike-sorting files created from the spike-sorter.2. New events are concatenated to those previously imported. These new events signify the neuronal activity on each channel. On this example, the spike sorter picked up 28792 spikes from a single neuron on channel with the label AD01, 18888 spikes from a single neuron on the channel with the label AD02, whereas the channel with label AD08 contained 2 neurons with 24475 and 2812 spikes respectively etc. {{attachment:ssShowSpikes.png}} For users that are interested to locate the outputs of the spike sorters, those files are placed in a folder with the suffix (“_Spikes”) within the database. The following section shows the windows with the different user selection options for each spike-sorter. At the end of each spike-sorter users can find the citation for each one. === WaveClus === When WaveClus is selected as the spike sorter, the following window appears: {{attachment:WaveclusWindow.png}} : There is a demultiplexing process that separates the electrodes into separate files in the background. '''The larger the RAM selected is, the faster this process will be performed. Make sure a high portion of your Computer’s RAM is selected.'''Activate the parallel toolbox and run processes in parallel. Greatly improves the speed of the process (requires Matlab’s parallel toolbox).C. Create Images: The waveclus algorithm allows saving of a high-res image of the clusters created. However it affects the speed of the process. Before the algorithm is applied on the signals, each channel is demultiplexed on separate files. These will be located inside the tmp folder.Specifically for the tutorial, this folder would be:~tmp/Tutorial_e-Phys/Floyd/@rawytu288c-01_converted/data_0raw_ytu288c-01_converted Neural Comput. 16, 1661–1687 (2004) === UltraMegaSort2000 === When the UltraMegaSort2000 spike sorter is selected, the following window appears: {{attachment:UMSWindow.png}} During this process, the user can select:A. Maximum RAM to use: B. Parallel Processing: Activate the parallel toolbox and run processes in parallel. Greatly improves the speed of the process (requires Matlab’s parallel toolbox).C. Filtering parameters: The RAW signals need to be bandpass filtered before given as an input to the spike sorter. The two fields allow the user to select the Lower and Upper cutoff frequencies respectively.D. Options: The parameters of the UltraMegasort2000 algorithm. Before the algorithm is applied on the signals, each channel is demultiplexed on separate files. These will be located inside the tmp folder. · Hill, D.N., Mehta, S. & Kleinfeld, D. J. Neurosci. 31, 8699–8705 (2011).· Fee, M.S., Mitra, P.P. & Kleinfeld, D. Automatic sorting of multiple unit neuronal signals in the presence of anisotropic and non-Gaussian variability. J. Neurosci. Methods 69, 175–188 (1996). === Kilosort === The KiloSort algorithm is intended for high-density multi-electrode arrays/probes, where the same neuron can potentially be recorded from multiple electrodes. In order to follow the convention that we have kept through all the spike-sorters, once the spike-sorting procedure is complete, each neuron is assigned to a single electrode, according to which electrode the waveforms of this neuron had highest amplitude. {{attachment:KilosortWindow.png}} During this process, the user can select:A. Parallel Processing: B. GPU processing: Activate the GPU processing.C. Samplesize for appending: D. Parameters: The parameters of the KiloSort algorithm. Before the algorithm is applied on the signals, the raw signal is converted into a .dat file to be used as an input to the KiloSort function. This file can be located inside the tmp folder.· Pachitariu M, Steinmetz NA, Kadir S, Carandini M and Harris KD (2016). Fast and accurate spike sorting of high-channel count probes with Kilosort. Advances In Neural Information Processing Systems. 4448-4456 == Supervised Spike Sorting == {{attachment:ssSupervised.png||width="700"}} During the supervised spike-sorting step, the user inspects the clustering outputs of the spike sorters and modifies them. At the time this tutorial is being written, we have integrated the graphical user interface (GUI) of WaveClus and UltraMegaSort2000 in Brainstorm. Users can use the GUI of these spike sorters and every modification they perform in the clusters, automatically updates the events in the database of the raw file it corresponds to.To open the supervised spike-sorting process, users can either double-click on the Spike Sortingicon, or drag and drop it to the process box, click Run->Electrophysiology->Supervised Spike Sorting. The GUI that will open will correspond to the sorter that performed the unsupervised-spike-sorting (WaveClus or UltraMegaSort2000).The figure below shows what the main window will look like once the supervised-spike sorter gets activated. A new tab (“Spikes”) appears, and a drop-down gets activated with the label of the electrode that is currently open on the spike sorter. The Save and Next button saves the current modification/settings on the spike sorter for the selected electrode from the drop-down, and moves to the next channel on the list that picked up spikes.If supervised-spike-sorting has already been performed on the selected electrode, a star (*) symbol will appear at the end of the label on the drop-down. This will be useful in case supervised-spike-sorting was not completed in one shot and users went back to the file to finish it, without wasting time spike-sorting the same channels all over again. '''IMPORTANT'''''': Saving SHOULD NOT be performed through the spike-sorter's GUI. Only with the Save and Next button on the main Brainstorm window.''' {{attachment:ssOutput.png}} === WaveClus === A tutorial and information on how to use the WaveClus spike sorter can be found at: [[https://www2.le.ac.uk/centres/csn/research-2/spike-sortingIn|https://www2.le.ac.uk/centres/csn/research-2/spike-sorting]] In brief, the top raw is just a filtered version of the raw signal (spiking activity can already be seen). The red line running along this 60seconds segment represents the threshold applied for the spike-sorting.In the example below, there are two neurons clustered on this electrode. Neuron 1 (Cluster 1) elicited 3,277 spikes and Neuron 2 (cluster 2) 1078 spikes. Users can either accept (default), reject (cluster it as Noise (Cluster 0)), or modify (change temperature) the current clusters.Once all the modifications are made, the user needs to press Save and Next on the main Brainstorm window. {{attachment:WaveclusSupervised.png||width="700"}} · Quian Quiroga, R., Nadasdy, Z. &Ben-Shaul, Y. Unsupervised spike detection and sorting with wavelets and super-paramagnetic clustering. Neural Comput. 16, 1661–1687 (2004) === UltraMegaSort2000 === {{attachment:UMSSupervised.png||width="700"}} == Spike sorting outside of Brainstorm == In case you have data that have already been spike-sorted, you can use the spike-timestamps as separate events for each electrode, and subsequently import the events from a single events file into Brainstorm. A tutorial on how to convert the events to a Brainstorm-compatible format can be found [[https://neuroimage.usc.edu/brainstorm/e-phys/ConvertToBrainstormEvents|here]].