NEUREQUA.artefact_detections

Functions

detectPeaks(x[, mph, mpd, threshold, edge, kpsh, valley])

Detect peaks in data based on their amplitude and other features.

bandpass_filter_bessel(data, lowcut, highcut, sr[, order])

Apply a band-pass filter on our raw LFPs

smooth_binary_array(binary_array, kernel_size)

Function used to obtain time series data from binary ones

get_peri_stimulus_counts(actVect[, window_ms, ...])

Compute peri-stimulus histogram for each spike in a spike train using vectorized operations.

get_timeWins4AdjacentPoints(arr)

Get the time (start,end) of each burst

get_timeWins_mergeIfNext(timeWins, nbPointsToMerge)

Merges overlapping or close windows where gaps are < nbPointsToMerge points, and returns the indices of the merged windows.

get_timeWinsTemplatedSignal(signal, timeWins[, ...])

get_timeWinsIntersect(itwA, itwB, lastPoint[, ...])

return itwTot

get_timeWins4TimeVect(timeVect[, dtype])

return timeWins

detect_movement_artifacts(chTraces, chGoodInds, tetInds)

Detection of movement artifacts using vectorized operations.

detect_subtle_artifacts(chTraces, chGoodInds, tetInds, ...)

Detects subtle artifacts using bandpass filtering and peak detection.

get_tickLocsNLabels_centered(tickMin, tickMax, nTicks)

return tickLocs, tickLabels

plot_chTraces(chTraces, sr[, chInds, t_sample, ...])

chInds is a np.array

plot_detected_events(chTraces, chRegs, SR, ...[, ...])

Functions used to plot the artifacts detected

ratio_artefact(data, badWins_final, path, sub, sess)

Module Contents

NEUREQUA.artefact_detections.detectPeaks(x, mph=None, mpd=1, threshold=0, edge='rising', kpsh=False, valley=False)[source]

Detect peaks in data based on their amplitude and other features.

Parameters:
  • x (1D array_like) – data.

  • mph ({None, number}, optional (default = None)) – detect peaks that are greater than minimum peak height.

  • mpd (positive integer, optional (default = 1)) – detect peaks that are at least separated by minimum peak distance (in number of data).

  • threshold (positive number, optional (default = 0)) – detect peaks (valleys) that are greater (smaller) than threshold in relation to their immediate neighbors.

  • edge ({None, 'rising', 'falling', 'both'}, optional (default = 'rising')) – for a flat peak, keep only the rising edge (‘rising’), only the falling edge (‘falling’), both edges (‘both’), or don’t detect a flat peak (None).

  • kpsh (bool, optional (default = False)) – keep peaks with same height even if they are closer than mpd.

  • valley (bool, optional (default = False)) – if True (1), detect valleys (local minima) instead of peaks.

Returns:

ind – indeces of the peaks in x.

Return type:

1D array_like

Notes

The detection of valleys instead of peaks is performed internally by simply negating the data: ind_valleys = detect_peaks(-x)

The function can handle NaN’s

See this IPython Notebook [1].

References

NEUREQUA.artefact_detections.bandpass_filter_bessel(data, lowcut, highcut, sr, order=2)[source]

Apply a band-pass filter on our raw LFPs

Parameters:
  • data (numpy array) – Numpy array containing the data to filter

  • lowcut (int) – Low cut frequency to apply on band-pass filter

  • highcut (int) – High cut frequency to apply on band-pass filter

  • sr (int) – Sampling rate of the signal to filter

  • order (int) – default = 2 Order of the filter to apply

Returns:

y – Matrices containing the filtered data between lowcut and highcut

Return type:

numpy array

NEUREQUA.artefact_detections.smooth_binary_array(binary_array, kernel_size)[source]

Function used to obtain time series data from binary ones

Parameters:
  • binary_array (array) – Array composed of 0 or 1 only (e.g. spike data)

  • kernel_size (int) – Size of the kernel to compute the convolution

Returns:

convolve_signal – binary_array but transformed into time serie

Return type:

array

NEUREQUA.artefact_detections.get_peri_stimulus_counts(actVect, window_ms=10, sampling_rate=1250)[source]

Compute peri-stimulus histogram for each spike in a spike train using vectorized operations.

Parameters:
  • actVect (Numpy array) – Binary vector (1 for spike, 0 for no spike).

  • window_ms (int) – Window size in milliseconds around each spike.

  • sampling_rate (int) – Sampling rate in Hz.

Returns:

peri_stimulus_counts – Array of the same length as spkTimes with the sum of spikes in the ±window around each spike.

Return type:

Numpy array

NEUREQUA.artefact_detections.get_timeWins4AdjacentPoints(arr)[source]

Get the time (start,end) of each burst

Parameters:

arr (Numpy array) – Made of indices (np.where(bool))

Returns:

start_ends – Array of shape (X, 2) with X corresponds to bursts

Return type:

Numpy array

NEUREQUA.artefact_detections.get_timeWins_mergeIfNext(timeWins, nbPointsToMerge)[source]

Merges overlapping or close windows where gaps are < nbPointsToMerge points, and returns the indices of the merged windows.

Parameters:
  • timeWins (ndarray) – Shape (n, 2), each row is [start, end]

  • nbPointsToMerge (int) – Max gap allowed between consecutive windows to merge.

Returns:

  • merged_windows (ndarray) – Shape (m, 2), merged windows.

  • merged_indices (list of lists) – Indices of the original windows contributing to each merged window.

