Exemplo n.º 1
0
    def test_1b(self):
        sig, fields = wfdb.srdsamp('sampledata/a103l',
                             sampfrom=12500, sampto=40000, channels=[2, 0])
        siground = np.round(sig, decimals=8)
        targetsig = np.genfromtxt('tests/targetoutputdata/target1b')

        # Compare data streaming from physiobank
        pbsig, pbfields = wfdb.srdsamp('a103l', pbdir = 'challenge/2015/training',
                             sampfrom=12500, sampto=40000, channels=[2, 0])
        assert np.array_equal(siground, targetsig)
        assert np.array_equal(sig, pbsig) and fields == pbfields
Exemplo n.º 2
0
    def test_2a(self):
        sig, fields = wfdb.srdsamp('sampledata/100')
        siground = np.round(sig, decimals=8)
        targetsig = np.genfromtxt('tests/targetoutputdata/target2a')

        # Compare data streaming from physiobank
        pbsig, pbfields = wfdb.srdsamp('100', pbdir = 'mitdb')
        # This comment line was manually added and is not present in the original physiobank record
        del(fields['comments'][0])

        assert np.array_equal(siground, targetsig)
        assert np.array_equal(sig, pbsig) and fields == pbfields
Exemplo n.º 3
0
    def test_10(self):
        sig, fields = wfdb.srdsamp('sampledata/03700181',
                             channels=[0, 2], sampfrom=1000, sampto=16000)
        siground = np.round(sig, decimals=8)
        targetsig = np.genfromtxt('tests/targetoutputdata/target10')

        # Compare data streaming from physiobank
        pbsig, pbfields = wfdb.srdsamp('03700181', pbdir = 'mimicdb/037',
                             channels=[0, 2], sampfrom=1000, sampto=16000)

        assert np.array_equal(siground, targetsig)
        assert np.array_equal(sig, pbsig) and fields == pbfields
Exemplo n.º 4
0
    def test_3b(self):
        sig, fields = wfdb.srdsamp('sampledata/s0010_re', sampfrom=5000,
                             sampto=38000, channels=[13, 0, 4, 8, 3])
        siground = np.round(sig, decimals=8)
        targetsig = np.genfromtxt('tests/targetoutputdata/target3b')

        # Compare data streaming from physiobank
        pbsig, pbfields = wfdb.srdsamp('s0010_re', sampfrom=5000, pbdir = 'ptbdb/patient001',
                             sampto=38000, channels=[13, 0, 4, 8, 3])

        assert np.array_equal(siground, targetsig)
        assert np.array_equal(sig, pbsig) and fields == pbfields
Exemplo n.º 5
0
    def test_1d(self):
        sig, fields = wfdb.srdsamp('sampledata/3000003_0003',
                             sampfrom=125, sampto=1000, channels=[1])
        siground = np.round(sig, decimals=8)
        targetsig = np.genfromtxt('tests/targetoutputdata/target1d')
        targetsig = targetsig.reshape(len(targetsig), 1)

        # Compare data streaming from physiobank
        pbsig, pbfields = wfdb.srdsamp('3000003_0003', pbdir = 'mimic2wdb/30/3000003/',
                             sampfrom=125, sampto=1000, channels=[1])

        assert np.array_equal(siground, targetsig)
        assert np.array_equal(sig, pbsig) and fields == pbfields
Exemplo n.º 6
0
def downloadWFDB(onda, carpeta, sdb, ondaName):
    signalII = None
    try:
        sig, fields = wfdb.srdsamp(onda,
                                   pbdir='mimic2wdb/matched/' + carpeta,
                                   sampto=1)
        channel = fields['signame'].index("II")
        if (channel != None):
            sig, fields = wfdb.srdsamp(onda,
                                       pbdir='mimic2wdb/matched/' + carpeta,
                                       channels=[channel])
            print("downloaded:" + ondaName)
            uploadToSDB(sig[:, 0], ondaName, sdb)
    except ValueError:
        print(ondaName + " no contains Signal II")
    return
Exemplo n.º 7
0
def generateCSVPerFile(filename):
    if (re.search('\.dat', filename) != None):
        fileBasename = os.path.splitext(filename)[0]
        print("fileBasename = ", fileBasename)
        try:
            sig, fields = wfdb.srdsamp(fileBasename)
        except ValueError:
            print("ValueError occurred for fieBasename ", fileBasename)
            return
        numChannels = len(fields['signame'])
        for chIndex in range(numChannels):
            channelName = fields['signame'][chIndex]
            perChannelFilename = '.'.join([fileBasename, channelName, 'csv'])
            print("perChannelFilename=", perChannelFilename)

            numSamples = fields['fs'] * 3600
            fileHandle = open(perChannelFilename, 'w')
            for chIndex in range(numChannels):
                for i in range(numSamples):
                    timeStamp = i * fields['fs']
                    fileHandle.write(','.join(
                        [str(timeStamp), str(sig[i, chIndex])]))
                    fileHandle.write('\n')
            fileHandle.close()
    return
