Exemplo n.º 1
0
def getQRSPeaks(datadir,
                qrsdir,
                record,
                q_suffix,
                qrs_suffix,
                inp_sfreq,
                signal_index=0):
    dataset = np.loadtxt(datadir + record)
    qpeaks = []
    with open(qrsdir + record + q_suffix + str(signal_index)) as qpeaks_input:
        for line in qpeaks_input:
            qpeak = int(getSplittedLine(line)[0])
            qpeaks.append(qpeak)
    with open(qrsdir + record + qrs_suffix + str(signal_index),
              "w") as qrs_output:
        qrs_window = []
        for qpeak in qpeaks:
            rpeak = findNextPeak(qpeak, 1, dataset, inp_sfreq, signal_index)
            speak = findNextPeak(rpeak, -1, dataset, inp_sfreq, signal_index)
            print >> qrs_output, "%d\t%d\t%d" % (qpeak, rpeak, speak)
def getMuvInsteadOfADC(muv_qrs, muv_filename, signal_index):
    data = np.loadtxt(muv_filename)[:, 1 + int(signal_index)]

    with open(muv_qrs + "_copy") as src, open(muv_qrs, "w") as dst:
        line = src.readline()
        print >> dst, line.strip()
        prev_rpeak = -1
        for line in src:
            line = getSplittedLine(line)
            rpeak = int(line[0])
            if prev_rpeak >= 0:
                interval = rpeak - prev_rpeak
                print >> dst, "%d\t%.1f\t%d\t%s" % (
                    prev_rpeak, amplitude, interval, "\t".join(prev_line[3:]))

            argmin_of_qs = int(line[3])
            amplitude = float(line[1])
            if amplitude > -100000.0:
                amplitude = data[rpeak + 1] - data[argmin_of_qs + 1]
            prev_line = line
            prev_rpeak = rpeak
def mergeLogFiles(records_list_filename,
                  timestamp,
                  dst_dir="good_data_log",
                  signals_count=3):
    hd_path = "/Volumes/WD500GB/WFDB/"
    log_path = "/Users/shushu/Documents/WFDB/log/"
    dst_log_path = log_path + dst_dir + "/"

    records_list = defaultdict(set)
    with open(hd_path + records_list_filename) as records_list_file:
        for line in records_list_file:
            line = getSplittedLine(line)
            records_list[line[-1]].add("/".join(line[:2]))

    for signal_index in xrange(int(signals_count)):
        signal_index = str(signal_index)
        suffix = timestamp + "_" + signal_index
        for filename in os.listdir(log_path):
            if filename.endswith(suffix):
                mergeLogFile(records_list[signal_index], log_path + filename,
                             dst_log_path + filename)
def get_noisy_segments(data_filename,
                       data_line_count,
                       r_peaks,
                       amplitudes_max,
                       signal=0):
    window = []
    segments = []
    segment = []
    window_size = 30
    min_segment_size = 80
    r_peak_index = 0
    prev_t_sample = 0
    for line in open(data_filename):
        line = getSplittedLine(line)
        sample = int(line[0])
        signal_value = float(line[1 + signal])
        if is_between_prev_t_and_next_q_samples(sample, r_peaks, prev_t_sample,
                                                r_peak_index, data_line_count):
            segment.append([sample, signal_value])
            window.append(signal_value)
            if len(window) > window_size:
                window = window[-window_size:]
                window_is_noisy = window_is_noisy_centered(
                    window, amplitudes_max, window_size)
                if window_is_noisy:
                    window = []
                    segment = segment[:-window_size]
                    if len(segment) > min_segment_size:
                        segments.append(segment[:-window_size])
                    segment = []
        else:
            if next_q_sample_is_reached(sample, r_peaks, r_peak_index):
                window = []
                if len(segment) > min_segment_size:
                    segments.append(segment)
                segment = []

                prev_t_sample = r_peaks[r_peak_index].t_sample
                r_peak_index += 1
    return segments
