-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluate-phase-classification.py
253 lines (234 loc) · 11.6 KB
/
evaluate-phase-classification.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import math
import shutil
import pandas as pd
from tqdm import tqdm
import os
import csv
import numpy as np
import json
class Parser(object):
def __init__(self, sctrack, pcnadeep, GT):
self.sctrack = pd.read_csv(sctrack)
self.pcnadeep = pd.read_csv(pcnadeep)
self.GT = pd.read_csv(GT)
def compare(self, row_GT: pd.Series, row_predcit: pd.Series):
"""
Compare the records of GT and predict, and if they match, return the corresponding GT value.
Otherwise, return False based on the error type
"""
# Fs = FP + FN
try:
gt_phase = row_GT['phase']
except KeyError:
gt_phase = row_GT['cell_type']
try:
predict_phase = row_predcit['phase']
except KeyError:
predict_phase = row_predcit['cell_type']
record = {'G1': False, 'S': False, 'G2': False, 'M': False, 'FP_G1': False, 'FP_S': False, 'FP_G2': False,
'FP_M': False,
'FN_G1': False, 'FN_S': False, 'FN_G2': False, 'FN_M': False
}
if predict_phase[-1] == '*':
if predict_phase[:-1] == gt_phase:
# if predict_phase[:-1] == 'G1' or predict_phase[:-1] == 'G2':
record[predict_phase[:-1]] = True
else:
record['FP_' + predict_phase[:-1]] = True
record['FN_' + gt_phase] = True
else:
if predict_phase == gt_phase:
record[predict_phase] = True
else:
record['FP_' + predict_phase] = True
record['FN_' + gt_phase] = True
return record
def recall(self, TP, FN):
"""recall score"""
return TP / (TP + FN)
def precision(self, TP, FP):
return TP / (TP + FP)
def F1(self, recall, precision):
return 2 * precision * recall / (precision + recall)
def evaluate(self):
"""
evaluate the accuracy for tracking result, compare with ground truth table.
"""
sctrack_NUM = 0
sctrack_TP_G1 = 0
sctrack_TP_S = 0
sctrack_TP_G2 = 0
sctrack_TP_M = 0
sctrack_FP_G1 = 0
sctrack_FP_S = 0
sctrack_FP_G2 = 0
sctrack_FP_M = 0
sctrack_FN_G1 = 0
sctrack_FN_S = 0
sctrack_FN_G2 = 0
sctrack_FN_M = 0
pcnadeep_NUM = 0
pcnadeep_TP_G1 = 0
pcnadeep_TP_S = 0
pcnadeep_TP_G2 = 0
pcnadeep_TP_M = 0
pcnadeep_FP_G1 = 0
pcnadeep_FP_S = 0
pcnadeep_FP_G2 = 0
pcnadeep_FP_M = 0
pcnadeep_FN_G1 = 0
pcnadeep_FN_S = 0
pcnadeep_FN_G2 = 0
pcnadeep_FN_M = 0
mp_sctrack = {}
mp_pcnadeep = {}
data_pcnadeep = self.pcnadeep.sort_values(by='frame_index')
data_sctrack = self.sctrack.sort_values(by='frame_index')
data_GT = self.GT.sort_values(by='frame_index')
frames = pd.unique(data_GT['frame_index'])
for frame in tqdm(frames):
tmp_pcnadeep_dict = {}
tmp_sctrack_dict = {}
tmp_df_GT = data_GT[data_GT['frame_index'] == frame]
tmp_df_sctrack = data_sctrack[data_sctrack['frame_index'] == frame]
tmp_df_pcnadeep = data_pcnadeep[data_pcnadeep['frame_index'] == frame]
for _, row in tmp_df_GT.iterrows():
matched_sctrack_flag = False
matched_pcnadeep_flag = False
gt_phase = row['phase']
gt_position = (row['center_x'], row['center_y'])
for _, sctrack_row in tmp_df_sctrack.iterrows():
sctrack_position = (sctrack_row['center_x'], sctrack_row['center_y'])
dist = math.sqrt(
(sctrack_position[1] - gt_position[1]) ** 2 + (sctrack_position[0] - gt_position[0]) ** 2)
if dist < 10:
matched_sctrack_flag = True
tmp_sctrack_dict[gt_position] = sctrack_position
match_result = self.compare(row, sctrack_row)
# sctrack_NUM += 1
if match_result['G1']:
sctrack_TP_G1 += 1
if match_result['S']:
sctrack_TP_S += 1
if match_result['G2']:
sctrack_TP_G2 += 1
if match_result['M']:
sctrack_TP_M += 1
if match_result['FP_G1']:
sctrack_FP_G1 += 1
if match_result['FP_S']:
sctrack_FP_S += 1
if match_result['FP_G2']:
sctrack_FP_G2 += 1
if match_result['FP_M']:
sctrack_FP_M += 1
if match_result['FN_G1']:
sctrack_FN_G1 += 1
if match_result['FN_S']:
sctrack_FN_S += 1
if match_result['FN_G2']:
sctrack_FN_G2 += 1
if match_result['FN_M']:
sctrack_FN_M += 1
break
if not matched_sctrack_flag:
if gt_phase == 'G1':
sctrack_FN_G1 += 1
if gt_phase == 'S':
sctrack_FN_S += 1
if gt_phase == 'G2':
sctrack_FN_G2 += 1
if gt_phase == 'M':
sctrack_FN_M += 1
sctrack_NUM += 1
for _, pcnadeep_row in tmp_df_pcnadeep.iterrows():
pcnadeep_position = (pcnadeep_row['center_x'], pcnadeep_row['center_y'])
dist = math.sqrt(
(pcnadeep_position[1] - gt_position[1]) ** 2 + (pcnadeep_position[0] - gt_position[0]) ** 2)
if dist < 10:
matched_pcnadeep_flag = True
tmp_pcnadeep_dict[gt_position] = pcnadeep_position
# try:
# if pcnadeep_row['phase'][:-1] == '*':
# continue
# except KeyError:
# if pcnadeep_row['cell_type'][:-1] == '*':
# continue
# pcnadeep_NUM += 1
match_result = self.compare(row, pcnadeep_row)
if match_result['G1']:
pcnadeep_TP_G1 += 1
if match_result['S']:
pcnadeep_TP_S += 1
if match_result['G2']:
pcnadeep_TP_G2 += 1
if match_result['M']:
pcnadeep_TP_M += 1
if match_result['FP_G1']:
pcnadeep_FP_G1 += 1
if match_result['FP_S']:
pcnadeep_FP_S += 1
if match_result['FP_G2']:
pcnadeep_FP_G2 += 1
if match_result['FP_M']:
pcnadeep_FP_M += 1
if match_result['FN_G1']:
pcnadeep_FN_G1 += 1
if match_result['FN_S']:
pcnadeep_FN_S += 1
if match_result['FN_G2']:
pcnadeep_FN_G2 += 1
if match_result['FN_M']:
pcnadeep_FN_M += 1
break
if not matched_pcnadeep_flag:
if gt_phase == 'G1':
pcnadeep_FN_G1 += 1
if gt_phase == 'S':
pcnadeep_FN_S += 1
if gt_phase == 'G2':
pcnadeep_FN_G2 += 1
if gt_phase == 'M':
pcnadeep_FN_M += 1
pcnadeep_NUM += 1
mp_sctrack[frame] = tmp_sctrack_dict
mp_pcnadeep[frame] = tmp_pcnadeep_dict
# break
sctrack_result = [sctrack_NUM, sctrack_TP_G1, sctrack_TP_S, sctrack_TP_G2, sctrack_TP_M, sctrack_FP_G1, sctrack_FP_S,
sctrack_FP_G2, sctrack_FP_M, sctrack_FN_G1, sctrack_FN_S, sctrack_FN_G2, sctrack_FN_M]
pcnadeep_result = [pcnadeep_NUM, pcnadeep_TP_G1, pcnadeep_TP_S, pcnadeep_TP_G2, pcnadeep_TP_M, pcnadeep_FP_G1,
pcnadeep_FP_S, pcnadeep_FP_G2, pcnadeep_FP_M, pcnadeep_FN_G1, pcnadeep_FN_S, pcnadeep_FN_G2,
pcnadeep_FN_M]
sctrack_recall = [self.recall(sctrack_TP_G1, sctrack_FN_G1), self.recall(sctrack_TP_S, sctrack_FN_S),
self.recall(sctrack_TP_G2, sctrack_FN_G2), self.recall(sctrack_TP_M, sctrack_FN_M),
self.recall(sum([sctrack_TP_G1, sctrack_TP_S, sctrack_TP_G2, sctrack_TP_M]),
sum([sctrack_FN_G1, sctrack_FN_S, sctrack_FN_G2, sctrack_FN_M]))]
sctrack_precision = [self.precision(sctrack_TP_G1, sctrack_FP_G1), self.precision(sctrack_TP_S, sctrack_FP_S),
self.precision(sctrack_TP_G2, sctrack_FP_G2), self.precision(sctrack_TP_M, sctrack_FP_M),
self.precision(sum([sctrack_TP_G1, sctrack_TP_S, sctrack_TP_G2, sctrack_TP_M]),
sum([sctrack_FP_G1, sctrack_FP_S, sctrack_FP_G2, sctrack_FP_M]))]
sctrack_F1 = [self.F1(*i) for i in zip(sctrack_recall, sctrack_precision)]
pcnadeep_recall = [self.recall(pcnadeep_TP_G1, pcnadeep_FN_G1), self.recall(pcnadeep_TP_S, pcnadeep_FN_S),
self.recall(pcnadeep_TP_G2, pcnadeep_FN_G2), self.recall(pcnadeep_TP_M, pcnadeep_FN_M),
self.recall(sum([pcnadeep_TP_G1, pcnadeep_TP_S, pcnadeep_TP_G2, pcnadeep_TP_M]),
sum([pcnadeep_FN_G1, pcnadeep_FN_S, pcnadeep_FN_G2, pcnadeep_FN_M]))]
pcnadeep_precision = [self.precision(pcnadeep_TP_G1, pcnadeep_FP_G1),
self.precision(pcnadeep_TP_S, pcnadeep_FP_S),
self.precision(pcnadeep_TP_G2, pcnadeep_FP_G2),
self.precision(pcnadeep_TP_M, pcnadeep_FP_M),
self.precision(sum([pcnadeep_TP_G1, pcnadeep_TP_S, pcnadeep_TP_G2, pcnadeep_TP_M]),
sum([pcnadeep_FP_G1, pcnadeep_FP_S, pcnadeep_FP_G2, pcnadeep_FP_M]))]
pcnadeep_F1 = [self.F1(*i) for i in zip(pcnadeep_recall, pcnadeep_precision)]
sctrack_ret = sctrack_result + sctrack_recall + sctrack_precision + sctrack_F1
pcnadeep_ret = pcnadeep_result + pcnadeep_recall + pcnadeep_precision + pcnadeep_F1
# print(f'TP_G1 TP_S TP_G2 TP_M Fs\nsctrack: {sctrack_result}\npcnadeep: {pcnadeep_result}', end='')
# return sctrack_result, pcnadeep_result
return sctrack_ret, pcnadeep_ret
def test():
gt = r"G:\paper\evaluate_data\incorrect-data-test\normal-dataset\copy_of_1_xy01\5-track-GT.csv"
sctrack = fr"G:\paper\evaluate_data\incorrect-data-test\normal-dataset\copy_of_1_xy01\tracking_output\track.csv"
pcnadeep = fr"G:\paper\evaluate_data\incorrect-data-test\normal-dataset\copy_of_1_xy01\track\refined-pcnadeep(CCDeep_format).csv"
parse = Parser(sctrack, pcnadeep, gt)
sctrack_ret, pcnadeep_ret = parse.evaluate()
print('sctrack: ', sctrack_ret)
print('pcnadeep: ', pcnadeep_ret)