forked from EpistasisLab/scikit-mdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
531 lines (457 loc) · 19.3 KB
/
tests.py
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# -*- coding: utf-8 -*-
"""
scikit-MDR was primarily developed at the University of Pennsylvania by:
- Randal S. Olson ([email protected])
- Tuan Nguyen ([email protected])
- and many more generous open source contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
from mdr import MDR, MDRClassifier, ContinuousMDR
import numpy as np
import random
import warnings
import inspect
from sklearn.metrics import accuracy_score, zero_one_loss
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import cross_val_score, StratifiedKFold
def test_mdr_init():
"""Ensure that the MDR instantiator stores the MDR variables properly"""
mdr_obj = MDR()
assert mdr_obj.tie_break == 1
assert mdr_obj.default_label == 0
assert mdr_obj.class_count_matrix is None
assert mdr_obj.feature_map is None
mdr_obj2 = MDR(tie_break=1, default_label=2)
assert mdr_obj2.tie_break == 1
assert mdr_obj2.default_label == 2
assert mdr_obj.class_count_matrix is None
assert mdr_obj.feature_map is None
def test_mdr_fit():
"""Ensure that the MDR 'fit' function constructs the right matrix to count each class, as well as the right map from feature instances to labels"""
features = np.array([ [2, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[1, 1]])
classes = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
mdr = MDR()
mdr.fit(features, classes)
assert len(mdr.class_count_matrix) == 4
assert len(mdr.feature_map) == 4
assert mdr.class_count_matrix[(2, 0)][1] == 1
assert mdr.class_count_matrix[(0, 0)][0] == 3
assert mdr.class_count_matrix[(0, 0)][1] == 6
assert mdr.class_count_matrix[(1, 1)][0] == 2
assert mdr.class_count_matrix[(0, 1)][1] == 3
assert mdr.feature_map[(2, 0)] == 1
assert mdr.feature_map[(0, 0)] == 1
assert mdr.feature_map[(1, 1)] == 0
assert mdr.feature_map[(0, 1)] == 1
# 2 0 count: 1 label 1; maps to 1
# 0 0 count: 3 label 0; 6 label 1; maps to 1 *tie_break*
# 1 1 count: 2 label 0; maps to 0
# 0 1 count: 3 label 1; maps to 1
def test_mdr_transform():
"""Ensure that the MDR 'transform' function maps a new set of feature instances to the desired labels"""
features = np.array([ [2, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[1, 1]])
classes = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
mdr = MDR()
mdr.fit(features, classes)
test_features = np.array([ [2, 2],
[1, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[1, 0],
[0, 0],
[1, 0],
[0, 0]])
new_features = mdr.transform(test_features)
assert np.array_equal(new_features, [[0], [0], [1], [1], [1], [1], [0], [1], [1], [1], [1], [0], [1], [0], [1]])
# 2 0 count: 1 label 1; maps to 1
# 0 0 count: 3 label 0; 6 label 1; maps to 1 *tie_break*
# 1 1 count: 2 label 0; maps to 0
# 0 1 count: 3 label 1; maps to 1
def test_mdr_fit_transform():
"""Ensure that the MDR 'fit_transform' function combines both fit and transform, and produces the right predicted labels"""
features = np.array([[2, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[1, 1]])
classes = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
mdr = MDR()
new_features = mdr.fit_transform(features, classes)
assert np.array_equal(new_features, [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [0], [0]])
def test_mdr_score():
"""Ensure that the MDR 'score' function outputs the right default score, as well as the right custom metric if specified"""
features = np.array([[2, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[1, 1]])
classes = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
mdr = MDRClassifier()
mdr.fit(features, classes)
assert mdr.score(features, classes) == 12. / 15
def test_mdr_custom_score():
"""Ensure that the MDR 'score' function outputs the right custom score passed in from the user"""
features = np.array([[2, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[1, 1]])
classes = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
mdr = MDRClassifier()
mdr.fit(features, classes)
assert mdr.score(features = features, class_labels = classes, scoring_function = accuracy_score) == 12. / 15
assert mdr.score(features = features, class_labels = classes, scoring_function = zero_one_loss) == 1 - 12. / 15
assert mdr.score(features = features, class_labels = classes, scoring_function = zero_one_loss, normalize=False) == 15 - 12
def test_mdr_fit_raise_ValueError():
"""Ensure that the MDR 'fit' function raises ValueError when it is not a binary classification (temporary)"""
features = np.array([[2, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[1, 1]])
classes = np.array([1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
mdr = MDR()
try:
mdr.fit(features, classes)
except ValueError:
assert True
else:
assert False
classes = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
try:
mdr.fit(features, classes)
except ValueError:
assert True
else:
assert False
def test_mdr_score_raise_ValueError():
"""Ensure that the MDR 'score' function raises ValueError when 'fit' has not already been called"""
features = np.array([[2, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[1, 1]])
classes = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
mdr = MDRClassifier()
try:
mdr.score(features, classes)
except ValueError:
assert True
else:
assert False
def test_mdr_sklearn_pipeline():
"""Ensure that MDR can be used as a transformer in a scikit-learn pipeline"""
features = np.array([[2, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[1, 1]])
classes = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
clf = make_pipeline(MDR(), LogisticRegression())
cv_scores = cross_val_score(clf, features, classes, cv=StratifiedKFold(n_splits=5, shuffle=True))
assert np.mean(cv_scores) > 0.
def test_mdr_sklearn_pipeline_parallel():
"""Ensure that MDR can be used as a transformer in a parallelized scikit-learn pipeline"""
features = np.array([[2, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[1, 1]])
classes = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
clf = make_pipeline(MDR(), LogisticRegression())
cv_scores = cross_val_score(clf, features, classes, cv=StratifiedKFold(n_splits=5, shuffle=True), n_jobs=-1)
assert np.mean(cv_scores) > 0.
"""
Continuous MDR tests
"""
def test_continuous_mdr_init():
"""Ensure that the ContinuousMDR instantiator stores the ContinuousMDR variables properly"""
cmdr_obj = ContinuousMDR()
assert cmdr_obj.tie_break == 1
assert cmdr_obj.default_label == 0
assert cmdr_obj.overall_mean_trait_value == 0.
cmdr_obj2 = ContinuousMDR(tie_break=1, default_label=2)
assert cmdr_obj2.tie_break == 1
assert cmdr_obj2.default_label == 2
def test_continuous_mdr_fit():
"""Ensure that the ContinuousMDR 'fit' function constructs the right matrix to count each class, as well as the right map from feature instances to labels"""
features = np.array([ [2, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[1, 1]])
targets = np.array([0.9, 0.8, 0.9, 0.85, 0.75, 0.95, 0.9, 1.0, 0.75, 0.8, 0.1, 0.2, 0.3, 0.25, 0.05])
cmdr = ContinuousMDR()
cmdr.fit(features, targets)
assert len(cmdr.mdr_matrix_values) == 4
assert len(cmdr.feature_map) == 4
assert cmdr.mdr_matrix_values[(0, 0)] == [0.8, 0.85, 0.75, 0.95, 1.0, 0.75, 0.1, 0.2, 0.3]
assert cmdr.mdr_matrix_values[(0, 1)] == [0.9, 0.9, 0.8]
assert cmdr.mdr_matrix_values[(1, 1)] == [0.25, 0.05]
assert cmdr.mdr_matrix_values[(2, 0)] == [0.9]
assert cmdr.feature_map[(0, 0)] == 1
assert cmdr.feature_map[(0, 1)] == 1
assert cmdr.feature_map[(1, 1)] == 0
assert cmdr.feature_map[(2, 0)] == 1
def test_continuous_mdr_transform():
"""Ensure that the ContinuousMDR 'transform' function maps a new set of feature instances to the desired labels"""
features = np.array([ [2, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[1, 1]])
targets = np.array([0.9, 0.8, 0.9, 0.85, 0.75, 0.95, 0.9, 1.0, 0.75, 0.8, 0.1, 0.2, 0.3, 0.25, 0.05])
cmdr = ContinuousMDR()
cmdr.fit(features, targets)
test_features = np.array([ [2, 2],
[1, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[1, 0],
[0, 0],
[1, 0],
[0, 0]])
expected_outputs = [[0],
[0],
[1],
[1],
[1],
[1],
[0],
[1],
[1],
[1],
[1],
[0],
[1],
[0],
[1]]
new_features = cmdr.transform(test_features)
print(new_features)
assert np.array_equal(new_features, expected_outputs)
def test_continuous_mdr_fit_transform():
"""Ensure that the ContinuousMDR 'fit_transform' function combines both fit and transform, and produces the right predicted labels"""
features = np.array([[2, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[1, 1]])
targets = np.array([0.9, 0.8, 0.9, 0.85, 0.75, 0.95, 0.9, 1.0, 0.75, 0.8, 0.1, 0.2, 0.3, 0.25, 0.05])
expected_outputs = [[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[0],
[0]]
cmdr = ContinuousMDR()
new_features = cmdr.fit_transform(features, targets)
assert np.array_equal(new_features, expected_outputs)
def test_continuous_mdr_score():
"""Ensure that the ContinuousMDR 'score' function outputs the right default score"""
features = np.array([[2, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[1, 1]])
targets = np.array([0.9, 0.8, 0.9, 0.85, 0.75, 0.95, 0.9, 1.0, 0.75, 0.8, 0.1, 0.2, 0.3, 0.25, 0.05])
cmdr = ContinuousMDR()
cmdr.fit(features, targets)
assert round(cmdr.score(features, targets), 3) == 2.514
def test_continuous_mdr_score_raise_ValueError():
"""Ensure that the ContinuousMDR 'score' function raises ValueError when 'fit' has not already been called"""
features = np.array([[2, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 1],
[1, 1]])
classes = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
cmdr = ContinuousMDR()
try:
cmdr.score(features, classes)
except ValueError:
assert True
else:
assert False