コード例 #1
0
ファイル: new_sample.py プロジェクト: zGolgooni/MasterThesis
def new_sample(path, name, sampling_rate, i):
    x_signal, y_signal = read_sample(path,
                                     name,
                                     sampling_rate,
                                     preprocess=False)
    rpeaks = detect_r_points(y_signal, y_signal.shape[0], sampling_rate)
    print('y_shape %d' % y_signal.shape[0])
    print('rpeaks_shape %d' % rpeaks.shape[0])
    label, type = classify_by_rpoints(y_signal, rpeaks, sampling_rate)
    '''
    plot_length = 40000
    trace1 = go.Scatter(y=y_signal[:plot_length], x=x_signal[:plot_length], name='Signal')
    trace2 = go.Scatter(y=y_signal[rpeaks], x=x_signal[rpeaks],mode='markers', name='rpeaks')
    figure = go.Figure(data=[trace1, trace2])
    py.plot(figure, filename=name)
    print('Plotting is done! :)')
    '''
    print('% s Suggested label by simple handy classification = %s' %
          (name, label))
    #if label == 'Arrhythmic':
    if 1 == 1:
        trace1 = go.Scatter(y=y_signal[:], x=x_signal[:], name='Signal')
        trace2 = go.Scatter(y=y_signal[rpeaks[:, 0]],
                            x=x_signal[rpeaks[:, 0]],
                            mode='markers',
                            name='rpeaks')
        layout = go.Layout(title=name)
        figure = go.Figure(data=[trace1, trace2], layout=layout)
        py.plot(figure, filename=name)
        #print('Plotting is done! :)  sampling rate = %s' %sampling_rates[i])
    return label
コード例 #2
0
def load_sample_test(path, name, sampling_rate=1000, dimension=5000, step=2):
    x_signal, y_signal = read_sample(path, name, sampling_rate)

    length = y_signal.shape[0]
    rpeaks = detect_r_points(y_signal, length, sampling_rate)
    sample_x = np.empty([0, dimension])
    for index in range(0, len(rpeaks), step):
        if rpeaks[index] + dimension < length:
            x = np.empty([dimension])
            x = y_signal[rpeaks[index]:rpeaks[index] + dimension, 0]
            sample_x = np.append(sample_x, np.array([x]), axis=0)
    return sample_x
コード例 #3
0
ファイル: train_data.py プロジェクト: zGolgooni/MasterThesis
def load_sample_raw(path,
                    name,
                    real_label,
                    sampling_rate,
                    explanation,
                    dimension1=4,
                    dimension2=500,
                    step=500,
                    train=True):
    x_signal, y_signal = read_sample(path,
                                     name,
                                     sampling_rate,
                                     preprocess=False)
    length = y_signal.shape[0]
    sample_x = np.empty([0, dimension1, dimension2])
    sample_y = np.empty([0, 1])

    if real_label == 'Normal':
        label = 0
    else:
        label = 1
    episode = dimension1 * dimension2
    if (real_label == 'Normal') | (train is False) | (
            explanation == 'Bradicardia') | (explanation == 'Tachicardia') | (
                explanation == 'long QT'):
        pointer = 10
        while pointer + episode < length:
            temp = y_signal[pointer:pointer + episode, 0]
            x = np.reshape(temp, [dimension1, dimension2])
            y = np.array(label)
            sample_x = np.append(sample_x, np.array([x]), axis=0)
            sample_y = np.append(sample_y, np.array(label))
            pointer += step
    else:
        rpeaks = detect_r_points(y_signal, length, sampling_rate)
        problems = analyze_rpoints(y_signal, rpeaks, sampling_rate)
        for p in problems:
            problem = int(p)
            pointer = max(problem - episode + dimension2, 10)
            while pointer + episode < min(length, problem + 7000):
                x = y_signal[pointer:pointer + episode, 0]
                y = np.array(label)
                sample_x = np.append(sample_x, np.array([x]), axis=0)
                sample_y = np.append(sample_y, np.array(label))
                pointer += step

    sample_y = np.reshape(sample_y, [sample_y.shape[0], 1])
    sample_x = np.reshape(sample_x,
                          [sample_x.shape[0], dimension1, dimension2])
    return sample_x, sample_y
