Loading SpikeSorting Data

Spikesorted output of electrophysiology data.

Relevant Alf objects

  • channels

  • clusters

  • spikes


from one.api import ONE
from brainbox.io.one import SpikeSortingLoader
one = ONE(base_url='https://openalyx.internationalbrainlab.org')
pid = 'da8dfec1-d265-44e8-84ce-6ae9c109b8bd'
sl = SpikeSortingLoader(pid=pid, one=one)
spikes, clusters, channels = sl.load_spike_sorting()
clusters = sl.merge_clusters(spikes, clusters, channels)

Alternatively, one can instantiate the spike sorting loader using the session unique identifier eid and the probe name pname:

eid, pname = one.pid2eid(pid)
sl = SpikeSortingLoader(eid=eid, pname=pname, one=one)
spikes, clusters, channels = sl.load_spike_sorting()
clusters = sl.merge_clusters(spikes, clusters, channels)

More details

Important information

  1. Data sorted with pykilosort is loaded by default. If the pykilosort spikesorting data is not available, the matlab kilosort 2.5 version will be loaded. See Example 1 for more information.

  2. The channel locations in the brain can come from several sources. it will load the most advanced version of the histology available, regardless of the spike sorting version loaded. The steps, from most to least advanced, are:

    • alf: the final version of channel locations, same as resolved with the difference that data has been written out to files

    • resolved: channel location alignments have been agreed upon

    • aligned: channel locations have been aligned, but review or other alignments are pending, potentially not accurate

    • traced: the histology track has been recovered from microscopy, however the depths may not match, inacurate data

  3. The attributes mlapdv, atlas_ids and acronyms in the clusters and channels objects are only available for probe insertions where sl.histology is equal to traced, aligned, resolved or alf.

  4. The cluster and channel locations in the brain are only considered final for probe insertions with sl.histology='resolved'or sl.histology='alf'.

Useful modules

Exploring spikesorting data

Example 1: Loading different spikesorting versions

# By default, if available, the data spikesorted with pykilosort is loaded.
# To find the spikesorting version that is loaded we can use

# To see all available spikesorted data for this probe insertion we can list the collections.
# N.B. ks2.5 matlab spikesorted data is stored in the alf/probe00 folder

# The following can be used to load a specific version of spikesorting

# pykilosort version
spikes, clusters, channels = sl.load_spike_sorting(spike_sorter='pykilosort')

# ks2.5 matlab version
spikes, clusters, channels = sl.load_spike_sorting(spike_sorter='')

Example 2: Loading additional data

# The default spikes and cluster attributes loaded are:
# spikes - amps, clusters, depths, times
# cluster - channels, depths, metrics

#Other attributes can additionally be loaded in the following way
spikes, clusters, channels = sl.load_spike_sorting(dataset_types=['clusters.amps', 'spikes.samples'])
clusters = sl.merge_clusters(spikes, clusters, channels)

Example 3: Compute firing rate across session

from brainbox.ephys_plots import image_fr_plot
from iblutil.numerical import bincount2D
import numpy as np

time_bin = 0.05 # time bin in seconds
depth_bin = 10 # depth bin in um

# Remove any nan values
kp_idx = np.bitwise_and(~np.isnan(spikes['times']), ~np.isnan(spikes['depths']))

fr, time, depth = bincount2D(spikes['times'][kp_idx], spikes['depths'][kp_idx], time_bin, depth_bin)

Example 4: Find clusters labelled as good

good_clusterIDs = clusters['cluster_id'][clusters['label'] == 1]

Example 5: Plot a raster for all units

sl.raster(spikes, channels)
(<Figure size 1600x900 with 4 Axes>,
 array([[<Axes: title={'center': '2020-09-21_1_SWC_043_probe00, None \n17_575_242 spikes, 914 clusters'}>,
         <Axes: >],
        [<Axes: xlabel='time (secs)', ylabel='depth (um)'>,
         <Axes: title={'center': 'alf'}>]], dtype=object))

Other relevant examples