def load_data(data_id, icus): x, y = data.load_icus(data_id, icus, data.targets[0]) x = sequence.pad_sequences(x) y = np.asarray(y) skf = StratifiedKFold(n_splits=5, random_state=True) for i, (train_idx, test_idx) in enumerate(skf.split(x, y)): x_test = x[test_idx] y_test = y[test_idx] return x_test, y_test
def main(): # for k in ['a', 'b', 'c']: icu_names = ["", "Coronary", "Cardiac", "Medical", "Surgical"] for icu in [1, 2, 3, 4]: # for icu in [1]: filename = "uti-" + str(icu) print("Target: " + filename) x, y, icu_ids = data.load_icus("60", [icu], data.targets[0]) # x = np.asarray(x) last_x = [] for xi in x: last_x.append(xi[len(xi) - 1, :]) last_x = np.asarray(last_x) y = np.asarray(y) y[y == 0] = -1 # plot_heatmap(filename, last_x, y) heatmap(last_x, y, icu_names[icu] + " ICU - Raw Death Heatmap", data.targets[0])
def main(): model_names = 'ft-m3r', 'fc-m3r' for model_name in model_names: for icu in [1, 2, 3, 4]: m = model_name + "-" + str(icu) model = models.load_model("models/m1-" + m + ".h5", custom_objects={ "precision": precision, "recall": recall, "fmeasure": fmeasure }) gen_model = Model(input=model.input, output=model.layers[2].output) # print(gen_model.layers) x, y = data.load_icus("60", [icu], data.targets[0]) x = sequence.pad_sequences(x) y = np.asarray(y) neural_x = gen_model.predict(x) if 'ft' in model_name: title = "Fine Tuning - " else: title = "Focused - " plot_heatmap(title + " UTI - " + str(icu), neural_x, y)
from sklearn.metrics import roc_auc_score from sklearn.model_selection import train_test_split # Local imports sys.path.append( "/home/tiago/Documentos/research/mestrado/Medical/medical/src/") import models import prepare_data as data from models import precision, recall, fmeasure, Switch ########### random_state = 13 # Load and split data X, y, icu = data.load_icus("60", [1, 2, 3, 4], "", should_normalize=True) x_train, x_test, y_train, y_test, icu_train, icu_test = train_test_split( X, y, icu, train_size=0.7, random_state=random_state, stratify=y) x_train, x_val, y_train, y_val, icu_train, icu_val = train_test_split( x_train, y_train, icu_train, train_size=0.7, 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)
def plot_heatmap_and_patient(): icu_names = ["", "Coronary", "Cardiac", "Medical", "Surgical"] for icu in [1, 2, 3, 4]: # for icu in [1]: # for view_all in [True, False]: for view_all in [True]: if view_all: x_orig, y_orig, _ = data.load_icus("60", [icu], data.targets[0]) else: folds = conv_lstm.load_data("60", [icu], data.targets[0]) x_icu_train, y_icu_train, x_orig, y_orig = folds[4] max_1 = np.count_nonzero(np.asarray(y_orig) == 1) max_0 = np.count_nonzero(np.asarray(y_orig) == 0) for di in [0]: # dead_patient_x = np.asarray(x_orig)[np.asarray(y_orig) == 1][np.random.randint(max_1)] dead_patient_x = np.asarray(x_orig)[np.asarray(y_orig) == 1][di] # surv_patient_x = np.asarray(x_orig)[np.asarray(y_orig) == 0][np.random.randint(max_0)] surv_patient_x = np.asarray(x_orig)[np.asarray(y_orig) == 0][di] x_pad = sequence.pad_sequences(x_orig) y_pad = np.asarray(y_orig) dead_patient_x = np.floor(np.asarray(dead_patient_x)) surv_patient_x = np.floor(np.asarray(surv_patient_x)) model = models.load_model("models/ft-mvis-60-" + str(icu) + ".h5", custom_objects={ "precision": precision, "recall": recall, "fmeasure": fmeasure }) # print(mds.evaluate_model("ft-mvis-60-" + str(icu), x_pad, y_pad)) gen_model = Model(input=model.input, output=model.layers[3].output) neural_x = gen_model.predict(x_pad) x_score = model.predict_proba(x_pad) dead_patient_input = np.zeros( (dead_patient_x.shape[0], x_pad.shape[1], x_pad.shape[2])) for i in range(dead_patient_x.shape[0]): dead_patient_input[i, :i + 1, :] = dead_patient_x[:i + 1, :] dead_patient_x_neural = gen_model.predict(dead_patient_input) surv_patient_input = np.zeros( (surv_patient_x.shape[0], x_pad.shape[1], x_pad.shape[2])) for i in range(surv_patient_x.shape[0]): surv_patient_input[i, :i + 1, :] = surv_patient_x[:i + 1, :] surv_patient_x_neural = gen_model.predict(surv_patient_input) y_pad[y_pad == 0] = -1 y_orig[y_orig == 0] = -1 name = "_all" if view_all else "_test_only" visual.heatmap_withpatient(neural_x, y_pad, icu_names[icu] + " ICU - Neural Death Heatmap", "in-hospital-death", scores=x_score)
# -*- coding: utf-8 -*- import numpy as np import prepare_data as data import matplotlib.pyplot as plt import seaborn as sns from sklearn.preprocessing import normalize from keras.preprocessing import sequence #%% x, y, ids = data.load_icus("60", [1, 2, 3, 4], data.targets[0]) #%% x = sequence.pad_sequences(x, padding='post') y = np.asarray(y) ids = np.asarray(ids) #%% x = x[:, :47, :42] #%% x_norm = np.zeros(x.shape) for ts in range(x.shape[2]): x_norm[:, :, ts] = normalize(x[:, :, ts]) x = x_norm #%% #fig = plt.figure()
def load_data(data_path, data_id, icus, n_patients): x, y, icu_ids = data.load_icus(data_path, data_id, icus, should_normalize=True) y = np.asarray(y) icu_ids = np.asarray(icu_ids) folds = [] x_train = [[] for i in range(k_fold)] y_train = [[] for i in range(k_fold)] x_val = [[] for i in range(k_fold)] y_val = [[] for i in range(k_fold)] x_test = [[] for i in range(k_fold)] y_test = [[] for i in range(k_fold)] icu_ids_train = [[] for i in range(k_fold)] icu_ids_val = [[] for i in range(k_fold)] icu_ids_test = [[] for i in range(k_fold)] skf = StratifiedKFold(n_splits=k_fold, shuffle=True, random_state=33) for icu in np.unique(icu_ids): x_icu = x[icu_ids == icu] y_icu = y[icu_ids == icu] icu_ids_icu = icu_ids[icu_ids == icu] if n_patients < x_icu.shape[0]: x_icu, y_icu = data.subsampling(x_icu, y_icu, n_patients) for i, (train_idx, test_idx) in enumerate(skf.split(x_icu, y_icu)): x_icu_train, x_icu_val, y_icu_train, y_icu_val, icu_train, icu_val = train_test_split( x_icu[train_idx], y_icu[train_idx], icu_ids_icu[train_idx], test_size=0.2, random_state=42) x_train[i] += x_icu_train.tolist() y_train[i] += y_icu_train.tolist() x_val[i] += x_icu_val.tolist() y_val[i] += y_icu_val.tolist() x_test[i] += x_icu[test_idx].tolist() y_test[i] += y_icu[test_idx].tolist() icu_ids_train[i] += icu_train.tolist() icu_ids_val[i] += icu_val.tolist() icu_ids_test[i] += icu_ids_icu[test_idx].tolist() for i in range(k_fold): x_train[i], y_train[i], icu_ids_train[i] = data.oversample( np.asarray(x_train[i]), np.asarray(y_train[i]), np.asarray(icu_ids_train[i])) x_val[i] = np.asarray(x_val[i]) y_val[i] = np.asarray(y_val[i]) x_test[i] = np.asarray(x_test[i]) y_test[i] = np.asarray(y_test[i]) icu_ids_val[i] = np.asarray(icu_ids_val[i]) icu_ids_test[i] = np.asarray(icu_ids_test[i]) folds.append( (x_train[i], y_train[i], x_val[i], y_val[i], x_test[i], y_test[i], icu_ids_train[i], icu_ids_val[i], icu_ids_test[i])) return folds
def load_data(data_id, icus, n_patients, feature_sequence): x, y, icu_ids = data.load_icus(data_id, icus, data.targets[0]) if data_id == 'X': x = sequence.pad_sequences(x, padding='pre', dtype='float64', maxlen=200) else: x = sequence.pad_sequences(x, padding='pre', dtype='float64') print(x.shape) x = x[:, :, np.array(feature_sequence)] x_norm = np.zeros(x.shape) for ts in range(x.shape[1]): x_norm[:, ts, :] = normalize(x[:, ts, :]) x = x_norm y = np.asarray(y) icu_ids = np.asarray(icu_ids) folds = [] x_train = [[] for i in range(k_fold)] y_train = [[] for i in range(k_fold)] x_val = [[] for i in range(k_fold)] y_val = [[] for i in range(k_fold)] x_test = [[] for i in range(k_fold)] y_test = [[] for i in range(k_fold)] icu_ids_train = [[] for i in range(k_fold)] icu_ids_val = [[] for i in range(k_fold)] icu_ids_test = [[] for i in range(k_fold)] skf = StratifiedKFold(n_splits=k_fold, shuffle=True, random_state=33) for icu in np.unique(icu_ids): x_icu = x[icu_ids == icu] y_icu = y[icu_ids == icu] icu_ids_icu = icu_ids[icu_ids == icu] if n_patients < x_icu.shape[0]: x_icu, y_icu = subsampling(x_icu, y_icu, n_patients) for i, (train_idx, test_idx) in enumerate(skf.split(x_icu, y_icu)): # O erro ta aqui, eh por causa do conjunto de validacao x_icu_train, x_icu_val, y_icu_train, y_icu_val, icu_train, icu_val = train_test_split( x_icu[train_idx], y_icu[train_idx], icu_ids_icu[train_idx], test_size=0.2, random_state=42) x_train[i] += x_icu_train.tolist() y_train[i] += y_icu_train.tolist() x_val[i] += x_icu_val.tolist() y_val[i] += y_icu_val.tolist() x_test[i] += x_icu[test_idx].tolist() y_test[i] += y_icu[test_idx].tolist() icu_ids_train[i] += icu_train.tolist() icu_ids_val[i] += icu_val.tolist() icu_ids_test[i] += icu_ids_icu[test_idx].tolist() for i in range(k_fold): x_train[i], y_train[i], icu_ids_train[i] = oversample( np.asarray(x_train[i]), np.asarray(y_train[i]), np.asarray(icu_ids_train[i])) x_val[i] = np.asarray(x_val[i]) y_val[i] = np.asarray(y_val[i]) x_test[i] = np.asarray(x_test[i]) y_test[i] = np.asarray(y_test[i]) icu_ids_val[i] = np.asarray(icu_ids_val[i]) icu_ids_test[i] = np.asarray(icu_ids_test[i]) print("%d - Train - y_true: %d, y_false: %d" % (i, np.count_nonzero(y_train[i] == 0), np.count_nonzero(y_train[i] == 1))) print("%d - Test - y_true: %d, y_false: %d" % (i, np.count_nonzero(y_test[i] == 0), np.count_nonzero(y_test[i] == 1))) folds.append( (x_train[i], y_train[i], x_val[i], y_val[i], x_test[i], y_test[i], icu_ids_train[i], icu_ids_val[i], icu_ids_test[i])) return folds