-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtestRaggedCollator.py
More file actions
204 lines (179 loc) · 7.52 KB
/
testRaggedCollator.py
File metadata and controls
204 lines (179 loc) · 7.52 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import unittest
from typing import Dict, List, Tuple
import torch
from chebai.preprocessing.collate import RaggedCollator
from chebai.preprocessing.structures import XYData
class TestRaggedCollator(unittest.TestCase):
"""
Unit tests for the RaggedCollator class.
"""
@classmethod
def setUpClass(cls) -> None:
"""
Set up the test environment by initializing a RaggedCollator instance.
"""
cls.collator = RaggedCollator()
def test_call_with_valid_data(self) -> None:
"""
Test the __call__ method with valid ragged data to ensure features, labels, and masks are correctly handled.
"""
data: List[Dict] = [
{"features": [1, 2], "labels": [True, False], "ident": "sample1"},
{"features": [3, 4, 5], "labels": [False, True, True], "ident": "sample2"},
{"features": [6], "labels": [True], "ident": "sample3"},
]
result: XYData = self.collator(data)
expected_x = torch.tensor([[1, 2, 0], [3, 4, 5], [6, 0, 0]])
expected_y = torch.tensor(
[[True, False, False], [False, True, True], [True, False, False]]
)
expected_mask_for_x = torch.tensor(
[[True, True, False], [True, True, True], [True, False, False]]
)
expected_lens_for_x = torch.tensor([2, 3, 1])
self.assertTrue(
torch.equal(result.x, expected_x),
"The feature tensor 'x' does not match the expected output.",
)
self.assertTrue(
torch.equal(result.y, expected_y),
"The label tensor 'y' does not match the expected output.",
)
self.assertTrue(
torch.equal(
result.additional_fields["model_kwargs"]["mask"], expected_mask_for_x
),
"The mask tensor does not match the expected output.",
)
self.assertTrue(
torch.equal(
result.additional_fields["model_kwargs"]["lens"], expected_lens_for_x
),
"The lens tensor does not match the expected output.",
)
self.assertEqual(
result.additional_fields["idents"],
("sample1", "sample2", "sample3"),
"The identifiers do not match the expected output.",
)
def test_call_with_missing_entire_labels(self) -> None:
"""
Test the __call__ method with data where some samples are missing labels.
"""
data: List[Dict] = [
{"features": [1, 2], "labels": [True, False], "ident": "sample1"},
{"features": [3, 4, 5], "labels": None, "ident": "sample2"},
{"features": [6], "labels": [True], "ident": "sample3"},
]
result: XYData = self.collator(data)
# https://github.com/ChEB-AI/python-chebai/pull/48#issuecomment-2324393829
expected_x = torch.tensor([[1, 2, 0], [3, 4, 5], [6, 0, 0]])
expected_y = torch.tensor(
[[True, False], [True, False]]
) # True -> 1, False -> 0
expected_mask_for_x = torch.tensor(
[[True, True, False], [True, True, True], [True, False, False]]
)
expected_lens_for_x = torch.tensor([2, 3, 1])
self.assertTrue(
torch.equal(result.x, expected_x),
"The feature tensor 'x' does not match the expected output when labels are missing.",
)
self.assertTrue(
torch.equal(result.y, expected_y),
"The label tensor 'y' does not match the expected output when labels are missing.",
)
self.assertTrue(
torch.equal(
result.additional_fields["model_kwargs"]["mask"], expected_mask_for_x
),
"The mask tensor does not match the expected output when labels are missing.",
)
self.assertTrue(
torch.equal(
result.additional_fields["model_kwargs"]["lens"], expected_lens_for_x
),
"The lens tensor does not match the expected output when labels are missing.",
)
self.assertEqual(
result.additional_fields["loss_kwargs"]["non_null_labels"],
[0, 2],
"The non-null labels list does not match the expected output.",
)
self.assertEqual(
len(result.additional_fields["loss_kwargs"]["non_null_labels"]),
result.y.shape[1],
"The length of non null labels list must match with target label variable size",
)
self.assertEqual(
result.additional_fields["idents"],
("sample1", "sample2", "sample3"),
"The identifiers do not match the expected output when labels are missing.",
)
def test_call_with_none_in_labels(self) -> None:
"""
Test the __call__ method with data where one of the elements in the labels is None.
"""
data: List[Dict] = [
{"features": [1, 2], "labels": [None, True], "ident": "sample1"},
{"features": [3, 4, 5], "labels": [True, False], "ident": "sample2"},
{"features": [6], "labels": [True], "ident": "sample3"},
]
result: XYData = self.collator(data)
expected_x = torch.tensor([[1, 2, 0], [3, 4, 5], [6, 0, 0]])
expected_y = torch.tensor(
[[False, True], [True, False], [True, False]]
) # None -> False
expected_mask_for_x = torch.tensor(
[[True, True, False], [True, True, True], [True, False, False]]
)
expected_lens_for_x = torch.tensor([2, 3, 1])
self.assertTrue(
torch.equal(result.x, expected_x),
"The feature tensor 'x' does not match the expected output when labels contain None.",
)
self.assertTrue(
torch.equal(result.y, expected_y),
"The label tensor 'y' does not match the expected output when labels contain None.",
)
self.assertTrue(
torch.equal(
result.additional_fields["model_kwargs"]["mask"], expected_mask_for_x
),
"The mask tensor does not match the expected output when labels contain None.",
)
self.assertTrue(
torch.equal(
result.additional_fields["model_kwargs"]["lens"], expected_lens_for_x
),
"The lens tensor does not match the expected output when labels contain None.",
)
self.assertEqual(
result.additional_fields["idents"],
("sample1", "sample2", "sample3"),
"The identifiers do not match the expected output when labels contain None.",
)
def test_call_with_empty_data(self) -> None:
"""
Test the __call__ method with an empty list to ensure it raises an error.
"""
data: List[Dict] = []
with self.assertRaises(
Exception, msg="Expected an Error when no data is provided"
):
self.collator(data)
def test_process_label_rows(self) -> None:
"""
Test the process_label_rows method to ensure it pads label sequences correctly.
"""
labels: Tuple = ([True, False], [False, True, True], [True])
result: torch.Tensor = self.collator.process_label_rows(labels)
expected_output = torch.tensor(
[[True, False, False], [False, True, True], [True, False, False]]
)
self.assertTrue(
torch.equal(result, expected_output),
"The processed label rows tensor does not match the expected output.",
)
if __name__ == "__main__":
unittest.main()