forked from Project-MONAI/MONAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_adaptors.py
More file actions
160 lines (112 loc) · 4.48 KB
/
Copy pathtest_adaptors.py
File metadata and controls
160 lines (112 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import itertools
import unittest
from monai.transforms.adaptors import FunctionSignature, adaptor, apply_alias, to_kwargs
class TestAdaptors(unittest.TestCase):
def test_function_signature(self):
def foo(image, label=None, *a, **kw):
pass
_ = FunctionSignature(foo)
def test_single_in_single_out(self):
def foo(image):
return image * 2
it = itertools.product(["image", ["image"]], [None, "image", ["image"], {"image": "image"}])
for i in it:
d = {"image": 2}
dres = adaptor(foo, i[0], i[1])(d)
self.assertEqual(dres["image"], 4)
d = {"image": 2}
dres = adaptor(foo, "image")(d)
self.assertEqual(dres["image"], 4)
d = {"image": 2}
dres = adaptor(foo, "image", "image")(d)
self.assertEqual(dres["image"], 4)
d = {"image": 2}
dres = adaptor(foo, "image", {"image": "image"})(d)
self.assertEqual(dres["image"], 4)
d = {"img": 2}
dres = adaptor(foo, "img", {"img": "image"})(d)
self.assertEqual(dres["img"], 4)
d = {"img": 2}
dres = adaptor(foo, ["img"], {"img": "image"})(d)
self.assertEqual(dres["img"], 4)
def test_multi_in_single_out(self):
def foo(image, label):
return image * label
it = itertools.product(["image", ["image"]], [None, ["image", "label"], {"image": "image", "label": "label"}])
for i in it:
d = {"image": 2, "label": 3}
dres = adaptor(foo, i[0], i[1])(d)
self.assertEqual(dres["image"], 6)
self.assertEqual(dres["label"], 3)
it = itertools.product(
["newimage", ["newimage"]], [None, ["image", "label"], {"image": "image", "label": "label"}]
)
for i in it:
d = {"image": 2, "label": 3}
dres = adaptor(foo, i[0], i[1])(d)
self.assertEqual(dres["image"], 2)
self.assertEqual(dres["label"], 3)
self.assertEqual(dres["newimage"], 6)
it = itertools.product(["img", ["img"]], [{"img": "image", "lbl": "label"}])
for i in it:
d = {"img": 2, "lbl": 3}
dres = adaptor(foo, i[0], i[1])(d)
self.assertEqual(dres["img"], 6)
self.assertEqual(dres["lbl"], 3)
def test_default_arg_single_out(self):
def foo(a, b=2):
return a * b
d = {"a": 5}
dres = adaptor(foo, "c")(d)
self.assertEqual(dres["c"], 10)
d = {"b": 5}
with self.assertRaises(TypeError):
dres = adaptor(foo, "c")(d)
def test_multi_out(self):
def foo(a, b):
return a * b, a / b
d = {"a": 3, "b": 4}
dres = adaptor(foo, ["c", "d"])(d)
self.assertEqual(dres["c"], 12)
self.assertEqual(dres["d"], 3 / 4)
def test_dict_out(self):
def foo(a):
return {"a": a * 2}
d = {"a": 2}
dres = adaptor(foo, {"a": "a"})(d)
self.assertEqual(dres["a"], 4)
d = {"b": 2}
dres = adaptor(foo, {"a": "b"}, {"b": "a"})(d)
self.assertEqual(dres["b"], 4)
class TestApplyAlias(unittest.TestCase):
def test_apply_alias(self):
def foo(d):
d["x"] *= 2
return d
d = {"a": 1, "b": 3}
result = apply_alias(foo, {"b": "x"})(d)
self.assertDictEqual({"a": 1, "b": 6}, result)
class TestToKwargs(unittest.TestCase):
def test_to_kwargs(self):
def foo(**kwargs):
results = {k: v * 2 for k, v in kwargs.items()}
return results
def compose_like(fn, data):
data = fn(data)
return data
d = {"a": 1, "b": 2}
actual = compose_like(to_kwargs(foo), d)
self.assertDictEqual(actual, {"a": 2, "b": 4})
with self.assertRaises(TypeError):
actual = compose_like(foo, d)