def human_compare_with_raw_rois(ann1roipth, ann2roipth, cutoff=30): #format ZYX, and remove any rois missaved ann1_zyx_rois = np.asarray( [[int(yy) for yy in xx.replace(".roi", "").split("-")] for xx in read_roi_zip(ann1roipth, include_roi_name=True)]) ann2_zyx_rois = np.asarray( [[int(yy) for yy in xx.replace(".roi", "").split("-")] for xx in read_roi_zip(ann2roipth, include_roi_name=True)]) paired, tp, fp, fn = pairwise_distance_metrics( ann1_zyx_rois, ann2_zyx_rois, cutoff ) #returns true positive = tp; false positive = fp; false negative = fn precision = tp / (tp + fp) recall = tp / (tp + fn) #calculating precision and recall f1 = 2 * ( (precision * recall) / (precision + recall)) #calculating f1 score print( "\n Finished calculating statistics for set params\n\n\nReport:\n***************************\n\ Cutoff: {} \n\ F1 score: {}% \n\ true positives, false positives, false negatives: {} \n\ precision: {}% \n\ recall: {}%\n".format(cutoff, round(f1 * 100, 2), (tp, fp, fn), round(precision * 100, 2), round(recall * 100, 2))) return tp, fp, fn, f1
for sz in flds: arrs = [os.path.join(sz, xx) for xx in os.listdir(sz) if xx[-3:] == "npy"]; arrs.sort() #reinit tps = []; fps = []; fns = [] print(sz) for i in range(len(arrs)): #load points dict points_dict = load_dictionary("/home/wanglab/Documents/cfos_inputs/cfos_points_dictionary.p") #set ground truth ground_truth = list(points_dict[os.path.basename(arrs[i][:-13])+".npy"]) clearmap = np.load(arrs[i]) paired,tp,fp,fn = pairwise_distance_metrics(ground_truth, clearmap, cutoff = cutoff) #returns true positive = tp; false positive = fp; false negative = fn tps.append(tp); fps.append(fp); fns.append(fn) #append matrix to save all values to calculate f1 score tps_s.append(tps); fps_s.append(fps); fns_s.append(fns) tp = sum(tps); fp = sum(fps); fn = sum(fns) #sum all the elements in the lists precision = tp/(tp+fp); recall = tp/(tp+fn) #calculating precision and recall f1 = 2*( (precision*recall)/(precision+recall) ) #calculating f1 score #%% #left y axis #n_cells_per_size n_cells_per_size = np.asarray(n_cells_per_size) n_cells_per_size_av = np.mean(n_cells_per_size, axis = 1).astype(int)
fls.sort() #init dataframe print("brain: %s\ncutoff: %s \n\n" % (os.path.basename(brain), cutoff)) df = pd.DataFrame() df["parameters"] = [os.path.basename(xx) for xx in fls] df["tp"] = np.zeros(len(df)) df["fp"] = np.zeros(len(df)) df["fn"] = np.zeros(len(df)) df["f1"] = np.zeros(len(df)) for n, fl in enumerate(fls): if n % 50 == 0: print(n) detected_cells = np.load(fl, allow_pickle=True) paired, tp, fp, fn = pairwise_distance_metrics(annotated_cells[br], detected_cells, cutoff=cutoff, verbose=False) try: precision = tp / (tp + fp) recall = tp / (tp + fn) #calculating precision and recall f1 = 2 * ((precision * recall) / (precision + recall)) #calculating f1 score except Exception as e: print(e) f1 = np.nan #if tp, fn, etc. are 0 df.loc[df.parameters == os.path.basename(fl), "f1"] = f1 df.loc[df.parameters == os.path.basename(fl), "tp"] = tp df.loc[df.parameters == os.path.basename(fl), "fp"] = fp df.loc[df.parameters == os.path.basename(fl), "fn"] = fn
df["fn"] = np.zeros(len(df)) df["f1"] = np.zeros(len(df)) for n, fld in enumerate(list(dct.keys())): if n % 100 == 0: print(n) detected_cells = np.asarray([v for k, v in dct[fld].items()]) #cells were accidently saved as x y z instead detected_cells_reshape = np.asarray([ np.array([[z, y, x] for x, y, z in vol]) for vol in detected_cells ]) measures = [ pairwise_distance_metrics(annotated_cells[i], detected_cells_reshape[i], cutoff=cutoff, verbose=False) for i in range(len(detected_cells_reshape)) ] tp = sum([measure[1] for measure in measures]) fp = sum([measure[2] for measure in measures]) fn = sum([measure[3] for measure in measures]) try: precision = tp / (tp + fp) recall = tp / (tp + fn) #calculating precision and recall f1 = 2 * ((precision * recall) / (precision + recall)) #calculating f1 score except Exception as e: print(e)
tps = [] fps = [] fns = [] #set voxel cutoff value cutoff = 5 for i in range(len(ann2_dsets)): #set ground truth print(ann1_dsets[i]) ann1_ground_truth = points_dict[ann1_dsets[i]] ann2_ground_truth = points_dict[ann2_dsets[i]] paired, tp, fp, fn = pairwise_distance_metrics( ann2_ground_truth, ann1_ground_truth, cutoff=30 ) #returns true positive = tp; false positive = fp; false negative = fn #f1 per dset precision = tp / (tp + fp) recall = tp / (tp + fn) #calculating precision and recall f1 = 2 * ( (precision * recall) / (precision + recall)) #calculating f1 score tps.append(tp) fps.append(fp) fns.append(fn) #append matrix to save all values to calculate f1 score tp = sum(tps) fp = sum(fps) fn = sum(fns) #sum all the elements in the lists precision = tp / (tp + fp)