def test_load_as_matrix_no_bytes(self): """ Test that a data element with no bytes fails to load. """ d = DataMemoryElement(content_type='image/png') # Not initializing any bytes inst = PilImageReader() with pytest.raises(IOError, match="Failed to identify image from bytes " "provided by DataMemoryElement"): inst.load_as_matrix(d)
def test_load_as_matrix_invalid_bytes(self): """ Test that data element with invalid data bytes fails to load. """ d = DataMemoryElement(content_type='image/png') d.set_bytes(b"not valid bytes") inst = PilImageReader() with pytest.raises(IOError, match="Failed to identify image from bytes " "provided by DataMemoryElement"): inst.load_as_matrix(d)
def test_load_as_matrix_other_io_exception(self, m_pil_open): """ Test that an IOError that does match conditions for alternate raise is raised as-is. """ expected_exception = IOError("some other exception message content") m_pil_open.side_effect = expected_exception inst = PilImageReader() with pytest.raises(IOError, match=str(expected_exception)): inst.load_as_matrix(self.gh_file_element)
def test_load_as_matrix_other_exception(self, m_pil_open): """ Test that some other exception raised from ``PIL.Image.open`` is passed through. """ expected_exception = RuntimeError("Some other exception") m_pil_open.side_effect = expected_exception inst = PilImageReader() with pytest.raises(RuntimeError, match=str(expected_exception)): inst.load_as_matrix(self.gh_file_element)
def test_load_as_matrix_with_crop_not_integer(self): """ Test passing a bounding box that is not integer aligned, which should raise an error in the super call. """ inst = PilImageReader() bb = AxisAlignedBoundingBox([100, 100.6], [200, 200.2]) with pytest.raises(ValueError, match=r"Crop bounding box must be " r"composed of integer " r"coordinates\."): inst.load_as_matrix(self.gh_file_element, pixel_crop=bb)
def test_load_as_matrix_with_crop(self): """ Test that passing valid crop bounding box results in the expected area. We load two images: the original with a crop specified, and a pre-cropped image. The results of each load should be the same, indicating the correct region from the source image is extracted. """ inst = PilImageReader() mat_expected = inst.load_as_matrix(self.gh_cropped_file_element) mat_actual = inst.load_as_matrix(self.gh_file_element, pixel_crop=self.gh_cropped_bbox) numpy.testing.assert_allclose(mat_actual, mat_expected)
def test_load_as_matrix_hopper(self): """ Test loading valid data Grace Hopper image data element (native RGB image). """ inst = PilImageReader() mat = inst.load_as_matrix(self.gh_file_element) assert isinstance(mat, numpy.ndarray) assert mat.dtype == numpy.uint8 assert mat.shape == (600, 512, 3)
def test_load_as_matrix_with_crop_not_in_bounds(self): """ Test that error is raised when crop bbox is not fully within the image bounds. """ inst = PilImageReader() # Nowhere close bb = AxisAlignedBoundingBox([5000, 6000], [7000, 8000]) with pytest.raises(RuntimeError, match=r"Crop provided not within input image\. " r"Image shape: \(512, 600\), crop: "): inst.load_as_matrix(self.gh_file_element, pixel_crop=bb) # Outside left side bb = AxisAlignedBoundingBox([-1, 1], [2, 2]) with pytest.raises(RuntimeError, match=r"Crop provided not within input image\. " r"Image shape: \(512, 600\), crop: "): inst.load_as_matrix(self.gh_file_element, pixel_crop=bb) # Outside top side bb = AxisAlignedBoundingBox([1, -1], [2, 2]) with pytest.raises(RuntimeError, match=r"Crop provided not within input image\. " r"Image shape: \(512, 600\), crop: "): inst.load_as_matrix(self.gh_file_element, pixel_crop=bb) # Outside right side bb = AxisAlignedBoundingBox([400, 400], [513, 600]) with pytest.raises(RuntimeError, match=r"Crop provided not within input image\. " r"Image shape: \(512, 600\), crop: "): inst.load_as_matrix(self.gh_file_element, pixel_crop=bb) # Outside bottom side bb = AxisAlignedBoundingBox([400, 400], [512, 601]) with pytest.raises(RuntimeError, match=r"Crop provided not within input image\. " r"Image shape: \(512, 600\), crop: "): inst.load_as_matrix(self.gh_file_element, pixel_crop=bb)
def test_load_as_matrix_explicit_grayscale(self): """ Test loading valid Grace Hopper image with an explicit conversion type to grayscale. Should result in a single channel image (only 2 dims). """ # 8-bit grayscale inst = PilImageReader(explicit_mode="L") mat = inst.load_as_matrix(self.gh_file_element) assert isinstance(mat, numpy.ndarray) assert mat.dtype == numpy.uint8 assert mat.shape == (600, 512)