Module thunderlab.eventdetection

Detect and handle peaks and troughs as well as threshold crossings in data arrays.

Peak detection

Threshold crossings

Event manipulation

Threshold estimation

Snippets

  • snippets(): cut out data snippets around a list of indices.

Peak detection with dynamic threshold:

Functions

def detect_peaks(data, threshold)

Detect peaks and troughs using a relative threshold.

This is an implementation of the algorithm by Bryan S. Todd and David C. Andrews (1999): The identification of peaks in physiological signals. Computers and Biomedical Research 32, 322-335.

Parameters

data : array
An 1-D array of input data where peaks are detected.
threshold : float or array of floats
A positive number or array of numbers setting the detection threshold, i.e. the minimum distance between peaks and troughs. In case of an array make sure that the threshold does not change faster than the expected intervals between peaks and troughs.

Returns

peaks : array of ints
Array of indices of detected peaks.
troughs : array of ints
Array of indices of detected troughs.

Raises

Valueerror

If threshold <= 0.

Indexerror

If data and threshold arrays differ in length.

def detect_peaks_fixed(data, threshold)

Detect peaks and troughs using a fixed, relative threshold.

Helper function for detect_peaks().

Parameters

data : array
An 1-D array of input data where peaks are detected.
threshold : float
A positive number setting the detection threshold, i.e. the minimum distance between peaks and troughs.

Returns

peaks : array of ints
Array of indices of detected peaks.
troughs : array of ints
Array of indices of detected troughs.
def detect_peaks_array(data, threshold)

Detect peaks and troughs using a variable relative threshold.

Helper function for detect_peaks().

Parameters

data : array
An 1-D array of input data where peaks are detected.
threshold : array
A array of positive numbers setting the detection threshold, i.e. the minimum distance between peaks and troughs.

Returns

peaks : array of ints
Array of indices of detected peaks.
troughs : array of ints
Array of indices of detected troughs.
def peak_width(time, data, peak_indices, trough_indices, peak_frac=0.5, base='max')

Width of each peak.

Peak width is computed from interpolated threshold crossings at peak_frac hieght of each peak.

Parameters

time : array
Time, must not be None.
data : array
The data with the peaks.
peak_indices : array
Indices of the peaks.
trough_indices : array
Indices of corresponding troughs.
peak_frac : float
Fraction of peak height where its width is measured.
base : string

Height and width of peak is measured relative to

  • 'left': trough to the left
  • 'right': trough to the right
  • 'min': the minimum of the two troughs to the left and to the right
  • 'max': the maximum of the two troughs to the left and to the right
  • 'mean': mean of the throughs to the left and to the rigth
  • 'closest': trough that is closest to peak

Returns

widths : array
Width at peak_frac height of each peak.

Raises

Valueerror

If an invalid value is passed to base.

def peak_size_width(time, data, peak_indices, trough_indices, peak_frac=0.75, base='closest')

Compute size and width of each peak.

Parameters

time : array
Time, must not be None.
data : array
The data with the peaks.
peak_indices : array
Indices of the peaks.
trough_indices : array
Indices of the troughs.
peak_frac : float
Fraction of peak height where its width is measured.
base : string

Height and width of peak is measured relative to

  • 'left': trough to the left
  • 'right': trough to the right
  • 'min': the minimum of the two troughs to the left and to the right
  • 'max': the maximum of the two troughs to the left and to the right
  • 'mean': mean of the throughs to the left and to the rigth
  • 'closest': trough that is closest to peak

Returns

peaks : 2-D array
First dimension is the peak index. Second dimension is time, height (value of data at the peak), size (peak height minus height of closest trough), width (at peak_frac size), 0.0 (count) of the peak. See peak_width().

Raises

Valueerror

If an invalid value is passed to base.

def threshold_crossings(data, threshold)

Detect crossings of a threshold with positive and negative slope.

Parameters

data : array
An 1-D array of input data where threshold crossings are detected.
threshold : float or array
A number or array of numbers setting the threshold that needs to be crossed.

Returns

up_indices : array of ints
A list of indices where the threshold is crossed with positive slope.
down_indices : array of ints
A list of indices where the threshold is crossed with negative slope.

Raises

Indexerror

If data and threshold arrays differ in length.

def threshold_crossing_times(time, data, threshold, up_indices, down_indices)

Compute times of threshold crossings by linear interpolation.

Parameters

time : array
Time, must not be None.
data : array
The data.
threshold : float
A number or array of numbers setting the threshold that was crossed.
up_indices : array of ints
A list of indices where the threshold is crossed with positive slope.
down_indices : array of ints
A list of indices where the threshold is crossed with negative slope.

