Exemplo n.º 1
0
def idxs2pred_memo_all(input_idx, atlas_idx):

    # objs = img_obj, mask_obj

    if input_idx in memo:
        input_obj = np2rs(memo[input_idx][0])
        input_mask_obj = np2rs(memo[input_idx][1])
    else:
        input_obj, input_mask_obj = idx2obj(input_idx)
        # store (img arr, mask arr) in memo
        memo[input_idx] = sitk.GetArrayFromImage(
            input_obj), sitk.GetArrayFromImage(input_mask_obj)

    if atlas_idx in memo:
        atlas_obj = np2rs(memo[atlas_idx][0])
        atlas_mask_obj = np2rs(memo[atlas_idx][1])
    else:
        atlas_obj, atlas_mask_obj = idx2obj(atlas_idx)
        memo[atlas_idx] = sitk.GetArrayFromImage(
            atlas_obj), sitk.GetArrayFromImage(atlas_mask_obj)

    # ground truth = input mask obj
    gt_bbox = mask2bbox(sitk2np(input_mask_obj))

    # predicted = atlas2pred mask obj
    pred_mask_obj = atlas2pred(input_obj, atlas_obj, atlas_mask_obj)
    pred_bbox = mask2bbox(sitk2np(pred_mask_obj))

    return pred_bbox, gt_bbox
Exemplo n.º 2
0
def idxs2pred_bbox(input_idx, atlas_idx):

    # objs = img_obj, mask_obj
    input_obj, input_mask_obj = idx2obj(input_idx)
    atlas_obj, atlas_mask_obj = idx2obj(atlas_idx)

    # ground truth = input mask obj
    gt_bbox = mask2bbox(sitk2np(input_mask_obj))

    # predicted = atlas2pred mask obj
    pred_mask_obj = atlas2pred(input_obj, atlas_obj, atlas_mask_obj)
    pred_bbox = mask2bbox(sitk2np(pred_mask_obj))

    # store bbox's in memo
    for idx, bbox in ((input_idx, gt_bbox),
                      (atlas_idx, mask2bbox(sitk2np(atlas_mask_obj)))):
        if idx not in memo: memo[idx] = bbox

    memo[(input_idx, atlas_idx)] = pred_bbox

    return pred_bbox, gt_bbox
Exemplo n.º 3
0
def get_info_df_row(folder, obj):

    img_obj, mask_obj = obj

    shape0, shape1, shape2 = img_obj.GetSize()
    imin, imax, jmin, jmax, kmin, kmax = mask2bbox(sitk2np(mask_obj))
    roi_size0, roi_size1, roi_size2 = imax - imin, jmax - jmin, kmax - kmin

    return {
        "folder": folder,
        "shape0": shape0,
        "shape1": shape1,
        "shape2": shape2,
        "roi_size0": roi_size0,
        "roi_size1": roi_size1,
        "roi_size2": roi_size2,
        "imin": imin,
        "imax": imax,
        "jmin": jmin,
        "jmax": jmax,
        "kmin": kmin,
        "kmax": kmax
    }
Exemplo n.º 4
0
def idxs2csc_bbox(input_idx, atlas_idx, pad_amts):

    # pad dict
    pad_cols = "pad_imin", "pad_imax", "pad_jmin", "pad_jmax", "pad_kmin", "pad_kmax"
    pad_dict = dict(zip(pad_cols, pad_amts))

    print("Getting obj")

    # objs = img_obj, mask_obj
    input_obj, input_mask_obj = idx2obj(input_idx)
    atlas_obj, atlas_mask_obj = idx2obj(atlas_idx)

    print("Loaded obj")

    # ground truth = input mask obj
    gt_mask_arr = sitk2np(input_mask_obj).astype(bool)
    gt_bbox = mask2bbox(gt_mask_arr)

    print("Getting pred")

    # predicted = atlas2pred mask obj
    pred_mask_arr = sitk2np(atlas2pred(input_obj, atlas_obj,
                                       atlas_mask_obj)).astype(bool)
    pred_bbox = mask2bbox(pred_mask_arr)

    # metric
    align0_dice = compute_dice_coefficient(gt_mask_arr, pred_mask_arr)

    print("Starting csc")

    # cascade: expand margin around pred_bbox
    imin, imax, jmin, jmax, kmin, kmax = pred_bbox
    imin_pad, imax_pad, jmin_pad, jmax_pad, kmin_pad, kmax_pad = pad_amts

    imin, jmin, kmin = [
        max(0, x - pad)
        for x, pad in zip((imin, jmin, kmin), (imin_pad, jmin_pad, kmin_pad))
    ]
    imax, jmax, kmax = [
        min(x - pad, shape)
        for x, pad, shape in zip((imin, jmin,
                                  kmin), (imin_pad, jmin_pad,
                                          kmin_pad), input_obj.GetSize())
    ]

    # cascade: re-align sub-brain (ROI + margin) region
    csc = True
    try:
        csc_gt_mask_arr = sitk2np(input_mask_obj[imin:imax, jmin:jmax,
                                                 kmin:kmax]).astype(bool)
        csc_pred_mask_arr = sitk2np(
            atlas2pred(input_obj[imin:imax, jmin:jmax, kmin:kmax],
                       atlas_obj[imin:imax, jmin:jmax, kmin:kmax],
                       atlas_mask_obj[imin:imax, jmin:jmax,
                                      kmin:kmax])).astype(bool)

        print("End csc")

        print("mask2bbox")
        csc_pred_bbox = mask2bbox(pred_mask_arr)
        csc_gt_bbox = mask2bbox(csc_gt_mask_arr)

        # metric
        align1_dice = compute_dice_coefficient(csc_gt_mask_arr,
                                               csc_pred_mask_arr)

    except:
        print("Error")
        csc = False

        csc_pred_bbox = pred_bbox
        csc_gt_bbox = gt_bbox
        align1_dice = align0_dice

        # store error
        if (input_idx, atlas_idx) in csc_errors:
            csc_errors[(input_idx, atlas_idx)].append(pad_dict)
        else:
            csc_errors[(input_idx, atlas_idx)] = [pad_dict]

    print("Return row")

    # store input idx, atlas idx, pad amts, bbox delta between gt and pred
    return {
        "input_idx": input_idx,
        "atlas_idx": atlas_idx,
        "align0": align0,
        "align1": align1,
        "csc": csc,
        **pad_dict,
        **bbox2diff(csc_gt_bbox, csc_pred_bbox)
    }
