Skip to content

Commit 95f0078

Browse files
Create move_dataset_slices_by_one.py (#453)
* Create move_dataset_slices_by_one.py * Update webknossos/examples/move_dataset_slices_by_one.py Co-authored-by: R Schwanhold <[email protected]> * incorporate PR feedback * move script to script-collection * add script-collection to linting and typechecking * format Co-authored-by: R Schwanhold <[email protected]>
1 parent 8f6e219 commit 95f0078

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

webknossos/lint.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ set -eEuo pipefail
44
pylint -j4 webknossos
55
# pylint -j4 tests/**/*.py # TODO add linting for tests
66
pylint -j4 examples/*.py
7+
pylint -j4 script-collection/*.py
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from argparse import ArgumentParser
2+
from pathlib import Path
3+
from typing import Tuple
4+
5+
from wkcuber.utils import ( # pylint: disable=import-error
6+
add_distribution_flags,
7+
add_scale_flag,
8+
get_executor_for_args,
9+
named_partial,
10+
)
11+
12+
from webknossos.dataset import Dataset, MagView, View
13+
14+
15+
def create_parser() -> ArgumentParser:
16+
parser = ArgumentParser(
17+
description="Copies a given dataset layer in mag1 while moving all slices from z+1 to z. Can be used to fix off-by-one errors."
18+
)
19+
20+
parser.add_argument("source_path", help="Path to input WKW dataset", type=Path)
21+
22+
parser.add_argument(
23+
"target_path",
24+
help="WKW dataset with which to compare the input dataset.",
25+
type=Path,
26+
)
27+
28+
parser.add_argument(
29+
"--layer_name",
30+
"-l",
31+
help="Name of the layer to compare (if not provided, all layers are compared)",
32+
default=None,
33+
)
34+
35+
add_distribution_flags(parser)
36+
add_scale_flag(parser)
37+
38+
return parser
39+
40+
41+
def move_by_one(
42+
src_mag: MagView,
43+
args: Tuple[View, int],
44+
) -> None:
45+
chunk_view, i = args
46+
del i
47+
size = chunk_view.size
48+
dst_offset = chunk_view.global_offset
49+
50+
src_offset = (
51+
dst_offset[0],
52+
dst_offset[1],
53+
dst_offset[2] + 1,
54+
)
55+
56+
data = src_mag.read(src_offset, size)
57+
chunk_view.write(data)
58+
59+
60+
def main() -> None:
61+
62+
args = create_parser().parse_args()
63+
64+
src_dataset = Dataset(args.source_path)
65+
src_layer = src_dataset.get_layer(args.layer_name)
66+
src_mag = src_layer.get_mag("1")
67+
68+
dst_dataset = Dataset.get_or_create(args.target_path, args.scale)
69+
dst_layer = dst_dataset.add_layer(args.layer_name, "color")
70+
dst_layer.bounding_box = src_layer.bounding_box
71+
72+
dst_mag = dst_layer.get_or_add_mag("1")
73+
74+
dst_view = dst_mag.get_view()
75+
76+
with get_executor_for_args(args) as executor:
77+
func = named_partial(move_by_one, src_mag)
78+
dst_view.for_each_chunk(
79+
func,
80+
chunk_size=dst_mag._get_file_dimensions(),
81+
executor=executor,
82+
)
83+
84+
85+
if __name__ == "__main__":
86+
main()

webknossos/typecheck.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ python -m mypy -p tests --disallow-untyped-defs --show-error-codes --strict-equa
99

1010
echo "Typecheck examples..."
1111
python -m mypy -p examples --disallow-untyped-defs --show-error-codes --strict-equality --namespace-packages --no-implicit-optional
12+
13+
echo "Typecheck script-collection..."
14+
python -m mypy -p script-collection --disallow-untyped-defs --show-error-codes --strict-equality --namespace-packages --no-implicit-optional

0 commit comments

Comments
 (0)