Returns

up_times : array of floats
Interpolated times where the threshold is crossed with positive slope.
down_times : array of floats
Interpolated times where the threshold is crossed with negative slope.
def trim(peaks, troughs)

Trims the peaks and troughs arrays such that they have the same length.

Parameters

peaks : array
List of peak indices or times.
troughs : array
List of trough indices or times.

Returns

peaks : array
List of peak indices or times.
troughs : array
List of trough indices or times.
def trim_to_peak(peaks, troughs)

Trims the peaks and troughs arrays such that they have the same length and the first peak comes first.

Parameters

peaks : array
List of peak indices or times.
troughs : array
List of trough indices or times.

Returns

peaks : array
List of peak indices or times.
troughs : array
List of trough indices or times.
def trim_closest(peaks, troughs)

Trims the peaks and troughs arrays such that they have the same length and that peaks-troughs is on average as small as possible.

Parameters

peaks : array
List of peak indices or times.
troughs : array
List of trough indices or times.

Returns

peaks : array
List of peak indices or times.
troughs : array
List of trough indices or times.
def merge_events(onsets, offsets, min_distance)

Merge events if they are closer than a minimum distance.

If the beginning of an event (onset, peak, or positive threshold crossing, is too close to the end of the previous event (offset, trough, or negative threshold crossing) the two events are merged into a single one that begins with the first one and ends with the second one.

Parameters

onsets : 1-D array
The onsets (peaks, or positive threshold crossings) of the events as indices or times.
offsets : 1-D array
The offsets (troughs, or negative threshold crossings) of the events as indices or times.
min_distance : int or float
The minimum distance between events. If the beginning of an event is separated from the end of the previous event by less than this distance then the two events are merged into one. If the event onsets and offsets are given in indices than min_distance is also in indices.

Returns

merged_onsets : 1-D array
The onsets (peaks, or positive threshold crossings) of the merged events as indices or times according to onsets.
merged_offsets : 1-D array
The offsets (troughs, or negative threshold crossings) of the merged events as indices or times according to offsets.
def remove_events(onsets, offsets, min_duration, max_duration=None)

Remove events that are too short or too long.

If the length of an event, i.e. offset (offset, trough, or negative threshold crossing) minus onset (onset, peak, or positive threshold crossing), is shorter than min_duration or longer than max_duration, then this event is removed.

Parameters

onsets : 1-D array
The onsets (peaks, or positive threshold crossings) of the events as indices or times.
offsets : 1-D array
The offsets (troughs, or negative threshold crossings) of the events as indices or times.
min_duration : int, float, or None
The minimum duration of events. If the event offset minus the event onset is less than min_duration, then the event is removed from the lists. If the event onsets and offsets are given in indices than min_duration is also in indices. If None then this test is skipped.
max_duration : int, float, or None
The maximum duration of events. If the event offset minus the event onset is larger than max_duration, then the event is removed from the lists. If the event onsets and offsets are given in indices than max_duration is also in indices. If None then this test is skipped.

Returns

onsets : 1-D array
The onsets (peaks, or positive threshold crossings) of the events with too short and too long events removed as indices or times according to onsets.
offsets : 1-D array
The offsets (troughs, or negative threshold crossings) of the events with too short and too long events removed as indices or times according to offsets.
def widen_events(onsets, offsets, max_time, duration)

Enlarge events on both sides without overlap.

Subtracts duration from the onsets and adds duration to the offsets. If two succeeding events are separated by less than two times the duration, then the offset of the previous event and the onset of the following event are set at the center between the two events.

Parameters

onsets : 1-D array
The onsets (peaks, or positive threshold crossings) of the events as indices or times.
offsets : 1-D array
The offsets (troughs, or negative threshold crossings) of the events as indices or times.
max_time : int or float
The maximum value for the end of the last event. If the event onsets and offsets are given in indices than max_time is the maximum possible index, i.e. the len of the data array on which the events where detected.
duration : int or float
The number of indices or the time by which the events should be enlarged. If the event onsets and offsets are given in indices than duration is also in indices.

Returns

onsets : 1-D array
The onsets (peaks, or positive threshold crossings) of the enlarged events.
offsets : 1-D array
The offsets (troughs, or negative threshold crossings) of the enlarged events.
def std_threshold(data, win_size=None, thresh_fac=5.0)

Estimates a threshold for peak detection based on the standard deviation of the data.

The threshold is computed as the standard deviation of the data multiplied with thresh_fac.

In case of Gaussian distributed data, setting thresh_fac=2.0 (two standard deviations) captures 68% of the data, thresh_fac=4.0 captures 95%, and thresh_fac=6.0 99.7%.

If win_size is given, then the threshold is computed for half-overlapping windows of size win_size separately. In this case the returned threshold is an array of the same size as data. Without a win_size a single threshold value determined from the whole data array is returned.

Parameters

data : 1-D array
The data to be analyzed.
win_size : int or None
Size of window in which a threshold value is computed.
thresh_fac : float
Factor by which the standard deviation is multiplied to set the threshold.

Returns

threshold : float or 1-D array
The computed threshold.
def median_std_threshold(data, win_size=100, thresh_fac=6.0, n_snippets=1000)

Estimate a threshold for peak detection based on the median standard deviation of data snippets.

On n_snippets snippets of win_size size the standard deviation of the data is estimated. The returned threshold is the median of these standard deviations that are larger than zero multiplied by thresh_fac.

Parameters

data : 1-D array of float
The data to be analysed.
win_size : int
Size of windows on which standarad deviations are computed.
thresh_fac : float
Factor by which the median standard deviation is multiplied to set the threshold.
n_snippets : int
Number of snippets on which the standard deviations are estimated.

Returns

threshold : float
The computed threshold.
def hist_threshold(data, win_size=None, thresh_fac=5.0, nbins=100, hist_height=np.float64(0.6065306597126334))

Estimate a threshold for peak detection based on a histogram of the data.

The standard deviation of the data is estimated from half the width of the histogram of the data at hist_height relative height. This estimates the data's standard deviation by ignoring tails of the distribution.

However, you need enough data to robustly estimate the histogram.

If win_size is given, then the threshold is computed for half-overlapping windows of size win_size separately. In this case the returned threshold is an array of the same size as data. Without a win_size a single threshold value determined from the whole data array is returned.

Parameters

data : 1-D array
The data to be analyzed.
win_size : int or None
Size of window in which a threshold value is computed.
thresh_fac : float
Factor by which the width of the histogram is multiplied to set the threshold.
nbins : int or list of floats
Number of bins or the bins for computing the histogram.
hist_height : float
Height between 0 and 1 at which the width of the histogram is computed.

Returns

threshold : float or 1-D array
The computed threshold.
center : float or 1-D array
The center (mean) of the width of the histogram.
def minmax_threshold(data, win_size=None, thresh_fac=0.8)

Estimate a threshold for peak detection based on minimum and maximum values of the data.

The threshold is computed as the difference between maximum and minimum value of the data multiplied with thresh_fac.

If win_size is given, then the threshold is computed for half-overlapping windows of size win_size separately. In this case the returned threshold is an array of the same size as data. Without a win_size a single threshold value determined from the whole data array is returned.

Parameters

data : 1-D array
The data to be analyzed.
win_size : int or None
Size of window in which a threshold value is computed.
thresh_fac : float
Factor by which the difference between minimum and maximum data value is multiplied to set the threshold.

Returns

threshold : float or 1-D array
The computed threshold.
def percentile_threshold(data, win_size=None, thresh_fac=1.0, percentile=1.0)

Estimate a threshold for peak detection based on an inter-percentile range of the data.

The threshold is computed as the range between the percentile and 100.0-percentile percentiles of the data multiplied with thresh_fac.

For very small values of percentile the estimated threshold approaches the one returned by minmax_threshold() (for same values of thresh_fac). For percentile=16.0 and Gaussian distributed data, the returned theshold is twice the one returned by std_threshold() or hist_threshold(), i.e. twice the standard deviation.

If you have knowledge about how many data points are in the tails of the distribution, then this method is preferred over hist_threshold(). For example, if you expect peaks that you want to detect using detect_peaks() at an average rate of 10Hz and these peaks are about 1ms wide, then you have a 1ms peak per 100ms period, i.e. the peaks make up 1% of the distribution. So you should set percentile=1.0 or lower. For much lower percentile values, you might choose thresh_fac slightly smaller than one to capture also smaller peaks. Setting percentile slightly higher might not change the estimated threshold too much, since you start incorporating the noise floor with a large density, but you may want to set thresh_fac larger than one to reduce false detections.

If win_size is given, then the threshold is computed for half-overlapping windows of size win_size separately. In this case the returned threshold is an array of the same size as data. Without a win_size a single threshold value determined from the whole data array is returned.

Parameters

data : 1-D array
The data to be analyzed.
win_size : int or None
Size of window in which a threshold value is computed.
percentile : float
The interpercentile range is computed at percentile and 100.0-percentile. If zero, compute maximum minus minimum data value as the interpercentile range.
thresh_fac : float
Factor by which the inter-percentile range of the data is multiplied to set the threshold.

Returns

threshold : float or 1-D array
The computed threshold.
def snippets(data, indices, start=-10, stop=10)

Cut out data arround each position given in indices.

Parameters

data : 1-D array
Data array from which snippets are extracted.
indices : list of int
Indices around which snippets are cut out.
start : int
Each snippet starts at index + start.
stop : int
Each snippet ends at index + stop.

Returns

snippet_data : 2-D array
The snippets: first index number of snippet, second index time.
def detect_dynamic_peaks(data, threshold, min_thresh, tau, time=None, check_peak_func=None, check_trough_func=None, **kwargs)

Detect peaks and troughs using a relative threshold.

The threshold decays dynamically towards min_thresh with time constant tau. Use check_peak_func or check_trough_func to reset the threshold to an appropriate size.

Based on Bryan S. Todd and David C. Andrews (1999): The identification of peaks in physiological signals. Computers and Biomedical Research 32, 322-335.

Parameters

data : array
An 1-D array of input data where peaks are detected.
threshold : float
A positive number setting the minimum distance between peaks and troughs.
min_thresh : float
The minimum value the threshold is allowed to assume.
tau : float
The time constant of the the decay of the threshold value given in indices (time is None) or time units (time is not None).
time : array
The (optional) 1-D array with the time corresponding to the data values.
check_peak_func : function

An optional function to be used for further evaluating and analysing a peak. The signature of the function is

r, th = check_peak_func(time, data, peak_inx, index, min_inx, threshold, **kwargs)

with the arguments:

  • time (array): the full time array that might be None
  • data (array): the full data array
  • peak_inx (int): the index of the detected peak
  • index (int): the current index
  • min_inx (int): the index of the trough preceeding the peak (might be 0)
  • threshold (float): the threshold value
  • min_thresh (float): the minimum value the threshold is allowed to assume.
  • tau (float): the time constant of the the decay of the threshold value given in indices (time is None) or time units (time is not None)
  • **kwargs: further keyword arguments provided by the user
  • r (scalar or np.array): a single number or an array with properties of the peak or None to skip the peak
  • th (float): a new value for the threshold or None (to keep the original value)
check_trough_func : function

An optional function to be used for further evaluating and analysing a trough. The signature of the function is

r, th = check_trough_func(time, data, trough_inx, index, max_inx, threshold, **kwargs)

with the arguments:

  • time (array): the full time array that might be None
  • data (array): the full data array
  • trough_inx (int): the index of the detected trough
  • index (int): the current index
  • max_inx (int): the index of the peak preceeding the trough (might be 0)
  • threshold (float): the threshold value
  • min_thresh (float): the minimum value the threshold is allowed to assume.
  • tau (float): the time constant of the the decay of the threshold value given in indices (time is None) or time units (time is not None)
  • **kwargs: further keyword arguments provided by the user
  • r (scalar or np.array): a single number or an array with properties of the trough or None to skip the trough
  • th (float): a new value for the threshold or None (to keep the original value)
kwargs : key-word arguments
Arguments passed on to check_peak_func and check_trough_func.

Returns

peak_list : array
List of peaks.
trough_list : array
List of troughs.
  • If time is None and no check_peak_func is given, then these are lists of the indices where the peaks/troughs occur.
  • If time is given and no check_peak_func/check_trough_func is given, then these are lists of the times where the peaks/troughs occur.
  • If check_peak_func or check_trough_func is given, then these are lists of whatever check_peak_func/check_trough_func return.

Raises

Valueerror

If threshold <= 0 or min_thresh <= 0 or tau <= 0.

Indexerror

If data and time arrays differ in length.

def accept_peak_size_threshold(time, data, event_inx, index, min_inx, threshold, min_thresh, tau, thresh_ampl_fac=0.75, thresh_weight=0.02)

Accept each detected peak/trough and return its index or time.

Adjust the threshold to the size of the detected peak. To be passed to the detect_dynamic_peaks() function.

Parameters

time : array
Time values, can be None.
data : array
The data in wich peaks and troughs are detected.
event_inx : int
Index of the current peak/trough.
index : int
Current index.
min_inx : int
Index of the previous trough/peak.
threshold : float
Threshold value.
min_thresh : float
The minimum value the threshold is allowed to assume..
tau : float
The time constant of the the decay of the threshold value given in indices (time is None) or time units (time is not None).
thresh_ampl_fac : float
The new threshold is thresh_ampl_fac times the size of the current peak.
thresh_weight : float
New threshold is weighted against current threshold with thresh_weight.

Returns

index : int
Index of the peak/trough if time is None.
time : int
Time of the peak/trough if time is not None.
threshold : float
The new threshold to be used.
def main()