コード例 #4
0
ファイル: train_data.py プロジェクト: zGolgooni/MasterThesis
def load_sample(path,
                name,
                real_label,
                sampling_rate,
                explanation,
                interval,
                dimension=4000,
                step=2,
                train=False,
                preprocess=False):
    x_signal, y_signal = read_sample(path, name, sampling_rate, preprocess)
    length = y_signal.shape[0]
    rpeaks = detect_r_points(y_signal, length, sampling_rate)
    sample_x = np.empty([0, dimension])
    sample_y = np.empty([0, 1])
    if real_label == 'Normal':
        label = 0
    else:
        label = 1
    for index in range(0, len(rpeaks), step):
        if rpeaks[index] + dimension < length:
            if (train is False) | (real_label == 'Normal') | (
                    explanation == 'Bradicardia'
            ) | (explanation == 'Tachicardia') | (explanation == 'Long QT'):
                x = np.empty([dimension])
                x = y_signal[rpeaks[index]:rpeaks[index] + dimension, 0]
                y = np.array(label)
                sample_x = np.append(sample_x, np.array([x]), axis=0)
                sample_y = np.append(sample_y, np.array(label))
            elif check_overlap(interval, x_signal[rpeaks[index]],
                               x_signal[rpeaks[index] + dimension]):
                x = np.empty([dimension, 1])
                x = y_signal[rpeaks[index]:rpeaks[index] + dimension, 0]
                y = np.array(label)
                sample_x = np.append(sample_x, np.array([x]), axis=0)
                sample_y = np.append(sample_y, np.array(label))

    return sample_x, sample_y
コード例 #5
0
def check_new_input(file_path, file_name, sampling_rate=1000, plot=True):
    #Create and load networks
    dimension = 5000
    dimension_fraction = 1
    cnn_model = Sequential()
    cnn_model.add(
        Conv1D(filters=16,
               kernel_size=80,
               strides=2,
               padding="same",
               input_shape=(dimension // dimension_fraction,
                            dimension_fraction)))
    cnn_model.add(BatchNormalization())
    cnn_model.add(Activation('selu'))
    cnn_model.add(MaxPooling1D())
    cnn_model.add(Dropout(0.5))
    cnn_model.add(Conv1D(filters=16, kernel_size=80, padding="same"))
    cnn_model.add(BatchNormalization())
    cnn_model.add(Activation('selu'))
    cnn_model.add(Dropout(0.5))
    cnn_model.add(Conv1D(filters=16, kernel_size=80, padding="same"))
    cnn_model.add(BatchNormalization())
    cnn_model.add(Activation('selu'))
    cnn_model.add(MaxPooling1D())
    cnn_model.add(Dropout(0.5))
    cnn_model.add(Flatten())
    cnn_model.add(Dense(1))
    cnn_model.add(Activation('sigmoid'))
    adam = Adam(lr=0.0005)
    cnn_model.compile(loss='binary_crossentropy',
                      optimizer=adam,
                      metrics=['binary_accuracy'])
    cnn_model.load_weights('./Trained_networks/cnn_model.h5')

    rnn_model = Sequential()
    init_one = keras.initializers.Ones()
    rnn_model.add(GRU(3, input_shape=(None, 1), kernel_initializer=init_one))
    rnn_model.add(BatchNormalization())
    rnn_model.add(PReLU())
    rnn_model.add(Dropout(0.5))
    rnn_model.add(Dense(1))
    rnn_model.add(Activation('sigmoid'))
    rnn_model.compile(loss='binary_crossentropy',
                      optimizer='adam',
                      metrics=['binary_accuracy'])
    rnn_model.load_weights('./Trained_networks/rnn_model.h5')

    #Classify new sample by CNN-RNN method
    sample_x = load_sample_test(file_path,
                                file_name,
                                sampling_rate=sampling_rate)
    if sample_x.shape[0] > 0:
        sample_x = np.reshape(sample_x, [
            sample_x.shape[0], dimension // dimension_fraction,
            dimension_fraction
        ])

        step1_predicted = cnn_model.predict(np.array(sample_x))
        reshaped_predicted = np.reshape(
            step1_predicted,
            [1, step1_predicted.shape[0], step1_predicted.shape[1]])
        final_predicted = rnn_model.predict(reshaped_predicted)
        if final_predicted < 0.5:
            predicted_label = 'Normal'
        else:
            predicted_label = 'Arrhythmic'

    #Plot signal
    if plot == True:
        x_signal, y_signal = read_sample(file_path,
                                         file_name,
                                         sampling_rate=sampling_rate,
                                         preprocess=False)
        trace1 = go.Scatter(y=y_signal[:], x=x_signal[:], name='Signal')
        layout = go.Layout(title=file_name)
        figure = go.Figure(data=[trace1], layout=layout)
        py.plot(figure, filename=file_name)
        print('Plotting is done! :)  ')

    print('Predicted label = %s' % predicted_label)
    return predicted_label
