Exemple #1
0
def recommend_slices_single_case(i, prediction_filenames,
                                 uncertainty_filenames, gt_filenames,
                                 save_path, find_best_slices_func, num_slices,
                                 slice_gap, default_size):
    uncertainty, affine, spacing, header = utils.load_nifty(
        uncertainty_filenames[i])
    prediction, _, _, _ = utils.load_nifty(prediction_filenames[i])
    gt, _, _, _ = utils.load_nifty(gt_filenames[i])
    adapted_slice_gap = adapt_slice_gap(uncertainty, slice_gap, default_size)
    # indices_dim_0: Sagittal
    # indices_dim_1: Coronal
    # indices_dim_2: Axial
    indices_dim_0, indices_dim_1, indices_dim_2 = find_best_slices_func(
        prediction, uncertainty, num_slices, adapted_slice_gap)
    recommended_slices = len(indices_dim_0) + len(indices_dim_1) + len(
        indices_dim_2)
    gt_slices = comp_gt_slices(gt)
    # total_recommended_slices += recommended_slices
    # total_gt_slices += gt_slices
    # print("{} recommended slices: {}, gt slices: {}, ratio: {}".format(os.path.basename(uncertainty_filenames[i]), recommended_slices, gt_slices, recommended_slices / gt_slices))
    # print("indices_dim_0: {}, indices_dim_1: {}, indices_dim_2: {}".format(indices_dim_0, indices_dim_1, indices_dim_2))
    filtered_mask = filter_mask(gt, indices_dim_0, indices_dim_1,
                                indices_dim_2)
    utils.save_nifty(save_path +
                     os.path.basename(uncertainty_filenames[i])[:-7] +
                     "_0001.nii.gz",
                     filtered_mask,
                     affine,
                     spacing,
                     header,
                     is_mask=True)
    return recommended_slices, gt_slices
Exemple #2
0
def round_mask(filename, save_path):
    mask, affine, spacing, header = utils.load_nifty(filename)
    mask = np.rint(mask)
    mask = mask.astype(np.uint8)
    utils.save_nifty(save_path + os.path.basename(filename),
                     mask,
                     affine,
                     spacing,
                     None,
                     is_mask=True)
Exemple #3
0
def remove_label(load_path, save_path, labels_to_remove):
    save_path = utils.fix_path(save_path)
    load_path = utils.fix_path(load_path)
    filenames = utils.load_filenames(load_path)

    for filename in tqdm(filenames):
        basename = os.path.basename(filename)
        mask, affine, spacing, header = utils.load_nifty(filename)
        for label in labels_to_remove:
            mask[mask == label] = 0
        mask = np.rint(mask)
        mask = mask.astype(int)
        utils.save_nifty(save_path + basename, mask, affine, spacing, header)
Exemple #4
0
def save_rois(save_dir, roi_dir, rois, img, uncertainty_mask, affine, spacing, header):
    # rois = [[img_roi, mask_roi, coords, img_with_bb], ...]
    Path(save_dir + roi_dir).mkdir(parents=True, exist_ok=True)
    for i, (img_roi, mask_roi, img_with_bb, mask_with_bb, x, y, z, width, length) in enumerate(rois):
        img_roi = Image.fromarray(img_roi).convert('L')
        mask_roi = Image.fromarray(mask_roi).convert('L')
        img_with_bb = Image.fromarray(img_with_bb).convert('L')
        mask_with_bb = Image.fromarray(mask_with_bb).convert('L')
        img_roi.save(save_dir + roi_dir + str(i).zfill(3) + "_img_x_" + str(x+1) + "_y_" + str(y+1) + "_z_" + str(z+1) + ".png")
        mask_roi.save(save_dir + roi_dir + str(i).zfill(3) + "_uncertainty_x_" + str(x+1) + "_y_" + str(y+1) + "_z_" + str(z+1) + ".png")
        img_with_bb.save(save_dir + roi_dir + str(i).zfill(3) + "_img_with_bb_x_" + str(x + 1) + "_y_" + str(y + 1) + "_z_" + str(z + 1) + ".png")
        mask_with_bb.save(save_dir + roi_dir + str(i).zfill(3) + "_mask_with_bb_x_" + str(x + 1) + "_y_" + str(y + 1) + "_z_" + str(z + 1) + ".png")
    utils.save_nifty(save_dir + roi_dir + "img.nii.gz", img, affine, spacing, header)
    utils.save_nifty(save_dir + roi_dir + "uncertainty_mask.nii.gz", uncertainty_mask, affine, spacing, header)
