Beispiel #1
0
 def test_find_closest_in_list(self):
     closest = nk.find_closest_in_list(1.8, [3, 5, 6, 1, 2])
     self.assertEqual(closest, 2)
def test_find_closest_in_list():
    closest1 = nk.find_closest_in_list(1.8, [3, 5, 6, 1, 2], strictly=True)
    closest2 = nk.find_closest_in_list(1.8, [3, 5, 6, 1, 2], strictly=False)
    assert closest1 == closest2
 def test_find_closest_in_list(self):
     closest1 = nk.find_closest_in_list(1.8, [3, 5, 6, 1, 2], strictly=True)
     closest2 = nk.find_closest_in_list(1.8, [3, 5, 6, 1, 2],
                                        strictly=False)
     self.assertEqual(closest1, closest2)
def test_find_closest_in_list():
    closest = nk.find_closest_in_list(1.8, [3, 5, 6, 1, 2])
    assert closest == 2
def ecg_wave_detector(ecg, rpeaks):
    q_waves = []
    p_waves = []
    p_waves_starts = []
    q_waves_starts = []
    s_waves = []
    t_waves = []
    t_waves_starts = []
    t_waves_ends = []
    for index, rpeak in enumerate(rpeaks[:-3]):

        try:
            epoch_before = np.array(ecg)[int(rpeaks[index-1]):int(rpeak)]
            epoch_before = epoch_before[int(len(epoch_before)/2):len(epoch_before)]
            epoch_before = list(reversed(epoch_before))

            q_wave_index = np.min(find_peaks(epoch_before)[0])
            q_wave = rpeak - int(q_wave_index)
            p_wave_index = q_wave_index + np.argmax(epoch_before[q_wave_index:])
            p_wave = rpeak - p_wave_index

            inter_pq = epoch_before[q_wave_index:p_wave_index]
            inter_pq_derivative = np.gradient(inter_pq, 2)
            q_start_index = nk.find_closest_in_list(len(inter_pq_derivative)/2, find_peaks(inter_pq_derivative)[0])
            q_start = q_wave - q_start_index

            q_waves.append(q_wave)
            p_waves.append(p_wave)
            q_waves_starts.append(q_start)
        except ValueError:
            pass
        except IndexError:
            pass

        try:
            epoch_after = np.array(ecg)[int(rpeak):int(rpeaks[index+1])]
            epoch_after = epoch_after[0:int(len(epoch_after)/2)]

            s_wave_index = np.min(find_peaks(epoch_after)[0])
            s_wave = rpeak + s_wave_index
            t_wave_index = s_wave_index + np.argmax(epoch_after[s_wave_index:])
            t_wave = rpeak + t_wave_index

            inter_st = epoch_after[s_wave_index:t_wave_index]
            inter_st_derivative = np.gradient(inter_st, 2)
            t_start_index = nk.find_closest_in_list(len(inter_st_derivative)/2, find_peaks(inter_st_derivative)[0])
            t_start = s_wave + t_start_index
            t_end = np.min(find_peaks(epoch_after[t_wave_index:])[0])
            t_end = t_wave + t_end

            s_waves.append(s_wave)
            t_waves.append(t_wave)
            t_waves_starts.append(t_start)
            t_waves_ends.append(t_end)
        except ValueError:
            pass
        except IndexError:
            pass
    ecg_waves = {"T_Waves": t_waves,
                 "P_Waves": p_waves,
                 "Q_Waves": q_waves,
                 "S_Waves": s_waves,
                 "Q_Waves_Onsets": q_waves_starts,
                 "T_Waves_Onsets": t_waves_starts,
                 "T_Waves_Ends": t_waves_ends}
    return(ecg_waves)