def test_deep_tissue_feature_extractor_aug(self): """ Test deep tissue feature extractor with pipeline runner and with augmentation. """ config_fname = os.path.join(self.current_path, 'config', 'feature_extraction', 'deep_tissue_feature_extractor_aug.yml') with open(config_fname, 'r') as file: config = yaml.safe_load(file) pipeline = PipelineRunner(output_path=self.out_path, **config) output = pipeline.run(output_name=self.image_name.replace('.png', ''), image_path=os.path.join(self.image_path, self.image_name)) features = output['features'] self.assertTrue(isinstance(features, torch.Tensor)) # check type # check number of superpixels self.assertEqual(features.shape[0], 23) # check number of augmentations self.assertEqual(features.shape[1], 4) self.assertEqual(features.shape[2], 1280) # check number features # Re-run with existing output & ensure equal output = pipeline.run(output_name=self.image_name.replace('.png', ''), image_path=os.path.join(self.image_path, self.image_name)) reload_features = output['features'] self.assertTrue(np.array_equal(features, reload_features))
def test_gaussian_tissue_mask_with_pipeline_runner(self): """ Test gaussian tissue mask with pipeline runner. """ # 1. Tissue mask detection with saving config_fname = os.path.join(self.current_path, 'config', 'tissue_mask', 'tissue_mask.yml') with open(config_fname, 'r') as file: config = yaml.safe_load(file) pipeline = PipelineRunner(output_path=self.out_path, **config) output = pipeline.run(output_name=self.image_name.replace('.jpg', ''), image_path=os.path.join(self.image_path, self.image_name)) tissue_mask = output['tissue_mask'] self.assertEqual(list(tissue_mask.shape), [1024, 1280]) # image HxW = mask HxW self.assertTrue(list(np.unique(tissue_mask)) == [0, 1]) # mask is binary # tissue pixel count large enough self.assertTrue(np.sum(tissue_mask) > 1e6) self.assertTrue(np.sum(tissue_mask) < 2e6) # but not too large # 2. Re-run with existing output & ensure equal output = pipeline.run(output_name=self.image_name.replace('.jpg', ''), image_path=os.path.join(self.image_path, self.image_name)) reload_tissue_mask = output['tissue_mask'] self.assertTrue(np.array_equal(tissue_mask, reload_tissue_mask))
def test_color_merged_superpixel_extractor_with_pipeline_runner(self): """ Test color merged superpixel extractor with pipeline runner. """ config_fname = os.path.join(self.current_path, 'config', 'superpixels', 'color_merged_extractor.yml') with open(config_fname, 'r') as file: config = yaml.safe_load(file) pipeline = PipelineRunner(output_path=self.out_path, **config) output = pipeline.run(output_name=self.image_name.replace('.png', ''), image_path=os.path.join(self.image_path, self.image_name)) superpixels = output['merged_superpixels_map'] self.assertTrue(isinstance(superpixels, np.ndarray)) # check type self.assertEqual(len(list(superpixels.shape)), 2) # mask is bi-dim # check number of instances self.assertEqual(len(np.unique(superpixels)), 23) # Re-run with existing output & ensure equal output = pipeline.run(output_name=self.image_name.replace('.jpg', ''), image_path=os.path.join(self.image_path, self.image_name)) reload_superpixels = output['merged_superpixels_map'] self.assertTrue(np.array_equal(superpixels, reload_superpixels))
def test_handcrafted_feature_extractor(self): """ Test handcrafted feature extractor with pipeline runner. """ config_fname = os.path.join(self.current_path, 'config', 'feature_extraction', 'handcrafted_feature_extractor.yml') with open(config_fname, 'r') as file: config = yaml.safe_load(file) pipeline = PipelineRunner(output_path=self.out_path, **config) output = pipeline.run(output_name=self.image_name.replace('.png', ''), image_path=os.path.join(self.image_path, self.image_name)) features = output['features'] self.assertTrue(isinstance(features, torch.Tensor)) # check type # check number of features per instance self.assertEqual(features.shape[1], 65) # check number of instances detected self.assertEqual(features.shape[0], 23) # Re-run with existing output & ensure equal output = pipeline.run(output_name=self.image_name.replace('.png', ''), image_path=os.path.join(self.image_path, self.image_name)) reload_features = output['features'] self.assertTrue(np.array_equal(features, reload_features))
def test_rag_builder_annotation_with_pipeline_runner(self): """ Test rag builder with annotation and pipeline runner. """ config_fname = os.path.join(self.current_path, 'config', 'graph_builder', 'rag_graph_builder_annotation.yml') with open(config_fname, 'r') as file: config = yaml.safe_load(file) pipeline = PipelineRunner(output_path=self.out_path, **config) output = pipeline.run( output_name=self.image_name.replace('.png', '_annotation'), image_path=os.path.join(self.image_path, self.image_name), annotation_path=os.path.join(self.image_path, self.annotation_name)) graph = output['graph'] labels, counts = np.unique(graph.ndata['label'].numpy(), return_counts=True) self.assertTrue(isinstance(graph, dgl.DGLGraph)) # check type self.assertEqual(graph.number_of_nodes(), 23) # check number of nodes self.assertEqual(graph.number_of_edges(), 112) # check number of edges # check assigned features self.assertEqual(graph.ndata["feat"].shape, (23, 1, 1282)) # check assigned centroid self.assertEqual(graph.ndata["centroid"].shape, (23, 2)) # check assigned node labels self.assertTrue(np.array_equal(labels, np.array([0, 1, 2, 4]))) # check assigned node label counts self.assertTrue(np.array_equal(counts, np.array([2, 9, 2, 10])))
def test_rag_builder_augmentation_with_pipeline_runner(self): """ Test rag builder with augmentation and pipeline runner. """ config_fname = os.path.join(self.current_path, 'config', 'graph_builder', 'rag_graph_builder_aug.yml') with open(config_fname, 'r') as file: config = yaml.safe_load(file) pipeline = PipelineRunner(output_path=self.out_path, **config) output = pipeline.run(output_name=self.image_name.replace( '.png', '_aug'), image_path=os.path.join(self.image_path, self.image_name)) graph = output['graph'] self.assertTrue(isinstance(graph, dgl.DGLGraph)) # check type self.assertEqual(graph.number_of_nodes(), 23) # check number of nodes self.assertEqual(graph.number_of_edges(), 112) # check number of edges # check assigned features self.assertEqual(graph.ndata["feat"].shape, (23, 4, 1282)) # check assigned centroid self.assertEqual(graph.ndata["centroid"].shape, (23, 2))
def test_graph_diameter_with_pipeline_runner(self): """ Test graph diameter with pipeline runner. """ config_fname = os.path.join(self.current_path, 'config', 'stats', 'graph_diameter.yml') with open(config_fname, 'r') as file: config = yaml.safe_load(file) pipeline = PipelineRunner(output_path=None, **config) output = pipeline.run(output_name=None, graph_path=os.path.join(self.graph_path, self.graph_name)) diameter = output['diameter'] self.assertEqual(diameter, 6) # check true diameter
def test_superpixel_counter_with_pipeline_runner(self): """ Test superpixel counter with pipeline runner. """ config_fname = os.path.join(self.current_path, 'config', 'stats', 'superpixel_counter.yml') with open(config_fname, 'r') as file: config = yaml.safe_load(file) pipeline = PipelineRunner(output_path=None, **config) output = pipeline.run(output_name=None, image_path=os.path.join(self.image_path, self.image_name)) count = output['counter'] self.assertEqual(count, 95) # check true count
def test_nuclei_extractor_with_pipeline_runner(self): """Test nuclei extraction with local model.""" config_fname = os.path.join(self.current_path, 'config', 'nuclei_extraction', 'nuclei_extractor.yml') with open(config_fname, 'r') as file: config = yaml.safe_load(file) pipeline = PipelineRunner(output_path=self.out_path, **config) output = pipeline.run(output_name=self.image_name.replace('.png', ''), image_path=os.path.join(self.image_path, self.image_name)) instance_map = output['nuclei_map'] instance_centroids = output['nuclei_centroids'] # 3. run tests self.assertTrue(isinstance(instance_map, np.ndarray)) self.assertTrue(isinstance(instance_centroids, np.ndarray)) self.assertEqual(len(instance_centroids), 331)
def test_deep_nuclei_feature_extractor_aug(self): """Test deep nuclei feature extractor with pipeline runner and with augmentation.""" config_fname = os.path.join(self.current_path, 'config', 'feature_extraction', 'deep_nuclei_feature_extractor_aug.yml') with open(config_fname, 'r') as file: config = yaml.safe_load(file) pipeline = PipelineRunner(output_path=self.out_path, **config) output = pipeline.run(output_name=self.image_name.replace('.png', ''), image_path=os.path.join(self.image_path, self.image_name), nuclei_map_path=os.path.join( self.nuclei_map_path, self.nuclei_map_name)) features = output['features'] self.assertTrue(isinstance(features, torch.Tensor)) # check type self.assertEqual(features.shape[0], 331) # check number of nuclei # check number of augmentations self.assertEqual(features.shape[1], 4) self.assertEqual(features.shape[2], 1280) # check number features
def test_knn_builder_with_pipeline_runner(self): """ Test knn builder with pipeline runner. """ config_fname = os.path.join(self.current_path, 'config', 'graph_builder', 'knn_graph_builder.yml') with open(config_fname, 'r') as file: config = yaml.safe_load(file) pipeline = PipelineRunner(output_path=self.out_path, **config) output = pipeline.run(output_name=self.image_name.replace('.png', ''), image_path=os.path.join(self.image_path, self.image_name), nuclei_map_path=os.path.join( self.nuclei_map_path, self.nuclei_map_name)) graph = output['graph'] self.assertTrue(isinstance(graph, dgl.DGLGraph)) # check type self.assertEqual(graph.number_of_nodes(), 331) # check number of nodes self.assertEqual(graph.number_of_edges(), 1655) # check number of edges