Skip to content

Commit 5d95fca

Browse files
authored
allow URLs for export-as-tiff (#1356)
* allow URLs for export-as-tiff * also work with sharing_token * changelog
1 parent 5900684 commit 5d95fca

File tree

3 files changed

+53
-19
lines changed

3 files changed

+53
-19
lines changed

webknossos/Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ For upgrade instructions, please check the respective _Breaking Changes_ section
1515
### Breaking Changes
1616

1717
### Added
18+
- Added support for remote datasets and annotations to `webknossos export-as-tiff`. [#1356](https://github.com/scalableminds/webknossos-libs/pull/1356)
1819

1920
### Changed
2021

webknossos/webknossos/cli/export_as_tiff.py

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
"""This module takes care of exporting tiff images."""
22

33
import logging
4+
import re
45
from argparse import Namespace
56
from functools import partial
67
from math import ceil
78
from multiprocessing import cpu_count
89
from pathlib import Path
910
from typing import Annotated, Any
11+
from urllib.parse import urlparse
1012

1113
import numpy as np
1214
import typer
1315
from PIL import Image
1416
from scipy.ndimage import zoom
17+
from upath import UPath
1518

19+
from ..annotation.annotation import _ANNOTATION_URL_REGEX, Annotation
20+
from ..client import webknossos_context
21+
from ..client._resolve_short_link import resolve_short_link
1622
from ..dataset import Dataset, MagView, View
23+
from ..dataset.dataset import _DATASET_DEPRECATED_URL_REGEX, _DATASET_URL_REGEX
1724
from ..dataset.defaults import DEFAULT_CHUNK_SHAPE
1825
from ..geometry import BoundingBox, Mag, Vec3Int
19-
from ..utils import get_executor_for_args, wait_and_ensure_success
26+
from ..utils import get_executor_for_args, is_fs_path, wait_and_ensure_success
2027
from ._utils import (
2128
DistributionStrategy,
2229
Vec2Int,
@@ -173,11 +180,9 @@ def export_tiff_stack(
173180
def main(
174181
*,
175182
source: Annotated[
176-
Any,
183+
str,
177184
typer.Argument(
178-
help="Path to your raw image data.",
179-
show_default=False,
180-
parser=parse_path,
185+
help="Path or URL to your raw image data.",
181186
),
182187
],
183188
target: Annotated[
@@ -258,10 +263,47 @@ def main(
258263
rich_help_panel="Executor options",
259264
),
260265
] = None,
266+
token: Annotated[
267+
str | None,
268+
typer.Option(
269+
help="Authentication token for WEBKNOSSOS instance "
270+
"(https://webknossos.org/auth/token).",
271+
rich_help_panel="WEBKNOSSOS context",
272+
envvar="WK_TOKEN",
273+
),
274+
] = None,
261275
) -> None:
262276
"""Export your WEBKNOSSOS dataset to TIFF image data."""
263277

264-
mag_view = Dataset.open(source).get_layer(layer_name).get_mag(mag)
278+
mag_view = None
279+
source_path = UPath(source)
280+
if not is_fs_path(source_path):
281+
url = resolve_short_link(source)
282+
parsed = urlparse(url)
283+
domain = f"{parsed.scheme}://{parsed.netloc}"
284+
285+
with webknossos_context(url=domain, token=token):
286+
if re.match(_DATASET_URL_REGEX, url) or re.match(
287+
_DATASET_DEPRECATED_URL_REGEX, url
288+
):
289+
mag_view = Dataset.open_remote(url).get_layer(layer_name).get_mag(mag)
290+
elif re.match(_ANNOTATION_URL_REGEX, url):
291+
mag_view = (
292+
Annotation.open_as_remote_dataset(annotation_id_or_url=url)
293+
.get_layer(layer_name)
294+
.get_mag(mag)
295+
)
296+
else:
297+
raise ValueError(
298+
"The provided URL does not lead to a dataset or annotation."
299+
)
300+
else:
301+
mag_view = Dataset.open(source_path).get_layer(layer_name).get_mag(mag)
302+
303+
if mag_view is None:
304+
raise ValueError(
305+
f"The provided source does not lead to a dataset or annotation. Got: {source}"
306+
)
265307

266308
bbox = BoundingBox.from_ndbbox(mag_view.bounding_box) if bbox is None else bbox
267309
bbox = bbox.align_with_mag(mag_view.mag)

webknossos/webknossos/dataset/dataset.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ def _parse_remote(
625625
sharing_token: str | None = None,
626626
webknossos_url: str | None = None,
627627
dataset_id: str | None = None,
628-
) -> tuple[AbstractContextManager, str]:
628+
) -> tuple[AbstractContextManager, str, str | None]:
629629
"""Parses the given arguments to
630630
* context_manager that should be entered,
631631
* dataset_id,
@@ -697,10 +697,7 @@ def _parse_remote(
697697
+ "Please see https://docs.webknossos.org/api/webknossos/client/context.html to adapt the URL and token."
698698
)
699699
context_manager = webknossos_context(webknossos_url, None)
700-
return (
701-
context_manager,
702-
dataset_id,
703-
)
700+
return (context_manager, dataset_id, sharing_token)
704701

705702
@classmethod
706703
def open_remote(
@@ -738,10 +735,7 @@ def open_remote(
738735

739736
from ..client.context import _get_context
740737

741-
(
742-
context_manager,
743-
dataset_id,
744-
) = cls._parse_remote(
738+
(context_manager, dataset_id, sharing_token) = cls._parse_remote(
745739
dataset_name_or_url,
746740
organization_id,
747741
sharing_token,
@@ -799,10 +793,7 @@ def download(
799793

800794
from ..client._download_dataset import download_dataset
801795

802-
(
803-
context_manager,
804-
dataset_id,
805-
) = cls._parse_remote(
796+
(context_manager, dataset_id, sharing_token) = cls._parse_remote(
806797
dataset_name_or_url, organization_id, sharing_token, webknossos_url
807798
)
808799

0 commit comments

Comments
 (0)