Exemplo n.º 1
0
def make_image_sequence(input_file, output_folder, prefix='man_seg_000_'):
    '''
    the fiji 'image sequence' can lead to the error :'Incorrect count for "ColorMap" when using Tiffreader, therefore made my own
    '''
    filehandler = FileHandler()
    filehandler.d_load_info['f_name'] = str(input_file)
    filehandler.d_save_info['save_dir'] = str(output_folder)
    a_img = filehandler.load_tif()
    for ix_z, a_slice in enumerate(a_img):
        suffix = "{:03d}".format(ix_z)
        filehandler.d_save_info['f_name'] = "{0}{1}".format(prefix, suffix)
        filehandler.save_data(a_slice)

    return
Exemplo n.º 2
0
def calculate_CTC_SEG_slice_detail(a_gt,
                                   a_res,
                                   output_folder,
                                   ix_z="",
                                   verbose=False,
                                   output=sys.stdout,
                                   d_d_gt_res_overlap={}):
    '''
	calculate SEG score for 2 slices, with subcalculations
	'''
    if output_folder:
        # f_name =  output_folder / "CTC_SEG_detail_calculations.txt"
        # f_output = open( f_name  ,"w" )
        fh = FileHandler()
        fh.d_save_info['save_dir'] = str(output_folder)

    a_gt_labels = np.unique(a_gt)
    a_gt_labels = np.delete(a_gt_labels, [0])
    if verbose: print('ground truth labels are ', a_gt_labels, file=output)

    SEG_counter = 0
    SEG_sum = 0
    jaccard_sum = 0

    for gt_label in a_gt_labels:
        a_bool_gt = a_gt == gt_label
        nb_gt_label_pix = np.sum(a_bool_gt)
        a_res_overlap = np.where(a_bool_gt, a_res, 0)
        a_bincount_overlap = np.bincount(a_res_overlap.flatten())
        a_bincount_overlap[0] = 0
        a_res_labels_overlap = np.unique(a_res_overlap)
        a_res_labels_overlap = np.delete(a_res_labels_overlap, [0])
        if verbose:
            print('  ground truth label {0} overlaps with res labels {1} '.
                  format(gt_label, a_res_labels_overlap),
                  file=output)

        max_overlap_label = 0
        max_overlap_ratio = 0
        max_jaccard = 0

        d_GT_volumes = d_d_gt_res_overlap.setdefault(
            "GT_volumes", {}
        )  #d_d_gt_res_overlap :  key = gt-label, value = dict (key = RES label, value = nb of overlapping pixels with gt-label)
        nb_pixel_self = d_GT_volumes.setdefault(gt_label, 0)
        d_GT_volumes[gt_label] = nb_pixel_self + np.sum(a_bool_gt)

        d_res_overlap = d_d_gt_res_overlap.setdefault(gt_label, {})
        for res_label in a_res_labels_overlap:
            nb_res_label_pix = np.sum(a_res == res_label)
            nb_overlap_pix = a_bincount_overlap[res_label]
            jaccard = (nb_overlap_pix) / (nb_gt_label_pix + nb_res_label_pix -
                                          nb_overlap_pix)
            overlap_ratio = nb_overlap_pix / nb_gt_label_pix
            if overlap_ratio > max_overlap_ratio:
                max_overlap_label = res_label
                max_overlap_ratio = overlap_ratio
                max_jaccard = jaccard

            rec = "    GT_label {0} ({4} pixels)  overlaps with RES_label {1}({5} pixels) with {6} pixels overlap.  \n    Jaccard = {3} with ratio (nb pix overlap/nb pix GT) = {2}\n".format(
                gt_label, res_label, (nb_overlap_pix / nb_gt_label_pix),
                jaccard, nb_gt_label_pix, nb_res_label_pix, nb_overlap_pix)

            nb_pixel_overlap = d_res_overlap.setdefault(res_label, 0)
            d_res_overlap[res_label] += nb_overlap_pix

            if verbose: print(rec, file=output)

        SEG_score = 0

        if max_overlap_ratio < 0.5:
            SEG_score = 0
            rec = "  SEG_score = 0 because max_overlap_ratio < 0.5 !"
        else:
            SEG_score = max_jaccard
            rec = "  SEG_score = {0} for gt_label {1} overlapping res_label {2} ".format(
                SEG_score, gt_label, max_overlap_label)

        SEG_counter += 1
        SEG_sum += SEG_score
        jaccard_sum += max_jaccard

        if verbose: print(rec, file=output)

        #write visual
        if output_folder:
            # f_output.write(rec)
            fh.d_save_info[
                'f_name'] = "GT_{0}(z{5})_vs_RES_{1}_SEG={2}|jac={3}|overlap={4}".format(
                    gt_label, max_overlap_label, round(SEG_score, 2),
                    round(max_jaccard, 2), round(max_overlap_ratio, 2),
                    str(ix_z).zfill(3))

            a_output = np.zeros_like(a_gt)
            # a_output [a_gt == gt_label] = gt_label
            # a_output [a_res == max_overlap_label] = max_overlap_label
            # a_output [(a_gt == gt_label) & (a_res == max_overlap_label)] = np.round(SEG_score*100)
            a_output[a_gt == gt_label] = 1
            a_output[a_res == max_overlap_label] = 2
            a_output[(a_gt == gt_label) & (a_res == max_overlap_label)] = 3
            fh.save_data(a_output, verbose=verbose)

    # if output_folder:f_output.close()
    a_res_labels = np.unique(a_res)
    a_res_labels = np.delete(a_res_labels, [0])

    if list(a_gt_labels):  # a slice with no GT is skipped (=SEG score rules)
        for res_label in a_res_labels:
            d_RES_volumes = d_d_gt_res_overlap.setdefault(
                "RES_volumes", {}
            )  #d_d_gt_res_overlap :  key = gt-label, value = dict (key = RES label, value = nb of overlapping pixels with gt-label)
            nb_pixel_self = d_RES_volumes.setdefault(res_label, 0)
            d_RES_volumes[res_label] = nb_pixel_self + np.sum(
                a_res == res_label)
            if res_label == 2:
                print(np.sum(a_res == res_label), 'for a total of ',
                      d_RES_volumes[res_label])

    return SEG_sum, jaccard_sum, SEG_counter, d_d_gt_res_overlap