Exemple #1
0
def run_classifier(header_file, signal_file, classifier_pickle):

    signal_names, Fs, n_samples = phyc.import_signal_names(header_file)

    # Get this subject's data as a dataframe
    this_data = phyc.get_subject_data_test(signal_file, signal_names)

    SaO2 = this_data.get(['SaO2']).values
    step        = Fs * 60
    window_size = Fs * 60

    # Initialize the X_subj and Y_subj matricies
    X_subj = np.zeros([((n_samples) // step), 1])

    for idx, k in enumerate(range(0, (n_samples-step+1), step)):
        X_subj[idx, :] = np.var(np.transpose(SaO2[k:k+window_size]), axis=1)

    # Load the classifier
    my_classifier = joblib.load(classifier_pickle)

    # Generate the prediction for the subjects.
    predictions = my_classifier.predict_proba(X_subj)
    predictions = predictions[:, 1]
    predictions = [x * np.ones([window_size]) for x in predictions]
    predictions = np.concatenate(predictions)
    predictions = np.append(predictions, np.zeros(np.size(this_data, 0)
                                                  - np.size(predictions, 0)))
    return predictions
Exemple #2
0
def loaddata(record_name):
    L.log_info("Loading record: " + str(record_name))
    header_file = record_name + '.hea'
    signal_file = record_name + '.mat'
    arousal_file = record_name + '-arousal.mat'

    # Get the signal names from the header file
    signal_names, Fs, n_samples = phyc.import_signal_names(header_file)
    signal_names = list(np.append(signal_names, 'arousals'))
    print("signal_names: " + str(signal_names))
    print("Fs: " + str(Fs))
    print("n_samples: " + str(n_samples))
    # Convert this subject's data into a pandas dataframe
    this_data = phyc.get_subject_data(arousal_file, signal_file, signal_names)

    # ----------------------------------------------------------------------
    # Generate the Features for the classificaition model - variance of SaO2
    # ----------------------------------------------------------------------

    # For the baseline, let's only look at how SaO2 might predict arousals


    arousals_originale = this_data.get(['arousals']).values
    SaO2 = this_data.get(['ECG']).values
    recordLength = SaO2.size
    signals=None
    arousals=None
    if(p_INPUT_FEAT==1):
        SaO2 = instfreq(SaO2)
        SaO2 = scale(SaO2)
        #SaO2 = standardize(SaO2)
        signals, arousals = phyc.signalsToMatrix(SaO2, arousals_originale, recordLength, p_WINDOW_SIZE, p_INPUT_FEAT);
        print("signals shape:" + str(signals.shape))
    if(p_INPUT_FEAT==13):
        ECG = this_data.get(['ECG']).values
        print("ECG shape:"+str(ECG.shape))
        ECG = instfreq(ECG)
        print("ECG shape:" + str(ECG.shape))
        signals = this_data[
            ['F3-M2', 'F4-M1', 'C3-M2', 'C4-M1', 'O1-M2', 'O2-M1', 'E1-M2', 'Chin1-Chin2', 'ABD', 'CHEST', 'AIRFLOW',
            'SaO2']].values
        signals = np.append(signals,ECG, axis=1)

        #signals = instfreq(signals)
        signals = scale(signals)
        #signals = standardize(signals)
        print("signals shape:" + str(signals.shape))
        print("signals shape:" + str(signals[0:20,:]))
        signals, arousals = phyc.signalsToMatrix(signals, arousals_originale, recordLength, p_WINDOW_SIZE, p_INPUT_FEAT);
        #signals = signals.values


    return signals, arousals,recordLength,arousals_originale
Exemple #3
0
def preprocess_record(record_name):
    header_file = record_name + '.hea'
    signal_file = record_name + '.mat'
    arousal_file = record_name + '-arousal.mat'

    # Get the signal names from the header file
    signal_names, Fs, n_samples = phyc.import_signal_names(header_file)
    signal_names = list(np.append(signal_names, 'arousals'))

    # Convert this subject's data into a pandas dataframe
    this_data = phyc.get_subject_data(arousal_file, signal_file, signal_names)

    # ----------------------------------------------------------------------
    # Generate the Features for the classificaition model - variance of SaO2
    # ----------------------------------------------------------------------

    # For the baseline, let's only look at how SaO2 might predict arousals

    SaO2 = this_data.get(['SaO2']).values
    arousals = this_data.get(['arousals']).values

    # We select a window size of 60 seconds with no overlap to compute
    # the features
    step = Fs * 60
    window_size = Fs * 60

    # Initialize the matrices that store our training data
    X_subj = np.zeros([((n_samples) // step), 1])
    Y_subj = np.zeros([((n_samples) // step), 1])

    # Extract the variance of the SaO2 in 60 second windows as a feature
    for idx, k in enumerate(range(0, (n_samples - step + 1), step)):
        X_subj[idx, 0] = np.var(np.transpose(SaO2[k:k + window_size]), axis=1)
        Y_subj[idx] = np.max(arousals[k:k + window_size])

    # Ignore records that do not contain any arousals
    if not np.any(Y_subj):
        sys.stderr.write('no arousals found in %s\n' % record_name)
        return
    print("X_subj.shape: " + str(X_subj.shape))
    print("np.ravel(Y_subj).shape: " + str(np.ravel(Y_subj).shape))

    # ---------------------------------------------------------------------
    # Train a (multi-class) Logistic Regression classifier
    # ---------------------------------------------------------------------
    my_classifier = LogisticRegression()
    my_classifier.fit(X_subj, np.ravel(Y_subj))

    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    # Save this algorithm for submission to Physionet Challenge:
    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    model_file = 'models/%s_model.pkl' % os.path.basename(record_name)
    joblib.dump(my_classifier, model_file)
Exemple #4
0
def loaddata(record_name):
    L.log_info("Loading record: " + str(record_name))
    header_file = record_name + '.hea'
    signal_file = record_name + '.mat'
    arousal_file = record_name + '-arousal.mat'
    # Get the signal names from the header file
    signal_names, Fs, n_samples = phyc.import_signal_names(header_file)
    signal_names = list(np.append(signal_names, 'arousals'))
    this_data = phyc.get_subject_data(arousal_file, signal_file, signal_names)
    SaO2 = this_data.get(['SaO2']).values
    arousals = this_data.get(['arousals']).values
    recordLength = SaO2.size
    #print(this_data)
    #print(this_data.values)
    signals = this_data[
        ['F3-M2', 'F4-M1', 'C3-M2', 'C4-M1', 'O1-M2', 'O2-M1', 'E1-M2', 'Chin1-Chin2', 'ABD', 'CHEST', 'AIRFLOW',
         'SaO2', 'ECG']]
    signals = signals.values
    arousals=arousals.astype(np.int32)
    return signals,arousals, recordLength,SaO2