-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtestDynamicDataset.py
More file actions
381 lines (330 loc) · 13.9 KB
/
testDynamicDataset.py
File metadata and controls
381 lines (330 loc) · 13.9 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import unittest
from typing import Tuple
from unittest.mock import MagicMock, PropertyMock, patch
import pandas as pd
from chebai.preprocessing.datasets.base import _DynamicDataset
class TestDynamicDataset(unittest.TestCase):
"""
Test case for _DynamicDataset functionality, ensuring correct data splits and integrity
of train, validation, and test datasets.
"""
@classmethod
@patch.multiple(_DynamicDataset, __abstractmethods__=frozenset())
@patch.object(_DynamicDataset, "base_dir", new_callable=PropertyMock)
@patch.object(_DynamicDataset, "_name", new_callable=PropertyMock)
@patch("os.makedirs", return_value=None)
def setUpClass(
cls,
mock_makedirs,
mock_base_dir_property: PropertyMock,
mock_name_property: PropertyMock,
) -> None:
"""
Set up a base instance of _DynamicDataset for testing with mocked properties.
"""
# Mocking properties
mock_base_dir_property.return_value = "MockedBaseDirPropertyDynamicDataset"
mock_name_property.return_value = "MockedNamePropertyDynamicDataset"
# Mock Data Reader
ReaderMock = MagicMock()
ReaderMock.name.return_value = "MockedReader"
_DynamicDataset.READER = ReaderMock
# Creating an instance of the dataset
cls.dataset: _DynamicDataset = _DynamicDataset()
# Dataset with a balanced distribution of labels
X = [
[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],
]
y = [
[False, False],
[False, True],
[True, False],
[True, True],
[False, False],
[False, True],
[True, False],
[True, True],
[False, False],
[False, True],
[True, False],
[True, True],
[False, False],
[False, True],
[True, False],
[True, True],
]
cls.data_df = pd.DataFrame(
{"ident": [f"id{i + 1}" for i in range(len(X))], "features": X, "labels": y}
)
def test_get_test_split_valid(self) -> None:
"""
Test splitting the dataset into train and test sets and verify balance and non-overlap.
"""
# self.dataset.train_split = 0.5
# Test size will be 0.25 * 16 = 4
self.dataset.test_split = 0.25
self.dataset.validation_split = 0.25
train_df, test_df = self.dataset.get_test_split(self.data_df, seed=0)
# Assert the correct number of rows in train and test sets
self.assertEqual(len(train_df), 12, "Train set should contain 12 samples.")
self.assertEqual(len(test_df), 4, "Test set should contain 4 samples.")
# Check positive and negative label counts in train and test sets
train_pos_count, train_neg_count = self.get_positive_negative_labels_counts(
train_df
)
test_pos_count, test_neg_count = self.get_positive_negative_labels_counts(
test_df
)
# Ensure that the train and test sets have balanced positives and negatives
self.assertEqual(
train_pos_count, train_neg_count, "Train set labels should be balanced."
)
self.assertEqual(
test_pos_count, test_neg_count, "Test set labels should be balanced."
)
# Assert there is no overlap between train and test sets
train_idents = set(train_df["ident"])
test_idents = set(test_df["ident"])
self.assertEqual(
len(train_idents.intersection(test_idents)),
0,
"Train and test sets should not overlap.",
)
def test_get_test_split_missing_labels(self) -> None:
"""
Test the behavior when the 'labels' column is missing in the dataset.
"""
df_missing_labels = pd.DataFrame({"ident": ["id1", "id2"]})
with self.assertRaises(
KeyError, msg="Expected KeyError when 'labels' column is missing."
):
self.dataset.get_test_split(df_missing_labels)
def test_get_test_split_seed_consistency(self) -> None:
"""
Test that splitting the dataset with the same seed produces consistent results.
"""
train_df1, test_df1 = self.dataset.get_test_split(self.data_df, seed=42)
train_df2, test_df2 = self.dataset.get_test_split(self.data_df, seed=42)
pd.testing.assert_frame_equal(
train_df1,
train_df2,
obj="Train sets should be identical for the same seed.",
)
pd.testing.assert_frame_equal(
test_df1, test_df2, obj="Test sets should be identical for the same seed."
)
def test_get_train_val_splits_given_test(self) -> None:
"""
Test splitting the dataset into train and validation sets and verify balance and non-overlap.
"""
self.dataset.use_inner_cross_validation = False
# self.dataset.train_split = 0.5
self.dataset.test_split = 0.25
self.dataset.validation_split = 0.25
df_train_main, test_df = self.dataset.get_test_split(self.data_df, seed=0)
train_df, val_df = self.dataset.get_train_val_splits_given_test(
df_train_main, test_df, seed=42
)
# Ensure there is no overlap between train and test sets
train_idents = set(train_df["ident"])
test_idents = set(test_df["ident"])
self.assertEqual(
len(train_idents.intersection(test_idents)),
0,
"Train and test sets should not overlap.",
)
# Ensure there is no overlap between validation and test sets
val_idents = set(val_df["ident"])
self.assertEqual(
len(val_idents.intersection(test_idents)),
0,
"Validation and test sets should not overlap.",
)
# Ensure there is no overlap between train and validation sets
self.assertEqual(
len(train_idents.intersection(val_idents)),
0,
"Train and validation sets should not overlap.",
)
# Check positive and negative label counts in train and validation sets
train_pos_count, train_neg_count = self.get_positive_negative_labels_counts(
train_df
)
val_pos_count, val_neg_count = self.get_positive_negative_labels_counts(val_df)
# Ensure that the train and validation sets have balanced positives and negatives
self.assertEqual(
train_pos_count, train_neg_count, "Train set labels should be balanced."
)
self.assertEqual(
val_pos_count, val_neg_count, "Validation set labels should be balanced."
)
def test_get_train_val_splits_given_test_consistency(self) -> None:
"""
Test that splitting the dataset into train and validation sets with the same seed produces consistent results.
"""
test_df = self.data_df.iloc[12:] # Assume rows 12 onward are for testing
train_df1, val_df1 = self.dataset.get_train_val_splits_given_test(
self.data_df, test_df, seed=42
)
train_df2, val_df2 = self.dataset.get_train_val_splits_given_test(
self.data_df, test_df, seed=42
)
pd.testing.assert_frame_equal(
train_df1,
train_df2,
obj="Train sets should be identical for the same seed.",
)
pd.testing.assert_frame_equal(
val_df1,
val_df2,
obj="Validation sets should be identical for the same seed.",
)
def test_get_test_split_stratification(self) -> None:
"""
Test that the split into train and test sets maintains the stratification of labels.
"""
# self.dataset.train_split = 0.5
self.dataset.test_split = 0.25
self.dataset.validation_split = 0.25
train_df, test_df = self.dataset.get_test_split(self.data_df, seed=0)
number_of_labels = len(self.data_df["labels"][0])
# Check the label distribution in the original dataset
original_pos_count, original_neg_count = (
self.get_positive_negative_labels_counts(self.data_df)
)
total_count = len(self.data_df) * number_of_labels
# Calculate the expected proportions
original_pos_proportion = original_pos_count / total_count
original_neg_proportion = original_neg_count / total_count
# Check the label distribution in the train set
train_pos_count, train_neg_count = self.get_positive_negative_labels_counts(
train_df
)
train_total_count = len(train_df) * number_of_labels
# Calculate the train set proportions
train_pos_proportion = train_pos_count / train_total_count
train_neg_proportion = train_neg_count / train_total_count
# Assert that the proportions are similar to the original dataset
self.assertAlmostEqual(
train_pos_proportion,
original_pos_proportion,
places=1,
msg="Train set labels should maintain original positive label proportion.",
)
self.assertAlmostEqual(
train_neg_proportion,
original_neg_proportion,
places=1,
msg="Train set labels should maintain original negative label proportion.",
)
# Check the label distribution in the test set
test_pos_count, test_neg_count = self.get_positive_negative_labels_counts(
test_df
)
test_total_count = len(test_df) * number_of_labels
# Calculate the test set proportions
test_pos_proportion = test_pos_count / test_total_count
test_neg_proportion = test_neg_count / test_total_count
# Assert that the proportions are similar to the original dataset
self.assertAlmostEqual(
test_pos_proportion,
original_pos_proportion,
places=1,
msg="Test set labels should maintain original positive label proportion.",
)
self.assertAlmostEqual(
test_neg_proportion,
original_neg_proportion,
places=1,
msg="Test set labels should maintain original negative label proportion.",
)
def test_get_train_val_splits_given_test_stratification(self) -> None:
"""
Test that the split into train and validation sets maintains the stratification of labels.
"""
self.dataset.use_inner_cross_validation = False
# self.dataset.train_split = 0.5
self.dataset.test_split = 0.25
self.dataset.validation_split = 0.25
df_train_main, test_df = self.dataset.get_test_split(self.data_df, seed=0)
train_df, val_df = self.dataset.get_train_val_splits_given_test(
df_train_main, test_df, seed=42
)
number_of_labels = len(self.data_df["labels"][0])
# Check the label distribution in the original dataset
original_pos_count, original_neg_count = (
self.get_positive_negative_labels_counts(self.data_df)
)
total_count = len(self.data_df) * number_of_labels
# Calculate the expected proportions
original_pos_proportion = original_pos_count / total_count
original_neg_proportion = original_neg_count / total_count
# Check the label distribution in the train set
train_pos_count, train_neg_count = self.get_positive_negative_labels_counts(
train_df
)
train_total_count = len(train_df) * number_of_labels
# Calculate the train set proportions
train_pos_proportion = train_pos_count / train_total_count
train_neg_proportion = train_neg_count / train_total_count
# Assert that the proportions are similar to the original dataset
self.assertAlmostEqual(
train_pos_proportion,
original_pos_proportion,
places=1,
msg="Train set labels should maintain original positive label proportion.",
)
self.assertAlmostEqual(
train_neg_proportion,
original_neg_proportion,
places=1,
msg="Train set labels should maintain original negative label proportion.",
)
# Check the label distribution in the validation set
val_pos_count, val_neg_count = self.get_positive_negative_labels_counts(val_df)
val_total_count = len(val_df) * number_of_labels
# Calculate the validation set proportions
val_pos_proportion = val_pos_count / val_total_count
val_neg_proportion = val_neg_count / val_total_count
# Assert that the proportions are similar to the original dataset
self.assertAlmostEqual(
val_pos_proportion,
original_pos_proportion,
places=1,
msg="Validation set labels should maintain original positive label proportion.",
)
self.assertAlmostEqual(
val_neg_proportion,
original_neg_proportion,
places=1,
msg="Validation set labels should maintain original negative label proportion.",
)
@staticmethod
def get_positive_negative_labels_counts(df: pd.DataFrame) -> Tuple[int, int]:
"""
Count the number of True and False values within the labels column.
Args:
df (pd.DataFrame): The DataFrame containing the 'labels' column.
Returns:
Tuple[int, int]: A tuple containing the counts of True and False values, respectively.
"""
true_count = sum(sum(label) for label in df["labels"])
false_count = sum(len(label) - sum(label) for label in df["labels"])
return true_count, false_count
if __name__ == "__main__":
unittest.main()