Skip to content

Commit ee8cb99

Browse files
authored
Add example for scaling a skeleton. (#1147)
* Add example and test it. * Add example to docs and update changelog.
1 parent 2a79f51 commit ee8cb99

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

docs/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ nav:
107107
- webknossos-py/examples/download_segments.md
108108
- webknossos-py/examples/load_annotation_from_file.md
109109
- webknossos-py/examples/skeleton_path_length.md
110+
- webknossos-py/examples/upsample_skeleton.md
110111
- Administration Examples:
111112
- webknossos-py/examples/user_times.md
112113
- 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+
# Upsample a Skeleton
2+
3+
This example shows how to adapt the `voxel_size` of a skeleton annotation and move the nodes positions according to the scale factor.
4+
5+
```python
6+
--8<--
7+
webknossos/examples/upsample_skeleton.py
8+
--8<--
9+
```

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 example for scaling a skeleton. [#1147](https://github.com/scalableminds/webknossos-libs/pull/1147)
1819

1920
### Changed
2021
- Added options `--layer-name` and `--mag` for compress command of the CLI. [#1141](https://github.com/scalableminds/webknossos-libs/pull/1141)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from pathlib import Path
2+
3+
import webknossos as wk
4+
5+
INPUT_PATH = Path(__file__).parent.parent / "testdata" / "nmls" / "test_a.nml"
6+
OUTPUT_PATH = Path(__file__).parent.parent / "testoutput" / "nmls" / "test_a_scaled.nml"
7+
OUTPUT_PATH.parent.mkdir(parents=True, exist_ok=True)
8+
9+
FACTOR = wk.Vec3Int(4, 4, 1)
10+
11+
12+
def main() -> None:
13+
"""
14+
Scales a skeleton by a given factor. The factor is applied to the voxel size and the node positions.
15+
16+
This can be usefull when the skeletons are created in a downsampled version and need to be scaled back to the finest mag.
17+
"""
18+
skeleton = wk.Skeleton.load(INPUT_PATH)
19+
20+
skeleton.voxel_size = (
21+
FACTOR[0] * skeleton.voxel_size[0],
22+
FACTOR[1] * skeleton.voxel_size[1],
23+
FACTOR[2] * skeleton.voxel_size[2],
24+
)
25+
for tree in skeleton.flattened_trees():
26+
for node in tree.nodes:
27+
old_pos = node.position
28+
node.position = wk.Vec3Int(
29+
old_pos[0] * FACTOR[0], old_pos[1] * FACTOR[1], old_pos[2] * FACTOR[2]
30+
)
31+
32+
skeleton.save(OUTPUT_PATH)
33+
34+
35+
if __name__ == "__main__":
36+
main()

webknossos/tests/test_examples.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,12 @@ def test_load_annotation_file() -> None:
364364

365365
with tmp_cwd():
366366
load_annotation(annotation_file)
367+
368+
369+
def test_upsample_skeleton() -> None:
370+
import examples.upsample_skeleton as example
371+
372+
with tmp_cwd():
373+
(skeleton,) = exec_main_and_get_vars(example, "skeleton")
374+
375+
assert skeleton.voxel_size == (16, 16, 35)

0 commit comments

Comments
 (0)