|
1 | 1 | """This module takes care of exporting tiff images.""" |
2 | 2 |
|
3 | 3 | import logging |
| 4 | +import re |
4 | 5 | from argparse import Namespace |
5 | 6 | from functools import partial |
6 | 7 | from math import ceil |
7 | 8 | from multiprocessing import cpu_count |
8 | 9 | from pathlib import Path |
9 | 10 | from typing import Annotated, Any |
| 11 | +from urllib.parse import urlparse |
10 | 12 |
|
11 | 13 | import numpy as np |
12 | 14 | import typer |
13 | 15 | from PIL import Image |
14 | 16 | from scipy.ndimage import zoom |
| 17 | +from upath import UPath |
15 | 18 |
|
| 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 |
16 | 22 | from ..dataset import Dataset, MagView, View |
| 23 | +from ..dataset.dataset import _DATASET_DEPRECATED_URL_REGEX, _DATASET_URL_REGEX |
17 | 24 | from ..dataset.defaults import DEFAULT_CHUNK_SHAPE |
18 | 25 | 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 |
20 | 27 | from ._utils import ( |
21 | 28 | DistributionStrategy, |
22 | 29 | Vec2Int, |
@@ -173,11 +180,9 @@ def export_tiff_stack( |
173 | 180 | def main( |
174 | 181 | *, |
175 | 182 | source: Annotated[ |
176 | | - Any, |
| 183 | + str, |
177 | 184 | 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.", |
181 | 186 | ), |
182 | 187 | ], |
183 | 188 | target: Annotated[ |
@@ -258,10 +263,47 @@ def main( |
258 | 263 | rich_help_panel="Executor options", |
259 | 264 | ), |
260 | 265 | ] = 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, |
261 | 275 | ) -> None: |
262 | 276 | """Export your WEBKNOSSOS dataset to TIFF image data.""" |
263 | 277 |
|
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 | + ) |
265 | 307 |
|
266 | 308 | bbox = BoundingBox.from_ndbbox(mag_view.bounding_box) if bbox is None else bbox |
267 | 309 | bbox = bbox.align_with_mag(mag_view.mag) |
|
0 commit comments