Exemplo n.º 1
0
def general_training_target_test(data_path,
                                 model_id,
                                 data_id,
                                 output,
                                 n_patients,
                                 target_icu=None):
    """
    Train on all ICU and evaluate on test ICU
    """
    auc = []
    try:
        print("\n\n\n=== General training ===")
        folds = load_data(data_path, data_id, [1, 2, 3, 4], n_patients)
        for fold, (x_train, y_train, x_val, y_val, x_test, y_test,
                   icu_ids_train, icu_ids_val,
                   icu_ids_test) in enumerate(folds):
            layers, freezable = models.create_freezable_layers(
                model_id, x_train.shape[1], x_train.shape[2])
            checkpoint, early_stopping, model = models.create_model(
                "mt-" + model_id + "-" + data_id, layers)

            model.fit(x_train,
                      y_train,
                      epochs=n_epochs,
                      validation_data=(x_val, y_val),
                      callbacks=[early_stopping, checkpoint])

            icu_test_list = [
                target_icu
            ] if target_icu is not None else np.unique(icu_ids_test)
            icu_auc = []

            score, general_score = models.evaluate_model(
                "mt-" + model_id + "-" + data_id, x_test, y_test)

            print("\n\n=== General Score - \tFold %d - \tAUC %f" %
                  (fold, general_score))

            for test_icu in icu_test_list:
                score, auc_score = models.evaluate_model(
                    "mt-" + model_id + "-" + data_id,
                    x_test[icu_ids_test == test_icu],
                    y_test[icu_ids_test == test_icu])
                icu_auc.append(auc_score)

                print("\n=== ICU Test %d - \tFold %d - \tAUC %f" %
                      (test_icu, fold, auc_score))

                output.write(
                    "Mixed-%d,%d,%d,%d,%f,%f,%f,%f,%f,%f\n" %
                    (test_icu, fold, np.count_nonzero(y_test == 0),
                     np.count_nonzero(y_test == 1), score[0], score[1],
                     score[2], score[3], score[4], auc_score))
            auc.append(icu_auc)

    except Exception as e:
        print(e)

    avg_auc = np.array(auc).mean(axis=0).tolist()
    return avg_auc
def mixed(model_id, data_id, output, n_patients, feature_sequence):
    folds = load_data(data_id, [1, 2, 3, 4], n_patients, feature_sequence)
    for fold, (x_train, y_train, x_val, y_val, x_test, y_test, icu_ids_train,
               icu_ids_val, icu_ids_test) in enumerate(folds):
        layers, freezable = models.create_freezable_layers(
            model_id, x_train.shape[1], x_train.shape[2])
        checkpoint, early_stopping, model = models.create_model(
            "mt-" + model_id + "-" + data_id, layers)

        model.fit(x_train,
                  y_train,
                  epochs=n_epochs,
                  validation_data=(x_val, y_val),
                  callbacks=[early_stopping, checkpoint])

        score, auc_score = models.evaluate_model(
            "mt-" + model_id + "-" + data_id, x_test, y_test)
        print("ALL " + "," + str(auc_score))
        output.write(str(fold) + "," + str(auc_score))

        for test_icu in np.unique(icu_ids_test):
            score, auc_score = models.evaluate_model(
                "mt-" + model_id + "-" + data_id,
                x_test[icu_ids_test == test_icu],
                y_test[icu_ids_test == test_icu])
            print("ICU " + str(test_icu) + "," + str(auc_score))
            output.write("," + str(auc_score))
        output.write("\n")
