monai-weekly 1.5.dev2507__py3-none-any.whl → 1.5.dev2509__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,162 @@
1
+ # Copyright (c) MONAI Consortium
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software
7
+ # distributed under the License is distributed on an "AS IS" BASIS,
8
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
+ # See the License for the specific language governing permissions and
10
+ # limitations under the License.
11
+
12
+ from __future__ import annotations
13
+
14
+ import unittest
15
+
16
+ import numpy as np
17
+ import torch
18
+ from parameterized import parameterized
19
+
20
+ from monai.data import decollate_batch
21
+ from monai.metrics import AveragePrecisionMetric, compute_average_precision
22
+ from monai.transforms import Activations, AsDiscrete, Compose, ToTensor
23
+
24
+ _device = "cuda:0" if torch.cuda.is_available() else "cpu"
25
+ TEST_CASE_1 = [
26
+ torch.tensor([[0.1, 0.9], [0.3, 1.4], [0.2, 0.1], [0.1, 0.5]], device=_device),
27
+ torch.tensor([[0], [0], [1], [1]], device=_device),
28
+ True,
29
+ 2,
30
+ "macro",
31
+ 0.41667,
32
+ ]
33
+
34
+ TEST_CASE_2 = [
35
+ torch.tensor([[0.1, 0.9], [0.3, 1.4], [0.2, 0.1], [0.1, 0.5]], device=_device),
36
+ torch.tensor([[1], [1], [0], [0]], device=_device),
37
+ True,
38
+ 2,
39
+ "micro",
40
+ 0.85417,
41
+ ]
42
+
43
+ TEST_CASE_3 = [
44
+ torch.tensor([[0.1, 0.9], [0.3, 1.4], [0.2, 0.1], [0.1, 0.5]], device=_device),
45
+ torch.tensor([[0], [1], [0], [1]], device=_device),
46
+ True,
47
+ 2,
48
+ "macro",
49
+ 0.83333,
50
+ ]
51
+
52
+ TEST_CASE_4 = [
53
+ torch.tensor([[0.5], [0.5], [0.2], [8.3]]),
54
+ torch.tensor([[0], [1], [0], [1]]),
55
+ False,
56
+ None,
57
+ "macro",
58
+ 0.83333,
59
+ ]
60
+
61
+ TEST_CASE_5 = [torch.tensor([[0.5], [0.5], [0.2], [8.3]]), torch.tensor([0, 1, 0, 1]), False, None, "macro", 0.83333]
62
+
63
+ TEST_CASE_6 = [torch.tensor([0.5, 0.5, 0.2, 8.3]), torch.tensor([0, 1, 0, 1]), False, None, "macro", 0.83333]
64
+
65
+ TEST_CASE_7 = [
66
+ torch.tensor([[0.1, 0.9], [0.3, 1.4], [0.2, 0.1], [0.1, 0.5]]),
67
+ torch.tensor([[0], [1], [0], [1]]),
68
+ True,
69
+ 2,
70
+ "none",
71
+ [0.83333, 0.83333],
72
+ ]
73
+
74
+ TEST_CASE_8 = [
75
+ torch.tensor([[0.1, 0.9], [0.3, 1.4], [0.2, 0.1], [0.1, 0.5], [0.1, 0.5]]),
76
+ torch.tensor([[1, 0], [0, 1], [0, 0], [1, 1], [0, 1]]),
77
+ True,
78
+ None,
79
+ "weighted",
80
+ 0.66667,
81
+ ]
82
+
83
+ TEST_CASE_9 = [
84
+ torch.tensor([[0.1, 0.9], [0.3, 1.4], [0.2, 0.1], [0.1, 0.5], [0.1, 0.5]]),
85
+ torch.tensor([[1, 0], [0, 1], [0, 0], [1, 1], [0, 1]]),
86
+ True,
87
+ None,
88
+ "micro",
89
+ 0.71111,
90
+ ]
91
+
92
+ TEST_CASE_10 = [
93
+ torch.tensor([[0.1, 0.9], [0.3, 1.4], [0.2, 0.1], [0.1, 0.5]]),
94
+ torch.tensor([[0], [0], [0], [0]]),
95
+ True,
96
+ 2,
97
+ "macro",
98
+ float("nan"),
99
+ ]
100
+
101
+ TEST_CASE_11 = [
102
+ torch.tensor([[0.1, 0.9], [0.3, 1.4], [0.2, 0.1], [0.1, 0.5]]),
103
+ torch.tensor([[1], [1], [1], [1]]),
104
+ True,
105
+ 2,
106
+ "macro",
107
+ float("nan"),
108
+ ]
109
+
110
+ TEST_CASE_12 = [
111
+ torch.tensor([[0.1, 0.9], [0.3, 1.4], [0.2, 0.1], [0.1, 0.5]]),
112
+ torch.tensor([[0, 0], [1, 1], [2, 2], [3, 3]]),
113
+ True,
114
+ None,
115
+ "macro",
116
+ float("nan"),
117
+ ]
118
+
119
+ ALL_TESTS = [
120
+ TEST_CASE_1,
121
+ TEST_CASE_2,
122
+ TEST_CASE_3,
123
+ TEST_CASE_4,
124
+ TEST_CASE_5,
125
+ TEST_CASE_6,
126
+ TEST_CASE_7,
127
+ TEST_CASE_8,
128
+ TEST_CASE_9,
129
+ TEST_CASE_10,
130
+ TEST_CASE_11,
131
+ TEST_CASE_12,
132
+ ]
133
+
134
+
135
+ class TestComputeAveragePrecision(unittest.TestCase):
136
+
137
+ @parameterized.expand(ALL_TESTS)
138
+ def test_value(self, y_pred, y, softmax, to_onehot, average, expected_value):
139
+ y_pred_trans = Compose([ToTensor(), Activations(softmax=softmax)])
140
+ y_trans = Compose([ToTensor(), AsDiscrete(to_onehot=to_onehot)])
141
+ y_pred = torch.stack([y_pred_trans(i) for i in decollate_batch(y_pred)], dim=0)
142
+ y = torch.stack([y_trans(i) for i in decollate_batch(y)], dim=0)
143
+ result = compute_average_precision(y_pred=y_pred, y=y, average=average)
144
+ np.testing.assert_allclose(expected_value, result, rtol=1e-5)
145
+
146
+ @parameterized.expand(ALL_TESTS)
147
+ def test_class_value(self, y_pred, y, softmax, to_onehot, average, expected_value):
148
+ y_pred_trans = Compose([ToTensor(), Activations(softmax=softmax)])
149
+ y_trans = Compose([ToTensor(), AsDiscrete(to_onehot=to_onehot)])
150
+ y_pred = [y_pred_trans(i) for i in decollate_batch(y_pred)]
151
+ y = [y_trans(i) for i in decollate_batch(y)]
152
+ metric = AveragePrecisionMetric(average=average)
153
+ metric(y_pred=y_pred, y=y)
154
+ result = metric.aggregate()
155
+ np.testing.assert_allclose(expected_value, result, rtol=1e-5)
156
+ result = metric.aggregate(average=average) # test optional argument
157
+ metric.reset()
158
+ np.testing.assert_allclose(expected_value, result, rtol=1e-5)
159
+
160
+
161
+ if __name__ == "__main__":
162
+ unittest.main()
@@ -64,7 +64,7 @@ class TestConvertToOnnx(unittest.TestCase):
64
64
  rtol=rtol,
