Spaces:
Runtime error
Runtime error
File size: 7,127 Bytes
a2dba58 |
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# %%
import numpy as np
import torch
from pathlib import Path
import os, sys
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
HEAD = Path(os.getcwd()).parent.parent
head = HEAD / 'logs'
sys.path.append(HEAD)
from dataloaders.JSRT import JSRTDataset
from dataloaders.NIH import NIHDataset
from dataloaders.Montgomery import MonDataset
NIHPATH = "<PATH_TO_DATA>/NIH/"
NIHFILE = "correspondence_with_chestXray8.csv"
MONPATH = "<PATH_TO_DATA>/MontgomerySet/"
MONFILE = "patient_data.csv"
JSRTPATH = "<PATH_TO_DATA>/JSRT"
if __name__=="__main__":
predictions = {'baseline':{'JSRT':{}, 'NIH':{}, 'Montgomery':{}},
'LEDM':{'JSRT':{}, 'NIH':{}, 'Montgomery':{}},
'TEDM':{'JSRT':{}, 'NIH':{}, 'Montgomery':{}},}
files_needed = ["JSRT_val_predictions.pt", "JSRT_test_predictions.pt", "NIH_predictions.pt", "Montgomery_predictions.pt",]
for exp in ['baseline', 'LEDM', "TEDM"]:
for datasize in [1,3,6,12,24,49,98,197]:
if len(set(files_needed) - set(os.listdir(head / exp / str(datasize) ))) == 0:
for file in files_needed[1:]:
output = torch.load(head / exp / str(datasize) / file)
metrics_datasize = 197 if datasize == "None" else int(datasize)
predictions[exp][file.rsplit("_")[0]][metrics_datasize]= output['y_hat']
else:
print(f"Experiment {exp} is missing files")
# %%
img_size = 128
NIH_dataset = NIHDataset(NIHPATH, NIHPATH, NIHFILE, img_size)
JSRT_dataset = JSRTDataset(JSRTPATH, HEAD/ "data/", "JSRT_test_split.csv", img_size)
MON_dataset = MonDataset(MONPATH, MONPATH, MONFILE, img_size)
# %%
loaders = {'JSRT': JSRT_dataset, 'NIH': NIH_dataset, 'Montgomery': MON_dataset}
m ="dice"
sz=4
ftsize= 40
fig, all_axs = plt.subplots(6, 21, figsize=(21*sz, 6*sz))
all_patients = [17, 13, 0, 1, 72, 78]
# JSRT
dataset ="JSRT"
patient = np.random.randint(0, len(loaders[dataset]))
patient = all_patients[0]
print("JSRT1 - ", patient)
out = loaders[dataset][patient]
axs = all_axs[:3, :7]
for rowax, exp in zip(axs, ['baseline', 'LEDM', 'TEDM']):
rowax[0].imshow(out[0][0].numpy(), cmap='gray')
rowax[1].imshow(out[1][0].numpy(), interpolation='none', cmap='gray')
for ax, dssize in zip(rowax[2:], [1, 3, 6, 12, 197]):
ax.imshow(predictions[exp][dataset][dssize][patient].numpy()[0]>.5, interpolation='none')
axs[0, 0].set_title("JSRT - Image", fontsize=ftsize)
axs[0, 1].set_title("JSRT - GT", fontsize=ftsize)
axs[0, 2].set_title("1 (1%)" , fontsize=ftsize)
axs[0, 3].set_title("3 (2%)", fontsize=ftsize)
axs[0, 4].set_title("6 (3%)", fontsize=ftsize)
axs[0, 5].set_title("12 (6%)", fontsize=ftsize)
axs[0, 6].set_title("197 (100%)", fontsize=ftsize)
axs[0,0].set_ylabel("Baseline", fontsize=ftsize)
axs[1,0].set_ylabel("LEDM", fontsize=ftsize)
axs[2,0].set_ylabel("TEDM", fontsize=ftsize)
#
axs = all_axs[3:, :7]
dataset ="JSRT"
patient = np.random.randint(0, len(loaders[dataset]))
patient = all_patients[1]
print("JSRT2 - ", patient)
out = loaders[dataset][patient]
for rowax, exp in zip(axs, ['baseline', 'LEDM', 'TEDM']):
rowax[0].imshow(out[0][0].numpy(), cmap='gray')
rowax[1].imshow(out[1][0].numpy(), interpolation='none', cmap='gray')
for ax, dssize in zip(rowax[2:], [1, 3, 6, 12, 197]):
ax.imshow(predictions[exp][dataset][dssize][patient].numpy()[0]>.5, interpolation='none')
axs[0,0].set_ylabel("Baseline", fontsize=ftsize)
axs[1,0].set_ylabel("LEDM", fontsize=ftsize)
axs[2,0].set_ylabel("TEDM", fontsize=ftsize)
#
axs = all_axs[:3, 7:14]
dataset ="NIH"
patient = np.random.randint(0, len(loaders[dataset]))
patient = all_patients[2]
print("NIH1 - ", patient)
out = loaders[dataset][patient]
for rowax, exp in zip(axs, ['baseline', 'LEDM', 'TEDM']):
rowax[0].imshow(out[0][0].numpy(), cmap='gray')
rowax[1].imshow(out[1][0].numpy(), interpolation='none', cmap='gray')
for ax, dssize in zip(rowax[2:], [1, 3, 6, 12, 197]):
ax.imshow(predictions[exp][dataset][dssize][patient].numpy()[0]>.5, interpolation='none')
axs[0, 0].set_title("NIH - Image", fontsize=ftsize)
axs[0, 1].set_title("NIH - GT", fontsize=ftsize)
axs[0, 2].set_title("1 (1%)" , fontsize=ftsize)
axs[0, 3].set_title("3 (2%)", fontsize=ftsize)
axs[0, 4].set_title("6 (3%)", fontsize=ftsize)
axs[0, 5].set_title("12 (6%)", fontsize=ftsize)
axs[0, 6].set_title("197 (100%)", fontsize=ftsize)
#
#
axs = all_axs[3:, 7:14]
dataset ="NIH"
patient = np.random.randint(0, len(loaders[dataset]))
patient = all_patients[3]
print("NIH2 - ", patient)
out = loaders[dataset][patient]
for rowax, exp in zip(axs, ['baseline', 'LEDM', 'TEDM']):
rowax[0].imshow(out[0][0].numpy(), cmap='gray')
rowax[1].imshow(out[1][0].numpy(), interpolation='none', cmap='gray')
for ax, dssize in zip(rowax[2:], [1, 3, 6, 12, 197]):
ax.imshow(predictions[exp][dataset][dssize][patient].numpy()[0]>.5, interpolation='none')
#
#
axs = all_axs[:3, 14:]
dataset ="Montgomery"
patient = np.random.randint(0, len(loaders[dataset]))
patient = all_patients[4]
print("MON1 - ",patient)
out = loaders[dataset][patient]
for rowax, exp in zip(axs, ['baseline', 'LEDM', 'TEDM']):
rowax[0].imshow(out[0][0].numpy(), cmap='gray')
rowax[1].imshow(out[1][0].numpy(), interpolation='none', cmap='gray')
for ax, dssize in zip(rowax[2:], [1, 3, 6, 12, 197]):
ax.imshow(predictions[exp][dataset][dssize][patient].numpy()[0]>.5, interpolation='none')
axs[0, 0].set_title("Mont. - Image", fontsize=ftsize)
axs[0, 1].set_title("Mont. - GT", fontsize=ftsize)
axs[0, 2].set_title("1 (1%)", fontsize=ftsize)
axs[0, 3].set_title("3 (2%)", fontsize=ftsize)
axs[0, 4].set_title("6 (3%)", fontsize=ftsize)
axs[0, 5].set_title("12 (6%)", fontsize=ftsize)
axs[0, 6].set_title("197 (100%)", fontsize=ftsize)
#
axs = all_axs[3:, 14:]
dataset ="Montgomery"
patient = np.random.randint(0, len(loaders[dataset]))
patient = all_patients[5]
print("MON2 - ",patient)
out = loaders[dataset][patient]
for rowax, exp in zip(axs, ['baseline', 'LEDM', 'TEDM']):
rowax[0].imshow(out[0][0].numpy(), cmap='gray')
rowax[1].imshow(out[1][0].numpy(), interpolation='none', cmap='gray')
for ax, dssize in zip(rowax[2:], [1, 3, 6, 12, 197]):
ax.imshow(predictions[exp][dataset][dssize][patient].numpy()[0]>.5, interpolation='none')
# remove ticks
for ax in all_axs.flatten():
ax.set_xticks([])
ax.set_yticks([])
sns.despine(ax=ax, left=True, bottom=True)
plt.subplots_adjust(wspace=0.00,
hspace=0.00)
plt.tight_layout()
plt.savefig("visualisations2.pdf", bbox_inches='tight')
plt.show()
|