def test_augment_focus(self): filename = get_loadable_filenames()[0] augmented = util.augment_focus(filename) for foundname, focus_correct in zip(augmented, ['F-15', 'F0', 'F15']): *head, focus_found, image_number = util.split_all(foundname) self.assertTrue(os.path.exists(foundname)) self.assertEqual(focus_found, focus_correct)
def test_read_images_for_stage_returns_correct_type(self): p = MockStagePredictor(device='cpu') filenames = get_loadable_filenames()[:1] box = (0, 0, 500, 500) names_and_boxes = [(nm, box) for nm in filenames] tensor = p._read_images_for_stage(names_and_boxes) self.assertIsInstance(tensor, torch.Tensor)
def test_predict_returns_annotated_images(self): p = MockMaskRCNNPredictor(device='cpu') filenames = get_loadable_filenames()[:1] names_and_boxes = [(nm, (100, 100, 200, 200)) for nm in filenames] out = p._predict(names_and_boxes) for entry in out: self.assertIsInstance(entry, AnnotatedImage)
def test_read_images_for_stage_returns_correct_size(self): p = MockStagePredictor(device='cpu') filenames = get_loadable_filenames()[:1] box = (0, 0, 500, 500) names_and_boxes = [(nm, box) for nm in filenames] tensor = p._read_images_for_stage(names_and_boxes) self.assertEqual(tensor.size(), (1, 3) + p.input_shape)
def test_normalization_is_correct(self): filename = get_loadable_filenames()[:1] loaded = load_images_for_fragmentation_regressor(filename) raw = util.read_image(filename[0]) # The normalization should increase the standard deviation of # the image by ~1/ 0.229, which is at least 4: self.assertGreater(loaded.std(), 4 * raw.std())
def test_read_image_returns_correct_shape(self): filenames = get_loadable_filenames()[:1] names_and_boxes = [(nm, (10, 10, 300, 300)) for nm in filenames] p = MockMaskRCNNPredictor() for name, box in names_and_boxes: loaded = p.read_image(name, box) self.assertEqual(loaded.size(), (3, ) + p.input_shape)
def test_crop_box_is_used_with_resize_nearest(self): # we crop to a 1 px image, and check that all image values # are the same value filename = get_loadable_filenames()[0] box = (1, 1, 2, 2) image = util.load_and_crop_image(filename, box) correct_px_value = np.array(Image.open(filename))[box[0], box[1]] self.assertTrue(np.all(np.array(image) == correct_px_value))
def test_on_images(self): filenames = get_loadable_filenames() images_ram = [util.load_image_into_ram(nm) for nm in filenames] loader = util.TransformingCollection(images_ram, util.read_image) index = 0 image_filename = util.read_image(filenames[index]) image_loader = loader[index] self.assertTrue(np.all(image_filename == image_loader))
def test_read_image_crops(self): # we crop to a 1-px region and ensure that the std is 0: filenames = get_loadable_filenames()[:1] names_and_boxes = [(nm, (100, 100, 1, 1)) for nm in filenames] p = MockMaskRCNNPredictor(device='cpu') for name, box in names_and_boxes: loaded = p.read_image(name, box) n = loaded.detach().numpy() self.assertAlmostEqual(n.std(), 0, places=6) # single precision
def test_under_predict(self): p = MockFragPredictor(device='cpu') filenames = get_loadable_filenames()[:1] # we can only augment 001.jpg boxes = [(10, 10, p.input_shape[0], p.input_shape[1]) for _ in filenames] filenames_and_boxes = [(f, b) for f, b in zip(filenames, boxes)] out = p._predict(filenames_and_boxes) for value in out: self.assertIsInstance(value, AnnotatedImage)
def setUpClass(cls): p = Pipeline('cpu') filenames = get_loadable_filenames()[:1] cls.out = p.predict_all(filenames) cls.keys = ['zona', 'boxes', 'frag', 'stage_raw', 'stage_smooth', 'pronuclei', 'blastomeres'] # Then we want to make sure that the detector actually picked up # pronuclei & cells, otherwise these are crappy tests: for key in ['pronuclei', 'blastomeres']: assert len(cls.out[key].iterate_over_images()) > 0
def test_crop_images_in_ram(self): p = MockFragPredictor(device='cpu') filenames = get_loadable_filenames()[:2] boxes = [(10, 10, p.input_shape[0], p.input_shape[1]) for _ in filenames] filenames_and_boxes = [(f, b) for f, b in zip(filenames, boxes)] cropped = p._crop_images_in_ram(filenames_and_boxes) for i, nm in enumerate(filenames): box = boxes[i] out = cropped[i] self.assertIsInstance(out, io.BytesIO)
def setUpClass(cls): filenames = get_loadable_filenames()[:1] cls.result = predict(filenames, device='cpu', zona_compressed=True)
def test_read_image_for_torch_returns_torch(self): filenames = get_loadable_filenames() as_torch = util.read_images_for_torch(filenames) self.assertIsInstance(as_torch, torch.Tensor)
def test_dimension_is_correct(self): filenames = get_loadable_filenames()[:2] loaded = load_images_for_fragmentation_regressor(filenames) correct_leading_dimensions = (len(filenames), 3) self.assertEqual(len(loaded.size()), 4)
def test_under_predict(self): filenames = get_loadable_filenames()[:2] p = MockZonaPredictor(device='cpu') out = p._predict(filenames) for value in out: self.assertIsInstance(value, AnnotatedImage)
def test_numeric_dtype_is_float32(self): filenames = get_loadable_filenames()[:2] loaded = load_images_for_fragmentation_regressor(filenames) self.assertEqual(loaded.dtype, torch.float32)
def test_datatype_is_torch_tensor(self): filenames = get_loadable_filenames()[:2] loaded = load_images_for_fragmentation_regressor(filenames) self.assertIsInstance(loaded, torch.Tensor)
def test_load_image_as_bytes_io(self): filename = get_loadable_filenames()[0] loaded_into_ram = util.load_image_into_ram(filename) image0 = util.read_image(filename) image1 = util.read_image(loaded_into_ram) self.assertTrue(np.all(image0 == image1))
def test_read_image_returns_numpy(self): filename = get_loadable_filenames()[0] image = util.read_image(filename) self.assertIsInstance(image, np.ndarray)
def make_loader(): filenames = get_loadable_filenames() return util.ImageTransformingCollection(filenames)
def test_output_image_is_correct_shape(self): filename = get_loadable_filenames()[0] box = (1, 1, 100, 100) shape = (150, 140) image = util.load_and_crop_image(filename, box, output_shape=shape) self.assertEqual(image.size, shape)
def test_returns_pil_image(self): filename = get_loadable_filenames()[0] box = (1, 1, 2, 2) image = util.load_and_crop_image(filename, box) self.assertIsInstance(image, Image.Image)
def test_read_image_returns_correct_shape(self): filename = get_loadable_filenames()[0] image = util.read_image(filename) self.assertEqual(image.ndim, 3) self.assertEqual(image.shape[2], 3)
def test_read_image_returns_float_on_01(self): filename = get_loadable_filenames()[0] image = util.read_image(filename) self.assertGreaterEqual(image.min(), 0) self.assertLessEqual(image.max(), 1)
def test_read_image_for_torch_returns_correct_shape(self): # torch expects (n_images, channels, size filenames = get_loadable_filenames() as_torch = util.read_images_for_torch(filenames) n_channels = 3 self.assertEqual(as_torch.size()[:2], (len(filenames), n_channels))
def test_load_images_at_one_timepoint_returns_correct_shape(self): p = MockStagePredictor(device='cpu') f0_filename = get_loadable_filenames()[0] box = (0, 0, 500, 500) loaded = p._load_inputs_at_one_timepoint(f0_filename, box) self.assertEqual(loaded.shape, (3, ) + p.input_shape)