-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
123 lines (101 loc) · 4.35 KB
/
utils.py
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
import numpy as np
import pandas as pd
from dataset_utils import load_dataset
pathologies = {'DCM': 0, 'HCM': 1, 'MINF': 2, 'NOR': 3, 'RV': 4}
def compute_volume(seg, labels):
mask = np.zeros_like(seg, np.bool)
for l in labels:
mask[seg == l] = True
return np.sum(mask)
def get_df():
ds = load_dataset(root_dir='./ds-3d')
columns = ['lvc_edv', 'lvm_edv', 'rvc_edv', 'lvc_esv', 'lvm_esv', 'rvc_esv',
'lvc_sv', 'rvc_sv', 'lvc_ef', 'rvc_ef', 'lvc_rvc_edr', 'lvc_rvc_esr',
'lvm_lvc_edr', 'lvm_lvc_esr', 'height', 'weight', 'bmi', 'pathology']
patients = np.zeros((len(ds.keys()), len(columns)))
for i, pat in enumerate(ds):
lvc_edv = compute_volume(ds[pat]['ed_gt'], [3])
lvm_edv = compute_volume(ds[pat]['ed_gt'], [2])
rvc_edv = compute_volume(ds[pat]['ed_gt'], [1])
lvc_esv = compute_volume(ds[pat]['es_gt'], [3])
lvm_esv = compute_volume(ds[pat]['es_gt'], [2])
rvc_esv = compute_volume(ds[pat]['es_gt'], [1])
lvc_sv = np.abs(lvc_edv - lvc_esv)
rvc_sv = np.abs(rvc_edv - rvc_esv)
lvc_ef = lvc_sv / lvc_edv
rvc_ef = rvc_sv / rvc_edv
lvc_rvc_edr = lvc_edv / rvc_edv
lvc_rvc_esr = lvc_esv / rvc_esv
lvm_lvc_edr = lvm_edv / lvc_edv
lvm_lvc_esr = lvm_esv / lvc_esv
body_mass_index = ds[pat]['weight'] / ds[pat]['height'] ** 2
patients[i, 0] = lvc_edv
patients[i, 1] = lvm_edv
patients[i, 2] = rvc_edv
patients[i, 3] = lvc_esv
patients[i, 4] = lvm_esv
patients[i, 5] = rvc_esv
patients[i, 6] = lvc_sv
patients[i, 7] = rvc_sv
patients[i, 8] = lvc_ef
patients[i, 9] = rvc_ef
patients[i, 10] = lvc_rvc_edr
patients[i, 11] = lvc_rvc_esr
patients[i, 12] = lvm_lvc_edr
patients[i, 13] = lvm_lvc_esr
patients[i, 14] = ds[pat]['height']
patients[i, 15] = ds[pat]['weight']
patients[i, 16] = body_mass_index
patients[i, 17] = pathologies[ds[pat]['pathology']]
return pd.DataFrame(patients, columns=columns)
def get_seg_df():
ds = load_dataset(root_dir='./ds-3d')
columns = ['lvc_edv', 'lvm_edv', 'rvc_edv', 'lvc_esv', 'lvm_esv', 'rvc_esv',
'lvc_sv', 'rvc_sv', 'lvc_ef', 'rvc_ef', 'lvc_rvc_edr', 'lvc_rvc_esr',
'lvm_lvc_edr', 'lvm_lvc_esr', 'height', 'weight', 'bmi', 'pathology']
patients = np.zeros((len(ds.keys()), len(columns)))
for i, pat in enumerate(ds):
seg = np.load('./seg-pred/' + str(pat).zfill(3) + '.npy')
ed_seg = seg[:len(seg) // 2]
es_seg = seg[len(seg) // 2:]
lvc_edv = compute_volume(ed_seg, [3])
lvm_edv = compute_volume(ed_seg, [2])
rvc_edv = compute_volume(ed_seg, [1])
lvc_esv = compute_volume(es_seg, [3])
lvm_esv = compute_volume(es_seg, [2])
rvc_esv = compute_volume(es_seg, [1])
lvc_sv = np.abs(lvc_edv - lvc_esv)
rvc_sv = np.abs(rvc_edv - rvc_esv)
lvc_ef = lvc_sv / lvc_edv
rvc_ef = rvc_sv / rvc_edv
lvc_rvc_edr = lvc_edv / rvc_edv
lvc_rvc_esr = lvc_esv / rvc_esv
lvm_lvc_edr = lvm_edv / lvc_edv
lvm_lvc_esr = lvm_esv / lvc_esv
body_mass_index = ds[pat]['weight'] / ds[pat]['height'] ** 2
patients[i, 0] = lvc_edv
patients[i, 1] = lvm_edv
patients[i, 2] = rvc_edv
patients[i, 3] = lvc_esv
patients[i, 4] = lvm_esv
patients[i, 5] = rvc_esv
patients[i, 6] = lvc_sv
patients[i, 7] = rvc_sv
patients[i, 8] = lvc_ef
patients[i, 9] = rvc_ef
patients[i, 10] = lvc_rvc_edr
patients[i, 11] = lvc_rvc_esr
patients[i, 12] = lvm_lvc_edr
patients[i, 13] = lvm_lvc_esr
patients[i, 14] = ds[pat]['height']
patients[i, 15] = ds[pat]['weight']
patients[i, 16] = body_mass_index
patients[i, 17] = pathologies[ds[pat]['pathology']]
return pd.DataFrame(patients, columns=columns)
def get_folds():
folds = []
for i in range(5):
val_patients = np.arange(i + 1, 101, 5)
train_patients = np.delete(np.arange(1, 101), val_patients - 1)
folds.append((train_patients - 1, val_patients - 1))
return folds