brainbox.population.population

Population functions.

Code from https://github.com/cortex-lab/phylib/blob/master/phylib/stats/ccg.py by C. Rossant. Code for decoding by G. Meijer

Functions

decode

Use decoding to classify groups of trials (e.g.

lda_project

Use linear discriminant analysis to project population vectors to the line that best separates the two groups.

xcorr

Compute all pairwise cross-correlograms among the clusters appearing in spike_clusters.

xcorr(spike_times, spike_clusters, bin_size=None, window_size=None)[source]

Compute all pairwise cross-correlograms among the clusters appearing in spike_clusters.

:param : :type : param spike_times: Spike times in seconds. :param : :type : type spike_times: array-like :param : :type : param spike_clusters: Spike-cluster mapping. :param : :type : type spike_clusters: array-like :param : :type : param bin_size: Size of the bin, in seconds. :param : :type : type bin_size: float :param : :type : param window_size: Size of the window, in seconds. :param : :type : type window_size: float :param Returns an (n_clusters: :param n_clusters: :param winsize_samples) array with all pairwise: :param cross-correlograms.:

decode(spike_times, spike_clusters, event_times, event_groups, pre_time=0, post_time=0.5, classifier='bayes-multinomial', cross_validation='kfold', num_splits=5, prob_left=None, custom_validation=None, n_neurons='all', iterations=1, shuffle=False, phase_rand=False, pseudo_blocks=False)[source]

Use decoding to classify groups of trials (e.g. stim left/right). Classification is done using the population vector of summed spike counts from the specified time window. Cross-validation is achieved using n-fold cross validation or leave-one-out cross validation. Decoders can decode any number of groups. When providing the classfier with an imbalanced dataset (not the same number of trials in each group) the chance level will not be 1/groups. In that case, to compare the classification performance against change one has to either determine chance level by decoding a shuffled dataset or use the ‘auroc’ metric as readout (this metric is robust against imbalanced datasets)

Parameters
  • spike_times (1D array) – spike times (in seconds)

  • spike_clusters (1D array) – cluster ids corresponding to each event in spikes

  • event_times (1D or 2D array) – times (in seconds) of the events from the two groups, if it’s a 2D array every row will

  • event_groups (1D or 2D array) – group identities of the events, can be any number of groups, accepts integers and strings

  • pre_time (float) – time (in seconds) preceding the event times

  • post_time (float) – time (in seconds) following the event times

  • classifier (string or sklearn object) –

    which decoder to use, either input a scikit learn clf object directly or a string. When it’s a string options are (all classifiers are used with default options):

    ’bayes-multinomal’ Naive Bayes with multinomial likelihood ‘bayes-gaussian’ Naive Bayes with gaussian likelihood ‘forest’ Random forest ‘regression’ Logistic regression ‘lda’ Linear Discriminant Analysis

  • cross_validation (string of generator object) –

    which cross-validation method to use you can input the .split method of any sklearn cross-validation method or a string. If it’s a string, options are:

    ’none’ No cross-validation ‘kfold’ K-fold cross-validation ‘kfold-interleaved’ K-fold cross validation with interleaved trial selection ‘leave-one-out’ Leave out the trial that is being decoded ‘block’ Leave out the block the to-be-decoded trial is in

  • num_splits (integer) – ** only for ‘kfold’ cross-validation ** Number of splits to use for k-fold cross validation, a value of 5 means that the decoder will be trained on 4/5th of the data and used to predict the remaining 1/5th. This process is repeated five times so that all data has been used as both training and test set.

  • prob_left (1D array) – ** only for ‘block’ cross-validation ** the probability of the stimulus appearing on the left for each trial in event_times

  • custom_validation (generator) –

    ** only for ‘custom’ cross-validation ** a generator object with the splits to be used for cross validation using this format:

    (

    (split1_train_idxs, split1_test_idxs), (split2_train_idxs, split2_test_idxs), (split3_train_idxs, split3_test_idxs),

    …)

  • n_neurons (string or integer) – number of neurons to randomly subselect from the population (default is ‘all’)

  • iterations (int) – number of times to repeat the decoding (especially usefull when subselecting neurons)

  • shuffle (boolean) – whether to shuffle the trial labels each decoding iteration

  • phase_rand (boolean) – whether to use phase randomization of the activity over trials to use as a “chance” predictor

  • pseudo_blocks (boolean) – whether to generate pseudo blocks with the same statistics as the actual blocks to estimate chance level

Returns

results – dictionary with decoding results

accuracyfloat

accuracy of the classifier in percentage correct

f1float

F1 score of the classifier

aurocfloat

the area under the ROC curve of the classification performance

confusion_matrix2D array

normalized confusion matrix

predictions2D array with dimensions iterations x trials

predicted group label for all trials in every iteration

probabilities2D array with dimensions iterations x trials

classification probability for all trials in every iteration

Return type

dict

lda_project(spike_times, spike_clusters, event_times, event_groups, pre_time=0, post_time=0.5, cross_validation='kfold', num_splits=5, prob_left=None, custom_validation=None)[source]

Use linear discriminant analysis to project population vectors to the line that best separates the two groups. When cross-validation is used, the LDA projection is fitted on the training data after which the test data is projected to this projection.

spike_times1D array

spike times (in seconds)

spike_clusters1D array

cluster ids corresponding to each event in spikes

event_times1D array

times (in seconds) of the events from the two groups

event_groups1D array

group identities of the events, can be any number of groups, accepts integers and strings

pre_timefloat

time (in seconds) preceding the event times

post_timefloat

time (in seconds) following the event times

cross_validationstring
which cross-validation method to use, options are:

‘none’ No cross-validation ‘kfold’ K-fold cross-validation ‘leave-one-out’ Leave out the trial that is being decoded ‘block’ Leave out the block the to-be-decoded trial is in ‘custom’ Any custom cross-validation provided by the user

num_splitsinteger

** only for ‘kfold’ cross-validation ** Number of splits to use for k-fold cross validation, a value of 5 means that the decoder will be trained on 4/5th of the data and used to predict the remaining 1/5th. This process is repeated five times so that all data has been used as both training and test set.

prob_left1D array

** only for ‘block’ cross-validation ** the probability of the stimulus appearing on the left for each trial in event_times

custom_validationgenerator

** only for ‘custom’ cross-validation ** a generator object with the splits to be used for cross validation using this format:

(

(split1_train_idxs, split1_test_idxs), (split2_train_idxs, split2_test_idxs), (split3_train_idxs, split3_test_idxs),

…)

n_neuronsint

Group size of number of neurons to be sub-selected

Returns

lda_projection – the position along the LDA projection axis for the population vector of each trial

Return type

1D array