Beispiel #1
0
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
    imgsrc = "/jukebox/wang/zahra/kelly_cell_detection_analysis/comparison_to_clearmap/annotated_volumes"
    vols = [
        os.path.join(imgsrc,
                     os.path.basename(fl) + ".tif") for fl in brains
    ]
    vols.sort()
    roipths = [
        os.path.join(imgsrc, xx) for xx in os.listdir(imgsrc)
        if "RoiSet.zip" in xx
    ]
    roipths.sort()
    #these will be zyx
    #note that importing it this way, the z dimension does not start from 0, but neither does clearmaps, so probably ok???
    annotated_cells = np.array([
        np.array([[int(yy) for yy in xx.replace(".roi", "").split("-")]
                  for xx in read_roi_zip(roipth, points=True)])
        for roipth in roipths
    ])

    #voxel pair cutoff
    cutoff = 10

    #make dest
    if not os.path.exists(dst): os.mkdir(dst)

    for br, brain in enumerate(brains):
        fls = [os.path.join(brain, xx) for xx in os.listdir(brain)]
        fls.sort()
        #init dataframe
        print("brain: %s\ncutoff: %s \n\n" % (os.path.basename(brain), cutoff))
        df = pd.DataFrame()
from tools.conv_net.utils.io import pairwise_distance_metrics, read_roi_zip
from tools.utils.io import listdirfull

pth = "/jukebox/wang/zahra/cnn_to_clearmap_comparison/roc_curve/"
dst = os.path.join(pth, "results")
if not os.path.exists(dst): os.mkdir(dst)

src = os.path.join(pth, "cell_arrays")
brains = [os.path.join(src, xx) for xx in os.listdir(src)] #only using the ones from the new sweep
src = "/jukebox/wang/pisano/conv_net/annotations/all_better_res/h129/input_files/"
vols = [os.path.join(src, os.path.basename(fl)+".tif") for fl in brains]
roipths = [os.path.join(src, os.path.basename(fl)+".roi.zip") for fl in brains]
#these will be zyx 
#note that importing it this way, the z dimension does not start from 0, but neither does clearmaps, so probably ok???
annotated_cells = np.array([np.array([[int(yy) for yy in xx[0].replace(".roi", "").split("-")]for xx in
                            read_roi_zip(roipth, include_roi_name=True)]) for roipth in roipths])

#voxel pair cutoff
cutoff = 30

for br, brain in enumerate(brains):
    fls = [os.path.join(brain, xx) for xx in os.listdir(brain)]; fls.sort()
    #init dataframe
    print("cutoff: %s \n\n" % 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))
    "dp_ann_201904_an21_ymazefos_020719_hypothal_z450-469_dp_ann.RoiSet.zip",
    "dp_ann_an16_ymazecfos_z260-299_retrosplenial_dp_ann.Roiset.zip",
    "tp_ann_201904_an22_ymazefos_020719_pfc_z150-169.RoiSet.zip",
    "dp_ann_201904_an19_ymazefos_020719_cb_z380-399_dp_ann.RoiSet.zip",
    "tp_ann_201904_an4_ymazefos_020119_pfc_z200-219.RoiSet.zip"
]
roi_pths = [os.path.join(src, "raw_inputs/" + xx) for xx in rois]

dct = pickle.load(open(os.path.join(src, "clearmap_thresholds_sweep.p"), "rb"),
                  encoding="latin1")

#these will be zyx
#note that importing it this way, the z dimension does not start from 0, but neither does clearmaps, so probably ok???
annotated_cells = np.array([
    np.array([[int(yy) for yy in xx[0].replace(".roi", "").split("-")]
              for xx in read_roi_zip(roi_pth, include_roi_name=True)])
    for roi_pth in roi_pths
])

#voxel pair cutoff
cutoffs = [5, 7]

for cutoff in cutoffs:
    #init dataframe
    print("cutoff: %s \n\n" % cutoff)
    df = pd.DataFrame()
    df["parameters"] = list(dct.keys())

    df["tp"] = np.zeros(len(df))
    df["fp"] = np.zeros(len(df))
    df["fn"] = np.zeros(len(df))