Ejemplo n.º 1
0
def train_gen_mri(n=1):
    lesion_ids = [
        z[:z.find("_mri")] for z in os.listdir(C.train_data_dir)
        if z.endswith("_mri_art.npy")
    ]
    while True:
        lesion_id = random.choice(lesion_ids)
        x = np.load(join(C.train_data_dir, lesion_id + "_mri_art.npy"))
        y = np.load(join(C.train_data_dir,
                         lesion_id + "_mr_bl_liver_mask.npy"))

        angle = random.uniform(-20, 20) * math.pi / 180
        x = tr.rotate(x, angle)
        y = tr.rotate(y.astype(float), angle)

        crops = list(map(int,[random.uniform(0,.1) * x.shape[0], random.uniform(.9,1) * x.shape[0]] + \
            [random.uniform(0,.1) * x.shape[1], random.uniform(.9,1) * x.shape[1]] + \
            [random.uniform(0,.1) * x.shape[2], random.uniform(.9,1) * x.shape[2]]))
        x = x[crops[0]:crops[1], crops[2]:crops[3], crops[4]:crops[5]]
        y = y[crops[0]:crops[1], crops[2]:crops[3], crops[4]:crops[5]]

        x, _ = tr.rescale_img(x, C.dims)
        y, _ = tr.rescale_img(y, C.dims)
        y = np_utils.to_categorical(y, 2)
        yield np.expand_dims(x, 0), np.expand_dims(y, 0)
Ejemplo n.º 2
0
def seg_liver_mri(mri_img, save_path, mri_dims, model, tumor_mask_path=None):
    """Use a UNet to segment liver on MRI"""

    C = config.Config()
    #correct bias field!

    orig_shape = mri_img.shape

    x = mri_img
    x -= np.amin(x)
    x /= np.std(x)

    crops = list(map(int,[.05 * x.shape[0], .95 * x.shape[0]] + \
        [.05 * x.shape[1], .95 * x.shape[1]] + \
        [.05 * x.shape[2], .95 * x.shape[2]]))

    x = x[crops[0]:crops[1], crops[2]:crops[3], crops[4]:crops[5]]
    scale_shape = x.shape
    x = tr.rescale_img(x, C.dims)

    y = model.predict(np.expand_dims(x, 0))[0]
    liver_mask = (y[:, :, :, 1] > y[:, :, :, 0]).astype(float)
    liver_mask = tr.rescale_img(liver_mask, scale_shape)  #orig_shape)
    liver_mask = np.pad(liver_mask, ((crops[0], orig_shape[0] - crops[1]),
                                     (crops[2], orig_shape[1] - crops[3]),
                                     (crops[4], orig_shape[2] - crops[5])),
                        'constant')
    liver_mask = liver_mask > .5

    B3 = ball(3)
    B3 = B3[:, :, [0, 2, 3, 4, 6]]
    #B3 = ball(4)
    #B3 = B3[:,:,[1,3,5,6,8]]
    liver_mask = binary_opening(binary_closing(liver_mask, B3, 1), B3, 1)

    labels, num_labels = label(liver_mask, return_num=True)
    label_sizes = [
        np.sum(labels == label_id) for label_id in range(1, num_labels + 1)
    ]
    biggest_label = label_sizes.index(max(label_sizes)) + 1
    liver_mask[labels != biggest_label] = 0

    if tumor_mask_path is not None:
        tumor_mask, _ = masks.get_mask(tumor_mask_path, mri_dims,
                                       mri_img.shape)
        liver_mask[tumor_mask > tumor_mask.max() / 2] = liver_mask.max()

    masks.save_mask(liver_mask, save_path, mri_dims, save_mesh=True)