Exemplo n.º 8
0
def preprocess(count = 30):
    print("Preprocessing")
    sig, fields = wfdb.srdsamp('data/mitdb/100')

    ecg = sig[:500000,0]
    diff = np.diff(ecg)

    emax = np.max(ecg)
    emin = np.min(ecg)
    count = count - 2
    bins = [ emin + (emax - emin)/count * i for i in range(count+1) ]
    quantized = np.digitize(ecg, bins)
    dequant = np.empty(len(quantized))

    # dmax = np.max(diff)-0.01
    # dmin = np.min(diff)+0.01
    # count = count - 2
    # print(dmin,dmax)
    # bins = [ dmin + (dmax - dmin)/count * i for i in range(count+1) ]
    # print("bins",len(bins))
    # quantized = np.digitize(diff, bins)
    #
    # dequant = np.empty(len(quantized))

    running = 0
    for i in range(len(dequant)):
        v = quantized[i]
        running += bins[v-1]
        dequant[i] = running

    return ecg,quantized,dequant,bins
Exemplo n.º 9
0
def generateCSVPerFile(filename):
    if (re.search('\.dat', filename) != None):
        fileBasename = os.path.splitext(filename)[0]
        print("fileBasename = ", fileBasename)
        try:
            sig, fields = wfdb.srdsamp(fileBasename)
        except ValueError:
            print("ValueError occurred for fieBasename ", fileBasename)
            return
        numChannels = len(fields['signame'])
        numSamples = fields['fs'] * 3600
        allChannelsDF = pd.DataFrame(data=sig[:, :], columns=fields['signame'])
        print(allChannelsDF.shape)
        #        for i in range(20):
        #            allChannelsDF = allChannelsDF.add(other = sig[i, :])
        print(allChannelsDF.head())


#        for chIndex in range(numChannels):
#            channelName = fields['signame'][chIndex]
#            perChannelFilename = '.'.join([fileBasename, channelName, 'csv'])
#            print ("perChannelFilename=", perChannelFilename)
#
#            fileHandle = open(perChannelFilename, 'w')
#            for chIndex in range(numChannels):
#                for i in range(numSamples):
#                    timeStamp = i * fields['fs']
#                    fileHandle.write(','.join([str(timeStamp), str(sig[i, chIndex])]))
#                    fileHandle.write('\n')
#            fileHandle.close()
    return
def classify_alarm(data_path, ann_path, sample_name, ecg_ann_type, verbose=False):
    sig, fields = wfdb.srdsamp(data_path + sample_name)

    is_regular = is_sample_regular(data_path, ann_path, sample_name, ecg_ann_type)
    if is_regular:
        return False

    alarm_type = sample_name[0]
    if alarm_type == "a":
        arrhythmia_test = test_asystole

    elif alarm_type == "b":
        arrhythmia_test = test_bradycardia

    elif alarm_type == "t":
        arrhythmia_test = test_tachycardia

    elif alarm_type == "v":
        arrhythmia_test = test_ventricular_tachycardia

    elif alarm_type == "f":
        arrhythmia_test = test_ventricular_flutter_fibrillation

    else:
        raise Exception("Unknown arrhythmia alarm type")

    # try:
    return arrhythmia_test(data_path, ann_path, sample_name, ecg_ann_type, verbose)
Exemplo n.º 11
0
def run_one_sample():
    # sample_name = "v100s" # false alarm
    # sample_name = "v141l" # noisy at beginning
    # sample_name = "v159l" # quite clean
    # sample_name = "v206s" # high baseline
    # sample_name = "v143l"
    # sample_name = "v696s"
    sample_name = "v837l"
    metric = "min"
    channel_index = 0
    ann_fs = 250.
    ann_type = 'gqrs' + str(channel_index)

    sig, fields = wfdb.srdsamp(data_path + sample_name)
    channel_sig = sig[:, channel_index]

    vtach_beats, nonvtach_beats = ventricular_beat_annotations_dtw(
        channel_sig, ann_path, sample_name, metric, start_time, end_time,
        ann_type)

    plt.figure(figsize=[8, 5])
    plt.plot(channel_sig[int(start_time * 250.):int(end_time * 250.)], 'b-')
    plt.plot([int(index - 250. * start_time) for index in nonvtach_beats],
             [channel_sig[int(index)] for index in nonvtach_beats],
             'bo',
             markersize=8)
    plt.plot([int(index - 250. * start_time) for index in vtach_beats],
             [channel_sig[int(index)] for index in vtach_beats],
             'ro',
             markersize=8)
    plt.show()
