forked from ygCoconut/mAP_3Dvolume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_2DmAP.py
154 lines (129 loc) · 4.91 KB
/
demo_2DmAP.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
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
import os
import sys
path = os.path.dirname(os.path.abspath(""))+"/"
sys.path.append(path)
sys.path.insert(1, path+'cellpose/')
print(path)
from skimage import io
import numpy as np
from CellAnalysis.utils import *
from CellAnalysis import evaluation
from mAP_3Dvolume import vol3d_eval_custom, vol3d_util_custom
import mAP_3Dvolume as meanap
import glob
def get_scores(pred_seg, gt_seg):
sz_gt = np.array(gt_seg.shape)
sz_pred = pred_seg.shape
if np.abs((sz_gt - sz_pred)).max() > 0:
print('Warning: size mismatch. gt: ', sz_gt, ', pred: ', sz_pred)
sz = np.minimum(sz_gt, sz_pred)
pred_seg = pred_seg[:sz[0], :sz[1]]
gt_seg = gt_seg[:sz[0], :sz[1]]
ui, uc = np.unique(pred_seg, return_counts=True)
uc = uc[ui > 0]
ui = ui[ui > 0]
pred_score = np.ones([len(ui), 2], int)
pred_score[:, 0] = ui
pred_score[:, 1] = uc
thres = np.fromstring('5e3, 1.5e4', sep=",")
areaRng = np.zeros((len(thres) + 2, 2), int)
areaRng[0, 1] = 1e10
areaRng[-1, 1] = 1e10
areaRng[2:, 0] = thres
areaRng[1:-1, 1] = thres
return pred_score, areaRng
def get_precision(pred, gt):
pred_score, areaRng = get_scores(pred, gt)
result_p, result_fn, pred_score_sorted = meanap.vol3d_util_custom.seg_iou2d_sorted(pred, gt, pred_score, areaRng)
v3dEval = meanap.vol3d_eval_custom.VOL3Deval(result_p, result_fn, pred_score_sorted, output_name='map_output')
stats = v3dEval.get_stats()
return stats
def main():
# file root
file_root_0 = path + 'data/2P_functional/PC/'
file_root_1 = path + 'data/2P_functional/CP/'
file_root_2 = path + 'data/2P_functional/GT/'
file_root_3 = path + 'data/2P_functional/SD/'
file_root_4 = path + 'data/2P_functional/CP-notrain/'
vol_1 = []
cp = []
vol_2 = []
vol_3 = []
vol_4 = []
gt = []
cp_notrain = []
sd = []
pc = []
for name in sorted(glob.glob(file_root_0 + '*')):
pc.append(io.imread(name))
for name in sorted(glob.glob(file_root_1 + '*')):
if 'mask' in name:
cp.append(io.imread(name).astype(np.uint16))
else:
continue
for name in sorted(glob.glob(file_root_2 + '*')):
if 'mask' in name:
gt.append(io.imread(name).astype(np.uint16))
else:
continue
for name in sorted(glob.glob(file_root_3 + '*')):
sd.append(io.imread(name))
for name in sorted(glob.glob(file_root_4 + '*')):
if 'mask' in name:
cp_notrain.append(io.imread(name))
else:
continue
cp = np.array(cp)
cp_notrain = np.array(cp_notrain)
gt = np.array(gt)
sd = np.array(sd)
pc = np.array(pc)
dist_thresh = 0.5
adc_cp = []
adpc_cp = []
adgc_cp = []
adc_cp_notrain = []
adpc_cp_notrain = []
adgc_cp_notrain = []
adc_sd = []
adpc_sd = []
adgc_sd = []
adc_pc = []
adpc_pc = []
adgc_pc = []
ap_pc = []
ap_cp = []
ap_sd = []
ap_cp_notrain = []
for i in range(np.array(gt).shape[0]):
stats_pc = get_precision(pc[i], gt[i])
ap_pc.append(stats_pc['Average Precision'][:, 0])
min_adc, min_adpc, min_adgc = evaluation.average_distance_between_centroids(gt[i], pc[i], dist_thresh=dist_thresh,
all_stats=False, size=(0.6, 0.6))
adc_pc.append(min_adc)
adpc_pc.append(min_adpc)
adgc_pc.append(min_adgc)
stats_cp = get_precision(cp[i], gt[i])
ap_cp.append(stats_cp['Average Precision'][:, 0])
min_adc, min_adpc, min_adgc = evaluation.average_distance_between_centroids(gt[i], cp[i], dist_thresh=dist_thresh,
all_stats=False, size=(0.6, 0.6))
adc_cp.append(min_adc)
adpc_cp.append(min_adpc)
adgc_cp.append(min_adgc)
stats_sd = get_precision(sd[i], gt[i])
ap_sd.append(stats_sd['Average Precision'][:, 0])
min_adc, min_adpc, min_adgc = evaluation.average_distance_between_centroids(gt[i], sd[i], dist_thresh=dist_thresh,
all_stats=False, size=(0.6, 0.6))
adc_sd.append(min_adc)
adpc_sd.append(min_adpc)
adgc_sd.append(min_adgc)
stats_cp_notrain = get_precision(cp_notrain[i], gt[i])
ap_cp_notrain.append(stats_cp_notrain['Average Precision'][:, 0])
min_adc, min_adpc, min_adgc = evaluation.average_distance_between_centroids(gt[i], cp_notrain[i],
dist_thresh=dist_thresh,
all_stats=False, size=(0.6, 0.6))
adc_cp_notrain.append(min_adc)
adpc_cp_notrain.append(min_adpc)
adgc_cp_notrain.append(min_adgc)
if __name__ == '__main__':
main()