def visualize_prediction_came(feature_name, pred_results, M, labels, train_len=0, output_prefix=""): data_counter = 0 validation_offset = 0 for m in M: metadata = m scalars = np.abs(pred_results * 100. / np.max(pred_results)) image_root = "/datagrid/Medical/microscopy/CAMELYON16/training" image_name = os.path.basename(metadata[0]) patch_size = 1024 # int(os.path.basename(metadata[0]).split('_')[3][1:]) image_name_parts = image_name.split('_') if 'tumor' in metadata[0]: image_root += "/tumor/t" image_path = image_root + "{}_{}.tif".format(image_name_parts[1][1:], image_name_parts[2]) elif 'normal' in metadata[0]: image_root += "/normal/N" image_path = image_root + "{}_{}.tif".format(image_name_parts[1][1:], image_name_parts[2]) continue else: image_root = "/datagrid/Medical/microscopy/CAMELYON16/testing/" image_path = image_root + "Test_{}.tif".format(image_name_parts[2]) si = CamelyonSlideImage(image_path, None) print(" [VisOutput] Loading image {}".format(image_path)) if not os.path.exists(image_path): print (" !! FAILED, IMAGE NOT FOUND !!") break patchvis = si.get_patch_visualization(6, metadata[2], patch_size, scalars=scalars[ validation_offset + m[3]:validation_offset + m[3] + len( m[2])], line_thickness=1, show=False, filled=True) # if data_counter < train_len: # cv2.putText(patchvis, 'test', (40, 40), cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), 2, cv2.LINE_AA) # else: # cv2.putText(patchvis, 'vali', (40, 40), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 2, cv2.LINE_AA) cv2.imwrite("{}_{}_{}".format(output_prefix, image_name, feature_name) + ".png", patchvis) data_counter += 1
parser.add_argument("-o", "--output_prefix", type=str, help='Output image prefix', default=None, required=True) parser.add_argument("-m", "--mask", type=str, help='Mask image, tissue class with max value within', default=None, required=True) parser.add_argument("-ml", "--mask-level", type=int, help='Extraction level of the mask image', default=6, required=True) parser.add_argument("-t", "--tumor-annot", type=str, help='Tumor annotation file (default: searching for standard locations/extensions)', default=None, required=False) opt_group = parser.add_argument_group("Optional arguments") opt_group.add_argument("-l", "--extract-level", type=int, help='Level for extracting patches', default=3, required=False) cmdline_args = parser.parse_args() si = CamelyonSlideImage(image_path=cmdline_args.image, tissue_mask_path=cmdline_args.mask, tumor_annotation_file=cmdline_args.tumor_annot) tissue_mask_img = cv2.imread(cmdline_args.mask, cv2.IMREAD_GRAYSCALE) tissue_mask_img -= np.min(tissue_mask_img) im2, contours, hierarchy = cv2.findContours(tissue_mask_img, mode=cv2.RETR_CCOMP, method=cv2.CHAIN_APPROX_NONE) for ci, c in enumerate(contours): minx = im2.size miny = im2.size maxx = 0 maxy = 0
opt_group.add_argument("-md", "--modulation-matrix", type=str, help='Modulation matrix for separation optimization' ' (add option \'randsep\' to the list \'-e\'', default=None, required=False) opt_group.add_argument("--preview", help='Flag whether to show intermediate results', action='store_true') cmdline_args = parser.parse_args() si = None if '.ndpi' not in cmdline_args.image: si = CamelyonSlideImage(image_path=cmdline_args.image, tissue_mask_path=cmdline_args.mask) is_camelyon = True rgb_from_her = np.array([[0.7595018, 0.51920101, 0.38165572], [0.4895436, 0.74380669, 0.50788103], [0.53518641, 0.76756465, 0.35352657]]) modulation_matrix = None if cmdline_args.convolution_matrix is not None: rgb_from_her = eval(cmdline_args.convolution_matrix) print('Retrieved color-mixing matrix: \n ' + str(rgb_from_her)) her_from_rgb = np.linalg.inv(rgb_from_her) if cmdline_args.modulation_matrix is not None: modulation_matrix = eval(cmdline_args.modulation_matrix) print('Retrieved modulation matrix: \n ' + str(modulation_matrix))
def visualize_prediction(algorithms, pred_results, M, labels, train_len=0, output_prefix=""): visualize_prediction.counter += 1 data_counter = 0 validation_offset = 0 for m in M: metadata = m for algorithm in algorithms: alg_name = algorithm[2] # mean_preds = np.sum(np.array(pred_results[alg_name])-np.array(labels), axis=0) mean_preds = np.sum(np.array(pred_results[alg_name]), axis=0) max_preds = np.max(np.array(pred_results[alg_name]), axis=0) min_preds = np.min(np.array(pred_results[alg_name]), axis=0) sd_preds = np.sum(np.array(pred_results[alg_name]), axis=0) # sd_preds = np.std(np.array(pred_results[alg_name])-np.array(labels), axis=0) scalars = np.abs(mean_preds * 100. / np.max(mean_preds)) border_scalars = np.zeros_like(scalars) for li, label in enumerate(labels): if label > 0: border_scalars[li] = 100 * min_preds[li] else: border_scalars[li] = 100 * max_preds[li] image_root = "/datagrid/Medical/microscopy/CAMELYON16/training" image_name = os.path.basename(metadata[0]) image_name_parts = image_name.split('_') patch_size = int(image_name_parts[5][1:]) if 'tumor' in metadata[0]: image_root += "/tumor/" image_path = image_root + "tumor_{}.tif".format( image_name_parts[2]) elif 'normal' in metadata[0]: image_root += "/normal/" image_path = image_root + "Normal_{}.tif".format( image_name_parts[2]) si = CamelyonSlideImage(image_path, None) print(" [VisOutput] Loading image {}".format(image_path)) if not os.path.exists(image_path): print(" !! FAILED, IMAGE NOT FOUND !!") break patchvis = si.get_patch_visualization( 6, metadata[2], patch_size, scalars=scalars[validation_offset + m[3]:validation_offset + m[3] + len(m[2])], border_scalars=border_scalars[validation_offset + m[3]:validation_offset + m[3] + len(m[2])], line_thickness=1, show=False, filled=True) if data_counter < train_len: cv2.putText(patchvis, 'test', (40, 40), cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), 2, cv2.LINE_AA) else: cv2.putText(patchvis, 'vali', (40, 40), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 2, cv2.LINE_AA) data_counter += 1 if data_counter == train_len: validation_offset = m[3] + len(m[2]) cv2.imwrite( "/local/temporary/clf_vis/{}_l0_vis_".format(output_prefix) + image_name + "_" + alg_name + "features_{:d}.png".format(visualize_prediction.counter), patchvis)
if len(patches) < 1: print("Nothing to do for input directory " + input_file) quit() flush_event_id = 0 flush_time = 600 image_features = [] n = len(patches) i = 1 avg_time = 0 cum_time = 0 dsigma = np.array([0.25, 0.125, 0.0625]) si = CamelyonSlideImage(patches[0][3], None) is_camelyon = True area_low = 100 if patches[0][3].find('.ndpi') > -1: si = NDPISlideImage(patches[0][3], None) is_camelyon = False area_low = 50 show_intermediate = False if 'show' in options: show_intermediate = True si._visualize = True st_elem = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) st_elem_r1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1, 1))
if lsplit[0] == '{}.tif'.format( patches[0][0]) and lsplit[1] is not 'None': rgb_from_her = eval(lsplit[1]) break if rgb_from_her is not None: her_from_rgb = np.linalg.inv(rgb_from_her) show_intermediate = False level_prefix = 'L0' area_low = 100 st_elem = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) st_elem_r1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1, 1)) si = CamelyonSlideImage(fpath, None) for patch_descriptor in patches: p_location = patch_descriptor[1] p_size = patch_descriptor[2] label = patch_descriptor[3] structural_features = {} textural_features = {} orig_patch = si.load_patch(p_location, p_size, base_extract_level) ori_patch = cv2.cvtColor(np.copy(orig_patch), cv2.COLOR_RGBA2RGB) ori_patch_mf = cv2.medianBlur(ori_patch, 3) if p_size[0] // pow(2, base_extract_level) < 256: orig_patch_in = ori_patch_mf
"--csv", type=str, help='CSV file for saving the complete patch metadata', default=None, required=False) opt_group.add_argument( "--preview", type=str, help='File path for storing the preview image with annotated patches', default=None, required=False) cmdline_args = parser.parse_args() si = CamelyonSlideImage(image_path=cmdline_args.image, tissue_mask_path=cmdline_args.mask, tumor_annotation_file=cmdline_args.tumor_annot) if '.ndpi' in cmdline_args.image: si = NDPISlideImage(image_path=cmdline_args.image, tissue_mask_path=cmdline_args.mask, tumor_annotation_file=cmdline_args.tumor_annot) p_shift = cmdline_args.patch_stride if p_shift < 0: p_shift = cmdline_args.patch_size p = si.get_annotated_patches( extract_level=cmdline_args.extract_level, min_coverage_extraction=cmdline_args.min_coverage, min_tumor_coverage=0.6, p_size=cmdline_args.patch_size,
image_features = [] n = len(patches) i = 1 avg_time = 0 cum_time = 0 dsigma = np.array([0.25, 0.125, 0.0625]) show_intermediate = False if 'show' in options: show_intermediate = True if patch_extract_level > 1: dsigma *= 2 * patch_extract_level si = CamelyonSlideImage(patches[0][3], None) is_camelyon = True area_low = 100 if patches[0][3].find('.ndpi') > -1: si = NDPISlideImage(patches[0][3], None) is_camelyon = False area_low = 70 for patch_descriptor in patches: location = patch_descriptor[0] p_size = patch_descriptor[1] label = patch_descriptor[2] if 'doublesize' in options: p_size = (2 * p_size[0], 2 * p_size[1])
visited_no_patches = 0 if 0 < max_no_patches < len(patches_in['NO']): random.shuffle(patches_in['NO']) patches += patches_in['NO'][:max_no_patches] else: patches += patches_in['NO'] if len(patches) < 1: print("Nothing to do for input directory " + input_file) quit() n = len(patches) i = 1 si = CamelyonSlideImage(patches[0][3], None) is_camelyon = True area_low = 100 if patches[0][3].find('.ndpi') > -1: si = NDPISlideImage(patches[0][3], None) is_camelyon = False area_low = 50 blur_score = [] bckg_score = [] patch_vis_meta = [] tot_area = 1. / ((patches[0][1])[0] * (patches[0][1])[1]) for patch_descriptor in patches: p_location = patch_descriptor[0]