def test_asystole(data_path, ann_path, sample_name, ecg_ann_type, verbose=False):
    sig, fields = wfdb.srdsamp(data_path + sample_name)
    channels = fields['signame']
    fs = fields['fs']

    # Start and end given in seconds
    start, end, alarm_duration = get_start_and_end(fields)
    current_start = start
    current_end = current_start + parameters.ASYSTOLE_WINDOW_SIZE

    max_score = 0

    while current_end < end:
        start_index, end_index = int(current_start*fs), int(current_end*fs)
        subsig = sig[start_index:end_index,:]
        summed_asystole_score = calc_summed_asystole_score(ann_path, sample_name, subsig, channels, ecg_ann_type,
                                                           current_start, current_end, verbose)
        max_score = max(max_score, summed_asystole_score)

        current_start += parameters.ASYSTOLE_ROLLING_INCREMENT
        current_end = current_start + parameters.ASYSTOLE_WINDOW_SIZE

    if verbose:
        print(sample_name + " has max asystole score: " + str(max_score))

    return max_score > 0
Exemplo n.º 13
0
def calculateFourierTransform(filename):
    if (re.search('\.dat', filename) != None):
        fileBasename = os.path.splitext(filename)[0]
        print("fileBasename = ", fileBasename)
        try:
            sig, fields = wfdb.srdsamp(fileBasename)
        except ValueError:
            print("ValueError occurred for fieBasename ", fileBasename)
            return
        numChannels = len(fields['signame'])
        numSamples = fields['fs'] * 3600
        # fields['fs'] contains the frequency
        numSamplesPerEpoch = int(fields['fs'] * 1000 / epochLength)
        allChannelsDF = pd.DataFrame(data=sig[:, :], columns=fields['signame'])
        llDf = pd.DataFrame(columns=fields['signame'])
        print(allChannelsDF.shape)
        #        for i in range(20):
        #            allChannelsDF = allChannelsDF.add(other = sig[i, :])
        print(allChannelsDF.head())

        for i in range(10):
            llDf = llDf.append(allChannelsDF.iloc[i] -
                               allChannelsDF.iloc[i + 1],
                               ignore_index=True)
        print(llDf.head())
    return
Exemplo n.º 14
0
def readData(filename):
    global annotationArray
    global signalArray
    #read in data using
    record = wfdb.rdsamp(filename, sampto=sampSize)
    annotation = wfdb.rdann(filename, 'atr', sampto=sampSize)
    sig, fields = wfdb.srdsamp(filename, sampto=sampSize)

    #read record and signal into an array

    for i in range(0, len(sig)):
        signalArray = np.append(
            signalArray,
            np.array([[float("{0:.3f}".format(i * sampIncrement)),
                       sig[i][0]]]),
            axis=0)

    #read annotations into array

    for i in range(0, len(annotation.annsamp)):
        annotationArray = np.append(
            annotationArray,
            np.array([[
                annotation.annsamp[i],
                float("{0:.3f}".format(annotation.annsamp[i] / 360)),
                float("{0:.3f}".format(signalArray[:,
                                                   1][annotation.annsamp[i]])),
                annotation.anntype[i]
            ]]),
            axis=0)
Exemplo n.º 15
0
def readData(filename):
    global annotationArray
    global signalArray
    #read in data using
    '''
    record = wfdb.rdsamp(filename, sampto = sampSize)
    annotation = wfdb.rdann(filename, 'atr', sampto = sampSize)
    sig, fields = wfdb.srdsamp(filename, sampto = sampSize)
    '''
    record = wfdb.rdsamp(filename)
    annotation = wfdb.rdann(filename, 'atr')
    sig, fields = wfdb.srdsamp(filename)
    print("\nReading in ",
          len(sig) / 300, "seconds of data from file", filename)

    for i in range(0, len(sig)):
        if (i % 100 == 0):
            sys.stdout.write("\rReading Data ... {0:.2f}%".format(
                (float(i) / len(sig)) * 100))
            sys.stdout.flush()
        signalArray.append(sig[i])

    sys.stdout.write("\rReading Data ... complete!")
    sys.stdout.flush()

    for i in range(1, len(annotation.annsamp)):
        annotationArray.append([annotation.annsamp[i], annotation.anntype[i]])
