Esempio n. 1
0
def estimate_hr_and_peaks(sampling_frequency, signal):
    peaks = qrs_detector(sampling_frequency, signal, filter_length='3.5s')
    # noinspection PyTypeChecker
    instantaneous_rates = (sampling_frequency * 60) / np.diff(peaks)

    # remove instantaneous rates which are lower than 30, higher than 240
    selector = (instantaneous_rates > 30) & (instantaneous_rates < 240)
    return {
        'hr': float(np.nan_to_num(instantaneous_rates[selector].mean())),
        'peaks': peaks
    }
    def estimate_average_heartrate(self, signal, sampling_frequency):
        """
        Calculates the total average heart rate of the signal.
        :param peaks: The detected peaks in the signal.
        :param sampling_frequency: The sampling frequency of the signal.
        :return: The calculated average heart rate.
        """
        peaks = qrs_detector(sampling_frequency, signal)
        instantaneous_rates = (sampling_frequency * 60) / np.diff(peaks)

        # remove instantaneous rates which are lower than 30, higher than 240
        selector = (instantaneous_rates > 30) & (instantaneous_rates < 240)
        return float(np.nan_to_num(
            instantaneous_rates[selector].mean())), peaks
Esempio n. 3
0
def estimate_average_heartrate(s, sampling_frequency):
    '''Estimates the average heart rate taking as base the input signal and its
  sampling frequency.

  This method will use the Pam-Tompkins detector available the MNE package to
  clean-up and estimate the heart-beat frequency based on the ECG sensor
  information provided.

  Returns:

    float: The estimated average heart-rate in beats-per-minute

  '''

    peaks = qrs_detector(sampling_frequency, s)
    instantaneous_rates = (sampling_frequency * 60) / numpy.diff(peaks)

    # remove instantaneous rates which are lower than 30, higher than 240
    selector = (instantaneous_rates > 30) & (instantaneous_rates < 240)
    return float(numpy.nan_to_num(instantaneous_rates[selector].mean())), peaks
Esempio n. 4
0
def estimate_heartbeat_peaks(s, sampling_frequency):
    '''Estimates the average heart rate taking as base the input signal and its
  sampling frequency.

  This method will use the Pam-Tompkins detector available the MNE package to
  clean-up and estimate the heart-beat frequency based on the ECG sensor
  information provided.

  Returns:

    float: The estimated average heart-rate in beats-per-minute

  '''

    peaks = qrs_detector(sampling_frequency, s)
    try:
        print('Length before handling outlier hr values: {0} minutes'.format(
            peaks[-1] / (sampling_frequency * 60)))

        # identify index of outlier peaks which lead to heart rate lower than 40, higher than 120
        instantaneous_rates = (sampling_frequency * 60) / numpy.diff(peaks)
        hr_selector = (instantaneous_rates >= 40) & (instantaneous_rates <=
                                                     120)
        peaks_selector = numpy.insert(hr_selector, 0, True)
        # print(selector_peaks, peaks)

        # replace the outlier hr with the successive hr value
        instantaneous_rates = instantaneous_rates[hr_selector]
        time_stamps = peaks[
            peaks_selector] / sampling_frequency  # remove the timestamp of outlier peaks
        time_stamps = time_stamps[
            1:]  # replace the initial hr with the first hr
        assert len(instantaneous_rates) == len(time_stamps)
        print('Length after handling outlier hr values: {0} minutes'.format(
            time_stamps[-1] / 60))
        return float(numpy.nan_to_num(instantaneous_rates.mean())
                     ), peaks, instantaneous_rates, time_stamps
    except:
        print('No detected qrs peaks')
        return -1.0, numpy.array([]), numpy.array([]), numpy.array([])
