fast-mri/scripts/11.visualize_multiple_frocs.py

146 lines
5.9 KiB
Python
Raw Normal View History

2022-04-29 10:51:19 +02:00
from sfransen.utils_quintin import *
import matplotlib.pyplot as plt
import argparse
import matplotlib.ticker as tkr
from umcglib.froc.p_auc import partial_auc
import numpy as np
parser = argparse.ArgumentParser(
description='Visualise froc results')
parser.add_argument('-saveas',
help='')
parser.add_argument('-comparison',
help='')
parser.add_argument('--experiment', '-s',
metavar='[series_name]', required=True, nargs='+',
help='List of series to include, must correspond with' +
"path files in ./data/")
parser.add_argument('-yaml_metric',
help='List of series to include, must correspond with' +
"path files in ./data/")
args = parser.parse_args()
if args.comparison:
2022-09-05 11:16:50 +02:00
colors = ['b','c','r','m','g','g','y','y']
plot_type = ['-','-','-','--','-','--','-','--']
2022-04-29 10:51:19 +02:00
else:
2023-03-28 14:48:28 +02:00
colors = ['b','r','g','k','c','y']
2022-04-29 10:51:19 +02:00
plot_type = ['-','-','-','-','-','-']
2022-09-05 11:16:50 +02:00
2022-04-29 10:51:19 +02:00
yaml_metric = args.yaml_metric
experiments = args.experiment
print(experiments)
experiment_path = []
auroc = []
paufroc = []
False_possitives = []
sensitivity = []
fig = plt.figure(1)
ax = fig.add_subplot(111)
2023-03-28 14:48:28 +02:00
False_possitives_mean = np.linspace(0.1, 1, 200)
2022-05-06 15:29:01 +02:00
for idx in range(len(args.experiment)):
2022-04-29 10:51:19 +02:00
2022-05-06 15:29:01 +02:00
paufroc = []
2022-09-05 11:16:50 +02:00
experiment_path = []
auroc = []
paufroc = []
False_possitives = []
sensitivity = []
for fold in range(5):
2022-05-06 15:29:01 +02:00
# print('fold:',fold)
2022-04-29 10:51:19 +02:00
experiment_metrics = {}
experiment_path = f'./../train_output/{experiments[idx]}_{fold}/froc_metrics_{yaml_metric}.yml'
experiment_metrics = read_yaml_to_dict(experiment_path)
2023-03-28 14:48:28 +02:00
pfroc = partial_auc(experiment_metrics["sensitivity"],experiment_metrics["FP_per_case"],low=0.0, high=1)
2022-04-29 10:51:19 +02:00
paufroc.append(round(pfroc,2))
False_possitives.append(experiment_metrics["FP_per_case"])
sensitivity_ = np.interp(False_possitives_mean,experiment_metrics["FP_per_case"],experiment_metrics["sensitivity"])
sensitivity.append(sensitivity_)
2022-09-05 11:16:50 +02:00
2022-05-06 15:29:01 +02:00
print(f'pfROC van {experiments[idx]}: {paufroc}')
2022-04-29 10:51:19 +02:00
# calculate mean and std
sensitivity_mean = np.squeeze(np.mean(sensitivity,axis=0))
sensitivity_std = np.multiply(np.squeeze(np.std(sensitivity,axis=0)),2)
plt.plot(False_possitives_mean, sensitivity_mean,color=colors[idx],linestyle=plot_type[idx])
2022-09-05 11:16:50 +02:00
plt.fill_between(False_possitives_mean, np.subtract(sensitivity_mean,sensitivity_std), np.add(sensitivity_mean,sensitivity_std),alpha=0.10,color=colors[idx],)
2022-04-29 10:51:19 +02:00
ax.set(xscale="log")
ax.axes.xaxis.set_minor_locator(tkr.LogLocator(base=10, subs='all'))
ax.axes.xaxis.set_minor_formatter(tkr.NullFormatter())
ax.axes.xaxis.set_major_formatter(tkr.ScalarFormatter())
2022-09-05 11:16:50 +02:00
ax.axes.get_xaxis()
ax.axes.get_yaxis()
2023-03-28 14:48:28 +02:00
ax.axes.set_xlim(left=0.1, right=1)
ax.axes.xaxis.set_major_locator(tkr.FixedLocator([0.1,0.5,1]))
2022-04-29 10:51:19 +02:00
fpr = []
tpr = []
2022-05-06 15:29:01 +02:00
fpr_mean = np.linspace(0, 1, 200)
2022-04-29 10:51:19 +02:00
2022-05-06 15:29:01 +02:00
for idx in range(len(args.experiment)):
2022-09-05 11:16:50 +02:00
aufroc = []
experiment_path = []
2022-05-06 15:29:01 +02:00
auroc = []
2022-09-05 11:16:50 +02:00
fpr = []
tpr = []
for fold in range(5):
2022-04-29 10:51:19 +02:00
print('fold:',fold)
experiment_metrics = {}
experiment_path = f'./../train_output/{experiments[idx]}_{fold}/froc_metrics_{yaml_metric}.yml'
experiment_metrics = read_yaml_to_dict(experiment_path)
2022-05-06 15:29:01 +02:00
auroc.append(round(experiment_metrics['auroc'],3))
2022-04-29 10:51:19 +02:00
fpr.append(experiment_metrics["fpr"])
tpr_ = np.interp(fpr_mean,experiment_metrics["fpr"],experiment_metrics["tpr"])
tpr.append(tpr_)
2022-05-06 15:29:01 +02:00
print(f'auROC van {experiments[idx]}: {auroc}')
2022-04-29 10:51:19 +02:00
tpr_mean = np.squeeze(np.mean(tpr,axis=0))
tpr_std = np.multiply(np.squeeze(np.std(tpr,axis=0)),2)
plt.figure(2)
plt.plot(fpr_mean, tpr_mean,color=colors[idx],linestyle=plot_type[idx])
2022-09-05 11:16:50 +02:00
plt.fill_between(fpr_mean, np.subtract(tpr_mean,tpr_std), np.add(tpr_mean,tpr_std),alpha=0.10,color=colors[idx],)
2022-04-29 10:51:19 +02:00
print(auroc)
experiments = [exp.replace('train_10h_', '') for exp in experiments]
experiments = [exp.replace('train_n0.001_', '') for exp in experiments]
experiments = [exp.replace('_', ' ') for exp in experiments]
# experiments = ['10% noise','1% noise','0.1% noise','0.05% noise']
concat_func = lambda x,y: x + " (" + str(y) + ")"
experiments_paufroc = list(map(concat_func,experiments,paufroc)) # list the map function
plt.figure(1)
2022-09-05 11:16:50 +02:00
plt.title('fROC curve', fontsize=20)
plt.xlabel('False positive lesions', fontsize=18)
plt.ylabel('Sensitivity', fontsize=18)
2022-05-06 15:29:01 +02:00
# plt.legend(experiments_paufroc,loc='lower right')
2022-09-05 11:16:50 +02:00
# plt.legend(['$T2_{tra}$ $ADC_{b50-b400}$ $b1400_{b50-b400}$','$T2_{tra}$ $ADC_{b50-b800}$ $b1400_{b50-b800}$','$T2_{tra}$ $ADC_{b50-b400-b800}$ $b1400_{b50-b400-b800}$'],loc='lower right')
2023-03-28 14:48:28 +02:00
plt.legend(['$T2_{tra}$ $ADC_{b50-b400-b800}$ $b1400_{b50-b400-b800}$','$T2_{tra}$ b50 b400 b800'],loc='lower right')
# plt.legend(["All b-values","Omitting b800","Omitting b400"],loc='lower right',fontsize=16)
2022-05-06 15:29:01 +02:00
2022-04-29 10:51:19 +02:00
# plt.xlim([0,50])
plt.grid()
plt.ylim([0,1])
plt.yticks([0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1])
plt.savefig(f"./../train_output/fROC_{args.saveas}.png", dpi=300)
concat_func = lambda x,y: x + " (" + str(y) + ")"
experiments_auroc = list(map(concat_func,experiments,auroc)) # list the map function
plt.figure(2)
2022-09-05 11:16:50 +02:00
plt.title('ROC curve',fontsize=20)
2022-05-06 15:29:01 +02:00
# plt.legend(experiments_auroc,loc='lower right')
2023-03-28 14:48:28 +02:00
plt.legend(['$T2_{tra}$ $ADC_{b50-b400-b800}$ $b1400_{b50-b400-b800}$','$T2_{tra}$ b50 b400 b800'],loc='lower right')
2022-09-05 11:16:50 +02:00
# plt.legend(['$T2_{tra}$ $ADC_{b50-b400}$ $b1400_{b50-b400}$','$T2_{tra}$ $ADC_{b50-b800}$ $b1400_{b50-b800}$','$T2_{tra}$ $ADC_{b50-b400-b800}$ $b1400_{b50-b400-b800}$'],loc='lower right')
2023-03-28 14:48:28 +02:00
# plt.legend(["All b-values","Omitting b800","Omitting b400"],loc='lower right',fontsize=16)
2022-09-05 11:16:50 +02:00
plt.xlabel('False positive rate',fontsize=18)
plt.ylabel('True positive rate',fontsize=18)
2022-05-06 15:29:01 +02:00
plt.ylim([0,1])
plt.xlim([0,1])
2022-04-29 10:51:19 +02:00
plt.grid()
plt.savefig(f"./../train_output/ROC_{args.saveas}.png", dpi=300)