Skip to content

Commit 22feedb

Browse files
Detect mappings (#136)
* finished implemention, example mappings needed * added another assert with comment * finished tests * formatted files
1 parent 2d1668f commit 22feedb

File tree

7 files changed

+228
-1
lines changed

7 files changed

+228
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "test",
3+
"classes": [
4+
[
5+
461032,
6+
461089,
7+
1643267,
8+
1643289
9+
],
10+
[
11+
15029780,
12+
15029823,
13+
15029824,
14+
15029832,
15+
15029833,
16+
15029842,
17+
15029849,
18+
15029913
19+
],
20+
[
21+
15029853,
22+
15029859,
23+
15029897,
24+
15029914
25+
],
26+
[
27+
15030077,
28+
15030188,
29+
15030198,
30+
15030209,
31+
15030210,
32+
15030254,
33+
15030273,
34+
15030310
35+
]
36+
]
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "test",
3+
"classes": [
4+
[
5+
461032,
6+
461089,
7+
1643267,
8+
1643289
9+
],
10+
[
11+
15029780,
12+
15029823,
13+
15029824,
14+
15029832,
15+
15029833,
16+
15029842,
17+
15029849,
18+
15029913
19+
],
20+
[
21+
15029853,
22+
15029859,
23+
15029897,
24+
15029914
25+
],
26+
[
27+
15030077,
28+
15030188,
29+
15030198,
30+
15030209,
31+
15030210,
32+
15030254,
33+
15030273,
34+
15030310
35+
]
36+
]
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "test",
3+
"classes": [
4+
[
5+
461032,
6+
461089,
7+
1643267,
8+
1643289
9+
],
10+
[
11+
15029780,
12+
15029823,
13+
15029824,
14+
15029832,
15+
15029833,
16+
15029842,
17+
15029849,
18+
15029913
19+
],
20+
[
21+
15029853,
22+
15029859,
23+
15029897,
24+
15029914
25+
],
26+
[
27+
15030077,
28+
15030188,
29+
15030198,
30+
15030209,
31+
15030210,
32+
15030254,
33+
15030273,
34+
15030310
35+
]
36+
]
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "test",
3+
"classes": [
4+
[
5+
461032,
6+
461089,
7+
1643267,
8+
1643289
9+
],
10+
[
11+
15029780,
12+
15029823,
13+
15029824,
14+
15029832,
15+
15029833,
16+
15029842,
17+
15029849,
18+
15029913
19+
],
20+
[
21+
15029853,
22+
15029859,
23+
15029897,
24+
15029914
25+
],
26+
[
27+
15030077,
28+
15030188,
29+
15030198,
30+
15030209,
31+
15030210,
32+
15030254,
33+
15030273,
34+
15030310
35+
]
36+
]
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "test",
3+
"classes": [
4+
[
5+
461032,
6+
461089,
7+
1643267,
8+
1643289
9+
],
10+
[
11+
15029780,
12+
15029823,
13+
15029824,
14+
15029832,
15+
15029833,
16+
15029842,
17+
15029849,
18+
15029913
19+
],
20+
[
21+
15029853,
22+
15029859,
23+
15029897,
24+
15029914
25+
],
26+
[
27+
15030077,
28+
15030188,
29+
15030198,
30+
15030209,
31+
15030210,
32+
15030254,
33+
15030273,
34+
15030310
35+
]
36+
]
37+
}

tests/test_metadata.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
refresh_metadata,
99
read_datasource_properties,
1010
read_metadata_for_layer,
11+
detect_mappings,
1112
)
1213

1314

@@ -66,5 +67,38 @@ def write_custom_layer(target_path, layer_name, dtype, num_channels):
6667
dataset.write(off=(0, 0, 0), data=data)
6768

6869

70+
def test_mapping_detection():
71+
# NOTE: the mappings do not match do the actual wkw data. Therefore do not use them
72+
expected_mappings = [
73+
"test_mapping_1.json",
74+
"test_mapping_2.json",
75+
"test_mapping_3.json",
76+
"test_mapping_4.json",
77+
"test_mapping_5.json",
78+
]
79+
datapath_with_mappings = "testdata/test_metadata"
80+
layer_name_with_mapping = "segmentation"
81+
detected_mappings = detect_mappings(datapath_with_mappings, layer_name_with_mapping)
82+
83+
# test if all detected mappings are int the expected_mappings
84+
assert all(
85+
detected_mapping in expected_mappings for detected_mapping in detected_mappings
86+
), "Found unexpected mapping(s)."
87+
# test if all mappings were detected
88+
assert len(expected_mappings) == len(
89+
detected_mappings
90+
), "Did not find all mappings."
91+
92+
datapath_without_mappings = "testdata/WT1_wkw"
93+
layer_name_without_mapping = "color"
94+
detected_mappings = detect_mappings(
95+
datapath_without_mappings, layer_name_without_mapping
96+
)
97+
assert (
98+
len(detected_mappings) == 0
99+
), "Found mapping(s) even though there should not be any mapping."
100+
101+
69102
if __name__ == "__main__":
70103
test_element_class_convertion()
104+
test_mapping_detection()

wkcuber/metadata.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import wkw
44
import logging
55
import numpy as np
6+
import glob
67

78
from argparse import ArgumentParser
89
from glob import iglob
@@ -268,13 +269,20 @@ def detect_standard_layer(
268269
}
269270

270271

272+
def detect_mappings(dataset_path, layer_name):
273+
pattern = path.join(dataset_path, layer_name, "mappings", "*.json")
274+
mapping_files = glob.glob(pattern)
275+
mapping_file_names = [path.basename(mapping_file) for mapping_file in mapping_files]
276+
return mapping_file_names
277+
278+
271279
def detect_segmentation_layer(
272280
dataset_path, layer_name, max_id, compute_max_id=False, exact_bounding_box=None
273281
):
274282
layer_info = detect_standard_layer(
275283
dataset_path, layer_name, exact_bounding_box, category="segmentation"
276284
)
277-
layer_info["mappings"] = []
285+
layer_info["mappings"] = detect_mappings(dataset_path, layer_name)
278286
layer_info["largestSegmentId"] = max_id
279287

280288
if compute_max_id:

0 commit comments

Comments
 (0)