-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.py
More file actions
89 lines (69 loc) · 2.55 KB
/
Copy pathhelper.py
File metadata and controls
89 lines (69 loc) · 2.55 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
from os import listdir as _listdir, getcwd, mkdir, path
from os.path import isfile as _isfile,join as _join, abspath, splitext
def sort_func(s):
sort_string = s.split('/')[-1].rstrip()
return sort_string
def load_files(data_file_location):
data = []
startswith = None
endswith = None
contains = None
contains_not = None
for path in data_file_location:
gg = [(_join(path, f) if path != "." else f) for f in _listdir(path) if
_isfile(_join(path, f)) and (startswith == None or f.startswith(startswith)) and (
endswith == None or f.endswith(endswith)) and (contains == None or contains in f) and (
contains_not == None or (not (contains_not in f)))]
data.append(gg)
combined_list = []
# Sort the lists:
for i in range(len(data)):
elem = sorted(data[i])
combined_list = combined_list + elem
combined_list = strip_files_of_doubles(combined_list)
combined_list = sorted(combined_list, key=sort_func)
return combined_list
def strip_files_of_doubles(files):
new_files = []
for file in files:
split = file.split('.')
if (split[-1] != "img" and split[-1] != "mat"):
new_files.append(file)
return new_files
def compute_scores(pred, label):
assert pred.shape == label.shape, "Shape mismatch between prediction and label when calculating scores"
shape = pred.shape
TP = 0.0
TN = 0.0
FP = 0.0
FN = 0.0
for i in range(0, shape[0]):
if (i % 25 == 0):
print("Comleted", float(i) / float(shape[0]) * 100, "%")
for j in range(0, shape[1]):
for k in range(0, shape[2]):
if (pred[i][j][k] == 1 and label[i][j][k] >= 1):
TP += 1
elif (pred[i][j][k] == 1 and label[i][j][k] == 0):
FP += 1
elif (pred[i][j][k] == 0 and label[i][j][k] >= 1):
FN += 1
elif (pred[i][j][k] == 0 and label[i][j][k] == 0):
TN += 1
print(TP) # 1343322.0
print(TN) # 46602103.0
print(FP) # 381066.0
print(FN) # 22600.0
if ((2 * TP + FP + FN) == 0):
dice_coefficient = 1.0
else:
dice_coefficient = float((2 * TP)) / float((2 * TP + FP + FN))
if ((TP + FN) == 0):
sensitivity = 1.0
else:
sensitivity = float(TP) / float((TP + FN))
if ((TN + FP) == 0):
specificity = 1.0
else:
specificity = float(TN) / float((TN + FP))
return dice_coefficient, sensitivity, specificity