def plot_only_prediction(predict): fig, axes = plt.subplots(2, 1) fig.suptitle('Prediction Results', fontweight='bold') predict_int_avg = moving_average(predict[0], 30) axes[0].plot(predict_int_avg) axes[0].set_title('Interictal Predictions') axes[0].set_xlim(0, len(predict_int_avg)) axes[0].set_ylim(0, 1.05) axes[0].axhline(y=max(predict_int_avg), color='red', linewidth=0.5, label=str(max(predict_int_avg))) axes[0].legend() predict_pre_avg = moving_average(predict[1], 30) axes[1].plot(predict_pre_avg) axes[1].set_title('Precital Predictions') axes[1].set_xlim(0, len(predict_pre_avg)) axes[1].set_ylim(0, 1.05) axes[1].axhline(y=max(predict_pre_avg), color='red', linewidth=0.5, label=str(max(predict_pre_avg))) axes[1].legend() fig.show()
def plot_subject_scores(testParams, foldPairs, labels, featureParams, timingParams): fig, axes = plt.subplots(1, 1) fig.suptitle('ROC Analysis', fontweight='bold') stepLen = (featureParams.windowLength * (1 - featureParams.overlap)) refractoryLen = timingParams.refractoryLen * 60 refractoryPeriod = int(refractoryLen / stepLen) testPredictsAll = [] testLabels = np.full(len(labels), fill_value=-1, dtype=int) for s in range(len(testParams)): testLabels[foldPairs[s][0][2]] = np.squeeze( np.hstack((np.full(len(testParams[s][0].testPred[0]), fill_value=labelTypes["interictal"]), np.full(len(testParams[s][0].testPred[1]), fill_value=labelTypes["preictal"])))) for f in range(len(testParams[0])): testPredicts = np.zeros(len(labels), dtype=float) for s in range(len(testParams)): testPredicts[foldPairs[s][f][2]] += np.squeeze( np.hstack((np.squeeze(testParams[s][f].testPred[0]), np.squeeze(testParams[s][f].testPred[1])))) testPredicts = moving_average(testPredicts, 30) testPredictsAll.append(testPredicts) # Compute ROC curve and area the curve for testPredicts in testPredictsAll: fpr, tpr, fps, tps, thresholds = my_roc_curve( testLabels, testPredicts, pos_label=labelTypes["preictal"], neg_label=labelTypes["interictal"], refractory_period=refractoryPeriod, rate_period=60 * 60 / stepLen, point=5000) axes.plot(fpr, tpr) axes.set_ylabel("True Positive Rate (sensitivity)") axes.set_xlabel("False Positive Rate(per hour)") axes.set_ylim(0, 1.02) axes.set_xlim(-0.01, 1) fig.show()
def plot_subject_prediction_train(testParams, foldPairs, labels): fig, axes = plt.subplots(1, 1) fig.suptitle('Prediction Results', fontweight='bold') predictions = np.zeros((len(labels)), dtype=float) labels[labels != 1] = 0 for s in range(len(testParams)): for f in range(len(testParams[s])): predictions[foldPairs[s][f][2]] += np.squeeze( np.hstack((np.squeeze(testParams[s][f].testPred[0]), np.squeeze(testParams[s][f].testPred[1])))) predictions = predictions / len(testParams[0]) predictions = moving_average(predictions, 30) axes.plot(predictions) axes.plot(labels) axes.set_xlim(0, len(predictions)) axes.set_ylim(0, 1.05) axes.legend() fig.show()
def gen_subject_roc(test_id): result = load_results(test_id) featureParams = result['featureParams'] timingParams = result['timingParams'] labels = result['dataset'].labels foldPairs = result["dataset"].fold_pairs testParams = result["trainResult"].testParams stepLen = (featureParams.windowLength * (1 - featureParams.overlap)) refractoryLen = timingParams.refractoryLen * 60 refractoryPeriod = int(refractoryLen / stepLen) testLabels = np.full(len(labels), fill_value=-1, dtype=int) intHour = 0 for s in range(len(testParams)): intHour += len(testParams[s][0].testPred[0]) testLabels[foldPairs[s][0][2]] = np.squeeze( np.hstack((np.full(len(testParams[s][0].testPred[0]), fill_value=labelTypes["interictal"]), np.full(len(testParams[s][0].testPred[1]), fill_value=labelTypes["preictal"])))) intHour = intHour * stepLen / (60 * 60) testPredictsMean = np.zeros(len(labels), dtype=float) testPredictsAll = [] for f in range(len(testParams[0])): testPredicts = np.zeros(len(labels), dtype=float) for s in range(len(testParams)): testPredicts[foldPairs[s][f][2]] += np.squeeze( np.hstack((np.squeeze(testParams[s][f].testPred[0]), np.squeeze(testParams[s][f].testPred[1])))) testPredicts = moving_average(testPredicts, 30) testPredictsMean += testPredicts / len(testParams[0]) testPredictsAll.append(testPredicts) testPredictsAll.append(testPredictsMean) fpr_all = [] tpr_all = [] fps_all = [] tps_all = [] thresholds_all = [] cutoff_all = [] for testPredicts, idx in zip(testPredictsAll, range(len(testPredictsAll))): fpr, tpr, fps, tps, thresholds = my_roc_curve( testLabels, testPredicts, pos_label=labelTypes["preictal"], neg_label=labelTypes["interictal"], refractory_period=refractoryPeriod, rate_period=60 * 60 / stepLen, point=20) fpr_all.append(fpr) tpr_all.append(tpr) fps_all.append(fps) tps_all.append(tps) thresholds_all.append(thresholds) cutoff_all.append(find_cutoff_idx(fpr, tpr)) results = { "fpr": fpr_all, "tpr": tpr_all, "fps": fps_all, "tps": tps_all, "thresholds": thresholds_all, "cutoff": cutoff_all, "intHour": intHour, "sCount": int(tps_all[-1][-1]) } # save results and params test_name = globals.file_test + "test_roc_" + str(test_id) + ".mat" pickle_out = open(test_name, "wb") pickle.dump(results, pickle_out) pickle_out.close()
def compare_subject_prediction(results, plotLabels=None, plotTitle=None): fig, axes = plt.subplots(1, 1) fig.suptitle('Prediction Results', fontweight='bold') #colors = cm.gist_ncar(np.linspace(0, 1, 4)) colors = cm.tab20(np.linspace(0, 1, 20)) tickPeriod = 60 * 60 for result, color in zip(results, colors): featureParams = result['featureParams'] timingParams = result['timingParams'] labels = result['dataset'].labels foldPairs = result["dataset"].fold_pairs testParams = result["trainResult"].testParams stepLen = (featureParams.windowLength * (1 - featureParams.overlap)) testLabels = np.full(len(labels), fill_value=-1, dtype=int) intHour = 0 for s in range(len(testParams)): intHour += len(testParams[s][0].testPred[0]) testLabels[foldPairs[s][0][2]] = np.squeeze( np.hstack((np.full(len(testParams[s][0].testPred[0]), fill_value=labelTypes["interictal"]), np.full(len(testParams[s][0].testPred[1]), fill_value=labelTypes["preictal"])))) intHour = intHour * stepLen / (60 * 60) testPredictsMean = np.zeros(len(labels), dtype=float) for f in range(len(testParams[0])): testPredicts = np.zeros(len(labels), dtype=float) for s in range(len(testParams)): testPredicts[foldPairs[s][f][2]] += np.squeeze( np.hstack((np.squeeze(testParams[s][f].testPred[0]), np.squeeze(testParams[s][f].testPred[1])))) testPredicts = moving_average(testPredicts, 30) testPredictsMean += testPredicts / len(testParams[0]) if plotLabels: plotLabel = get_plot_label_values(result, plotLabels) axes.plot(testPredictsMean, label=plotLabel, color=color) axes.set_ylabel("Prediction Value") axes.set_xlabel("Time (hour)") ticks = range(0, len(testPredictsMean), int(tickPeriod / stepLen)) axes.set_xticks(ticks, minor=False) axes.set_xticklabels(range(len(ticks)), fontdict=None, minor=False) axes.xaxis.grid(which='major', alpha=0.7, linestyle=':') pos_idx = find_ranges(testLabels, val=1) seizure_idx = np.transpose(pos_idx)[1] + int( timingParams.sphLen * 60 / stepLen) for sei, idx in zip(seizure_idx, range(len(seizure_idx))): if idx == 0: axes.axvline(x=sei, linewidth=2, color='r', label='seizure onsets') else: axes.axvline(x=sei, linewidth=2, color='r') axes.set_xlim(0, len(testPredictsMean)) axes.set_ylim(0, 1.05) handles, labels = axes.get_legend_handles_labels() axes.legend(handles, labels, ncol=1, loc=0, handletextpad=0.5, columnspacing=0.2) if plotTitle: axes.set_title(get_plot_label_values(result, plotTitle), loc="center") print("\tinterictal Hour:{}\tseizure Count:{}".format( intHour, len(seizure_idx))) fig.show()
def compare_subject_roc(results, plotLabels=None, plotTitle=None): fig, axes = plt.subplots(1, 1) fig.suptitle('ROC Analysis', fontweight='bold') #colors = cm.gist_ncar(np.linspace(0, 1, 4)) colors = cm.tab20(np.linspace(0, 1, 20)) for result, color in zip(results, colors): featureParams = result['featureParams'] timingParams = result['timingParams'] labels = result['dataset'].labels foldPairs = result["dataset"].fold_pairs testParams = result["trainResult"].testParams stepLen = (featureParams.windowLength * (1 - featureParams.overlap)) refractoryLen = timingParams.refractoryLen * 60 refractoryPeriod = int(refractoryLen / stepLen) testLabels = np.full(len(labels), fill_value=-1, dtype=int) for s in range(len(testParams)): testLabels[foldPairs[s][0][2]] = np.squeeze( np.hstack((np.full(len(testParams[s][0].testPred[0]), fill_value=labelTypes["interictal"]), np.full(len(testParams[s][0].testPred[1]), fill_value=labelTypes["preictal"])))) testPredictsMean = np.zeros(len(labels), dtype=float) for f in range(len(testParams[0])): testPredicts = np.zeros(len(labels), dtype=float) for s in range(len(testParams)): testPredicts[foldPairs[s][f][2]] += np.squeeze( np.hstack((np.squeeze(testParams[s][f].testPred[0]), np.squeeze(testParams[s][f].testPred[1])))) testPredicts = moving_average(testPredicts, 30) testPredictsMean += testPredicts / len(testParams[0]) fpr, tpr, fps, tps, thresholds = my_roc_curve( testLabels, testPredictsMean, pos_label=labelTypes["preictal"], neg_label=labelTypes["interictal"], refractory_period=refractoryPeriod, rate_period=60 * 60 / stepLen, point=20) cutoff = find_cutoff_idx(fpr, tpr) if plotLabels: plotLabel = get_plot_label_values(result, plotLabels) axes.plot(fpr, tpr, label=plotLabel, color=color) axes.plot(fpr[cutoff], tpr[cutoff], 'o', mfc="None", color=color, label="({},{})".format(round(fpr[cutoff], 2), round(tpr[cutoff], 2))) print( "\t" + plotLabel + "\tfpr:{:2.4f}\ttpr:{:2.4f}\tfps:{:2d}\ttps:{:2d}".format( fpr[cutoff], tpr[cutoff], int(fps[cutoff]), int(tps[cutoff]))) axes.set_ylabel("Sensitivity") axes.set_xlabel("False Positive Rate (per hour)") axes.set_ylim(0, 1.02) axes.set_xlim(-0.01, 1) handles, labels = axes.get_legend_handles_labels() axes.legend(flip(handles, 2), flip(labels, 2), ncol=2, loc=4, handletextpad=0.15, columnspacing=0.2) if plotTitle: axes.set_title(get_plot_label_values(result, plotTitle), loc="center") fig.show()