[637b40]: / notebooks / bland_altman.py

Download this file

148 lines (120 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# %%
from pathlib import Path
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.transforms as transforms
import numpy as np
import pandas as pd
from pyCompare import blandAltman
test_path = "../results/hold-out-test.csv"
external_all_path = "../results/external-data.csv"
prospective_all_path = "../results/prospective-data.csv"
figureSize = (4, 4)
def dice_plot(
x,
y,
title,
figureSize=figureSize,
x_label="Reference htTKV (mL)",
y_label="Dice Similarity Coefficient",
):
dpi = 72
fig, ax = plt.subplots(figsize=figureSize, dpi=dpi)
draw = True
meanColour = "#6495ED"
loaColour = "coral"
pointColour = "#6495ED"
ax.scatter(x, y, alpha=0.5, c=pointColour)
mean = np.mean(y)
sd = np.std(y)
sd95 = 1.96 * np.std(y)
trans = transforms.blended_transform_factory(ax.transAxes, ax.transData)
offset = 0.08
ax.text(
0.97,
mean - 2 * offset,
f"Mean {mean:.2f}",
ha="right",
va="bottom",
transform=trans,
)
ax.text(
0.97,
mean - 3 * offset,
f"SD {sd:.2f}",
ha="right",
va="bottom",
transform=trans,
)
ax.text(
0.97,
mean - 4 * offset,
f"±1.96 SD in coral",
ha="right",
va="bottom",
transform=trans,
)
ax.axhspan(mean - sd95, mean + sd95, facecolor=loaColour, alpha=0.2)
# Hide the right and top spines
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
ax.set_title(title, pad=10)
ax.set_ylim([0, 1.05])
ax.axhline(np.mean(y), color=meanColour, linestyle="--")
ax.set_yticks(np.arange(0, 1.1, 0.1))
# %%
# external_ds = pd.read_csv(external_path)
# prospective_ds = pd.read_csv(prospective_path)
prospective_ds = pd.read_csv(prospective_all_path)
prospective_ds.TKV_Pred /= 1000 # corrects units to mL
prospective_ds.TKV_GT /= 1000 # corrects units to mL
test_ds = pd.read_csv(test_path)
test_ds.TKV_GT /= 1000 # corrects units to mL
test_ds.TKV_Pred /= 1000 # corrects units to mL
external_ds = pd.read_csv(external_all_path)
# prospective_ds = pd.read_csv(prospective_all_path)
# %%
blandAltman(
prospective_ds.TKV_GT,
prospective_ds.TKV_Pred,
percentage=True,
title="BA Plot - Prospective dataset",
figureSize=figureSize,
)
# %%
blandAltman(
external_ds.TKV_GT,
external_ds.TKV_Pred,
percentage=True,
title="BA Plot - External dataset",
figureSize=figureSize,
)
# %%
blandAltman(
test_ds.TKV_GT,
test_ds.TKV_Pred,
percentage=True,
title="BA Plot - Hold-out-test dataset",
figureSize=figureSize,
)
# %%
dice_plot(
prospective_ds.TKV_GT,
prospective_ds.patient_dice,
title="Dice by TKV - Prospective dataset",
figureSize=figureSize,
)
dice_plot(
external_ds.TKV_GT,
external_ds.patient_dice,
title="Dice by TKV - External dataset",
figureSize=figureSize,
)
dice_plot(
test_ds.TKV_GT,
test_ds.patient_dice,
title="Dice by TKV - Hold-out-test dataset",
figureSize=figureSize,
)