def test_create_windows(self): # test with an empty image self.assertRaises(ValueError, retina.Window, self._retina_image, 8) # test with a full data image self._retina_image.np_image[:, :] = 1 windows = retina.Window(self._retina_image, 8) self.assertEqual(windows.windows.shape[0], self._image_size, "expected 64 windows") # test with an image half filled with data self._retina_image.np_image[:, 0:int(self._image_size/2)] = 0 windows = retina.Window(self._retina_image, 8) self.assertEqual(windows.windows.shape[0], self._image_size/2, "expected 32 windows")
def test_set_tag_layer(self): image = retina.Retina(None, _image_path) image.reshape_by_window(56) window = retina.Window(image, 56, min_pixels=10) tags = np.full([window.shape[0], 4], 100) tags[:, 3] = 50 window.tags = tags window.set_tag_layer()
def test_mode(self): window = retina.Window(self._retina_image, 8, min_pixels=0) assert_array_equal(window.shape, [64, 1, 8, 8], "window shape is incorrect") window.mode = window.mode_tensorflow assert_array_equal(window.shape, [64, 8, 8, 1], "window shape is incorrect") window.mode = window.mode_pytorch assert_array_equal(window.shape, [64, 1, 8, 8], "window shape is incorrect")
def test_create_windows_combined(self): windows = retina.Window(self._retina_image, 8, "combined", 0) # combined should create (width/(dimension/2) - 1) * (height/(dimension/2) -1) # here is (64/4 -1) * (64/4 -1) = 225 self.assertEqual(windows.windows.shape[0], 225, "there should be 225 windows created") # fail with no window created self.assertRaises(ValueError, retina.Window, self._retina_image, 8, "combined")
def test_iterator(self): image = retina.Retina(None, _image_path) image.reshape_by_window(56) windows = retina.Window(image, 56, min_pixels=10) size = windows.windows.shape[0] iterated_size = 0 for _ in windows: iterated_size += 1 self.assertEqual(size, iterated_size, "iterated structure size does not match") for window in windows: assert_array_equal(window, windows.windows[0]) break
def density(image: np.ndarray, window_size: int = 10, min_pixels: int = 10, creation_method: str = "separated", threshold: float = 0.97) -> dict: image = retina.Retina(image, "tortuosity_density") dimension = image.reshape_by_window(window_size, True) image.threshold_image() image.skeletonization() windows = retina.Window(image, dimension, min_pixels=min_pixels, method=creation_method) evaluation = \ { "uri": "tortuosity_density", "data": [], # "image": image.original_base64 # TODO: maybe return a processed image? } for i in range(0, windows.shape[0]): window = windows.windows[i, 0] w_pos = windows.w_pos[i] image = retina.Retina(window, "td") vessels = retina.detect_vessel_border(image) processed_vessel_count = 0 for vessel in vessels: if len(vessel[0]) > 10: processed_vessel_count += 1 tortuosity_density = tortuosity_measures.tortuosity_density( vessel[0], vessel[1]) if tortuosity_density > threshold: evaluation["data"].append( _tortuosity_window( w_pos[0, 0].item(), w_pos[0, 1].item(), w_pos[1, 0].item(), w_pos[1, 1].item(), "{0:.2f}".format(tortuosity_density))) return evaluation
def test_set_tag_layer_tags_not_set(self): image = retina.Retina(None, _image_path) image.reshape_by_window(56) window = retina.Window(image, 56, min_pixels=10) with self.assertRaises(ValueError): window.set_tag_layer()
def test_tags_wrong_input(self): window = retina.Window(self._retina_image, 8, min_pixels=0) with self.assertRaises(ValueError): tags = np.zeros([1, 4]) window.tags = tags
def test_tags(self): window = retina.Window(self._retina_image, 8, min_pixels=0) tags = np.zeros([window.shape[0], 4]) window.tags = tags assert_array_equal(tags, window.tags, "tags does not match")
def test_save_window_wrong_id(self): self._retina_image.np_image[:, :] = 1 window = retina.Window(self._retina_image, 8, min_pixels=0) self.assertRaises(ValueError, window.save_window, 80, "./")
def test_save_window(self): self._retina_image.np_image[:, :] = 1 window = retina.Window(self._retina_image, 8, min_pixels=0) window.save_window(1, "./") self.assertTrue(os.path.isfile(window._window_filename(1)), "file not found") os.unlink(window._window_filename(1))
from retipy import configuration, retina, tortuosity_measures parser = argparse.ArgumentParser() parser.add_argument("-c", "--configuration", help="the configuration file location", default="resources/retipy.config") args = parser.parse_args() CONFIG = configuration.Configuration(args.configuration) for filename in sorted(glob.glob(os.path.join(CONFIG.image_directory, '*.png'))): print("processing {}...".format(filename)) segmentedImage = retina.Retina(None, filename) segmentedImage.threshold_image() segmentedImage.reshape_square() window_sizes = segmentedImage.get_window_sizes() window = retina.Window(segmentedImage, window_sizes[-1], min_pixels=CONFIG.pixels_per_window) tortuosity_measures.evaluate_window(window, CONFIG.pixels_per_window, CONFIG.sampling_size, CONFIG.r_2_threshold) hf = h5py.File( CONFIG.output_folder + "/" + segmentedImage.filename + ".h5", 'w') hf.create_dataset('windows', data=window.windows) hf.create_dataset('tags', data=window.tags) hf.close()