Exemplo n.º 5
0
def atlas2pred_bbox(input_obj, atlas_obj, atlas_mask_obj):
    return mask2bbox(sitk2np(atlas2pred(input_obj, atlas_obj, atlas_mask_obj)))
Exemplo n.º 6
0
def idxs2csc_bbox(input_idx, atlas_idx, pad_amts):

    # pad dict
    pad_cols = "pad_imin", "pad_imax", "pad_jmin", "pad_jmax", "pad_kmin", "pad_kmax"
    pad_dict = dict(zip(pad_cols, pad_amts))

    print("Getting obj")

    # objs = img_obj, mask_obj
    input_obj, input_mask_obj = idx2obj(input_idx)
    atlas_obj, atlas_mask_obj = idx2obj(atlas_idx)

    input_arr, gt_mask_arr = (sitk2np(o) for o in (input_obj, input_mask_obj))
    atlas_arr, atlas_mask_arr = (sitk2np(o)
                                 for o in (atlas_obj, atlas_mask_obj))

    del input_obj
    del input_mask_obj
    del atlas_obj
    del atlas_mask_obj

    gc.collect()

    print("Loaded obj")

    # ground truth = input mask obj
    #gt_mask_arr   = sitk2np(input_mask_obj).astype(bool)
    gt_bbox = mask2bbox(gt_mask_arr)

    print("Getting pred")

    # predicted = atlas2pred mask obj
    pred_mask_arr = atlas2pred(input_arr, atlas_arr, atlas_mask_arr)
    pred_bbox = mask2bbox(pred_mask_arr)

    print("Pred bbox", pred_bbox)

    # metric
    align0_dice = compute_dice_coefficient(gt_mask_arr, pred_mask_arr)

    print("align0 dice", align0_dice)

    print("Starting csc")

    # cascade: expand margin around pred_bbox
    imin, imax, jmin, jmax, kmin, kmax = pred_bbox
    imin_pad, imax_pad, jmin_pad, jmax_pad, kmin_pad, kmax_pad = pad_amts

    mins = imin, jmin, kmin
    maxs = imax, jmax, jmax
    min_pads = imin_pad, jmin_pad, kmin_pad
    max_pads = imax_pad, jmax_pad, kmax_pad

    imin, jmin, kmin = [max(0, x - pad) for x, pad in zip(mins, min_pads)]
    imax, jmax, kmax = [
        min(x + pad, shape)
        for x, pad, shape in zip(maxs, max_pads, input_arr.shape)
    ]

    # slice obj
    print("Pred bbox", pred_bbox)
    print("ROI + mrg", imin, imax, jmin, jmax, kmin, kmax)
    slc = np.s_[imin:imax, jmin:jmax, kmin:kmax]

    # cascade: re-align sub-brain (ROI + margin) region
    csc = True
    try:
        print("input shape", gt_mask_arr.shape)
        print("Arr[slice] shape", input_arr[slc].shape)
        print("Slice ", slc)

        #return

        csc_gt_mask_arr = gt_mask_arr[slc]
        csc_pred_mask_arr = atlas2pred(input_arr[slc], atlas_arr[slc],
                                       atlas_mask_arr[slc])

        print("End csc")

        print("mask2bbox csc pred")
        csc_pred_bbox = mask2bbox(csc_pred_mask_arr)
        print("mask2bbox gt")
        csc_gt_bbox = mask2bbox(csc_gt_mask_arr)

        # metric
        align1_dice = compute_dice_coefficient(csc_gt_mask_arr,
                                               csc_pred_mask_arr)

    except Exception as e:
        print("Error")
        print(e)
        csc = False

        csc_pred_bbox = pred_bbox
        csc_gt_bbox = gt_bbox
        align1_dice = align0_dice

        # store error
        if (input_idx, atlas_idx) in csc_errors:
            csc_errors[(input_idx, atlas_idx)].append(pad_dict)
        else:
            csc_errors[(input_idx, atlas_idx)] = [pad_dict]

    print("Return row")

    # store input idx, atlas idx, pad amts, bbox delta between gt and pred
    return {
        "input_idx": input_idx,
        "atlas_idx": atlas_idx,
        "align0": align0_dice,
        "align1": align1_dice,
        "csc": csc,
        **pad_dict,
        **bbox2diff(csc_gt_bbox, csc_pred_bbox)
    }