|
| 1 | +from pathlib import Path |
| 2 | +from tempfile import TemporaryDirectory |
| 3 | +from typing import Iterator |
| 4 | + |
| 5 | +import pytest |
| 6 | +from upath import UPath |
| 7 | + |
| 8 | +import webknossos as wk |
| 9 | +from webknossos import Dataset, MagView |
| 10 | +from webknossos.utils import is_remote_path |
| 11 | + |
| 12 | +pytestmark = [pytest.mark.with_vcr] |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(scope="module") |
| 16 | +def sample_bbox() -> wk.BoundingBox: |
| 17 | + return wk.BoundingBox((2807, 4352, 1794), (10, 10, 10)) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture(scope="module") |
| 21 | +def sample_remote_dataset(sample_bbox: wk.BoundingBox) -> Iterator[wk.Dataset]: |
| 22 | + url = "https://webknossos.org/datasets/scalable_minds/l4_sample_dev" |
| 23 | + with TemporaryDirectory() as temp_dir: |
| 24 | + yield wk.Dataset.download(url, path=Path(temp_dir) / "ds", bbox=sample_bbox) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture(scope="module") |
| 28 | +def sample_remote_mags() -> list[wk.MagView]: |
| 29 | + mag_urls = [ |
| 30 | + "https://data-humerus.webknossos.org/data/zarr/scalable_minds/l4_sample_dev/color/1/", |
| 31 | + "https://data-humerus.webknossos.org/data/zarr/scalable_minds/l4_sample_dev/color/2-2-1/", |
| 32 | + "https://data-humerus.webknossos.org/data/zarr/scalable_minds/l4_sample_dev/color/4-4-2/", |
| 33 | + "https://data-humerus.webknossos.org/data/zarr/scalable_minds/l4_sample_dev/segmentation/1/", |
| 34 | + "https://data-humerus.webknossos.org/data/zarr/scalable_minds/l4_sample_dev/segmentation/2-2-1/", |
| 35 | + "https://data-humerus.webknossos.org/data/zarr/scalable_minds/l4_sample_dev/segmentation/4-4-2/", |
| 36 | + ] |
| 37 | + mags = [MagView._ensure_mag_view(url) for url in mag_urls] |
| 38 | + return mags |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture(scope="module") |
| 42 | +def sample_remote_layer() -> list[wk.Layer]: |
| 43 | + remote_dataset_url = "https://webknossos.org/datasets/scalable_minds/l4_sample_dev" |
| 44 | + remote_dataset = Dataset.open_remote(remote_dataset_url) |
| 45 | + return list(remote_dataset.layers.values()) |
| 46 | + |
| 47 | + |
| 48 | +def test_add_remote_mags_from_mag_view( |
| 49 | + sample_remote_mags: list[wk.MagView], sample_remote_dataset: wk.Dataset |
| 50 | +) -> None: |
| 51 | + for remote_mag in sample_remote_mags: |
| 52 | + mag_path = remote_mag.path |
| 53 | + layer_type = remote_mag.layer.category |
| 54 | + assert is_remote_path(mag_path), "Remote mag does not have remote path." |
| 55 | + layer_name = f"test_remote_layer_{mag_path.parent.name}_{mag_path.name}_object" |
| 56 | + new_layer = sample_remote_dataset.add_layer( |
| 57 | + layer_name, |
| 58 | + layer_type, |
| 59 | + data_format=remote_mag.info.data_format, |
| 60 | + dtype_per_channel=remote_mag.get_dtype(), |
| 61 | + ) |
| 62 | + new_layer.add_remote_mag(remote_mag) |
| 63 | + added_mag = sample_remote_dataset.layers[layer_name].mags[remote_mag.mag] |
| 64 | + # checking whether the added_mag.path matches the mag_url with or without a trailing slash. |
| 65 | + assert ( |
| 66 | + added_mag.path == mag_path or added_mag.path == mag_path.parent |
| 67 | + ), "Added remote mag's path does not match remote path of mag added." |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.skip( |
| 71 | + reason="The test is flaky when trying to fetch the required datasource-properties.json from data-humerus.webknossos.org. Disable it for now." |
| 72 | +) |
| 73 | +def test_add_remote_mags_from_path( |
| 74 | + sample_remote_mags: list[wk.MagView], |
| 75 | + sample_remote_dataset: wk.Dataset, |
| 76 | +) -> None: |
| 77 | + for remote_mag in sample_remote_mags: |
| 78 | + mag_path = remote_mag.path |
| 79 | + layer_type = remote_mag.layer.category |
| 80 | + assert is_remote_path(mag_path), "Remote mag does not have remote path." |
| 81 | + # Additional .parent calls are needed as the first .parent only removes the trailing slash. |
| 82 | + layer_name = f"test_remote_layer_{mag_path.parent.name}_{mag_path.name}_path" |
| 83 | + new_layer = sample_remote_dataset.add_layer( |
| 84 | + layer_name, |
| 85 | + layer_type, |
| 86 | + data_format=remote_mag.info.data_format, |
| 87 | + dtype_per_channel=remote_mag.get_dtype(), |
| 88 | + ) |
| 89 | + new_layer.add_remote_mag(str(remote_mag.path)) |
| 90 | + added_mag = sample_remote_dataset.layers[layer_name].mags[remote_mag.mag] |
| 91 | + # checking whether the added_mag.path matches the mag_url with or without a trailing slash. |
| 92 | + assert ( |
| 93 | + added_mag.path == mag_path or added_mag.path == mag_path.parent |
| 94 | + ), "Added remote mag's path does not match remote path of mag added." |
| 95 | + |
| 96 | + |
| 97 | +def test_add_remote_layer_from_object( |
| 98 | + sample_remote_layer: list[wk.Layer], sample_remote_dataset: wk.Dataset |
| 99 | +) -> None: |
| 100 | + for layer in sample_remote_layer: |
| 101 | + assert is_remote_path(layer.path), "Remote mag does not have remote path." |
| 102 | + layer_name = f"test_remote_layer_{layer.category}_object" |
| 103 | + sample_remote_dataset.add_remote_layer(layer, layer_name) |
| 104 | + new_layer = sample_remote_dataset.layers[layer_name] |
| 105 | + assert ( |
| 106 | + is_remote_path(new_layer.path) |
| 107 | + and layer.path.as_uri() == new_layer.path.as_uri() |
| 108 | + ), "Added layer should have a remote path matching the remote layer added." |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.skip( |
| 112 | + reason="The test is flaky when trying to fetch the required datasource-properties.json from data-humerus.webknossos.org. Disable it for now." |
| 113 | +) |
| 114 | +def test_add_remote_layer_from_path( |
| 115 | + sample_remote_layer: list[wk.Layer], |
| 116 | + sample_remote_dataset: wk.Dataset, |
| 117 | +) -> None: |
| 118 | + for layer in sample_remote_layer: |
| 119 | + assert is_remote_path(layer.path), "Remote mag does not have remote path." |
| 120 | + layer_name = f"test_remote_layer_{layer.category}_path" |
| 121 | + sample_remote_dataset.add_remote_layer(UPath(layer.path), layer_name) |
| 122 | + new_layer = sample_remote_dataset.layers[layer_name] |
| 123 | + assert ( |
| 124 | + is_remote_path(new_layer.path) and new_layer.path == layer.path |
| 125 | + ), "Added layer should have a remote path matching the remote layer added." |
0 commit comments