def test_thumbnail(): """Test the image thumbnail for square data.""" np.random.seed(0) data = np.random.random((30, 30)) layer = Image(data) layer._update_thumbnail() assert layer.thumbnail.shape == layer._thumbnail_shape
def test_thumbnail(): """Test the image thumbnail for square data.""" shapes = [(40, 40), (20, 20), (10, 10)] np.random.seed(0) data = [np.random.random(s) for s in shapes] layer = Image(data, is_pyramid=True) layer._update_thumbnail() assert layer.thumbnail.shape == layer._thumbnail_shape
def test_narrow_thumbnail(): """Ensure that the thumbnail generation works for very narrow images. See: https://github.com/napari/napari/issues/641 and https://github.com/napari/napari/issues/489 """ image = np.random.random((1, 2048)) layer = Image(image) layer._update_thumbnail() thumbnail = layer.thumbnail[..., :3] # ignore alpha channel middle_row = thumbnail.shape[0] // 2 assert np.all(thumbnail[:middle_row - 1] == 0) assert np.all(thumbnail[middle_row + 1:] == 0) assert np.mean(thumbnail[middle_row - 1:middle_row + 1]) > 0
class Image3DSuite: """Benchmarks for the Image layer with 3D data.""" params = [2**i for i in range(4, 11)] def setup(self, n): if "CI" in os.environ and n > 512: raise NotImplementedError("Skip on CI (not enough memory)") np.random.seed(0) self.data = np.random.random((n, n, n)) self.new_data = np.random.random((n, n, n)) self.layer = Image(self.data) def time_create_layer(self, n): """Time to create an image layer.""" Image(self.data) def time_set_view_slice(self, n): """Time to set view slice.""" self.layer._set_view_slice() def time_update_thumbnail(self, n): """Time to update thumbnail.""" self.layer._update_thumbnail() def time_get_value(self, n): """Time to get current value.""" self.layer.get_value((0, ) * 3) def time_set_data(self, n): """Time to get current value.""" self.layer.data = self.new_data def time_refresh(self, n): """Time to refresh view.""" self.layer.refresh() def mem_layer(self, n): """Memory used by layer.""" return Image(self.data) def mem_data(self, n): """Memory used by raw data.""" return self.data
class Image2DSuite: """Benchmarks for the Image layer with 2D data.""" params = [2**i for i in range(4, 13)] def setup(self, n): np.random.seed(0) self.data = np.random.random((n, n)) self.new_data = np.random.random((n, n)) self.layer = Image(self.data) def time_create_layer(self, n): """Time to create an image layer.""" Image(self.data) def time_set_view_slice(self, n): """Time to set view slice.""" self.layer._set_view_slice() def time_update_thumbnail(self, n): """Time to update thumbnail.""" self.layer._update_thumbnail() def time_get_value(self, n): """Time to get current value.""" self.layer.get_value() def time_set_data(self, n): """Time to get current value.""" self.layer.data = self.new_data def time_refresh(self, n): """Time to refresh view.""" self.layer.refresh() def mem_layer(self, n): """Memory used by layer.""" return self.layer def mem_data(self, n): """Memory used by raw data.""" return self.data
def test_out_of_range_no_contrast(dtype): data = np.full((10, 15), -3.2, dtype=dtype) layer = Image(data) layer._update_thumbnail()
def test_out_of_range_image(dtype): data = -1.7 - 0.001 * np.random.random((10, 15)).astype(dtype) layer = Image(data) layer._update_thumbnail()