Exemplo n.º 3
0
def train_on_target(data_path, model_id, data_id, output, n_patients):
    """
    Train and evaluate only on target ICU
    """
    auc = []
    try:
        icu_types = [1, 2, 3, 4]

        for held_out_icu in icu_types:
            print("=== Target: " + str(held_out_icu) + " ===")
            folds = load_data(data_path, data_id, [held_out_icu], n_patients)
            fold_auc = []
            for fold, (x_train, y_train, x_val, y_val, x_test, y_test,
                       icu_ids_train, icu_ids_val,
                       icu_ids_test) in enumerate(folds):
                layers, freezable = models.create_freezable_layers(
                    model_id, x_train.shape[1], x_train.shape[2])

                print("=== Train on Target ===")
                checkpoint, early_stopping, model = models.create_model(
                    "fc-" + model_id + "-" + data_id + "-" + str(held_out_icu),
                    layers)
                model.fit(x_train,
                          y_train,
                          epochs=n_epochs,
                          validation_data=(x_val, y_val),
                          callbacks=[early_stopping, checkpoint])

                score, auc_score = models.evaluate_model(
                    "fc-" + model_id + "-" + data_id + "-" + str(held_out_icu),
                    x_test, y_test)
                fold_auc.append(auc_score)

                print("=== Fold: " + str(fold) + ",Loss: " + str(score[0]) +
                      ",Acc: " + str(score[1]) + ",Prec: " + str(score[2]) +
                      ",Rec: " + str(score[3]) + ",F1: " + str(score[4]) +
                      ",AUC: " + str(auc_score) + " ====")

                output.write(
                    "Target-%d,%d,%d,%d,%f,%f,%f,%f,%f,%f\n" %
                    (held_out_icu, fold, np.count_nonzero(y_test == 0),
                     np.count_nonzero(y_test == 1), score[0], score[1],
                     score[2], score[3], score[4], auc_score))
            auc.append(fold_auc)
    except Exception as e:
        print(e)

    avg_auc = np.array(auc).mean(axis=1).tolist()
    return avg_auc
Exemplo n.º 4
0
def general_training(data_path, model_id, data_id, output, n_patients):
    """
    Train and evaluate on all ICU
    """
    auc = []
    try:
        print("=== General training and testing ===")
        folds = load_data(data_path, data_id, [1, 2, 3, 4], n_patients)
        for fold, (x_train, y_train, x_val, y_val, x_test, y_test,
                   icu_ids_train, icu_ids_val,
                   icu_ids_test) in enumerate(folds):
            layers, freezable = models.create_freezable_layers(
                model_id, x_train.shape[1], x_train.shape[2])
            checkpoint, early_stopping, model = models.create_model(
                "mtt-" + model_id + "-" + data_id, layers)

            model.fit(x_train,
                      y_train,
                      epochs=n_epochs,
                      validation_data=(x_val, y_val),
                      callbacks=[early_stopping, checkpoint])
            score, auc_score = models.evaluate_model(
                "mtt-" + model_id + "-" + data_id, x_test, y_test)
            auc.append(auc_score)

            print("=== General Test, Fold: " + str(fold) + ",Loss: " +
                  str(score[0]) + ",Acc: " + str(score[1]) + ",Prec: " +
                  str(score[2]) + ",Rec: " + str(score[3]) + ",F1: " +
                  str(score[4]) + ",AUC: " + str(auc_score) + " ====")

            output.write("General-Test-%d,%d,%d,%f,%f,%f,%f,%f,%f\n" %
                         (fold, np.count_nonzero(y_test == 0),
                          np.count_nonzero(y_test == 1), score[0], score[1],
                          score[2], score[3], score[4], auc_score))

    except Exception as e:
        print(e)

    avg_auc = np.array(auc).mean().tolist()
    return avg_auc
Exemplo n.º 5
0
    random_state=random_state,
    stratify=y_train)

icu_train = np.array(icu_train)
icu_val = np.array(icu_val)
icu_test = np.array(icu_test)

x_train, y_train, icu_train = data.oversample(x_train, y_train, icu_train)
icu_train = icu_train.flatten()

############

# Define model
model_id = "m_conv_lstm_switch"

layers, freezable = models.create_freezable_layers(model_id, x_train.shape[1],
                                                   x_train.shape[2])