Exemplo n.º 5
0
def plotLog(timestamp, errors_prefix = "", mode = "muv"):
  local_path = "/Users/shushu/Documents/WFDB/"
  hd_path = "/Volumes/WD500GB/WFDB/"
  log_path = local_path + "log/good_data_log/"
  data_path = hd_path + "ptbdb/"
  plots_path = hd_path + "plots/"

  if mode != "adc":
    data_path = hd_path + "ptbdb_muv/"
    plots_path = hd_path + "plots_muv/"
  
  if errors_prefix:
    plots_path += errors_prefix + "/"
    print plots_path
  
  patients_to_diagnosis_map = getPatientsDiagnoses(local_path)
  
  log = log_path + "log_" + timestamp
  for signal_index in [2, ]:
    signal_log = log + "_" + str(signal_index)
    errors_records = getRecordsWithErrorsInQRSPeaks(log_path, errors_prefix, timestamp, signal_index)

    for line in open(signal_log):
      line = getSplittedLine(line)
      patient_record = line[0].split("=")[1]
      patient, record = patient_record.split("/")
      if patient in patients_to_diagnosis_map and (not errors_records or patient_record in errors_records):
        print patient_record
        diagnosis = patients_to_diagnosis_map[patient]
        qrs_count = str(len(open(data_path + patient_record + "_qrs_" + str(signal_index)).readlines()))
      
        info = "; ".join(line[1:]) + ";TotalQRS=" + qrs_count
        plotECG(patient_record, 
                 diagnosis + "; " + info, 
                 data_path,
                 plots_path,
                 signal_index = signal_index,
                 is_limited = True, xmin = 2000, xmax = 8000)
Exemplo n.º 6
0
def processDeviceCorr(muv_scmin, DEV_TYPE):
    signal_index = 0

    root = getDeviceRoot(DEV_TYPE)
    os.chdir(root)

    log_filename = "log"

    datadir = "data/"
    names_to_sfreqs_map = getNamesToSfreqsMap(datadir)

    resampledir = datadir + "resample/"
    qrsdir = "qrs_corr/"
    plotsdir = "plots_corr/"
    q_suffix = "_q_peaks_"
    qrs_suffix = "_qrs_peaks_"

    for record, inp_sfreq in names_to_sfreqs_map.iteritems():
        if DEV_TYPE == "TEST" and record in high_freq[DEV_TYPE] or \
        DEV_TYPE == "SFAX" and record not in ["6_2.txt", "5_1.txt", "5_2.txt", "3_1.txt", "1_2.txt",
        "2_1.txt", "1_1.txt", "3_2.txt", "4_1.txt", "4_2.txt"] and record == "7_1.txt":

            print DEV_TYPE, record
            #invertSignalBySign(root + datadir, "", record, signal_index, suffix = "_before_inv")

            data_filename = datadir + record
            qrs_input_filename = qrsdir + record + q_suffix + str(signal_index)
            qrs_output_filename = qrsdir + record + qrs_suffix + "with_errors_" + str(
                signal_index)
            corrected_qrs_output_filename = qrsdir + record + qrs_suffix + str(
                signal_index)

            lines_count = len([
                x for x in open(data_filename)
                if len(getSplittedLine(x)) > 1 + int(signal_index)
            ])
            """
      """
            getQPeaks(datadir,
                      record,
                      resampledir,
                      qrsdir,
                      q_suffix,
                      inp_sfreq,
                      muv_scmin,
                      signal_index,
                      ADC=False)
            q_count = len([x for x in open(qrs_input_filename)])
            print q_count



            calculateRPeaks(data_filename, lines_count, qrs_input_filename, \
                            qrs_output_filename, corrected_qrs_output_filename, \
                            record, log_filename,
                            signal_index = signal_index)

            plotQPeaks(datadir,
                       qrsdir,
                       plotsdir,
                       record,
                       qrs_suffix,
                       inp_sfreq,
                       peaks_count=3,
                       signal_index=signal_index,
                       start_from=0,
                       skip_windows=1,
                       window_size=10)
Exemplo n.º 7
0
patient_record = sys.argv[1].strip()

multiplier = 1.0
if len(sys.argv) > 2:
    multiplier = float(sys.argv[2])

path = "/Volumes/WD500GB/WFDB/"
db = "ptbdb"
if len(sys.argv) > 3:
    db = sys.argv[3]
path += db + "/"

filename = path + patient_record
raw_filename = filename + "_copy"

with open(filename, "w") as outp, open(raw_filename) as inp:
    line = inp.readline()
    line = getSplittedLine(line)
    elements_count = len(line)

    while True:
        signal_values = getSignalValues(line)
        if multiplier != 1.0:
            signal_values = signal_values * multiplier
        print >> outp, "%s\t%s" % (line[0], strOfList(signal_values))

        line = inp.readline()
        line = getSplittedLine(line)
        if len(line) < elements_count:
            break