Exemplo n.º 16
0
def generate_training(filename):
    training = []

    with open(filename, 'r') as f:
        reader = csv.DictReader(f)

        for row in reader:
            sample_name = row['sample_name']
            is_true_beat = int(row['is_vtach']) == 1

            start_time = float(row['start_time'])
            end_time = float(row['end_time'])

            # peak_time = float(row['peak_time'])
            # start_time = peak_time - AVERAGE_START_DIFF
            # end_time = peak_time + AVERAGE_END_DIFF

            sig, fields = wfdb.srdsamp(data_path + sample_name)
            start_index = int(start_time * 250.)
            end_index = int(end_time * 250.)
            channel_index = fields['signame'].index(row['lead'])

            beat_sig = sig[start_index:end_index, channel_index]

            training.append((beat_sig, is_true_beat, sample_name))

    return training
Exemplo n.º 17
0
def download_word(wave):
    """downloads the word, after searching it in the mimic data base.
    word -- the word that represents the wave
    """
    print(wave['word'], wave['from'], wave['to'], wave['record'])
    onda = wave['record'].split("/")[3]
    pbdir = wave['record'].replace("/" + onda, '')
    if 'mimic3wdb' in wave['record']:
        pbdir = onda.split("-")[0].replace("s", '').zfill(6)
        onda = onda.replace(onda.split("-")[0].replace("s", ''), pbdir)
        pbdir = (wave['record'].split("/")[0] + '/' +
                 wave['record'].split("/")[1] + '/p' + pbdir[:2] + '/p' +
                 pbdir + '/')
        onda = onda.replace('s', 'p')
    _, fields = wfdb.srdsamp(onda, pbdir=pbdir, sampto=1)
    signal_ii = fields['signame'].index("II")
    sfrom = wave['from'] - 20
    sto = wave['to']
    original = wfdb.rdsamp(onda,
                           pbdir=pbdir,
                           channels=[signal_ii],
                           sampfrom=sfrom,
                           sampto=sto).p_signals
    original = original[~np.isnan(original)]
    save_word(original, wave['word'])
    return original
Exemplo n.º 18
0
    def __init__(self, num_training_cases, dim):
        # Import data from dataset 14172
        signals, fields = wfdb.srdsamp('../data/14172', channels=[0, 1])
        # print(self.signals)
        # display(self.fields)
        self.annotation = wfdb.rdann(
            '../data/14172',
            'atr')  # Import annotations from the same data set
        peaks = self.annotation.annsamp  # Let's find the self.peaks of each ECG where the annotations are
        signals = np.matrix.transpose(signals)

        # Don't take into account border self.peaks in case they are interrupted
        peaks[-1] = 0
        peaks[-2] = 0
        peaks[0] = 0
        peaks[1] = 0

        self.size_hb = 125  # Size of heartbeat
        self.num_hb = sum(peaks > self.size_hb)  # Count number of annotations
        self.heartbeats = np.zeros([self.num_hb, 2 * self.size_hb
                                    ])  # Save space for the data matrix
        idx_hb = 0  # Initialize the index for each peak
        for i in peaks:
            if i > self.size_hb:  # just in case the first heartbeat is truncated
                self.heartbeats[idx_hb] = signals[0][
                    i - self.size_hb:i +
                    self.size_hb]  # take all samples from left and right of i
                idx_hb += 1
        self.heartbeats = np.transpose(self.heartbeats)

        self.num_hb = self.heartbeats.shape[1]
        print('Number of Heartbeats: ', self.num_hb)

        self.heartbeats = self.calculate_means(
        )  # Delete the mean from the data.

        self.print_kinds()  # View the kinds of hearbeats
        self.classes = self.calculate_classes(
        )  # Get the kind of each heartbeat (N, V, S, J or others)
        [
            self.heartbeatsN, self.heartbeatsV, self.heartbeatsS,
            self.heartbeatsJ
        ] = self.classify()  # get idx by class

        self.filter_data(dim=dim)
        [
            self.heartbeatsN, self.heartbeatsV, self.heartbeatsS,
            self.heartbeatsJ
        ] = self.classify()
        # Training Set
        self.num_training_cases = num_training_cases  # set number of training cases
        [self.trainN, self.trainV, self.trainS,
         self.trainJ] = self.generate_training_cases()
        self.training_classes = [
            i for i in range(0, 4) for _ in range(0, self.num_training_cases)
        ]
        self.training_set = np.concatenate(
            (self.trainN, self.trainV, self.trainS, self.trainJ), axis=1)
        self.training_set = np.transpose(self.training_set)