65
65
  atol=atol,
66
66
  )
67
- self.assertTrue(isinstance(onnx_model, onnx.ModelProto))
67
+ self.assertTrue(isinstance(onnx_model, onnx.ModelProto))
68
68
 
69
69
  @parameterized.expand(TESTS_ORT)
70
70
  @SkipIfBeforePyTorchVersion((1, 12))
@@ -21,14 +21,12 @@ from monai.data.synthetic import create_test_image_2d, create_test_image_3d
21
21
  from monai.transforms import GibbsNoise
22
22
  from monai.utils.misc import set_determinism
23
23
  from monai.utils.module import optional_import
24
- from tests.test_utils import TEST_NDARRAYS, assert_allclose
24
+ from tests.test_utils import TEST_NDARRAYS, assert_allclose, dict_product
25
25
 
26
26
  _, has_torch_fft = optional_import("torch.fft", name="fftshift")
27
27
 
28
- TEST_CASES = []
29
- for shape in ((128, 64), (64, 48, 80)):
30
- for input_type in TEST_NDARRAYS if has_torch_fft else [np.array]:
31
- TEST_CASES.append((shape, input_type))
28
+ params = {"shape": ((128, 64), (64, 48, 80)), "input_type": TEST_NDARRAYS if has_torch_fft else [np.array]}
29
+ TEST_CASES = list(dict_product(format="list", **params))
32
30
 
33
31
 
34
32
  class TestGibbsNoise(unittest.TestCase):