NEUREQUA.artefact_detections.get_timeWinsTemplatedSignal(signal, timeWins, nTimePoints=None, filling=np.nan)[source]
Parameters:
  • signal (1-D array) – Must be a ‘pure’ np.array, VECTOR shape. If it is an array of lists of len 1, please use: signal = np.ravel(signal) before passing signal into the function

  • timeWins (ndarray) – Shape (n, 2), each row is [start, end]

  • nTimePoints (int or None) – Default = None

  • filling (nan) – Numpy nan

Returns:

templatedSignal – Array containing the signal in time windows with burst artifact

Return type:

array

NEUREQUA.artefact_detections.get_timeWinsIntersect(itwA, itwB, lastPoint, firstPoint=0, ifDisplay=False, lfp=None)[source]

return itwTot

lfp to add if you want to check overlapping windows

NEUREQUA.artefact_detections.get_timeWins4TimeVect(timeVect, dtype=int)[source]

return timeWins

NEUREQUA.artefact_detections.detect_movement_artifacts(chTraces, chGoodInds, tetInds, verbose=True, SR=20000, thresh_mvA=3, extend_mvA_ms=200, threshCorr=0.6)[source]

Detection of movement artifacts using vectorized operations.

Parameters:
  • chTraces (ndarray) – EEG/LFP data array (channels x time).

  • chGoodInds (list) – Indices of good channels.

  • (list) (- tetInds) – List of tetrode indices.

  • (int) (- extend_mvA_ms) – Sampling rate (default=20000 Hz).

  • (float) (- threshCorr) – Z-score threshold for movement artifacts.

  • (int)

  • (ms). (Extension of detected artifacts)

  • (float) – Correlation threshold for refinement.

Returns:

- badWins_refined (ndarray)

Return type:

Array of refined bad time windows.

NEUREQUA.artefact_detections.detect_subtle_artifacts(chTraces, chGoodInds, tetInds, chRegs, SR=20000, verbose=True, extend_final_com_ms=200, lowcut_detPeaks=300, highcut_detPeaks=7000, order_detPeaks=2, thresh_dt1=10, nContacts_dt1=3, nTetrodes_dt1=2, extend_final_ms_dt1=20, thresh_peakDetect=4.5, window_ms_dt2=5, thresh_dt2=10, nContacts_dt2=2, nTetrodes_dt2=2, extend_final_ms_dt2=20)[source]

Detects subtle artifacts using bandpass filtering and peak detection.

Parameters:

  • chTraces (ndarray):

    EEG/LFP data array.

  • chGoodInds (array):

    Array of good channel indices.

  • tetInds (list):

    List of tetrode indices.

  • chRegs (list):

    Array mapping channels to regions.

  • SR (int):

    Sampling rate (default=20000 Hz).

  • lowcut_detPeaks (int):

    Low cut-off frequency for peak detection.

  • highcut_detPeaks (int):

    High cut-off frequency for peak detection.

  • order_detPeaks (int):

    Filter order.

  • thresh_dt1 (float):

    Z-score threshold for the first detector.

  • nContacts_dt1 (int):

    Minimum number of contacts required for detection in first detector.

  • nTetrodes_dt1 (int):

    Minimum number of tetrodes required for first detector.

  • extend_final_ms_dt1 (int):

    Extension window for first detector (ms).

  • thresh_peakDetect (float):

    Peak detection threshold.

  • window_ms_dt2 (int):

    Window size for second detector (ms).

  • thresh_dt2 (float):

    Threshold for spike count in the second detector.

  • nContacts_dt2 (int):

    Minimum number of contacts required for second detector.

  • nTetrodes_dt2 (int):

    Minimum number of tetrodes required for second detector.

  • extend_final_ms_dt2 (int):

    Extension window for second detector (ms).

Returns:

  • badWins_final (ndarray):

    Array of detected bad time windows.

Steps: - Filters signals in a high-frequency range (300-7000 Hz). - Identifies peaks crossing amplitude and gradient thresholds. - Uses channel and tetrode-level consensus to refine detection.

NEUREQUA.artefact_detections.get_tickLocsNLabels_centered(tickMin, tickMax, nTicks, dtype=float, conversion=1)[source]

return tickLocs, tickLabels

convert will be multiplied to tickLabels to convert sample_rate to secs: 1/sr to convert sample_rate to ms: 1000/sr & vice versa

ex: get_tickLocsNLabels_centered(0, 4*1250, 5, convert = 1000/sr) return (array([ 0., 1250., 2500., 3750., 5000.]),

array([-2000., -1000., -0., 1000., 2000.]))

NEUREQUA.artefact_detections.plot_chTraces(chTraces, sr, chInds=None, t_sample=None, chCols=None, win_s=1, hspace=-200, title=False, legend=True, chLegend=None, xlabel=True, fontsize_legend=20, lw=1, nTicks=5, roundTickLabels=2, locLegend='upper right', titleLegend=None, title_fontsizeLegend=20, lwLegend=1, alpha=1, xTickFS=20, labFS=30, yTickFS=12, bbox_to_anchor=None, ignoreCh=[])[source]

chInds is a np.array Adviced size: plt.figure(figsize=(30,nCh*1.5))

legendLabels bbox_to_anchor=(1.1, 1.05) is good

NEUREQUA.artefact_detections.plot_detected_events(chTraces, chRegs, SR, badWinArray, badWini, ignoreCh=[], win_s=2, lw=1, hspace=-7000, extend_final_com_ms=200)[source]

Functions used to plot the artifacts detected

NEUREQUA.artefact_detections.ratio_artefact(data, badWins_final, path, sub, sess)[source]
Parameters:
  • data (2-D array) – 2-D array with format nChannels x nSamples containing your recording

  • badWins_final (Array) – Output of the function get_timeWinsIntersect

  • path (String) – Path where your excel table is stored

  • sub (String) – Identifiant of your subject

  • sess (String) – Name of the current session you are analyzing