def stat_slide_info(gt_type, eva_type): stat_cfg = init.stat_config() path_info = init.path_info() outs_path = os.path.join(path_info.outs, stat_cfg.info_file) vs_file = gname.get_VS_file_path() files = open(vs_file, "r") fd_outs = open(outs_path, "ab+") #print "type: %d %d "%(gt_type,eva_type) if stat_cfg.gt_itc_mode == 0: GtStr = stat_cfg.GT_str16 else: GtStr = stat_cfg.GT_str17 match_cnt = 0 match_id_str = "" for line in files: st = line.split(",") #print "[%s %s] [%s %s]" %(st[0],GtStr[gt_type],st[1],stat_cfg.eva_str[eva_type]) if str(st[0]) == str(GtStr[gt_type]): if str(st[1]) == str(stat_cfg.eva_str[eva_type]): match_cnt += 1 match_id_str = "%s,%d" % (match_id_str, int(st[2])) context = "%s,%s,%d,%s\n" % (GtStr[gt_type], stat_cfg.eva_str[eva_type], match_cnt, match_id_str) fd_outs.write(context) fd_outs.close() files.close() return match_cnt
def loop_patient(bar_img, gate): slide_info = init.slide() path_info = init.path_info() cfg = init.config() patient_info = ["", "", "", "", ""] init.set_positive_gate(gate) ## patients loop for patient_id in range(slide_info.start_id, slide_info.end_id): print( "################### Patient:%d Gate:%1.4lf ###################" % (patient_id, gate)) for node_id in range(slide_info.node_num): print(">>>Patient:%d Node:%d" % (patient_id, node_id)) #get score slide_score = slide_run.gen_slide_score(patient_id, node_id) #generate heatmap draw_heatmap_img(slide_score, patient_id, node_id, cfg.step) #positive check if bar_img == 1: gate_cnt = positive_check.section_stat(slide_score) positive_check.section_bar_img(gate_cnt, (patient_id, node_id)) positive_flag, mask_dat = positive_check.positive_find( slide_score, gate) if positive_flag: mask_dat = positive_check.transfer_find(slide_score, mask_dat) if cfg.gen_slide_info_xml_mode == 1: positive_xml.gen_xml(mask_dat, patient_id, node_id, gate) print("patient:" + str(patient_id) + " node:" + str(node_id) + " is Positive!") #mask_dat = positive_check.slide_mask(slide_score,cfg.positive_gate) #metastasis_check.get_positive_count(mask_dat) slide_type_str = metastasis_check.get_slide_type( patient_id, node_id, mask_dat) patient_info[node_id] = slide_type_str else: patient_info[node_id] = cfg.slide_type[0] if (slide_score.shape[0] != 100) & (slide_score.shape[1] != 100): metastasis_check.save_slide_info(patient_id, node_id, 0, 0, patient_info[node_id]) #pnstage_file_name = "%s_%1.3lf.csv" %(cfg.result_csv_file,init.get_positive_gate()) #result_path = os.path.join(path_info.pns_info,pnstage_file_name) result_path = gname.get_pnstage_file_path() #print "patient :%d "%(patient_id) print patient_info result.result(patient_info, result_path, patient_id) if cfg.eva_mode == 1: val_kappa = eva.evaluate_main() statinfo.write_stat_info(val_kappa) print "Threshold:%1.3lf Kappa:%1.5lf" % (cfg.positive_gate, val_kappa) stat_cfg = init.stat_config() if stat_cfg.pnstage_kappa_mode == 1: statinfo.write_pn_stage_kappa()
def write_pn_stage_kappa(): path_info = init.path_info() stat_cfg = init.stat_config() gate = init.get_positive_gate() c17_gt_path = os.path.join(path_info.config, stat_cfg.c17_pnstage) c17_eva_path = gname.get_pnstage_file_path() kappa_score = eva.pnstage_kappa(c17_eva_path, c17_gt_path) file_path = gname.get_pnstage_kappa_file_path() result_pnstage_kappa = open(file_path, "ab+") context_str = "%lf,%lf\n" % (gate, kappa_score) result_pnstage_kappa.write(context_str) result_pnstage_kappa.close()
def write_stat_info(kappa): stat_cfg = init.stat_config() vs_file = gname.get_VS_file_path() cnt = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] if os.path.exists(vs_file): #files = open(vs_file,"r") if stat_cfg.gt_itc_mode == 0: cnt[0] = stat_slide_info(0, 0) # negative #negative cnt[1] = stat_slide_info(1, 2) # micro #micro cnt[2] = stat_slide_info(2, 3) # macro #macro cnt[3] = stat_slide_info(0, 1) # negative #itc cnt[4] = stat_slide_info(0, 2) # negative #micro cnt[5] = stat_slide_info(0, 3) # negative #macro cnt[6] = stat_slide_info(1, 3) # micro #macro cnt[7] = stat_slide_info(2, 2) # macro #micro cnt[8] = stat_slide_info(2, 1) # macro #itc cnt[9] = stat_slide_info(2, 0) # macro #negative cnt[10] = stat_slide_info(1, 1) # micro #itc cnt[11] = stat_slide_info(1, 0) # micro #negative #files.close() elif stat_cfg.gt_itc_mode == 1: cnt[0] = stat_slide_info(0, 0) # negative #negative cnt[1] = stat_slide_info(1, 1) # itc #itc cnt[2] = stat_slide_info(2, 2) # micro #micro cnt[3] = stat_slide_info(3, 3) # macro #macro cnt[4] = stat_slide_info(0, 1) # negative #itc cnt[5] = stat_slide_info(0, 2) # negative #micro cnt[6] = stat_slide_info(0, 3) # negative #macro cnt[7] = stat_slide_info(1, 0) # itc #negative cnt[8] = stat_slide_info(1, 2) # itc #micro cnt[9] = stat_slide_info(1, 3) # itc #macro cnt[10] = stat_slide_info(2, 0) # micro #negative cnt[11] = stat_slide_info(2, 1) # micro #itc cnt[12] = stat_slide_info(2, 3) # micro #macro cnt[13] = stat_slide_info(3, 0) # macro #negative cnt[14] = stat_slide_info(3, 1) # macro #itc cnt[15] = stat_slide_info(3, 2) # macro #micro rate = write_stat_sum(cnt) write_kappa_score(rate, kappa) else: print "cannot find file :%s " % (vs_file)
def write_kappa_score(rate, kappa): stat_cfg = init.stat_config() #path_info = init.path_info() #outs_path = os.path.join(path_info.outs,stat_cfg.info_file) outs_path = gname.get_result_info_path() fd_outs = open(outs_path, "ab+") gate = init.get_positive_gate() context = "Threshold:,%lf\n" % (gate) context = context + "Kappa:,%1.5lf\n" % (kappa) context = context + "\n\n" context = context + "#,#,#,#,#,#,#,#,#,#,#,#,#,#\n" fd_outs.write(context) fd_outs.close() outs_path = gname.get_kappa_file_path() fd_outs = open(outs_path, "ab+") context = "%1.4lf,%lf,%lf\n" % (gate, kappa, rate) fd_outs.write(context) fd_outs.close()
def calculate_kappa(ground_truth_map, result_map): stage_list = ['itc', 'micro', 'macro', 'negative'] stat_cfg = init.stat_config() ground_truth_stage_list = [] result_stage_list = [] for patient_id, result_stage in result_map.iteritems(): if stat_cfg.gt_itc_mode == 0: result_stage = result_stage.replace('itc', 'negative') result_stage_list.append(result_stage) ground_truth_stage_list.append(ground_truth_map[patient_id]) #print ground_truth_stage_list, result_stage_list # Return the Kappa score. cohen_kappa_score = sklearn.metrics.cohen_kappa_score( y1=ground_truth_stage_list, y2=result_stage_list, labels=stage_list, weights='quadratic') #print cohen_kappa_score return cohen_kappa_score
def get_result_info_path(): path_info = init.path_info() stat_cfg = init.stat_config() path_file = os.path.join(path_info.outs, stat_cfg.info_file) return path_file
def get_VS_file_path(): path_info = init.path_info() stat_cfg = init.stat_config() file_name = "%s_%1.3lf.csv" % (stat_cfg.vs_file, init.get_positive_gate()) path_file = os.path.join(path_info.outs, file_name) return path_file
def get_GT_C17_path(): #cfg = init.config() path_info = init.path_info() stat_cfg = init.stat_config() path_file = os.path.join(path_info.config, stat_cfg.GT_file_C17) return path_file
def get_pnstage_kappa_file_path(): path_info = init.path_info() stat_cfg = init.stat_config() path_file = os.path.join(path_info.outs, stat_cfg.patient_kappa_file) return path_file