model = models.build_model(layers)
optimizer = optimizers.Adam()

model.compile(loss='binary_crossentropy',
              optimizer=optimizer,
              metrics=['accuracy', precision, recall, fmeasure])

early_stopping = callbacks.EarlyStopping(monitor='val_loss', patience=15)
checkpoint = callbacks.ModelCheckpoint(model_id + ".h5",
                                       monitor='val_loss',
                                       save_best_only=True)

# Fit
model.fit(x_train,
          y_train,
Exemplo n.º 6
0
def domain_adaptation(data_path,
                      model_id,
                      data_id,
                      output,
                      n_patients,
                      shuffle,
                      use_target,
                      target_icu=None):
    """
    Train on all ICU, fine tunning and evaluate on target ICU
    """
    auc = []
    try:
        icu_types = [1, 2, 3, 4]

        target_icu_list = [target_icu] if target_icu is not None else icu_types
        for held_out_icu in target_icu_list:
            icus = list(icu_types)
            if not use_target:
                icus.remove(held_out_icu)
            folds = load_data(data_path, data_id, icus, n_patients)
            icu_folds = load_data(data_path, data_id, [held_out_icu],
                                  n_patients)
            model_name = "ft-" + model_id + "-" + data_id + "-" + str(
                held_out_icu)

            fold_auc = []
            for fold in range(k_fold):
                x_train, y_train, x_val, y_val, x_test, y_test, icu_ids_train, icu_ids_val, icu_ids_test = folds[
                    fold]

                print("=== Held out: " + str(held_out_icu) + " ===")
                layers, freezable = models.create_freezable_layers(
                    model_id, x_train.shape[1], x_train.shape[2])

                print("=== General training ===")
                checkpoint, early_stopping, model = models.create_model(
                    model_name, layers)
                model.fit(x_train,
                          y_train,
                          epochs=n_epochs,
                          validation_data=(x_val, y_val),
                          callbacks=[early_stopping, checkpoint])
                if use_target:
                    score, auc_score_gt = models.evaluate_model(
                        model_name, x_test[icu_ids_test == held_out_icu],
                        y_test[icu_ids_test == held_out_icu])
                else:
                    score, auc_score_gt = models.evaluate_model(
                        model_name, x_test, y_test)

                print("=== Fine tuning ===")
                model = models.get_model(model_name)
                # layers = model.layers

                x_icu_train, y_icu_train, x_val, y_val, x_icu_test, y_icu_test, icu_ids_train, icu_ids_val, icu_ids_test = icu_folds[
                    fold]
                checkpoint, early_stopping, new_model = models.create_model(
                    model_name, model.layers, freezable, shuffle)

                new_model.fit(x_icu_train,
                              y_icu_train,
                              epochs=n_epochs,
                              validation_data=(x_val, y_val),
                              callbacks=[early_stopping, checkpoint])

                score, auc_score = models.evaluate_model(
                    model_name, x_icu_test, y_icu_test)
                fold_auc.append([auc_score_gt, auc_score])

                print("\n=== ICU %d - \tFold %d - \tAUC GT %f - \tAUC FT %f" %
                      (held_out_icu, fold, auc_score_gt, auc_score))

                y_zero_count = np.count_nonzero(y_icu_test == 0)
                y_one_count = np.count_nonzero(y_icu_test == 1)

                output.write("DA-%d-%s-%s,%d,%d,%d,%f,%f,%f,%f,%f,%f\n" %
                             (held_out_icu, shuffle, use_target, fold,
                              y_zero_count, y_one_count, score[0], score[1],
                              score[2], score[3], score[4], auc_score))
            auc.append(fold_auc)
    except Exception as e:
        print(e)

    avg_auc = np.array(auc).mean(axis=1)

    auc_df = pd.DataFrame(avg_auc.mean(axis=1))
    print(auc_df)

    return avg_auc.tolist()