def count_proportion(): id2zyxd = utils_lung.read_luna_annotations(pathfinder.LUNA_LABELS_PATH) luna_data_paths = utils_lung.get_patient_data_paths(pathfinder.LUNA_DATA_PATH) luna_data_paths = [p for p in luna_data_paths if '.mhd' in p] n_white = 0 n_black = 0 for k, p in enumerate(luna_data_paths): img, origin, pixel_spacing = utils_lung.read_mhd(p) img = data_transforms.hu2normHU(img) id = os.path.basename(p).replace('.mhd', '') print id annotations = id2zyxd[id] img_out, annotations_out = data_transforms.transform_scan3d(img, pixel_spacing=pixel_spacing, p_transform=config().p_transform, p_transform_augment=None, # config().p_transform_augment, luna_annotations=annotations, luna_origin=origin) mask = data_transforms.make_3d_mask_from_annotations(img_out.shape, annotations_out, shape='sphere') n_white += np.sum(mask) n_black += mask.shape[0] * mask.shape[1] * mask.shape[2] - np.sum(mask) print 'white', n_white print 'black', n_black
def count_proportion(): id2zyxd = utils_lung.read_luna_annotations(pathfinder.LUNA_LABELS_PATH) luna_data_paths = utils_lung.get_patient_data_paths( pathfinder.LUNA_DATA_PATH) luna_data_paths = [p for p in luna_data_paths if '.mhd' in p] n_white = 0 n_black = 0 for k, p in enumerate(luna_data_paths): img, origin, pixel_spacing = utils_lung.read_mhd(p) img = data_transforms.hu2normHU(img) id = os.path.basename(p).replace('.mhd', '') print id annotations = id2zyxd[id] img_out, annotations_out = data_transforms.transform_scan3d( img, pixel_spacing=pixel_spacing, p_transform=config().p_transform, p_transform_augment=None, # config().p_transform_augment, luna_annotations=annotations, luna_origin=origin) mask = data_transforms.make_3d_mask_from_annotations(img_out.shape, annotations_out, shape='sphere') n_white += np.sum(mask) n_black += mask.shape[0] * mask.shape[1] * mask.shape[2] - np.sum(mask) print 'white', n_white print 'black', n_black
def data_prep_function(data, patch_centers, pixel_spacing, p_transform, p_transform_augment, **kwargs): x = data_transforms.transform_dsb_candidates(data=data, patch_centers=patch_centers, p_transform=p_transform, p_transform_augment=p_transform_augment, pixel_spacing=pixel_spacing) x = data_transforms.hu2normHU(x) return x
def data_prep_function(data, patch_center, luna_annotations, pixel_spacing, luna_origin, p_transform, p_transform_augment, **kwargs): x = data_transforms.hu2normHU(data) x, patch_annotation_tf, annotations_tf = data_transforms.transform_patch3d(data=x, luna_annotations=luna_annotations, patch_center=patch_center, p_transform=p_transform, p_transform_augment=p_transform_augment, pixel_spacing=pixel_spacing, luna_origin=luna_origin) return x, patch_annotation_tf
def data_prep_function(data, patch_center, luna_annotations, pixel_spacing, luna_origin, p_transform, p_transform_augment, **kwargs): x, patch_annotation_tf, annotations_tf = data_transforms.transform_patch3d(data=data, luna_annotations=luna_annotations, patch_center=patch_center, p_transform=p_transform, p_transform_augment=p_transform_augment, pixel_spacing=pixel_spacing, luna_origin=luna_origin) x = data_transforms.hu2normHU(x) y = data_transforms.make_3d_mask_from_annotations(img_shape=x.shape, annotations=annotations_tf, shape='sphere') return x, y
def data_prep_function(data, patch_center, pixel_spacing, luna_origin, p_transform, p_transform_augment, **kwargs): x, patch_annotation_tf = data_transforms.transform_patch3d(data=data, luna_annotations=None, patch_center=patch_center, p_transform=p_transform, p_transform_augment=p_transform_augment, pixel_spacing=pixel_spacing, luna_origin=luna_origin) x = data_transforms.hu2normHU(x) return x
def data_prep_function(data, patch_center, pixel_spacing, luna_origin, p_transform, p_transform_augment, world_coord_system, **kwargs): x, patch_annotation_tf = data_transforms.transform_patch3d(data=data, luna_annotations=None, patch_center=patch_center, p_transform=p_transform, p_transform_augment=p_transform_augment, pixel_spacing=pixel_spacing, luna_origin=luna_origin, world_coord_system=world_coord_system) x = data_transforms.hu2normHU(x) return x
def data_prep_function(data, luna_annotations, pixel_spacing, luna_origin, p_transform=p_transform, p_transform_augment=None): # make sure the data is processed the same way x, annotations_tf, tf_matrix = data_transforms.transform_scan3d(data=data, pixel_spacing=pixel_spacing, p_transform=p_transform, luna_annotations=luna_annotations, p_transform_augment=None, luna_origin=luna_origin) x = data_transforms.hu2normHU(x) y = data_transforms.make_3d_mask_from_annotations(img_shape=x.shape, annotations=annotations_tf, shape='sphere') return x, y, annotations_tf, tf_matrix
def data_prep_function(data, luna_annotations, pixel_spacing, luna_origin, p_transform=p_transform, p_transform_augment=None): # MAKE SURE THAT DATA IS PREPROCESSED THE SAME WAY x, annotations_tf, tf_matrix = data_transforms.transform_scan3d(data=data, pixel_spacing=pixel_spacing, p_transform=p_transform, luna_annotations=luna_annotations, p_transform_augment=None, luna_origin=luna_origin) x = data_transforms.hu2normHU(x) y = data_transforms.make_3d_mask_from_annotations(img_shape=x.shape, annotations=annotations_tf, shape='sphere') return x, y, annotations_tf, tf_matrix
def data_prep_function(data, pid, patch_centers, pixel_spacing, p_transform, p_transform_augment, **kwargs): x = data_transforms.transform_dsb_candidates(data=data, patch_centers=patch_centers, p_transform=p_transform, p_transform_augment=p_transform_augment, pixel_spacing=pixel_spacing) bins, original_borders = rescale_params_hist_eq[pid] x = data_transforms.apply_hist_eq_patch(x, bins, original_borders) x = data_transforms.hu2normHU(x) return x
def data_prep_function(data, pid, patch_centers, pixel_spacing, p_transform, p_transform_augment, **kwargs): x = data_transforms.transform_dsb_candidates( data=data, patch_centers=patch_centers, p_transform=p_transform, p_transform_augment=p_transform_augment, pixel_spacing=pixel_spacing) bins, original_borders = rescale_params_hist_eq[pid] x = data_transforms.apply_hist_eq_patch(x, bins, original_borders) x = data_transforms.hu2normHU(x) return x
def data_prep_function(data, patch_center, pixel_spacing, luna_origin, p_transform, p_transform_augment, world_coord_system, **kwargs): data_eq = utils_lung.histogram_equalization(data) x, patch_annotation_tf = data_transforms.transform_patch3d( data=data_eq, luna_annotations=None, patch_center=patch_center, p_transform=p_transform, p_transform_augment=p_transform_augment, pixel_spacing=pixel_spacing, luna_origin=luna_origin, world_coord_system=world_coord_system) x = data_transforms.hu2normHU(x) return x
def data_prep_function(data, pid, patch_center, pixel_spacing, luna_origin, p_transform, p_transform_augment, world_coord_system, **kwargs): x, patch_annotation_tf = data_transforms.transform_patch3d(data=data, luna_annotations=None, patch_center=patch_center, p_transform=p_transform, p_transform_augment=p_transform_augment, pixel_spacing=pixel_spacing, luna_origin=luna_origin, world_coord_system=world_coord_system) bins, original_borders = rescale_params_hist_eq[pid] x = data_transforms.apply_hist_eq_patch(x, bins, original_borders) x = data_transforms.hu2normHU(x) return x
def test1(): image_dir = utils.get_dir_path('analysis', pathfinder.METADATA_PATH) image_dir = image_dir + '/test_luna/' utils.auto_make_dir(image_dir) id2zyxd = utils_lung.read_luna_annotations(pathfinder.LUNA_LABELS_PATH) luna_data_paths = utils_lung.get_patient_data_paths( pathfinder.LUNA_DATA_PATH) luna_data_paths = [p for p in luna_data_paths if '.mhd' in p] print len(luna_data_paths) print id2zyxd.keys() for k, p in enumerate(luna_data_paths): img, origin, pixel_spacing = utils_lung.read_mhd(p) img = data_transforms.hu2normHU(img) id = os.path.basename(p).replace('.mhd', '') for nodule_zyxd in id2zyxd.itervalues(): zyx = np.array(nodule_zyxd[:3]) voxel_coords = utils_lung.world2voxel(zyx, origin, pixel_spacing) diameter_mm = nodule_zyxd[-1] radius_px = diameter_mm / pixel_spacing[1] / 2. roi_radius = (radius_px, radius_px) slice = img[voxel_coords[0], :, :] slice_prev = img[voxel_coords[0] - 1, :, :] slice_next = img[voxel_coords[0] + 1, :, :] roi_center_yx = (voxel_coords[1], voxel_coords[2]) mask = data_transforms.make_2d_mask(slice.shape, roi_center_yx, roi_radius, masked_value=0.1) plot_2d(slice, mask, id, image_dir) plot_2d_4(slice, slice_prev, slice_next, mask, id, image_dir) a = [{'center': roi_center_yx, 'diameter_mm': diameter_mm}] p_transform = { 'patch_size': (256, 256), 'mm_patch_size': (360, 360) } slice_patch, mask_patch = data_transforms.luna_transform_slice( slice, a, pixel_spacing[1:], p_transform, None) plot_2d(slice_patch, mask_patch, id, image_dir)
def test1(): image_dir = utils.get_dir_path('analysis', pathfinder.METADATA_PATH) image_dir = image_dir + '/test_luna/' utils.auto_make_dir(image_dir) # sys.stdout = logger.Logger(image_dir + '/test_luna.log') # sys.stderr = sys.stdout id2zyxd = utils_lung.read_luna_annotations(pathfinder.LUNA_LABELS_PATH) luna_data_paths = utils_lung.get_patient_data_paths( pathfinder.LUNA_DATA_PATH) luna_data_paths = [p for p in luna_data_paths if '.mhd' in p] print len(luna_data_paths) print id2zyxd.keys() for k, p in enumerate(luna_data_paths): img, origin, spacing = utils_lung.read_mhd(p) img = data_transforms.hu2normHU(img) id = os.path.basename(p).replace('.mhd', '') for roi in id2zyxd[id]: zyx = np.array(roi[:3]) voxel_coords = utils_lung.world2voxel(zyx, origin, spacing) print spacing radius_mm = roi[-1] / 2. radius_px = radius_mm / spacing[1] print 'r in pixels =', radius_px # roi_radius = (32.5, 32.5) roi_radius = (radius_px, radius_px) slice = img[voxel_coords[0], :, :] roi_center_yx = (voxel_coords[1], voxel_coords[2]) # print slice.shape, slice_resample.shape mask = make_circular_mask(slice.shape, roi_center_yx, roi_radius) plot_2d(slice, mask, id, image_dir) slice_mm, _ = resample(slice, spacing[1:]) roi_center_mm = tuple( int(r * ps) for r, ps in zip(roi_center_yx, spacing[1:])) mask_mm = make_circular_mask(slice_mm.shape, roi_center_mm, (radius_mm, radius_mm)) plot_2d(slice_mm, mask_mm, id, image_dir)
def test1(): image_dir = utils.get_dir_path('analysis', pathfinder.METADATA_PATH) image_dir = image_dir + '/test_luna/' utils.auto_make_dir(image_dir) id2zyxd = utils_lung.read_luna_annotations(pathfinder.LUNA_LABELS_PATH) luna_data_paths = utils_lung.get_patient_data_paths(pathfinder.LUNA_DATA_PATH) luna_data_paths = [p for p in luna_data_paths if '.mhd' in p] print len(luna_data_paths) print id2zyxd.keys() for k, p in enumerate(luna_data_paths): img, origin, pixel_spacing = utils_lung.read_mhd(p) img = data_transforms.hu2normHU(img) id = os.path.basename(p).replace('.mhd', '') for nodule_zyxd in id2zyxd.itervalues(): zyx = np.array(nodule_zyxd[:3]) voxel_coords = utils_lung.world2voxel(zyx, origin, pixel_spacing) diameter_mm = nodule_zyxd[-1] radius_px = diameter_mm / pixel_spacing[1] / 2. roi_radius = (radius_px, radius_px) slice = img[voxel_coords[0], :, :] slice_prev = img[voxel_coords[0] - 1, :, :] slice_next = img[voxel_coords[0] + 1, :, :] roi_center_yx = (voxel_coords[1], voxel_coords[2]) mask = data_transforms.make_2d_mask(slice.shape, roi_center_yx, roi_radius, masked_value=0.1) plot_2d(slice, mask, id, image_dir) plot_2d_4(slice, slice_prev, slice_next, mask, id, image_dir) a = [{'center': roi_center_yx, 'diameter_mm': diameter_mm}] p_transform = {'patch_size': (256, 256), 'mm_patch_size': (360, 360)} slice_patch, mask_patch = data_transforms.luna_transform_slice(slice, a, pixel_spacing[1:], p_transform, None) plot_2d(slice_patch, mask_patch, id, image_dir)
def test1(): image_dir = utils.get_dir_path('analysis', pathfinder.METADATA_PATH) image_dir = image_dir + '/test_luna/' utils.auto_make_dir(image_dir) # sys.stdout = logger.Logger(image_dir + '/test_luna.log') # sys.stderr = sys.stdout id2zyxd = utils_lung.read_luna_annotations(pathfinder.LUNA_LABELS_PATH) luna_data_paths = utils_lung.get_patient_data_paths(pathfinder.LUNA_DATA_PATH) luna_data_paths = [p for p in luna_data_paths if '.mhd' in p] print len(luna_data_paths) print id2zyxd.keys() for k, p in enumerate(luna_data_paths): img, origin, spacing = utils_lung.read_mhd(p) img = data_transforms.hu2normHU(img) id = os.path.basename(p).replace('.mhd', '') for roi in id2zyxd[id]: zyx = np.array(roi[:3]) voxel_coords = utils_lung.world2voxel(zyx, origin, spacing) print spacing radius_mm = roi[-1] / 2. radius_px = radius_mm / spacing[1] print 'r in pixels =', radius_px # roi_radius = (32.5, 32.5) roi_radius = (radius_px, radius_px) slice = img[voxel_coords[0], :, :] roi_center_yx = (voxel_coords[1], voxel_coords[2]) # print slice.shape, slice_resample.shape mask = make_circular_mask(slice.shape, roi_center_yx, roi_radius) plot_2d(slice, mask, id, image_dir) slice_mm, _ = resample(slice, spacing[1:]) roi_center_mm = tuple(int(r * ps) for r, ps in zip(roi_center_yx, spacing[1:])) mask_mm = make_circular_mask(slice_mm.shape, roi_center_mm, (radius_mm, radius_mm)) plot_2d(slice_mm, mask_mm, id, image_dir)