Skip to content

Commit 60e78be

Browse files
Add examples for Dataset functions in documentation. (#901)
* Add example for Dataset functions in documentation. * Update docs/src/webknossos-py/examples/image_list_to_wkw.md Co-authored-by: Tom Herold <[email protected]> * Add examples to mkdocs.yml and resolve issues. * Add tests to ensure correct behaviour of examples. * Refine markdown of image stack to dataset example. * Format test_example.py with black version 23.1.0. --------- Co-authored-by: Tom Herold <[email protected]>
1 parent d36e0b0 commit 60e78be

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed

docs/mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ nav:
8080
- webknossos-py/examples/dataset_usage.md
8181
- webknossos-py/examples/upload_tiff_stack.md
8282
- webknossos-py/examples/upload_image_data.md
83+
- webknossos-py/examples/create_dataset_from_images.md
84+
- webknossos-py/examples/image_stack_to_dataset.md
8385
- webknossos-py/examples/download_image_data.md
8486
- webknossos-py/examples/download_tiff_stack.md
8587
- webknossos-py/examples/remote_datasets.md
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Create Dataset from Images
2+
3+
This example shows how to [convert a folder of images into a new dataset](../../api/webknossos/dataset/dataset.md#Dataset.from_images).
4+
5+
```python
6+
--8<--
7+
webknossos/examples/create_dataset_from_images.py
8+
--8<--
9+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Image Stack to Dataset
2+
3+
This example shows how to [create a new WEBKNOSSOS dataset from a stack of images](../../api/webknossos/dataset/dataset.md#Dataset.from_images), e.g. Tiff, JPEG, etc files.
4+
5+
There are a few assumptions we made about the images used for this example:
6+
7+
- all images have the same size
8+
- they have the same dtype (e.g. `uint8` or `float`)
9+
- they are greyscale images from microscopy / MRI / CT scan, therefore the category is `color`
10+
- masks and segmentations are not included yet
11+
12+
```python
13+
--8<--
14+
webknossos/examples/image_stack_to_dataset.py
15+
--8<--
16+
```

docs/src/webknossos-py/installation.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ You can install it from [pypi](https://pypi.org/project/webknossos/), e.g. via p
1010
```bash
1111
pip install webknossos
1212
```
13+
14+
For extended file format conversation support it is necessary to install the optional dependencies:
15+
16+
```bash
17+
pip install "webknossos[all]"
18+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from pathlib import Path
2+
3+
from webknossos import Dataset
4+
from webknossos.dataset import COLOR_CATEGORY
5+
6+
INPUT_DIR = Path(__file__).parent.parent / "testdata" / "tiff"
7+
OUTPUT_DIR = Path("testoutput/my_tiff_dataset")
8+
9+
10+
def main() -> None:
11+
"""Convert a folder of image files to a WEBKNOSSOS dataset."""
12+
dataset = Dataset.from_images(
13+
input_path=INPUT_DIR,
14+
output_path=OUTPUT_DIR,
15+
voxel_size=(11, 11, 11),
16+
layer_category=COLOR_CATEGORY,
17+
compress=True,
18+
)
19+
20+
print(f"Saved {dataset.name} at {dataset.path}.")
21+
22+
# dataset.upload()
23+
24+
25+
if __name__ == "__main__":
26+
main()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from pathlib import Path
2+
3+
from webknossos import Dataset
4+
from webknossos.dataset import COLOR_CATEGORY
5+
6+
INPUT_FILES = (
7+
Path(__file__).parent.parent / "testdata" / "tiff" / "test.0000.tiff",
8+
Path(__file__).parent.parent / "testdata" / "tiff" / "test.0001.tiff",
9+
)
10+
OUTPUT_FOLDER = Path("testoutput/tiff_dataset")
11+
12+
13+
def main() -> None:
14+
"""Convert a list of images into a WEBKNOSSOS dataset and directly add them as a new layer."""
15+
dataset = Dataset(
16+
dataset_path=OUTPUT_FOLDER,
17+
voxel_size=(11, 11, 11),
18+
name="My_new_dataset",
19+
exist_ok=False,
20+
)
21+
dataset.add_layer_from_images(
22+
images=INPUT_FILES,
23+
layer_name="test",
24+
category=COLOR_CATEGORY,
25+
)
26+
27+
# dataset.upload()
28+
29+
30+
if __name__ == "__main__":
31+
main()

webknossos/tests/test_examples.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,39 @@ def test_dataset_usage() -> None:
7272
assert data_in_mag2_subset.shape == (3, 256, 256, 16)
7373

7474

75+
def test_create_dataset_from_images() -> None:
76+
with tmp_cwd():
77+
import examples.create_dataset_from_images as example
78+
79+
(dataset,) = exec_main_and_get_vars(example, "dataset")
80+
assert dataset.voxel_size == (11, 11, 11)
81+
assert len(dataset.layers) == 1
82+
assert dataset.get_layer("tiff").get_finest_mag().read().shape == (
83+
1,
84+
265,
85+
265,
86+
257,
87+
)
88+
assert dataset.get_layer("tiff").dtype_per_channel == "uint8"
89+
90+
91+
def test_image_stack_to_dataset() -> None:
92+
with tmp_cwd():
93+
import examples.image_stack_to_dataset as example
94+
from webknossos.dataset import COLOR_CATEGORY
95+
96+
(dataset,) = exec_main_and_get_vars(example, "dataset")
97+
assert len(dataset.layers) == 1
98+
assert dataset.get_layer("test").category == COLOR_CATEGORY
99+
assert dataset.get_layer("test").get_finest_mag().read().shape == (
100+
1,
101+
265,
102+
265,
103+
2,
104+
)
105+
assert dataset.get_layer("test").dtype_per_channel == "uint8"
106+
107+
75108
@pytest.mark.block_network(allowed_hosts=[".*"])
76109
@pytest.mark.vcr(ignore_hosts=["webknossos.org", "data-humerus.webknossos.org"])
77110
def test_apply_merger_mode() -> None:

0 commit comments

Comments
 (0)