Calculate features (spike_sort.features
)¶
Provides functions to calculate spike waveforms features.
Features¶
Functions starting with fet implement various features calculated from the spike waveshapes. They have usually one required argument Spike waveforms structure and some can have optional arguments (see below)
Each of the function returns a mapping structure (dictionary) with the following keys:
- data – an array of shape (n_spikes x n_features)
- names – a list of length n_features with feature labels
The following features are implemented:
fetPCA
(spike_data, *args, **kwargs)Calculate principal components (PCs). fetP2P
(spike_data, *args, **kwargs)Calculate peak-to-peak amplitudes of spike waveforms. fetSpIdx
(spike_data, *args, **kwargs)Spike sequential index (0,1,2, …) fetSpTime
(spike_data, *args, **kwargs)Spike occurrence time in milliseconds. fetSpProjection
(spike_data, *args, **kwargs)Projection coefficient of spikes on an averaged waveform
Tools¶
This module provides a few tools to facilitate working with features data structure:
split_cells (features, idx[, which]) |
return the spike features splitted into separate cells |
select (features_dict, features_ids) |
Choose selected features from the collection |
combine (feat_data[, norm, feat_method_names]) |
Combine features into a single structure |
normalize (features[, copy]) |
Normalize features |
Auxiliary¶
PCA (data[, ncomps]) |
Perfrom a principle component analysis. |
add_mask (feature_function) |
Decorator to copy mask from waveshapes to features |
Reference¶
Provides functions to calculate spike waveforms features.
Functions starting with fet implement various features calculated from the spike waveshapes. They have usually one required argument Spike waveforms structure (but there are exceptions!).
Each of the function returns a (mapping) object with following keys:
- data – an array of shape (n_spikes, n_features)
- names – a list of length n_features with feature labels
-
spike_sort.core.features.
PCA
(data, ncomps=2)¶ Perfrom a principle component analysis.
Parameters: data : array
(n_vars, n_obs) array where n_vars is the number of variables (vector dimensions) and n_obs the number of observations
Returns: evals : array
sorted eigenvalues
evecs : array
sorted eigenvectors
score : array
projection of the data on ncomps components
-
spike_sort.core.features.
WT
(*args, **kwargs)¶ Perfroms a batch 1D wavelet transform.
Parameters: data : array
(n_vars, n_obs, n_contacts) array where n_vars is the number of variables (vector dimensions), n_obs the number of observations and n_contacts is the number of contacts. Only 3D arrays are accepted.
wavelet : string or pywt.Wavelet
wavelet to be used to perform the transform
mode : string, optional
signal extension mode (see modes in PyWavelets documentation)
Returns: data : array
(n_coeffs, n_obs, n_contacts) 1D wavelet transform of each vector of the input data array. pywt.wavedec is used to perform the transform. For every vector of the input array, a 1D transformation is returned of the form [cA_n, cD_n, cD_n-1, …, cD_n2, cD_n1] where cA_n and cD_n are approximation and detailed coefficients of level n. cA_n and cD_n’s are stacked together in a single vector.
Notes
PyWavelets documentation contains more detailed information on the wavelet transform.
-
spike_sort.core.features.
add_mask
(feature_function)¶ Decorator to copy mask from waveshapes to features
-
spike_sort.core.features.
combine
(feat_data, norm=True, feat_method_names=None)¶ Combine features into a single structure
Parameters: args : tuple or list of dict
a tuple of feature data structures
Returns: combined_fetures : dict
-
spike_sort.core.features.
fetP2P
(spike_data, *args, **kwargs)¶ Calculate peak-to-peak amplitudes of spike waveforms.
Parameters: spikes : dict Returns: features : dict Examples
We will generate a spikewave structure containing only a single spike on a single channel
>>> import numpy as np >>> from spike_sort import features >>> time = np.arange(0,2*np.pi,0.01) >>> spikes = np.sin(time)[:,np.newaxis, np.newaxis] >>> spikewave = {"data": spikes, "time":time, "contacts":1, "FS":1} >>> p2p = features.fetP2P(spikewave) >>> print p2p['data'] [[ 1.99999683]]
-
spike_sort.core.features.
fetPCA
(spike_data, *args, **kwargs)¶ Calculate principal components (PCs).
Parameters: spikes : dict
ncomps : int, optional
number of components to retain
Returns: features : dict
-
spike_sort.core.features.
fetSpIdx
(spike_data, *args, **kwargs)¶ Spike sequential index (0,1,2, …)
-
spike_sort.core.features.
fetSpProjection
(spike_data, *args, **kwargs)¶ Projection coefficient of spikes on an averaged waveform
Parameters: spikes_data : dict
waveform data
labels : array
array of length equal to number of spikes that contains cluster labels
cell_id : int
label of cell on which all spikes should be projected.
Notes
labels can be also a boolean array in which case only spikes for which label is True value will be averaged to determine projection coefficient
-
spike_sort.core.features.
fetSpTime
(spike_data, *args, **kwargs)¶ Spike occurrence time in milliseconds.
-
spike_sort.core.features.
fetWT
(*args, **kwargs)¶ Calculate wavelet transform reduce the dimensionality
The dimensionality reduction is done by picking the most “interesting” wavelet coefficients or their linear combibations, depending on the select_method value
Parameters: spikes_data : dict
nfeatures : int, optional
number of wavelet coefficients to return
contacts : string or int or list, optional
contacts to be processed
wavelet : string or pywt.Wavelet, optional
wavelet to be used for transformation
mode : string, optional
signal extension mode (see modes in PyWavelets documentation)
select_method : string, optional
method to select the “interesting” coefficients. Following statistics are supported: ‘std’
standard deviation
- ‘std_r’
robust estimate of std (Quiroga et al, 2004)
- ‘ks’
Kolmogorov-Smirnov test for normality (useful for selecting coeffs with multimodal distributions)
- ‘dip’
DIP statistic (tests unimodality) by Hartigan & Hartigan 1985. Also useful for picking coeffs with multimodal distributions.
- ‘ksPCA’ and ‘dipPCA’
implementation of modality-weighted PCA (Takekawa et al, 2012) using ‘ks’ and ‘dip’ tests respectively
Returns: features : dict
-
spike_sort.core.features.
normalize
(features, copy=True)¶ Normalize features
-
spike_sort.core.features.
register
(feature_func)¶ feature function must take the spike data array and return a feature dictionary with at least two keys: data and names
-
spike_sort.core.features.
select
(features_dict, features_ids)¶ Choose selected features from the collection
-
spike_sort.core.features.
select_spikes
(features, idx)¶ Truncate features array to selected spikes. This method should be used to properly truncate the “features” structure
Parameters: features : features structure
features structure to truncate
idx : bool list or int list
indices of selected spikes
Returns: new_feats : features structure
new features structure containing only data for selected spike indices
-
spike_sort.core.features.
split_cells
(features, idx, which='all')¶ return the spike features splitted into separate cells