Exemple #5
0
def add_to_images_or_masks(image_path,
                           guiding_mask_path,
                           save_path,
                           is_mask=False):
    image_filenames = utils.load_filenames(image_path)
    guiding_mask_filenames = utils.load_filenames(guiding_mask_path)
    for i in tqdm(range(len(image_filenames))):
        image, affine, spacing, header = utils.load_nifty(image_filenames[i])
        guiding_mask, _, _, _ = utils.load_nifty(guiding_mask_filenames[i])
        image = np.stack([image, guiding_mask], axis=-1)
        utils.save_nifty(save_path + os.path.basename(image_filenames[i]),
                         image,
                         affine,
                         spacing,
                         header,
                         is_mask=is_mask)
Exemple #6
0
def comp_guiding_mask(load_path,
                      save_path,
                      slice_gap,
                      default_size,
                      slice_depth=3):
    filenames = utils.load_filenames(load_path)
    for filename in tqdm(filenames):
        mask, affine, spacing, header = utils.load_nifty(filename)
        adapted_slice_gap = adapt_slice_gap(mask, slice_gap, default_size)
        mask_slices = comp_slices_mask(mask,
                                       adapted_slice_gap,
                                       slice_depth=slice_depth)
        utils.save_nifty(save_path + os.path.basename(filename),
                         mask_slices,
                         affine,
                         spacing,
                         header,
                         is_mask=True)
Exemple #7
0
def merge_labels(load_mask, save_mask, load_label_table):
    mask, affine, spacing, header = utils.load_nifty(load_mask)
    mask = mask.astype(int)
    ggo, cons, pe = get_labels(load_label_table)

    for label in tqdm(np.concatenate((ggo, cons, pe), axis=0), disable=True):
        mask[mask == label] = -label

    for label in tqdm(ggo, disable=True):
        mask[mask == -label] = 1

    for label in tqdm(cons, disable=True):
        mask[mask == -label] = 2

    for label in tqdm(pe, disable=True):
        mask[mask == -label] = 3

    mask = np.rint(mask)
    mask = mask.astype(int)

    utils.save_nifty(save_mask, mask, affine, spacing, header)
Exemple #8
0
def comp_uncertainties(load_dir, save_dir, uncertainty_estimator, type="part"):
    load_dir = utils.fix_path(load_dir)
    save_dir = utils.fix_path(save_dir)
    filenames = utils.load_filenames(load_dir)
    cases, nr_labels, nr_parts = group_data(filenames)
    print("nr_cases: ", len(cases))
    print("nr_labels: ", nr_labels)
    print("nr_parts: ", nr_parts)

    for case in tqdm(cases):
        for label in range(nr_labels + 1):
            predictions = []
            for part in range(nr_parts + 1):
                name = load_dir + str(case).zfill(4) + "_" + str(
                    label) + "_" + type + "_" + str(part) + ".nii.gz"
                prediction, affine, spacing, header = utils.load_nifty(name)
                predictions.append(prediction.astype(np.float16))
            predictions = np.stack(predictions)
            uncertainty = uncertainty_estimator(predictions)
            name = save_dir + str(case).zfill(4) + "_" + str(label) + ".nii.gz"
            utils.save_nifty(name, uncertainty, affine, spacing, header)
Exemple #9
0
def round_mask(filename):
    mask, affine, spacing, header = utils.load_nifty(filename)
    mask = np.rint(mask)
    mask = mask.astype(int)
    utils.save_nifty(filename, mask, affine, spacing, header)