Ejemplo n.º 3
0
def get_rim_coverage(lesion_id, target_dir, min_threshold, r_frac=.15):
    P = lm.get_paths_dict(lesion_id, target_dir)

    if not exists(P['ball']['ct24']['img']):
        return np.nan

    img, D = hf.nii_load(P['ct24']['img'])
    M = masks.get_mask(P['ct24']['tumor'], D, img.shape)

    V = M.sum() * np.product(D)
    R = round(10 * V**(1 / 3) * r_frac)

    B3 = ball(R)
    B3 = tr.rescale_img(B3, D, [.1] * 3)
    B3[B3 < B3.max() / 2] = 0
    B3[B3 > 0] = 1

    coreM = binary_erosion(M, B3)
    T = max(min_threshold, img[coreM != 0].sum() / coreM.sum())
    rimM = M.astype(int) - coreM

    img[img > 450] = 450
    img = (img * rimM - T) / 34
    img[img < 0] = 0
    return img.sum() / rimM.sum()

    return (img[rimM > 0] > T).sum() / rimM.sum()
    """img, dims = hf.nii_load(P['ball']['ct24']['img'])
Ejemplo n.º 4
0
def seg_liver_ct(ct_path, save_path, model, tumor_mask_path=None):
    """Use a UNet to segment liver on CT"""

    C = config.Config()
    ct_img, ct_dims = hf.nii_load(ct_path)

    orig_shape = ct_img.shape

    x = tr.apply_window(ct_img)
    x -= np.amin(x)

    crops = list(map(int,[.05 * x.shape[0], .95 * x.shape[0]] + \
        [.05 * x.shape[1], .95 * x.shape[1]] + \
        [.05 * x.shape[2], .95 * x.shape[2]]))

    x = x[crops[0]:crops[1], crops[2]:crops[3], crops[4]:crops[5]]
    scale_shape = x.shape
    x = tr.rescale_img(x, C.dims)

    y = model.predict(np.expand_dims(x, 0))[0]
    liver_mask = (y[:, :, :, 1] > y[:, :, :, 0]).astype(float)
    liver_mask[x < 30] = 0
    liver_mask = tr.rescale_img(liver_mask, scale_shape)
    liver_mask = np.pad(liver_mask, ((crops[0], orig_shape[0] - crops[1]),
                                     (crops[2], orig_shape[1] - crops[3]),
                                     (crops[4], orig_shape[2] - crops[5])),
                        'constant')

    B3 = ball(3)
    B3 = B3[:, :, [0, 2, 3, 4, 6]]
    liver_mask = binary_opening(binary_closing(liver_mask, B3, 1), B3, 1)

    labels, num_labels = label(liver_mask, return_num=True)
    label_sizes = [
        np.sum(labels == label_id) for label_id in range(1, num_labels + 1)
    ]
    biggest_label = label_sizes.index(max(label_sizes)) + 1
    liver_mask[labels != biggest_label] = 0

    if tumor_mask_path is not None:
        tumor_mask, _ = masks.get_mask(tumor_mask_path, ct_dims, ct_img.shape)
        liver_mask[tumor_mask > tumor_mask.max() / 2] = liver_mask.max()

    masks.save_mask(liver_mask, save_path, ct_dims, save_mesh=True)
Ejemplo n.º 5
0
def _collect_unaug_data():
    """Return dictionary pointing to X (img data) and Z (filenames) and dictionary storing number of samples of each class."""
    orig_data_dict = {}
    num_samples = {}
    voi_df = drm.get_voi_dfs()[0]
    #voi_df = voi_df[voi_df["run_num"] <= C.test_run_num]
    patient_info_df = pd.read_csv(C.patient_info_path)
    patient_info_df["AccNum"] = patient_info_df["AccNum"].astype(str)

    for cls in C.classes_to_include:
        x = np.empty((10000, C.dims[0], C.dims[1], C.dims[2], C.nb_channels))
        z = []

        if C.dual_img_inputs:
            x2 = np.empty((10000, *C.context_dims, C.nb_channels))
        elif C.non_imaging_inputs:
            x2 = np.empty((10000, C.num_non_image_inputs))

        for index, lesion_id in enumerate(voi_df[voi_df["cls"] == cls].index):
            img_path = os.path.join(C.orig_dir, cls, lesion_id + ".npy")
            try:
                x[index] = np.load(img_path)
                if C.hard_scale:
                    x[index] = vm.scale_intensity(x[index], 1,
                                                  max_int=2)  #, keep_min=True)
            except:
                raise ValueError(img_path + " not found")
            z.append(lesion_id)

            if C.dual_img_inputs:
                tmp = np.load(
                    os.path.join(C.crops_dir, cls, lesion_id + ".npy"))
                x2[index] = tr.rescale_img(tmp, C.context_dims)[0]

            elif C.non_imaging_inputs:
                voi_row = voi_df.loc[lesion_id]
                patient_row = patient_info_df[patient_info_df["AccNum"] ==
                                              voi_row["acc_num"]]
                x2[index] = get_non_img_inputs(voi_row, patient_row)

        x.resize((index + 1, C.dims[0], C.dims[1], C.dims[2],
                  C.nb_channels))  #shrink first dimension to fit
        if C.dual_img_inputs or C.non_imaging_inputs:
            x2.resize((index + 1, *x2.shape[1:]))
            orig_data_dict[cls] = [x, x2, np.array(z)]
        else:
            orig_data_dict[cls] = [x, np.array(z)]

        num_samples[cls] = index + 1

    return orig_data_dict, num_samples
Ejemplo n.º 6
0
def train_generator(n=8):
    C = config.Config()

    fns = glob.glob(r"D:\CBCT\Train\NPYs\*_img.npy")
    while True:
        lesion_ids = random.sample(fns, n)
        X_train = np.empty((n, *C.proj_dims))
        Y_train = np.empty((n, *C.world_dims))

        for ix, lesion_id in enumerate(lesion_ids):
            X_train[ix] = np.load(lesion_id.replace("_img", "_proj"))
            Y_train[ix] = tr.rescale_img(np.load(lesion_id), C.world_dims)

        yield X_train, Y_train
Ejemplo n.º 7
0
def get_peripheral_coverage(lesion_id, target_dir, thresholds, R=35):
    P = lm.get_paths_dict(lesion_id, target_dir)
    img, D = hf.nii_load(P['ct24']['img'])
    M = masks.get_mask(P['ct24']['tumor'], D, img.shape)

    #B4 = ball(4)
    #B4 = B4[:,:,[1,3,5,6,8]]
    B3 = ball(R)
    B3 = tr.rescale_img(B3, D, [.1] * 3)
    B3[B3 < B3.max() / 2] = 0
    B3[B3 > 0] = 1
    M = binary_dilation(M, B3) - M.astype(int)

    return [(img * M > T).sum() / M.sum() for T in thresholds]
Ejemplo n.º 8
0
def train_gen_lip(n=5):
    lesion_ids = [
        z[:z.find("_lip")] for z in os.listdir(C.train_data_dir)
        if z.endswith("_lipdensity.npy")
    ]
    while True:
        X_train = np.empty((n, *C.small_dims, 4))
        Y_train = np.empty((n, *C.small_dims, 2))

        for ix in range(n):
            lesion_id = random.choice(lesion_ids)
            x = np.stack([
                np.load(join(C.train_data_dir, lesion_id + "_%s.npy" % x)) for
                x in ["mrbl_art", "mrbl_sub", "mrbl_equ", "mrbl_tumor_mask"]
            ], -1)
            y = np.load(join(C.train_data_dir, lesion_id + "_lipdensity.npy"))

            angle = random.uniform(-180, 180) * math.pi / 180
            x = tr.rotate(x, angle)
            y = tr.rotate(y.astype(float), angle)

            crops = list(map(int,[random.uniform(0,.1) * x.shape[0], random.uniform(.9,1) * x.shape[0]] + \
                [random.uniform(0,.1) * x.shape[1], random.uniform(.9,1) * x.shape[1]] + \
                [random.uniform(0,.1) * x.shape[2], random.uniform(.9,1) * x.shape[2]]))
            x = x[crops[0]:crops[1], crops[2]:crops[3], crops[4]:crops[5], :]
            y = y[crops[0]:crops[1], crops[2]:crops[3], crops[4]:crops[5]]

            x, _ = tr.rescale_img(x, C.small_dims)
            y, _ = tr.rescale_img(y, C.small_dims)
            y[y > 0] = 1
            y = np_utils.to_categorical(y, 2)
            #y = np.expand_dims(y,-1)

            X_train[ix] = np.expand_dims(x, 0)
            Y_train[ix] = np.expand_dims(y, 0)

        yield X_train, Y_train