def test_ventricular_flutter_fibrillation(data_path,
                                          ann_path,
                                          sample_name,
                                          ecg_ann_type,
                                          verbose=False,
                                          fs=parameters.DEFAULT_ECG_FS,
                                          ann_fs=parameters.DEFAULT_ECG_FS,
                                          std_threshold=parameters.VFIB_ABP_THRESHOLD,
                                          window_size=parameters.VFIB_WINDOW_SIZE,
                                          rolling_increment=parameters.VFIB_ROLLING_INCREMENT):
    sig, fields = wfdb.srdsamp(data_path + sample_name)
    channels = fields['signame']

    # Start and end given in seconds
    start, end, alarm_duration = get_start_and_end(fields)
    alarm_sig = sig[int(start*fs):int(end*fs),:]

    ecg_channels = get_channels_of_type(channels, "ECG")
    abp_channels = get_channels_of_type(channels, "BP")

    # Find max duration of low frequency signal from all channels
    dlfmax = 0
    for channel_index in ecg_channels:
        channel_index = int(channel_index)
        channel_dlfmax = calculate_dlfmax(alarm_sig[:,channel_index])
        dlfmax = max(dlfmax, channel_dlfmax)

    # Initialize R vector to a value based on the D_lfmax (duration of low frequency)
    if dlfmax > parameters.VFIB_DLFMAX_LIMIT:
        r_vector_value = 1.
    else:
        r_vector_value = 0.
    size = int((alarm_duration - window_size) / rolling_increment) + 1
    r_vector = [r_vector_value] * size

    # Adjust R vector based on whether standard deviation of ABP channel is > or < the threshold
    for channel_index in abp_channels:
        r_delta = get_abp_std_scores(alarm_sig[:,int(channel_index)], std_threshold, window_size, rolling_increment)
        r_vector = r_vector + r_delta

    # Adjust R vector based on dominant frequency in signal
    for channel_index in ecg_channels:
        channel_index = int(channel_index)

        dominant_freqs = get_dominant_freq_array(alarm_sig[:,channel_index])
        regular_activity = get_regular_activity_array(alarm_sig, fields, ann_path, sample_name, ecg_ann_type)
        adjusted_dominant_freqs = adjust_dominant_freqs(dominant_freqs, regular_activity)

        new_r_vector = np.array([])
        for dominant_freq, r_value in zip(adjusted_dominant_freqs, r_vector):
            if dominant_freq < parameters.VFIB_DOMINANT_FREQ_THRESHOLD:
                new_r_vector = np.append(new_r_vector, 0.)
            else:
                new_r_vector = np.append(new_r_vector, r_value)

        r_vector = new_r_vector

    return any([ r_value > 0 for r_value in r_vector ])
Exemplo n.º 20
0
    def test_3(self):
        sig, _ = wfdb.srdsamp('sampledata/100')
        lb = -5
        ub = 15

        x = wfdb.processing.normalize(sig[:, 0], lb, ub)
        assert x.shape[0] == sig.shape[0]
        assert numpy.min(x) >= lb
        assert numpy.max(x) <= ub
