Example #1
0
def test_stack_fastest_mirror_gets_fastest():
    # todo
    stack = Stack(None)
    stack.mirrors = [
        StackMirror(IMAGE_BASE + "2", 512, 512, TILE_SOURCE_TYPE, "png"),
        StackMirror(IMAGE_BASE + "1", 512, 512, TILE_SOURCE_TYPE, "png"),
        StackMirror(IMAGE_BASE + "3", 512, 512, TILE_SOURCE_TYPE, "png"),
    ]

    with mock.patch("timeit.timeit", side_effect=[2, 1, 3]):
        m = stack.get_fastest_mirror()

    assert m == stack.mirrors[1]
Example #2
0
def realistic_fetcher(request, gradient_h, tile_gen, tile_response_future_gen):
    tile_height, tile_width = gradient_h.shape
    stack = ProjectStack(
        dimension={
            "z": 100,
            "y": 200,
            "x": 300
        },
        translation={
            "z": 10,
            "y": 20,
            "x": 30
        },
        resolution={
            "z": 1,
            "y": 2,
            "x": 3
        },
        orientation="xy",
    )
    mirror = StackMirror(IMAGE_BASE, tile_height, tile_width, TILE_SOURCE_TYPE,
                         "png", "title", 0)
    stack.mirrors.append(mirror)

    fetcher = request.param(stack, preferred_mirror=0)

    side_effect = (tile_gen if request.param == ImageFetcher else
                   tile_response_future_gen)
    fetcher._fetch = mock.Mock(side_effect=side_effect)

    return fetcher
Example #3
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
Example #4
0
def test_stack_fastest_mirror_raises():
    stack = Stack(None)
    stack.mirrors = [
        StackMirror(IMAGE_BASE, 512, 512, TILE_SOURCE_TYPE, "png")
    ] * 3

    with pytest.raises(ValueError):
        stack.get_fastest_mirror(timeout=0.01)
Example #5
0
def test_stack_fastest_mirror_calls_get():
    stack = Stack(None)
    stack.mirrors = [
        StackMirror(IMAGE_BASE, 512, 512, TILE_SOURCE_TYPE, "png")
    ] * 3

    with mock.patch("requests.get") as mock_get:
        stack.get_fastest_mirror()

    assert mock_get.call_count == 3
Example #6
0
def mirror_generator(count=5):
    for idx in range(count):
        yield StackMirror(
            IMAGE_BASE + str(idx),
            100 + idx,
            200 + idx,
            TILE_SOURCE_TYPE,
            "png",
            title="title" + str(idx),
            position=idx * 2,
        )
Example #7
0
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}
Example #8
0
def test_imagefetcher_set_mirror_title_warns_too_many(min_fetcher):
    min_fetcher.stack.mirrors.append(
        StackMirror(IMAGE_BASE, 1, 1, TILE_SOURCE_TYPE, "png", "title0", 10))
    with pytest.warns(UserWarning, match="ore than one"):
        min_fetcher.mirror = "title0"
    assert min_fetcher._mirror == min_fetcher.stack.mirrors[0]
Example #9
0
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)
Example #10
0
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)
Example #11
0
def test_stackmirror_corrects_file_extension():
    other_args = (IMAGE_BASE, 1, 1, 1)
    mirror_dot = StackMirror(*other_args, file_extension=".png")
    mirror_no_dot = StackMirror(*other_args, file_extension="png")

    assert mirror_dot.file_extension == mirror_no_dot.file_extension == "png"
Example #12
0
def test_stackmirror_corrects_image_base():
    other_args = (1, 1, 1, "png")
    mirror_slash = StackMirror(IMAGE_BASE, *other_args)
    mirror_no_slash = StackMirror(IMAGE_BASE[:-1], *other_args)

    assert mirror_slash.image_base == mirror_no_slash.image_base == IMAGE_BASE