def test_openslided_tiler(self): # openslide_image_model = { # "type": "slide_image", # "image_path": "/home/dimathe47/Downloads/CMU-1-Small-Region.svs", # "name": "CMU-1-Small-Region.svs" # } image_path = "/home/dimathe47/Downloads/CMU-1-Small-Region.svs" image_model = generate_image_model(image_path) zoom_factor = 1 shape = (224, 224) step_shape = (224, 224) tiling_model = generate_tiling_model(image_model, zoom_factor, shape, step_shape) n_bins = 128 pilimage_to_matrix_model = generate_pilimage_to_matrix_model( tiling_model) histogram_model = generate_histogram_model(pilimage_to_matrix_model, n_bins, "temp/histograms.hdf5") compute_model(histogram_model, verbose=2)
def l2_distance_matrix_computer_factory(computer_func_params): # X_ds = computer_func_params["base_model"]["output_model"] # X = ds_utils.read_array(X_ds) X = computer_utils.compute_model(computer_func_params["base_model"]) # TODO consider all cases if isinstance(X, np.ndarray): pass elif isinstance(X, Iterable): X = list(X) def computer_(Q): Q = np.array(Q, copy=False) Q = Q.reshape((len(Q), -1)) distances_matrix = l2_distance_matrix(Q, X) return distances_matrix return Computer(computer_)
def test_compute(self): computer_utils.add_factory( "data_comes_from_some_place_other_than_output_of_another_model", data_comes_from_some_place_other_than_output_of_another_model, False) computer_utils.add_factory("my_sum", my_sum, False) model = { "type": "computer", "computer_func_name": "my_sum", "input_model": { "type": "inmemory_computer", "computer_func_name": "data_comes_from_some_place_other_than_output_of_another_model" } } output = computer_utils.compute_model(model) self.assertEqual(output, 45)
def build_result_slide_tiles_descriptors_models_view_item( distances: np.ndarray, tiles_descriptors_model: dict): # normalized_distances = distances/ np.max(distances) normalized_distances = (distances - np.min(distances)) / ( np.max(distances) - np.min(distances)) # alphas = [128 - 128 * dist for dist in normalized_distances] # alphas = [128 * 128 ** (-dist) for dist in normalized_distances] alphas = 128**(1 - normalized_distances) alphas = alphas.astype(int) # colors = [(0, 255, 0, int(alpha)) for alpha in alphas] rect_tiles_model = model_utils.find_rect_tiles_model( tiles_descriptors_model) rect_tiles = list(computer_utils.compute_model(rect_tiles_model)) slide_path = find_image_path(tiles_descriptors_model) slide_view_params = SlideViewParams(slide_path, grid_rects_0_level=rect_tiles, grid_color_alphas_0_level=alphas, grid_visible=True) item = SlideTilesDescriptorsModelsViewItem([tiles_descriptors_model], slide_view_params) return item
def on_search_action( self, selected_slide_tiles_descriptors_models_view_items: Iterable[ SlideTilesDescriptorsModelsViewItem]): if not self.query_viewer.slide_view_params.selected_rect_0_level: QMessageBox.question(self, 'Error', "No query rect selected", QMessageBox.Ok) return slide_tiles_descriptors_models = [] for selected_slide_tiles_descriptors_models_view_item in selected_slide_tiles_descriptors_models_view_items: for slide_tiles_descriptors_model in selected_slide_tiles_descriptors_models_view_item.slide_tiles_descriptors_models: slide_tiles_descriptors_models.append( slide_tiles_descriptors_model) if not slide_tiles_descriptors_models: QMessageBox.question(self, 'Error', "No db_models selected", QMessageBox.Ok) return n_selected_images = len( selected_slide_tiles_descriptors_models_view_items) chosen_tiles_descriptors_models = self.choose_tiles_descriptors_model( slide_tiles_descriptors_models, n_selected_images) if not chosen_tiles_descriptors_models: QMessageBox.question(self, 'Error', "No tiles_descriptors_model selected", QMessageBox.Ok) return self.statusBar().showMessage("Searching...") self.result_items_widget.list_model.update_items([]) selected_rect_0_level_int = [ int(x) for x in self.query_viewer.slide_view_params.selected_rect_0_level ] query_tile_descriptor_model = build_query_tiles_descriptors_model( self.query_viewer.slide_view_params.slide_path, selected_rect_0_level_int, chosen_tiles_descriptors_models[0]) print("query_tile_descriptor_model", query_tile_descriptor_model) result_items = [] query_descriptor = computer_utils.compute_model( query_tile_descriptor_model, force=True) query_descriptor = list(query_descriptor)[0] query_descriptor_model = {"type": "list", "list": [query_descriptor]} for chosen_tiles_descriptors_model in chosen_tiles_descriptors_models: distance_model = generate_distance_matrix_model( query_descriptor_model, chosen_tiles_descriptors_model) distances = computer_utils.compute_model(distance_model, force=True) distances = list(distances) distances = np.array(distances).squeeze() result_item = build_result_slide_tiles_descriptors_models_view_item( distances, chosen_tiles_descriptors_model) result_items.append(result_item) self.statusBar().showMessage( "Searching done. Visualization might take several seconds") self.result_items_widget.list_model.update_items(result_items) chosen_tiles_descriptors_model_str = tiles_descriptors_model_to_str( chosen_tiles_descriptors_model) self.ui.bottom_group_box.setTitle( "results for tiles_descriptors_model: {}".format( chosen_tiles_descriptors_model_str))