Skip to content

Commit a8945c5

Browse files
leowejstriebelnormanrz
authored
Download example (#612)
* add download example to docs * expand doc-string * add page to index * add bbox as param * use mags list * Update docs/src/webknossos-py/examples/download_image_data.md Co-authored-by: Jonathan Striebel <[email protected]> * Update docs/src/webknossos-py/examples/download_image_data.md Co-authored-by: Jonathan Striebel <[email protected]> * Update docs/src/webknossos-py/index.md Co-authored-by: Jonathan Striebel <[email protected]> * Update webknossos/webknossos/dataset/dataset.py Co-authored-by: Jonathan Striebel <[email protected]> * add test to test_examples * Update docs/src/webknossos-py/examples/download_image_data.md Co-authored-by: Jonathan Striebel <[email protected]> * Update test_examples.py * re-add allowed hosts for test_upload_image_data * use proper format * use webknossos.org, bc local wk instance doesn't have dataset * reformat Co-authored-by: Jonathan Striebel <[email protected]> Co-authored-by: Norman Rzepka <[email protected]>
1 parent 860e2f3 commit a8945c5

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Download Image Data
2+
3+
Downloads part of a [webknossos dataset](https://webknossos.org/datasets/scalable_minds/l4dense_motta_et_al_demo/view) from [webknossos.org](https://webknossos.org) using [`webknossos_context`](../../api/webknossos/client/context.md#webknossos_context).
4+
5+
```python
6+
--8<--
7+
webknossos/examples/download_image_data.py
8+
--8<--
9+
```

docs/src/webknossos-py/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ To get started, check out the [installation instructions](installation.md) and t
77
* [Intro to Dataset Usage](examples/dataset_usage.md)
88
* [How to Upload a Dataset](examples/upload_image_data.md)<br/>
99
![Cell Dataset uploaded to webKnossos](examples/upload_image_data_dataset.jpg){: style="max-width: calc(100%/2 - 1em);"}
10+
* [How to Download a Dataset](examples/download_image_data.md)
1011
* [Using a Volume Annotation to Train a Custom Segmenter](examples/learned_segmenter.md)<br/>
1112
![Volume Annotation used as Training Data](examples/learned_segmenter_annotation.png){: style="max-width: calc(100%/4 - 1em);"}
1213
![Result of the Segmenter on the Skin Dataset](examples/learned_segmenter_result.png){: style="max-width: calc(100%/4 - 1em);"}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import webknossos as wk
2+
3+
4+
def main() -> None:
5+
# Set the correct dataset name and organization where the dataset is saved on webknossos
6+
name = "l4dense_motta_et_al_demo"
7+
organization = "scalable_minds"
8+
9+
# Choose layers and mags to download
10+
layers = ["color"]
11+
mags = [wk.Mag(1)]
12+
13+
# Set the bounding box which should be downloaded
14+
bbox = wk.BoundingBox([50, 50, 50], [50, 50, 50])
15+
16+
# Download the datset
17+
ds = wk.Dataset.download(
18+
dataset_name=name,
19+
organization_name=organization,
20+
bbox=bbox,
21+
layers=layers,
22+
mags=mags,
23+
webknossos_url="https://webknossos.org",
24+
)
25+
26+
# Check that the layer was downloaded
27+
print(f"Layers: {ds.layers}")
28+
29+
30+
if __name__ == "__main__":
31+
main()

webknossos/tests/test_examples.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ def test_upload_image_data() -> None:
101101
assert url.startswith("http://localhost:9000/datasets/Organization_X/cell_")
102102

103103

104+
@pytest.mark.block_network(allowed_hosts=[".*"])
105+
@pytest.mark.vcr(ignore_hosts=["webknossos.org", "data-humerus.webknossos.org"])
106+
def test_download_image_data() -> None:
107+
with tmp_cwd():
108+
import examples.download_image_data as example
109+
110+
(ds,) = exec_main_and_get_vars(example, "ds")
111+
112+
assert list(ds.layers.keys()) == ["color"]
113+
114+
104115
class _DummyNearestNeighborClassifier:
105116
"""Faster replacement for a sklearn classifier,
106117
also removing the need for sklearn as a dependency."""

webknossos/webknossos/dataset/dataset.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,19 @@ def download(
221221
exist_ok: bool = False,
222222
webknossos_url: Optional[str] = None,
223223
) -> "Dataset":
224+
"""Downloads a dataset and returns the Dataset instance.
225+
226+
The `dataset_name` and `organization_name` specify which dataset to download.
227+
The `bbox`, `layers`, and `mags` parameters specify which parts of the dataset to download.
228+
If nothing is specified the whole image, all layers, and all mags are downloaded respectively.
229+
230+
The `path` and `exist_ok` parameter specify where to save the downloaded dataset and whether to overwrite
231+
if the `path` exists.
232+
233+
The `webknossos_url` specifies in which webknossos instance to search for the dataset. By default the
234+
configured url from `webknossos_context` is used, using https://webknossos.org as a fallback.
235+
"""
236+
224237
from webknossos.client._download_dataset import download_dataset
225238
from webknossos.client.context import _get_context, webknossos_context
226239

0 commit comments

Comments
 (0)