Skip to content

Commit 775aa6a

Browse files
normanrzjstriebel
andauthored
Moar examples + Dataset.{compress,downsample} (#813)
* adds 2 new examples and Dataset.compress and Dataset.downsample * add examples to docs * fixes * fixes? * changelog * Apply suggestions from code review Co-authored-by: Jonathan Striebel <[email protected]> * pr feedback * pr feedback * fix circular import * add snapshot, minor changes * explicit segment_ids * fix * Apply suggestions from code review Co-authored-by: Jonathan Striebel <[email protected]> Co-authored-by: Jonathan Striebel <[email protected]> Co-authored-by: Jonathan Striebel <[email protected]>
1 parent 11d4664 commit 775aa6a

File tree

9 files changed

+2006
-0
lines changed

9 files changed

+2006
-0
lines changed

docs/mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ nav:
7777
- Usage Examples:
7878
- Dataset Examples:
7979
- webknossos-py/examples/dataset_usage.md
80+
- webknossos-py/examples/upload_tiff_stack.md
8081
- webknossos-py/examples/upload_image_data.md
8182
- webknossos-py/examples/download_image_data.md
8283
- webknossos-py/examples/remote_datasets.md
@@ -86,6 +87,7 @@ nav:
8687
- webknossos-py/examples/learned_segmenter.md
8788
- webknossos-py/examples/skeleton_synapse_candidates.md
8889
- webknossos-py/examples/calculate_segment_sizes.md
90+
- webknossos-py/examples/download_segments.md
8991
- Administration Examples:
9092
- webknossos-py/examples/user_times.md
9193
- webknossos-py/examples/annotation_project_administration.md
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Download segment masks
2+
3+
This example [accesses a volume annotation on webKnososs](../../api/webknossos/annotation/annotation.md#Annotation.open_as_remote_dataset) and creates segment masks from selected segments. The segment masks are stored as tiff sequences. The segments have been selected from the "Segments" tab in webKnossos. The [BufferedSliceReader](../../api/webknossos/dataset/view.md#View.get_buffered_slice_reader) is used to efficiently request sections from the remote segmentation data.
4+
5+
```python
6+
--8<--
7+
webknossos/examples/download_segments.py
8+
--8<--
9+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Upload tiff stack
2+
3+
This example shows how to [create a new dataset from a tiff sequence](../../api/webknossos/dataset/dataset.md#Dataset.from_images), [compress](../../api/webknossos/dataset/dataset.md#Dataset.compress) it, [downsample](../../api/webknossos/dataset/dataset.md#Dataset.downsample) it and [upload](../../api/webknossos/dataset/dataset.md#Dataset.upload) it to webKnossos.
4+
5+
```python
6+
--8<--
7+
webknossos/examples/upload_tiff_stack.py
8+
--8<--
9+
```

webknossos/Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ For upgrade instructions, please check the respective *Breaking Changes* section
3030
[#811](https://github.com/scalableminds/webknossos-libs/pull/811)
3131
- `skeleton.save()` now also accepts paths with a `.zip` suffix. [#811](https://github.com/scalableminds/webknossos-libs/pull/811)
3232
- Added `annotation.get_volume_layer_segments()` to interact with information from the `Segments` tab in annotations. This method returns a dict from segment ids to an object containing optional segment `name`, `color` and `anchor_position`. [#812](https://github.com/scalableminds/webknossos-libs/pull/812)
33+
- Added convenience methods `Dataset.compress` and `Dataset.downsample` for compressing and downsampling all layers and mags in a dataset. [#813](https://github.com/scalableminds/webknossos-libs/pull/813)
34+
- Added examples for downloading segment masks from webKnossos and cubing & uploading tiff stacks. [#813](https://github.com/scalableminds/webknossos-libs/pull/813)
3335

3436
### Changed
3537

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import numpy as np
2+
from tifffile import imwrite
3+
4+
import webknossos as wk
5+
6+
ANNOTATION_ID = "634e8fe1010000b4006f3cf4"
7+
SEGMENT_IDS = [32, 667325]
8+
MAG = wk.Mag("4-4-1")
9+
10+
11+
def main() -> None:
12+
dataset = wk.Annotation.open_as_remote_dataset(
13+
ANNOTATION_ID, webknossos_url="https://webknossos.org"
14+
)
15+
mag_view = dataset.get_segmentation_layers()[0].get_mag(MAG)
16+
17+
z = mag_view.layer.bounding_box.topleft.z
18+
with mag_view.get_buffered_slice_reader() as reader:
19+
for slice_data in reader:
20+
slice_data = slice_data[0] # First channel only
21+
for segment_id in SEGMENT_IDS:
22+
segment_mask = (slice_data == segment_id).astype(
23+
np.uint8
24+
) * 255 # Make a binary mask 0=empty, 255=segment
25+
segment_mask = segment_mask.T # Tiff likes the data transposed
26+
imwrite(
27+
f"l4_sample_segments/seg{segment_id:04d}_mag{MAG}_z{z:04d}.tiff",
28+
segment_mask,
29+
)
30+
31+
print(f"Downloaded z={z:04d}")
32+
z += MAG.z
33+
34+
35+
if __name__ == "__main__":
36+
main()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from pathlib import Path
2+
from time import gmtime, strftime
3+
4+
import webknossos as wk
5+
6+
7+
def main() -> None:
8+
time_str = strftime("%Y-%m-%d_%H-%M-%S", gmtime())
9+
dataset = wk.Dataset.from_images(
10+
str(Path(__file__).parent.parent / "testdata" / "tiff"),
11+
f"tiff_dataset_{time_str}",
12+
voxel_size=(12, 12, 12),
13+
)
14+
dataset.compress()
15+
dataset.downsample()
16+
17+
remote_dataset = dataset.upload()
18+
url = remote_dataset.url
19+
print(f"Successfully uploaded {url}")
20+
21+
22+
if __name__ == "__main__":
23+
main()

0 commit comments

Comments
 (0)