common functions for the BOTMpy package
covariance estimator for timeseries data
Bases: object
covariance estimator base class
Bases: botmpy.common.covariance_estimator.BaseTimeSeriesCovarianceEstimator
covariance estimator for timeseries data
Given strips of (multi-channeled) data, this covariance estimator is able to estimate the time-lagged (block-)covariance-matrix, which has a (block-)toeplitz structure. Estimates are built by taking (cross- and) auto-correlations of the (multi-channeled) data pieces, for all lags in the defined range. From this data a toeplitz matrix is build (for each combination of channels).
init from given covariance matrix
Parameters: | |
---|---|
Return type: | TimeSeriesCovE |
Returns: | TimeSeriesCovE with the passed matrix set |
builds the block index set for an upper triangular matrix
Parameters: | ids (iterable) – set of channel ids |
---|
builds a block toeplitz matrix from a set of channel xcorrs
Parameters: |
|
---|
filter related utility functions
!! multi-channeled crosscorrelation filtering implementations are outsourced to botmpy.mcfilter !!
cross-correlation-tensor for a set of matched patterns and filters
The xcorr-tensor for a set of patterns (xi) and their matched filters (f) with a certain lag is returned as ndarray with dimensions [xi, f, tau]. All multi-channeled vectors are presented in their concatenated form.
Parameters: |
|
---|---|
Returns: | ndarray - The tensor of cross-correlation for each pattern with each filter. Dimensions as [xi, f, xcorr]. |
teager energy operator of range k [TEO]
The discrete teager energy operator (TEO) of window size k is defined as: M{S{Psi}[x(n)] = x^2(n) - x(n-k) x(n+k)}
Parameters: |
|
---|---|
Returns: | ndarray - Array of same shape as the input signal, holding the kteo response. |
Except : | If inconsistant dims or shapes. |
multiresolution teager energy operator using given k-values [MTEO]
The multi-resolution teager energy operator (MTEO) applies TEO operators of varying k-values and returns the reduced maximum response TEO for each input sample.
To assure a constant noise power over all kteo channels, we convolve the individual kteo responses with a window: h_k(i) = hamming(4k+1) / sqrt(3sum(hamming(4k+1)^2) + sum(hamming(4k+1)) ^2), as suggested in Choi et al., 2006.
Parameters: |
|
---|---|
Returns: | ndarray- Array of same shape as the input signal, holding the response of the kteo which response was maximum after smoothing for each sample in the input signal. |
general utility functions
sort matrix by rows
Parameters: | data (ndarray) – ndarray that should be sorted by its rows |
---|---|
Returns: | ndarray - data sorted by its rows. |
converts from templates/spikes that are concatenated across the channels to tensors that have an extra dim for the channels
Parameters: |
|
---|---|
Returns: | ndarray - data converted to tensor [templates][vars][channels] |
converts from templates/spikes that are not concatenated across the channels to vectors.
Parameters: | data (ndarray) – input array [templates][vars][channels] |
---|---|
Returns: | ndarray- data converted to concatenated vectors [templates][channels * vars] |
returns the concatenated vector for a multi-channeled vector
Parameters: | x (ndarray) – multi-channeled vector in matrix form |
---|---|
Returns: | ndarray - multi-channeled vector in channel concatenated form |
returns the multi-channeled vector from a concatenated representation
Parameters: |
|
---|---|
Returns: | ndarray - multi-channeled vector in matrix form |
cross-correlation for one-dimensional input signals of equal size
If :b: is not given the auto-correlation of :a: will be computed.
Parameters: |
|
---|---|
Returns: | ndarray - cross-correlate of :a: and :b: upt to lags :lag: |
Subtracts the multi-channeled vector (rows are channels) y from the vector x with a certain offset. x and y can due to the offset be only partly overlapping.
REM: from matlab
Parameters: | |
---|---|
Returns: | ndarray - data minus sub at offset, len(data) |
sort all arrays in a dictionary, works recursively
Parameters: | in_dict (dict) – dict to work on |
---|---|
Returns: |
functions for spike sorting
detect events by applying a threshold to the data
Parameters: |
|
---|---|
Return type: | ndarray |
Returns: | event samples |
for a set of epoch sets check if the combined set of epochs overlap and merge to one set with no overlapping epochs and no epochs of negative length.
Parameters: |
|
---|---|
Returns: | ndarray - merged epoch set [[start, end]] |
inverts epochs inverted
The first epoch will be mapped to [0, start] and the last will be mapped to [end of last epoch, :end:]. Epochs that accidentally become negative or zero-length will be omitted.
Parameters: |
|
---|---|
Returns: | ndarray - inverted epoch set |
returns the discrete epochs where the :binvec: is true
Parameters: | binvec (ndarray) – one-domensinal boolean ndarray. |
---|---|
Returns: | ndarray - epoch set where :binvec: is True [[start, end]] |
yields epoch set, given a spiketrain and cut parameter
Parameters: |
|
---|---|
Returns: | ndarray - epoch set of valid spike epochs, and if :with_corrected_st: is True additionally the corrected spike train |
yields epoch sets, given a spiketrain set and cut parameter
one set for each unit plus one for the noise epochs in a dict
Parameters: |
|
---|---|
Returns: | dict - one epoch set per spike train plus the merged noise epoch set. |
returns a generator of chunks from data given epochs
Parameters: |
|
---|---|
Returns: | generator - data chunks as per :epochs: |
extract spike waveforms of size tf from data
Parameters: |
|
---|---|
Returns: | ndarray - extracted spike data epochs |
cut 2-tuple (cut_left,cut_right) generating function
Used to generate epochs from events. Per default the epoch will be placed symmetrically around the event sample. :off: can be used to influence the placement. For odd tf values the extension of the cut_right part will be favored.
Parameters: |
---|
SNR from Mahalanobis distance (generalised euclidean distance)
Definition of signal to noise ratio (SNR) as derived from the Mahalanobis distance. For C=eye this is equivalent to snr_power.
Parameters: |
|
---|---|
Returns: | ndarray - SNR per waveform |
SNR from instantaneous variance
Definition of signal to noise ratio (SNR) as the ratio between the peak of a waveforms and the noise standard deviation.
Parameters: |
|
---|---|
Returns: | ndarray - SNR per waveform |
SNR from signal energy
Definition of signal to noise ratio (SNR) using the waveforms energy as defined by Rutishauser et al (2006)
Parameters: |
|
---|---|
Returns: | ndarray - SNR per waveform |
matrix operations
yield the matrix condition number w.r.t. l2-norm (using svd)
Parameters: | mat (ndarray) – input matrix |
---|---|
Returns: | float - condition number of :mat: or :inf: on error |
tries to condition the :mat: by imposing a spherical constraint on the covariance ellipsoid (adding alpha*eye)
solves: cond(mat + alpha*I) = target_cond for alpha
Note: this is a noop if the condition is already >= target_cond!
Parameters: | |
---|---|
Returns: | ndarray - matrix like :mat: conditioned s.t. cond = target_cond |
tries to condition :mat: by inflating the badly conditioned subspace of :mat: using a spherical constraint.
Parameters: | |
---|---|
Returns: | ndarray - matrix like :mat: conditioned s.t. cond = target_cond |
matrix based ringbuffer implementation
Bases: object
ringbuffer implementation based on pre-allocated ndarray
Ringbuffer behavior is archived by cycling though the buffer forward, wrapping around to the start upon reaching capacity.
append one datum at the end of the buffer, overwriting the oldest datum in the buffer if the capacity has been reached.
Parameters: | datum (ndarray) – ndarray of shape :self.dimension: |
---|
append iterable at the end of the buffer using multiple append’s
Parameters: | iterable (iterable) – iterable of objects to be stored in the ringbuffer |
---|
fill all slots of the ringbuffer with the same datum.
Parameters: | daaum – ndarray of shape :self.dimension: |
---|
return the buffer as a list and clear the RingBuffer
Convenience method. This returns self.tolist() and calls self.clear() afterwards.
Returns: | list - the buffer as a python list of the objects stored |
---|
yields the mean over the :last: entries
Parameters: | last (int) – number entries from the back of the ringbuffer to include for mean calculation. If None, use all contents Default=None |
---|---|
Returns: | ndarray(self.dimension) - mean over the last entries, or the appropriate zero element if the ringbuffer is empty. |
spikes alignment functions
Interpolates x, sampled at times s Output y is sampled at times r
inspired from from Matlab: http://phaseportrait.blogspot.com/2008/06/sinc-interpolation-in-matlab.html
Parameters: |
|
---|---|
Return ndarray: | output data time series (regular sample interval) |
return the per spike offset in samples (taus) of the maximum values to the desired alignment sample within the spike waveform.
Parameters: |
|
---|---|
Returns: | ndarray - offset per spike |
return the set of aligned spikes waveforms and the aligned spike train
Parameters: |
|
---|---|
Return type: | ndarray, ndarray |
Returns: | stacked spike events, spike train with events corrected for alignment |
constants for the common package