コード例 #6
0
ファイル: train_data.py プロジェクト: zGolgooni/MasterThesis
def load_sample_rbased(path,
                       name,
                       real_label,
                       sampling_rate,
                       explanation,
                       dimension1=4,
                       dimension2=500,
                       step=2,
                       train=False):
    x_signal, y_signal = read_sample(path,
                                     name,
                                     sampling_rate,
                                     preprocess=False)
    length = y_signal.shape[0]
    rpeaks = detect_r_points(y_signal, length, sampling_rate)

    counter_label0 = 0
    counter_label1 = 0

    counter_sample0 = 0
    counter_sample1 = 0

    sample_x = np.empty([0, dimension1, dimension2])
    sample_y = np.empty([0, 1])
    if real_label == 'Normal':
        label = 0
        counter_label0 += 1
        if real_label == 'Arrhythmic':
            print('oh oh label 0   , %s' % (real_label))
    else:
        label = 1
        counter_label1 += 1
        if real_label == 'Normal':
            print('oh oh label 1   , %s' % (real_label))

    if (real_label == 'Normal') | (train is False) | (
            explanation == 'Bradicardia') | (explanation == 'Tachicardia') | (
                explanation == 'long QT'):
        for index in range(0, len(rpeaks) - dimension1, step):
            if rpeaks[index + dimension1] + dimension2 < length:
                x = np.empty([dimension1, dimension2])
                for i in range(dimension1):
                    x[i] = y_signal[rpeaks[i + index]:rpeaks[i + index] +
                                    dimension2, 0]
                y = np.array(label)
                sample_x = np.append(sample_x, np.array([x]), axis=0)
                sample_y = np.append(sample_y, np.array(label))
                if label == 0:
                    counter_sample0 += 1
                else:
                    counter_sample1 += 1
    else:
        problems = analyze_rpoints(y_signal, rpeaks, sampling_rate)
        for p in problems:
            problem = int(p)
            for index in range(problem, problem + dimension1, step):
                if index + dimension1 < rpeaks.shape[0]:
                    if rpeaks[index + dimension1] + dimension2 < length:
                        x = np.empty([dimension1, dimension2])
                        for i in range(dimension1):
                            x[i] = y_signal[rpeaks[i +
                                                   index]:rpeaks[i + index] +
                                            dimension2, 0]
                        sample_x = np.append(sample_x, np.array([x]), axis=0)
                        sample_y = np.append(sample_y, np.array(label))
                        counter_sample1 += 1
    sample_y = np.reshape(sample_y, [sample_y.shape[0], 1])
    return sample_x, sample_y, counter_label0, counter_label1, counter_sample0, counter_sample1