Esempio n. 5
0
def run_algo(algorithm: str, sig: numpy.ndarray,
             freq_sampling: int) -> List[int]:
    """
    run a qrs detector on a signal

    :param algorithm: name of the qrs detector to use
    :type algorithm: str
    :param sig: values of the sampled signal to study
    :type sig: ndarray
    :param freq_sampling: value of sampling frequency of the signal
    :type freq_sampling: int
    :return: localisations of qrs detections
    :rtype: list(int)
    """
    detectors = Detectors(freq_sampling)
    if algorithm == 'Pan-Tompkins-ecg-detector':
        qrs_detections = detectors.pan_tompkins_detector(sig)
    elif algorithm == 'Hamilton-ecg-detector':
        qrs_detections = detectors.hamilton_detector(sig)
    elif algorithm == 'Christov-ecg-detector':
        qrs_detections = detectors.christov_detector(sig)
    elif algorithm == 'Engelse-Zeelenberg-ecg-detector':
        qrs_detections = detectors.engzee_detector(sig)
    elif algorithm == 'SWT-ecg-detector':
        qrs_detections = detectors.swt_detector(sig)
    elif algorithm == 'Matched-filter-ecg-detector' and freq_sampling == 360:
        qrs_detections = detectors.matched_filter_detector(
            sig, 'templates/template_360hz.csv')
    elif algorithm == 'Matched-filter-ecg-detector' and freq_sampling == 250:
        qrs_detections = detectors.matched_filter_detector(
            sig, 'templates/template_250hz.csv')
    elif algorithm == 'Two-average-ecg-detector':
        qrs_detections = detectors.two_average_detector(sig)
    elif algorithm == 'Hamilton-biosppy':
        qrs_detections = bsp_ecg.ecg(signal=sig,
                                     sampling_rate=freq_sampling,
                                     show=False)[2]
    elif algorithm == 'Christov-biosppy':
        order = int(0.3 * freq_sampling)
        filtered, _, _ = bsp_tools.filter_signal(signal=sig,
                                                 ftype='FIR',
                                                 band='bandpass',
                                                 order=order,
                                                 frequency=[3, 45],
                                                 sampling_rate=freq_sampling)
        rpeaks, = bsp_ecg.christov_segmenter(signal=filtered,
                                             sampling_rate=freq_sampling)
        rpeaks, = bsp_ecg.correct_rpeaks(signal=filtered,
                                         rpeaks=rpeaks,
                                         sampling_rate=freq_sampling,
                                         tol=0.05)
        _, qrs_detections = bsp_ecg.extract_heartbeats(
            signal=filtered,
            rpeaks=rpeaks,
            sampling_rate=freq_sampling,
            before=0.2,
            after=0.4)
    elif algorithm == 'Engelse-Zeelenberg-biosppy':
        order = int(0.3 * freq_sampling)
        filtered, _, _ = bsp_tools.filter_signal(signal=sig,
                                                 ftype='FIR',
                                                 band='bandpass',
                                                 order=order,
                                                 frequency=[3, 45],
                                                 sampling_rate=freq_sampling)
        rpeaks, = bsp_ecg.engzee_segmenter(signal=filtered,
                                           sampling_rate=freq_sampling)
        rpeaks, = bsp_ecg.correct_rpeaks(signal=filtered,
                                         rpeaks=rpeaks,
                                         sampling_rate=freq_sampling,
                                         tol=0.05)
        _, qrs_detections = bsp_ecg.extract_heartbeats(
            signal=filtered,
            rpeaks=rpeaks,
            sampling_rate=freq_sampling,
            before=0.2,
            after=0.4)
    elif algorithm == 'Gamboa-biosppy':
        order = int(0.3 * freq_sampling)
        filtered, _, _ = bsp_tools.filter_signal(signal=sig,
                                                 ftype='FIR',
                                                 band='bandpass',
                                                 order=order,
                                                 frequency=[3, 45],
                                                 sampling_rate=freq_sampling)
        rpeaks, = bsp_ecg.gamboa_segmenter(signal=filtered,
                                           sampling_rate=freq_sampling)
        rpeaks, = bsp_ecg.correct_rpeaks(signal=filtered,
                                         rpeaks=rpeaks,
                                         sampling_rate=freq_sampling,
                                         tol=0.05)
        _, qrs_detections = bsp_ecg.extract_heartbeats(
            signal=filtered,
            rpeaks=rpeaks,
            sampling_rate=freq_sampling,
            before=0.2,
            after=0.4)
    elif algorithm == 'mne-ecg':
        qrs_detections = mne_ecg.qrs_detector(freq_sampling, sig)
    elif algorithm == 'heartpy':
        rol_mean = rolling_mean(sig, windowsize=0.75, sample_rate=100.0)
        qrs_detections = hp_pkdetection.detect_peaks(
            sig, rol_mean, ma_perc=20, sample_rate=100.0)['peaklist']
    elif algorithm == 'gqrs-wfdb':
        qrs_detections = processing.qrs.gqrs_detect(sig=sig, fs=freq_sampling)
    elif algorithm == 'xqrs-wfdb':
        qrs_detections = processing.xqrs_detect(sig=sig, fs=freq_sampling)
    else:
        raise ValueError(
            f'Sorry... unknown algorithm. Please check the list {algorithms_list}'
        )
    cast_qrs_detections = [int(element) for element in qrs_detections]
    return cast_qrs_detections