def test_stackmirror_get_tile_index(): tile_side = 100 mirror = StackMirror(IMAGE_BASE, tile_side, tile_side, TILE_SOURCE_TYPE, "png") tile_idx_kwargs = { "zoom_level": 0, "height": tile_side, "width": tile_side } # trivial case coords = {"x": 0, "y": 0, "z": 0} idx, offset = mirror.get_tile_index(coords) assert idx == TileIndex(0, 0, 0, **tile_idx_kwargs) assert offset == {"x": 0, "y": 0, "z": 0} # test offset coords = {"x": 50, "y": 50, "z": 0} idx, offset = mirror.get_tile_index(coords) assert idx == TileIndex(0, 0, 0, **tile_idx_kwargs) assert offset == {"x": 50, "y": 50, "z": 0} # test tile idx coords = {"x": tile_side, "y": tile_side, "z": 1} idx, offset = mirror.get_tile_index(coords) assert idx == TileIndex(1, 1, 1, **tile_idx_kwargs) assert offset == {"x": 0, "y": 0, "z": 0} # test row/col correctness coords = {"x": tile_side, "y": 0, "z": 0} idx, offset = mirror.get_tile_index(coords) assert idx == TileIndex(depth=0, row=0, col=1, **tile_idx_kwargs) assert offset == {"x": 0, "y": 0, "z": 0}
def test_imagefetcher_roi_to_tiles(min_fetcher, roi, expected_drc, expected_yx_minmax): tile_side = 100 zoom_level = 0 mirror = StackMirror(IMAGE_BASE, tile_side, tile_side, TILE_SOURCE_TYPE, "png") min_fetcher.stack.mirrors = [mirror] min_fetcher.mirror = mirror (min_y, max_y), (min_x, max_x) = expected_yx_minmax expected_bounds = { "min": { "z": 0, "y": min_y, "x": min_x }, "max": { "z": 0, "y": max_y, "x": max_x }, } expected_tiles = { TileIndex(*drc, zoom_level=zoom_level, height=tile_side, width=tile_side) for drc in expected_drc } tiles, slicing = min_fetcher._roi_to_tiles(roi, zoom_level) assert tiles == expected_tiles assert slicing == expected_bounds
def test_tile_index_coords(): idx = TileIndex(5, 2, 1, None, 10, 20) result = idx.coords expected_result = {"z": 5, "y": 20, "x": 20} assert result == expected_result
def test_404_handled_correctly(min_fetcher): idx = TileIndex(0, 0, 0, 0, 100, 100) min_fetcher._session.get = mock.Mock(side_effect=HTTPError( response=mock.Mock(status_code=404))) with mock.patch("catpy.image.response_to_array", mock.Mock()): tile = min_fetcher._fetch(idx) assert tile.shape == (100, 100) assert (tile == 0).sum() == tile.size
def test_imagefetcher_get_tile_from_cache(realistic_fetcher, tile_gen): cached = next(tile_gen) height, width = cached.shape tile_index = TileIndex(0, 0, 0, 0, height, width) realistic_fetcher._tile_cache[tile_index] = cached out = realistic_fetcher.get([[0, 0, 0], [1, height, width]], ROIMode.SCALED, 0) realistic_fetcher._fetch.assert_not_called() assert np.allclose(out.squeeze(), cached)
def test_fill_tiled_cuboid(): kwargs = {"zoom_level": 1, "height": 2, "width": 3} min_tile = TileIndex(1, 2, 3, **kwargs) max_tile = TileIndex(2, 4, 3, **kwargs) results = fill_tiled_cuboid(min_tile, max_tile) expected_results = { TileIndex(1, 2, 3, **kwargs), TileIndex(2, 2, 3, **kwargs), TileIndex(1, 3, 3, **kwargs), TileIndex(2, 3, 3, **kwargs), TileIndex(1, 4, 3, **kwargs), TileIndex(2, 4, 3, **kwargs), } assert results == expected_results
def test_tile_index_comparable(name): kwargs = {"zoom_level": 1, "height": 10, "width": 20} idx1 = TileIndex(1, 2, 3, **kwargs) idx2 = TileIndex(4, 5, 6, **kwargs) assert idx1.is_comparable(idx2) kwargs[name] = kwargs[name] * 2 idx3 = TileIndex(7, 8, 9, **kwargs) assert not idx1.is_comparable(idx3)
def fill_tiled_cuboid(min_tile_idx, max_tile_idx): if not min_tile_idx.is_comparable(max_tile_idx): raise ValueError("Tile indices are not comparable (different zoom or size)") iters = [ range(getattr(min_tile_idx, name), getattr(max_tile_idx, name) + 1) for name in ("depth", "row", "col") ] return { TileIndex( depth, row, col, min_tile_idx.zoom_level, min_tile_idx.height, min_tile_idx.width, ) for depth, row, col in itertools.product(*iters) }
def test_imagefetcher_fetch(min_fetcher): idx = TileIndex(0, 0, 0, 0, 100, 100) min_fetcher._session.get = mock.Mock() with mock.patch("catpy.image.response_to_array", mock.Mock()): min_fetcher._fetch(idx) min_fetcher._session.get.assert_called_once()
def test_imagefetcher_get_tile_from_fetch(min_fetcher): idx = TileIndex(0, 0, 0, 0, 100, 100) min_fetcher._fetch = mock.Mock() min_fetcher._get_tile(idx) min_fetcher._fetch.assert_called_with(idx)
def test_stackmirror_raises_on_incompatible_tile_index(): mirror = StackMirror(IMAGE_BASE, 512, 512, TILE_SOURCE_TYPE, "png") tile_idx = TileIndex(0, 0, 0, 0, 256, 256) with pytest.raises(ValueError): mirror.generate_url(tile_idx)
def test_stackmirror_formats_url(tile_source_type): mirror = StackMirror(IMAGE_BASE, 256, 256, tile_source_type, "png") tile_idx = TileIndex(0, 0, 0, 0, 256, 256) response = mirror.generate_url(tile_idx) assert not set("{}").issubset(response)
def test_tile_index_url_kwargs(): idx = TileIndex(1, 2, 3, 4, 5, 6) d = idx.url_kwargs for key in ("depth", "row", "col", "zoom_level"): assert key in d
def test_fill_tiled_cuboid_raises(): min_tile = TileIndex(1, 2, 3, 1, 2, 3) max_tile = TileIndex(2, 4, 3, 4, 5, 6) with pytest.raises(ValueError): fill_tiled_cuboid(min_tile, max_tile)