Exemplo n.º 21
0
def get_full_ecg(filenumber, base_filename="mit_bih/"):
    max_length = 430
    
    annotation = wfdb.rdann(base_filename+str(filenumber), 'atr', sampto=650000)
    ecg_signal, _ = wfdb.srdsamp(base_filename+str(filenumber))

    ecg_lead_1 = ecg_signal[:,0]
    ecg_lead_2 = ecg_signal[:,1]

    unique_labels_df = annotation.get_contained_labels(inplace=False)
    annotation_indices = annotation.sample
    annotation_classes = annotation.symbol
    sampling_frequency = annotation.fs

    for index,beat_class in enumerate(annotation_classes):
        if beat_class not in beat_dict:
            np.delete(annotation_indices, index)
            np.delete(annotation_classes, index)

    complete_beats = []
    beat_indexes = []

    for index,current_beat in enumerate(annotation_indices):
        if index > 0 and index<len(annotation_indices)-1:
            beat_before = annotation_indices[index-1]
            beat_after = annotation_indices[index+1]

            start_recording = ((current_beat-beat_before)//2) + beat_before
            end_recording = ((beat_after-current_beat)//2)+current_beat

            complete_beat_1 = ecg_lead_1[start_recording:end_recording]
            complete_beat_2 = ecg_lead_2[start_recording:end_recording]

            if "st_petersburg" in base_filename and downsample_to_match_incart != 1:
                complete_beat_1 = signal.resample(complete_beat_1,int(round(len(complete_beat_1)*1.401)))
                complete_beat_2 = signal.resample(complete_beat_2,int(round(len(complete_beat_2)*1.401)))
            elif downsample_to_match_incart == 1:
                complete_beat_1 = signal.resample(complete_beat_1,257)
                complete_beat_2 = signal.resample(complete_beat_2,257)

            if len(complete_beat_1) <= max_length:
                complete_beat_1 = np.pad(complete_beat_1, (0, max_length - len(complete_beat_1)), 'constant')
                complete_beat_2 = np.pad(complete_beat_2, (0, max_length - len(complete_beat_2)), 'constant')

                complete_beat = np.append(complete_beat_1, complete_beat_2)

                #print(complete_beat)
                beat_label = annotation_classes[index]

                #beat_indexes.append([start_recording,end_recording,beat_label])
                this_index = current_beat - start_recording
                
                beat_indexes.append(this_index)
                complete_beats.append([complete_beat,beat_label])
    
    return complete_beats, beat_indexes
Exemplo n.º 22
0
def filtrar_ler(f):
    # read signal

    sig, fields = wfdb.srdsamp(f, channels=[0])
    allrecord = wfdb.rdsamp(f, channels=[0], physical=False)
    # time discrete signal -> numpy array
    xall = allrecord.d_signals[:, 0]
    # freq at which the signal is sampled
    fs = fields['fs']
    print("frequencia = " + str(fs))
    return xall, fs
def is_sample_regular(data_path,
                      ann_path,
                      sample_name,
                      ecg_ann_type,
                      start=None,
                      end=None,
                      verbose=False):
    sig, fields = wfdb.srdsamp(data_path + sample_name)
    channels = fields['signame']
    nonresp_channels = [ channels.index(channel) for channel in channels if channel != "RESP" ]

    if start is None or end is None:
        start, end, alarm_duration = get_start_and_end(fields)
    else:
        alarm_duration = end - start

    # try:
    #     invalids = {}
    #     for channel_index in nonresp_channels:
    #         channel = channels[channel_index]

    #         with open(ann_path + sample_name + "-invalids.csv", "r") as f:
    #             reader = csv.reader(f)
    #             channel_invalids = [ int(float(row[channel_index])) for row in reader]
    #             invalids[channel] = channel_invalids[start*250:end*250]

    # except Exception as e:
    #     print("Error finding invalids for sample " + sample_name, e)
    #     invalids = calculate_invalids_sig(sig, fields, start, end)

    invalids = calculate_invalids_sig(sig, fields, start, end)

    for channel_index in range(len(channels)):
        channel = channels[channel_index]
        channel_type = get_channel_type(channel)

        # Ignore respiratory channel
        if channel_type == "Resp":
            continue

        alarm_prefix = sample_name[0]
        # Only use ECG channels for ventricular fib
        if alarm_prefix == "f":
            if channel_type != "ECG":
                continue

        rr = get_channel_rr_intervals(ann_path, sample_name, channel_index, fields, ecg_ann_type)

        is_regular = check_interval_regular_activity(rr, invalids, alarm_duration, channel)

        # If any channel exhibits regular activity, deem signal as regular activity
        if is_regular:
            return True
    return False
Exemplo n.º 24
0
def loadAllData(sigType="MLII", directory="mitdb"):
    signalType = sigType
    for file in os.listdir(directory):
        if file.endswith(".dat"):
            sig, fields = wfdb.srdsamp(directory + "/" +
                                       os.path.splitext(file)[0])
            if (fields['signame'][0] == signalType):
                signalIndex = 0
            elif (fields['signame'][1] == signalType):
                signalIndex = 1
            readData(directory + '/' + os.path.splitext(file)[0])
    print("\nTotal signals ", len(signalArray))
Exemplo n.º 25
0
    def test_1(self):
        sig, fields = wfdb.srdsamp('sampledata/100')
        ann = wfdb.rdann('sampledata/100', 'atr')

        fs = fields['fs']
        fs_target = 50

        new_sig, new_ann = wfdb.processing.resample_singlechan(sig[:, 0], ann, fs, fs_target)

        expected_length = int(sig.shape[0]*fs_target/fs)

        assert new_sig.shape[0] == expected_length
def get_full_ecg(filenumber,
                 base_filename="external_original/mit_bih_normal/"):
    max_length = 1300

    annotation = wfdb.rdann(base_filename + str(filenumber),
                            'atr',
                            sampto=650000)
    ecg_signal, _ = wfdb.srdsamp(base_filename + str(filenumber))

    ecg_lead_1 = ecg_signal[:, 0]
    ecg_lead_2 = ecg_signal[:, 1]

    unique_labels_df = annotation.get_contained_labels(inplace=False)
    annotation_indices = annotation.sample
    annotation_classes = annotation.symbol
    sampling_frequency = annotation.fs

    for index, beat_class in enumerate(annotation_classes):
        if str(beat_class) not in beat_dict:
            np.delete(annotation_indices, index)
            np.delete(annotation_classes, index)

    complete_beats = []
    beat_indexes = []

    for index, current_beat in enumerate(annotation_indices):
        if index > 0 and index < len(annotation_indices) - 1:
            beat_before = annotation_indices[index - 1]
            beat_after = annotation_indices[index + 1]

            start_recording = ((current_beat - beat_before) // 2) + beat_before
            end_recording = ((beat_after - current_beat) // 2) + current_beat

            complete_beat_1 = ecg_lead_1[start_recording:end_recording]
            complete_beat_1 = np.pad(complete_beat_1,
                                     (0, max_length - len(complete_beat_1)),
                                     'constant')

            complete_beat_2 = ecg_lead_2[start_recording:end_recording]
            complete_beat_2 = np.pad(complete_beat_2,
                                     (0, max_length - len(complete_beat_2)),
                                     'constant')

            complete_beat = np.append(complete_beat_1, complete_beat_2)

            #print(complete_beat)
            beat_label = annotation_classes[index]

            beat_indexes.append([start_recording, end_recording, beat_label])
            complete_beats.append([complete_beat, beat_label])

    return complete_beats
Exemplo n.º 27
0
    def test_4c(self):
        sig, fields = wfdb.srdsamp('sampledata/03700181',
                             channels=[0, 2], sampfrom=1000, sampto=16000)
        siground = np.round(sig, decimals=8)
        targetsig = np.genfromtxt('tests/targetoutputdata/target4c')

        # Compare data streaming from physiobank
        pbsig, pbfields = wfdb.srdsamp('03700181', pbdir = 'mimicdb/037',
                             channels=[0, 2], sampfrom=1000, sampto=16000)

        # Test file writing. Multiple samples per frame and skew.
        # Have to read all the samples in the record, ignoring skew
        recordnoskew = wfdb.rdsamp('sampledata/03700181', physical=False,
                                   smoothframes=False, ignoreskew=True)
        recordnoskew.wrsamp(expanded=True)
        # Read the written record
        writesig, writefields = wfdb.srdsamp('03700181', channels=[0, 2],
                                              sampfrom=1000, sampto=16000)

        assert np.array_equal(siground, targetsig)
        assert np.array_equal(sig, pbsig) and fields == pbfields
        assert np.array_equal(sig, writesig) and fields == writefields
Exemplo n.º 28
0
def loadPhysionetMetadata(
        src,
        recordPrefix=BASE_ctu_uhb_ctgdb,
        selectedFields=['pH', 'BDecf', 'pCO2', 'BE', 'Apgar1', 'Apgar5']):
    recordName = recordPrefix + '/' + src
    # ctgRecord = wfdb.rdsamp(fname)
    sig, fields = wfdb.srdsamp(recordName)

    meta = {}
    for entry in fields['comments']:
        tokens = entry.split()
        if tokens[0] in selectedFields:
            meta[tokens[0]] = parseToken(tokens[1])
    return meta
Exemplo n.º 29
0
def read_signals(data_path):
    signals_dict = {}
    fields_dict = {}

    for filename in os.listdir(data_path):
        if filename.endswith(HEADER_EXTENSION):
            sample_name = filename.rstrip(HEADER_EXTENSION)

            sig, fields = wfdb.srdsamp(data_path + sample_name)

            signals_dict[sample_name] = sig
            fields_dict[sample_name] = fields

    return signals_dict, fields_dict
def test_bradycardia(data_path, ann_path, sample_name, ecg_ann_type, verbose=False):
    sig, fields = wfdb.srdsamp(data_path + sample_name)
    channels = fields['signame']

    # Start and end given in seconds
    start, end, alarm_duration = get_start_and_end(fields)

    rr_intervals_list = get_rr_intervals_list(ann_path, sample_name, ecg_ann_type, fields, start, end)
    best_channel_rr = find_best_channel(rr_intervals_list, alarm_duration)
    min_hr = get_min_hr(best_channel_rr, parameters.BRADYCARDIA_NUM_BEATS)

    if verbose:
        print(sample_name + " with min HR: " + str(min_hr))

    return min_hr < parameters.BRADYCARDIA_HR_MIN
def test_ventricular_tachycardia(data_path,
                                 ann_path,
                                 sample_name,
                                 ecg_ann_type,
                                 verbose=False,
                                 fs=parameters.DEFAULT_ECG_FS,
                                 order=parameters.ORDER,
                                 num_beats=parameters.VTACH_NUM_BEATS,
                                 std_threshold=parameters.VTACH_ABP_THRESHOLD,
                                 window_size=parameters.VTACH_WINDOW_SIZE,
                                 rolling_increment=parameters.VTACH_ROLLING_INCREMENT):

    sig, fields = wfdb.srdsamp(data_path + sample_name)
    channels = fields['signame']

    # Start and end given in seconds
    start_time, end_time, alarm_duration = get_start_and_end(fields)
    alarm_sig = sig[int(start_time*fs):int(end_time*fs),:]

    ecg_channels = get_channels_of_type(channels, "ECG")
    abp_channels = get_channels_of_type(channels, "BP")

    # Initialize R vector
    size = int((alarm_duration - window_size) / rolling_increment) + 1
    r_vector = [0.] * size

    # index = int(channels.index("II"))
    # ann_type = get_ann_type("II", index, ecg_ann_type)
    # r_delta = get_ventricular_beats_scores(alarm_sig[:,int(index)], ann_path, sample_name, ann_type, start_time, end_time, "II")
    # r_vector = r_vector + r_delta

    # Adjust R vector based on ventricular beats in signal
    for channel_index in ecg_channels:
        index = int(channel_index)
        channel_name = channels[index]
        ann_type = get_ann_type(channel_name, index, ecg_ann_type)

        r_delta = get_ventricular_beats_scores(alarm_sig[:,int(index)], ann_path, sample_name, ann_type, start_time, end_time, channel_name)
        r_vector = r_vector + r_delta

        # if verbose:
        #     channel_sig = alarm_sig[:,index]
        #     lf, sub = get_lf_sub(channel_sig, order)
        #     ventricular_beats = ventricular_beat_annotations(lf, sub, ann_path + sample_name, ann_type, start_time, end_time, verbose)
        #     max_hr = max_ventricular_hr(ventricular_beats, num_beats, fs)
        #     print(str(sample_name) + " on channel "  + str(channels[int(channel_index)]) + " with max ventricular HR: ", str(max_hr))

    return any([ r_value > 0 for r_value in r_vector ])
    def test_7(self):
        sig, fields = wfdb.srdsamp('sampledata/100', channels = [0, 1])
        ann = wfdb.rdann('sampledata/100', 'atr')
        fs = fields['fs']
        min_bpm = 10
        max_bpm = 350
        min_gap = fs*60/min_bpm
        max_gap = fs*60/max_bpm

        y_idxs = wfdb.processing.correct_peaks(sig[:,0], ann.annsamp, min_gap, max_gap, smooth_window=150)

        yz = numpy.zeros(sig.shape[0])
        yz[y_idxs] = 1
        yz = numpy.where(yz[:10000]==1)[0]

        assert numpy.array_equal(yz, [77, 370, 663, 947, 1231, 1515, 1809, 2045, 2403, 2706, 2998, 3283, 3560, 3863, 4171, 4466, 4765, 5061, 5347, 5634, 5919, 6215, 6527, 6824, 7106, 7393, 7670, 7953, 8246, 8539, 8837, 9142, 9432, 9710, 9998])
Exemplo n.º 33
0
def preprocess(decimation = 8):
    print("Preprocessing")
    sig, fields = wfdb.srdsamp('data/mitdb/100')

    # ecg = sig[:500000,0]
    ecg = sig[:,0]

    from scipy import signal
    # eorig = signal.resample(eorig, len(eorig))
    if decimation != 0:
        ecg = signal.decimate(ecg, decimation, ftype='fir')

    diff = np.diff(ecg)

    cum = 0
    filtered = np.empty_like(ecg)
    for i in range(len(diff)):
        cum *= 0.9
        cum += diff[i]
        filtered[i] = cum

    return ecg[:-1],diff,filtered[:-1]
Exemplo n.º 34
0
 def test_2e(self):
     sig, fields = wfdb.srdsamp('sampledata/311derive', sampfrom=1, sampto=978)
     sig = np.round(sig, decimals=8)
     targetsig = np.genfromtxt('tests/targetoutputdata/target2e')
     targetsig = targetsig.reshape([977, 1])
     assert np.array_equal(sig, targetsig)