def get_qrs_amplitudes_intervals(data_filename,
                                 data_line_count,
                                 q_samples,
                                 signal=0):
    r_peaks = []
    if data_line_count == 0:
        return r_peaks

    with open(data_filename) as data_file, open("f", "w") as outp:
        q_samples_count = len(q_samples)
        current_q = 0
        last_r_sample = 0
        qr_intervals = np.empty([0])
        qr_amplitudes = np.empty([0])
        q_size = Q_SIZE = 20
        S_SIZE = 10
        pq_size = 10
        q_window = []
        QRS_SIZE = 100
        MINUS_INF = -1e10
        min_of_q_s_value = min_of_q_values = 0
        qr_interval_min_value, qr_interval_max_value = 20, 200
        qr_amplitude_min_value, qr_amplitude_max_value = 150, MAX_AMP
        qr_interval_min_thres, qr_interval_max_thres = qr_interval_min_value, qr_interval_max_value
        qr_amplitude_min_thres, qr_amplitude_max_thres = qr_amplitude_min_value, qr_amplitude_max_value

        for line in data_file:
            splitted_line = getSplittedLine(line)
            if len(splitted_line) > 1 + signal:
                sample = int(splitted_line[0])
                signal_value = float(splitted_line[1 + signal])
                if current_q < q_samples_count and 0 <= q_samples[
                        current_q] - sample < pq_size:
                    q_window.append(signal_value)
                #если встретили Q зубец
                if current_q < q_samples_count and sample == q_samples[current_q] or \
                  current_q == q_samples_count and \
                  sample == min(q_samples[current_q - 1] + Q_SIZE + QRS_SIZE + S_SIZE, data_line_count - 1):
                    if current_q > 0:
                        r_peaks.append(RPeak(r_sample, r_value - min_of_q_s_value, \
                                             r_sample - last_r_sample, argmin_of_q_s_sample, \
                                             q_sample, t_sample))
                        print >> outp, "\nQCURR= ", current_q, "QSIZE= ", q_size
                        print >> outp, "QRAmpMin= %.2f\tQRAmpMax= %.2f" % (
                            qr_amplitude_min_thres, qr_amplitude_max_thres)
                        print >> outp, "R= ", r_value
                        print >> outp, "RSamp= %d\tCorrQ= %d\tSSamp= %d\tTSamp= %d\t" % (
                            r_sample, corrected_q_sample, s_sample, t_sample)
                        print >> outp, "QRS_SIZE= %.2f\tQRIntMinThres= %.2f,\tQRIntMaxThres= %.2f" % (
                            QRS_SIZE, qr_interval_min_thres,
                            qr_interval_max_thres)
                        #if r_sample < q_sample or s_sample < r_sample or t_sample < s_sample:
                        #  print "WOHOO", current_q

                        if r_sample > corrected_q_sample:
                            #print qr_intervals
                            qr_intervals, qr_interval_min_thres, qr_interval_max_thres = \
                                  get_qr_intervals_thres(qr_intervals,
                                                         r_sample - corrected_q_sample,
                                                         nsigma = 4,
                                                         max_thres_max_value = qr_interval_max_value,
                                                         min_thres_min_value = qr_interval_min_value,
                                                         stdev_add_term = 30)

                            qr_amplitudes, qr_amplitude_min_thres, qr_amplitude_max_thres = \
                                  get_qr_intervals_thres(qr_amplitudes,
                                                         r_value - min_of_q_values,
                                                         nsigma = 4,
                                                         filter_by_max = False,
                                                         #filter_by_min = False,
                                                         max_thres_max_value = qr_amplitude_max_value,
                                                         min_thres_min_value = qr_amplitude_min_value,
                                                         stdev_add_term = 80)

                        last_r_sample = r_sample
                    r_value, r_sample = MINUS_INF, sample
                    min_of_q_s_value = min_of_q_values = signal_value
                    half_window_size = min(qr_interval_min_thres / 2, 6)
                    window_size = 2 * half_window_size + 1
                    window = []
                    QRS_SIZE = 2 * qr_interval_max_thres
                    q_size = Q_SIZE + 60
                    corrected_q_sample = q_sample = sample
                    q_shift = 0
                    if len(q_window) > 0 and window_is_not_noisy(
                            q_window, qr_amplitude_max_thres):
                        window_min_index, window_min_value, window_min_sample = find_min_or_max_of_window(
                            q_window, sample, min_or_max="MIN")
                        if window_min_value < signal_value:
                            corrected_q_sample = q_sample = window_min_sample
                            min_of_q_s_value = min_of_q_values = window_min_value
                            q_shift = sample - q_sample
                            q_size += q_shift
                            qr_interval_max_thres += q_shift
                            window = q_window[window_min_index:]
                    last_r_peak = corrected_q_sample
                    r_value, r_sample = MINUS_INF, last_r_peak
                    s_sample, s_value = t_sample, t_value = r_sample, r_value
                    argmin_of_q_s_sample = corrected_q_sample
                    q_window = []
                    current_q += 1

                elif 0 < current_q <= q_samples_count:
                    window.append(signal_value)
                    if window_size - 1 <= sample - q_sample:
                        window = window[-window_size:]
                        window_middle_sample = sample - half_window_size - 1
                        #window_middle_sample = sample - half_window_size
                        window_middle_value = window[-half_window_size - 1]
                        window_not_noisy = window_is_not_noisy(
                            window, qr_amplitude_max_thres)
                        if sample - q_sample < q_size:
                            if q_size < q_shift + Q_SIZE + 50:
                                if window_not_noisy:
                                    if window_middle_value < min_of_q_values:
                                        min_of_q_s_value = min_of_q_values = window_middle_value
                                        argmin_of_q_s_sample = corrected_q_sample = window_middle_sample
                                        if corrected_q_sample - q_sample > q_size / 4:
                                            q_size += 10
                                        last_r_peak = corrected_q_sample
                                        r_sample, r_value = window_middle_sample, window_middle_value
                                else:
                                    q_size += 20 / float(window_size)
                        if window_middle_sample > corrected_q_sample and window_not_noisy:
                            #если встретили S зубец
                            if 0 < window_middle_sample - r_sample < qr_interval_max_thres + S_SIZE:
                                if window_middle_value < s_value:
                                    s_sample, s_value = window_middle_sample, window_middle_value
                                    t_sample, t_value = window_middle_sample, window_middle_value

                                if window_middle_value < min_of_q_s_value:
                                    argmin_of_q_s_sample, min_of_q_s_value = window_middle_sample, window_middle_value

                            T_SIZE = qr_interval_max_thres
                            #если встретили T зубец
                            if 0 < window_middle_sample - s_sample < T_SIZE:
                                if window_middle_value > t_value:
                                    t_sample, t_value = window_middle_sample, window_middle_value

                            #если встретили R зубец
                            if window_middle_value > r_value and \
                               max(qr_amplitude_min_thres, qr_amplitude_min_value) < window_middle_value - min_of_q_values < \
                               qr_amplitude_max_thres:
                                if last_r_peak == corrected_q_sample or window_middle_sample - corrected_q_sample < qr_interval_max_thres:
                                    r_sample, r_value = window_middle_sample, window_middle_value
                                    last_r_peak = r_sample
                                    s_sample, s_value = r_sample, r_value
    return r_peaks
        hd_path = sys.argv[3] + "/"

    signal = "0"
    if len(sys.argv) > 4:
        signal = sys.argv[4]

    data_filename = hd_path + patient
    qrs_input_filename = hd_path + patient + "_sqrs_output_" + signal

    qrs_output_filename = hd_path + patient + "_qrs_"
    corrected_qrs_output_filename = qrs_output_filename + "corrected_" + signal
    qrs_output_filename = qrs_output_filename + signal

    lines_count = len([
        x for x in open(data_filename)
        if len(getSplittedLine(x)) > 1 + int(signal)
    ])

    mode = "ONE"
    if len(sys.argv) > 5:
        mode = sys.argv[5]

    noise_by_window_size_filename = "noise_by_window_size_" + signal
    noise_filename = local_path + patient + "_noise_" + signal
    """
  process_noise(corrected_qrs_output_filename, data_filename, lines_count, patient, \
                noise_by_window_size_filename, noise_filename, signal = int(signal), mode = mode)
  """
    calculate_r_peaks(data_filename, lines_count, qrs_input_filename, \
                      qrs_output_filename, corrected_qrs_output_filename, \
                      patient, log_filename,