Skip to content

Commit 276c0ee

Browse files
normanrzjstriebel
andauthored
adds example for downloading a tiff stack (#862)
* adds example for downloading a tiff stack * lint * doh * url * try no:faulthandler * revert previous, update test durations, slightly smaller test-case * CI: use splitting-algorithm least_duration * even smaller testcase --------- Co-authored-by: Jonathan Striebel <[email protected]> Co-authored-by: Jonathan Striebel <[email protected]>
1 parent adeec06 commit 276c0ee

File tree

7 files changed

+415
-351
lines changed

7 files changed

+415
-351
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ jobs:
172172
- name: Python tests
173173
env:
174174
WK_TOKEN: ${{ secrets.WK_TOKEN }}
175-
run: ./test.sh --splits 3 --group ${{ matrix.group }}
175+
run: ./test.sh --splits 3 --group ${{ matrix.group }} --splitting-algorithm least_duration
176176

177177
- name: Check if git is dirty
178178
run: |

docs/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ nav:
8181
- webknossos-py/examples/upload_tiff_stack.md
8282
- webknossos-py/examples/upload_image_data.md
8383
- webknossos-py/examples/download_image_data.md
84+
- webknossos-py/examples/download_tiff_stack.md
8485
- webknossos-py/examples/remote_datasets.md
8586
- webknossos-py/examples/zarr_and_dask.md
8687
- Annotation Examples:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Download datasets as TIFF sequences
2+
3+
This example [accesses a dataset on webKnososs](../../api/webknossos/dataset/dataset.md#Dataset.open_remote) and creates a TIFF sequences from the downloaded data. The [BufferedSliceReader](../../api/webknossos/dataset/view.md#View.get_buffered_slice_reader) is used to efficiently request sections from the remote data.
4+
5+
```python
6+
--8<--
7+
webknossos/examples/download_tiff_stack.py
8+
--8<--
9+
```

webknossos/.test_durations

Lines changed: 349 additions & 348 deletions
Large diffs are not rendered by default.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from tifffile import imwrite
2+
3+
import webknossos as wk
4+
5+
DATASET_NAME = "l4_sample"
6+
LAYER_NAME = "color"
7+
MAG = wk.Mag("4-4-1")
8+
9+
10+
def main() -> None:
11+
dataset = wk.Dataset.open_remote(
12+
DATASET_NAME,
13+
organization_id="scalable_minds",
14+
webknossos_url="https://webknossos.org",
15+
)
16+
mag_view = dataset.get_layer(LAYER_NAME).get_mag(MAG)
17+
18+
z = mag_view.layer.bounding_box.topleft.z
19+
with mag_view.get_buffered_slice_reader() as reader:
20+
for slice_data in reader:
21+
slice_data = slice_data[0] # First channel only
22+
slice_data = slice_data.T # Tiff likes the data transposed
23+
24+
imwrite(
25+
f"l4_sample_tiff/mag{MAG}_z{z:04d}.tiff",
26+
slice_data,
27+
)
28+
29+
print(f"Downloaded z={z:04d}")
30+
z += MAG.z
31+
32+
33+
if __name__ == "__main__":
34+
main()

webknossos/tests/dataset/test_add_layer_from_images.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@
1919
def test_compare_tifffile(tmp_path: Path) -> None:
2020
ds = wk.Dataset(tmp_path, (1, 1, 1))
2121
l = ds.add_layer_from_images(
22-
"testdata/tiff/test.*.tiff",
22+
"testdata/tiff/test.02*.tiff",
2323
layer_name="compare_tifffile",
2424
compress=True,
2525
category="segmentation",
2626
topleft=(100, 100, 55),
27+
chunk_shape=(8, 8, 8),
28+
chunks_per_shard=(8, 8, 8),
2729
)
2830
assert l.bounding_box.topleft == wk.Vec3Int(100, 100, 55)
2931
data = l.get_finest_mag().read()[0, :, :]
3032
for z_index in range(0, data.shape[-1]):
31-
with TiffFile("testdata/tiff/test.0000.tiff") as tif_file:
33+
with TiffFile("testdata/tiff/test.0200.tiff") as tif_file:
3234
comparison_slice = tif_file.asarray().T
3335
assert np.array_equal(data[:, :, z_index], comparison_slice)
3436

webknossos/tests/test_examples.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,20 @@ def test_download_segments() -> None:
261261
len(list(output_path.iterdir()))
262262
== 2 * mag_view.layer.bounding_box.size.z / mag_view.mag.z
263263
)
264+
265+
266+
@pytest.mark.block_network(allowed_hosts=[".*"])
267+
@pytest.mark.vcr(ignore_hosts=["webknossos.org", "data-humerus.webknossos.org"])
268+
def test_download_tiff_stack() -> None:
269+
import examples.download_tiff_stack as example
270+
271+
with tmp_cwd():
272+
output_path = Path("l4_sample_tiff")
273+
output_path.mkdir()
274+
275+
(mag_view,) = exec_main_and_get_vars(example, "mag_view")
276+
277+
assert (
278+
len(list(output_path.iterdir()))
279+
== mag_view.layer.bounding_box.size.z / mag_view.mag.z
280+
)

0 commit comments

Comments
 (0)