-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathtest_explainer_base.py
More file actions
779 lines (678 loc) · 37 KB
/
test_explainer_base.py
File metadata and controls
779 lines (678 loc) · 37 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
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
import re
import numpy as np
import pandas as pd
import pytest
from rai_test_utils.datasets.tabular import create_housing_data
from raiutils.exceptions import UserConfigValidationException
from sklearn.ensemble import RandomForestRegressor
import dice_ml
from dice_ml.diverse_counterfactuals import CounterfactualExamples
from dice_ml.explainer_interfaces.explainer_base import ExplainerBase
from dice_ml.utils import helpers
from ..conftest import (private_data_object,
sample_adult_income_custom_query_11,
_load_adult_income_binary_model,
_load_custom_testing_binary_model)
@pytest.mark.parametrize("method", ['random', 'genetic', 'kdtree'])
class TestExplainerBaseBinaryClassification:
def _verify_feature_importance(self, feature_importance):
if feature_importance is not None:
for key in feature_importance:
assert feature_importance[key] >= 0.0
assert feature_importance[key] <= 1.0
def test_check_any_counterfactuals_computed(
self, method,
custom_public_data_interface,
sklearn_binary_classification_model_interface
):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
sample_custom_query = custom_public_data_interface.data_df[0:1]
cf_example = CounterfactualExamples(
data_interface=custom_public_data_interface,
test_instance_df=sample_custom_query)
cf_examples_arr = [cf_example]
with pytest.raises(
UserConfigValidationException,
match="No counterfactuals found for any of the query points! Kindly check your configuration."):
exp._check_any_counterfactuals_computed(cf_examples_arr=cf_examples_arr)
cf_example_has_cf = CounterfactualExamples(
data_interface=custom_public_data_interface,
final_cfs_df=sample_custom_query,
test_instance_df=sample_custom_query)
cf_example_no_cf = CounterfactualExamples(
data_interface=custom_public_data_interface,
test_instance_df=sample_custom_query)
cf_examples_arr = [cf_example_has_cf, cf_example_no_cf]
exp._check_any_counterfactuals_computed(cf_examples_arr=cf_examples_arr)
@pytest.mark.parametrize("desired_class", [1])
def test_local_feature_importance(
self, desired_class, method,
sample_custom_query_1, sample_counterfactual_example_dummy,
custom_public_data_interface,
sklearn_binary_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
sample_custom_query = pd.concat([sample_custom_query_1, sample_custom_query_1])
cf_explanations = exp.generate_counterfactuals(
query_instances=sample_custom_query,
total_CFs=15,
desired_class=desired_class)
cf_explanations.cf_examples_list[0].final_cfs_df = sample_counterfactual_example_dummy.copy()
cf_explanations.cf_examples_list[0].final_cfs_df_sparse = sample_counterfactual_example_dummy.copy()
cf_explanations.cf_examples_list[0].final_cfs_df.drop([0, 1, 2], inplace=True)
cf_explanations.cf_examples_list[0].final_cfs_df_sparse.drop([0, 1, 2], inplace=True)
cf_explanations.cf_examples_list[1].final_cfs_df = sample_counterfactual_example_dummy.copy()
cf_explanations.cf_examples_list[1].final_cfs_df_sparse = sample_counterfactual_example_dummy.copy()
cf_explanations.cf_examples_list[1].final_cfs_df.drop([0], inplace=True)
cf_explanations.cf_examples_list[1].final_cfs_df_sparse.drop([0], inplace=True)
local_importances = exp.local_feature_importance(
query_instances=None,
cf_examples_list=cf_explanations.cf_examples_list)
for local_importance in local_importances.local_importance:
self._verify_feature_importance(local_importance)
@pytest.mark.parametrize("desired_class", [1])
def test_global_feature_importance(
self, desired_class, method,
sample_custom_query_10, sample_counterfactual_example_dummy,
custom_public_data_interface,
sklearn_binary_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
cf_explanations = exp.generate_counterfactuals(
query_instances=sample_custom_query_10,
total_CFs=15,
desired_class=desired_class)
cf_explanations.cf_examples_list[0].final_cfs_df = sample_counterfactual_example_dummy.copy()
cf_explanations.cf_examples_list[0].final_cfs_df_sparse = sample_counterfactual_example_dummy.copy()
cf_explanations.cf_examples_list[0].final_cfs_df.drop([0, 1, 2, 3, 4], inplace=True)
cf_explanations.cf_examples_list[0].final_cfs_df_sparse.drop([0, 1, 2, 3, 4], inplace=True)
for index in range(1, len(cf_explanations.cf_examples_list)):
cf_explanations.cf_examples_list[index].final_cfs_df = sample_counterfactual_example_dummy.copy()
cf_explanations.cf_examples_list[index].final_cfs_df_sparse = sample_counterfactual_example_dummy.copy()
global_importance = exp.global_feature_importance(
query_instances=None,
cf_examples_list=cf_explanations.cf_examples_list)
self._verify_feature_importance(global_importance.summary_importance)
@pytest.mark.parametrize("desired_class", [1])
def test_columns_out_of_order(self, desired_class, method, sample_custom_query_1):
if method == 'genetic':
pytest.skip('DiceGenetic explainer fails this test case')
dataset = helpers.load_outcome_not_last_column_dataset()
d = dice_ml.Data(
dataframe=dataset, continuous_features=['Numerical'],
outcome_name='Outcome')
model = _load_custom_testing_binary_model()
m = dice_ml.Model(model=model, backend='sklearn')
exp = dice_ml.Dice(d, m, method=method)
exp._generate_counterfactuals(
query_instance=sample_custom_query_1,
total_CFs=0,
desired_class=desired_class,
desired_range=None,
permitted_range=None,
features_to_vary='all')
@pytest.mark.parametrize("desired_class", [1])
def test_incorrect_features_to_vary_list(
self, desired_class, method, sample_custom_query_1,
custom_public_data_interface,
sklearn_binary_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
with pytest.raises(
UserConfigValidationException,
match="Got features {" + "'unknown_feature'" + "} which are not present in training data"):
exp.generate_counterfactuals(
query_instances=sample_custom_query_1,
total_CFs=10,
desired_class=desired_class,
desired_range=None,
permitted_range=None,
features_to_vary=['unknown_feature'])
@pytest.mark.parametrize("desired_class", [1])
def test_incorrect_features_permitted_range(
self, desired_class, method, sample_custom_query_1,
custom_public_data_interface,
sklearn_binary_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
with pytest.raises(
UserConfigValidationException,
match="Got features {" + "'unknown_feature'" + "} which are not present in training data"):
exp.generate_counterfactuals(
query_instances=sample_custom_query_1,
total_CFs=10,
desired_class=desired_class,
desired_range=None,
permitted_range={'unknown_feature': [1, 30]},
features_to_vary='all')
@pytest.mark.parametrize("desired_class", [1])
def test_incorrect_values_permitted_range(
self, desired_class, method, sample_custom_query_1,
custom_public_data_interface,
sklearn_binary_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
with pytest.raises(UserConfigValidationException) as ucve:
exp.generate_counterfactuals(
query_instances=sample_custom_query_1,
total_CFs=10,
desired_class=desired_class,
desired_range=None,
permitted_range={'Categorical': ['d']},
features_to_vary='all')
assert 'The category {0} does not occur in the training data for feature {1}. Allowed categories are {2}'.format(
'd', 'Categorical', ['a', 'b', 'c']) in str(ucve)
# When no elements in the desired_class are present in the training data
@pytest.mark.parametrize("desired_class", [100, 'a'])
def test_unsupported_binary_class(
self, desired_class, method, sample_custom_query_1,
custom_public_data_interface,
sklearn_binary_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
with pytest.raises(UserConfigValidationException) as ucve:
exp.generate_counterfactuals(query_instances=sample_custom_query_1, total_CFs=3,
desired_class=desired_class)
if desired_class == 100:
assert "Desired class not present in training data!" in str(ucve)
else:
assert "The target class for {0} could not be identified".format(desired_class) in str(ucve)
# Testing if an error is thrown when the query instance has an unknown categorical variable
@pytest.mark.parametrize("desired_class", [1])
def test_query_instance_unknown_column(
self, desired_class, method, sample_custom_query_5,
custom_public_data_interface,
sklearn_binary_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
with pytest.raises(ValueError, match='not present in training data'):
exp.generate_counterfactuals(
query_instances=sample_custom_query_5, total_CFs=3,
desired_class=desired_class)
# Testing if an error is thrown when the query instance has an unknown categorical variable
@pytest.mark.parametrize("desired_class", [1])
def test_query_instance_outside_bounds(
self, desired_class, method, sample_custom_query_3,
custom_public_data_interface,
sklearn_binary_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
with pytest.raises(ValueError, match='has a value outside the dataset'):
exp.generate_counterfactuals(query_instances=sample_custom_query_3, total_CFs=1,
desired_class=desired_class)
# # Testing that the counterfactuals are in the desired class
@pytest.mark.parametrize("desired_class", [1])
def test_desired_class(
self, desired_class, method, sample_custom_query_2,
custom_public_data_interface,
sklearn_binary_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
ans = exp.generate_counterfactuals(query_instances=sample_custom_query_2,
features_to_vary='all',
total_CFs=2, desired_class=desired_class,
proximity_weight=0.2, sparsity_weight=0.2,
diversity_weight=5.0,
categorical_penalty=0.1,
permitted_range=None)
if method != 'kdtree':
assert all(ans.cf_examples_list[0].final_cfs_df[exp.data_interface.outcome_name].values == [desired_class] * 2)
else:
assert all(ans.cf_examples_list[0].final_cfs_df_sparse[exp.data_interface.outcome_name].values ==
[desired_class] * 2)
exp.serialize_explainer(method + '.pkl')
new_exp = ExplainerBase.deserialize_explainer(method + '.pkl')
ans = new_exp.generate_counterfactuals(query_instances=sample_custom_query_2,
features_to_vary='all',
total_CFs=2, desired_class=desired_class,
proximity_weight=0.2, sparsity_weight=0.2,
diversity_weight=5.0,
categorical_penalty=0.1,
permitted_range=None)
if method != 'kdtree':
assert all(ans.cf_examples_list[0].final_cfs_df[new_exp.data_interface.outcome_name].values == [desired_class] * 2)
else:
assert all(ans.cf_examples_list[0].final_cfs_df_sparse[new_exp.data_interface.outcome_name].values ==
[desired_class] * 2)
@pytest.mark.parametrize(("desired_class", "total_CFs", "permitted_range"),
[(1, 1, {'Numerical': [10, 150]})])
def test_permitted_range(
self, desired_class, method, total_CFs, permitted_range, sample_custom_query_2,
custom_public_data_interface,
sklearn_binary_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
ans = exp.generate_counterfactuals(query_instances=sample_custom_query_2,
permitted_range=permitted_range,
total_CFs=total_CFs, desired_class=desired_class)
for feature in permitted_range:
if method != 'kdtree':
assert all(
permitted_range[feature][0] <= ans.cf_examples_list[0].final_cfs_df[feature].values[i] <=
permitted_range[feature][1] for i in range(total_CFs))
else:
assert all(
permitted_range[feature][0] <= ans.cf_examples_list[0].final_cfs_df_sparse[feature].values[i] <=
permitted_range[feature][1] for i in range(total_CFs))
# Testing for 0 CFs needed
@pytest.mark.parametrize(("features_to_vary", "desired_class", "desired_range", "total_CFs", "permitted_range"),
[("all", 0, None, 0, None)])
def test_zero_cfs_internal(
self, method, features_to_vary, desired_class, desired_range, sample_custom_query_2, total_CFs,
permitted_range, custom_public_data_interface, sklearn_binary_classification_model_interface):
if method == 'genetic':
pytest.skip('DiceGenetic explainer does not handle the total counterfactuals as zero')
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
features_to_vary = exp.setup(features_to_vary, None, sample_custom_query_2, "inverse_mad")
exp._generate_counterfactuals(features_to_vary=features_to_vary, query_instance=sample_custom_query_2,
total_CFs=total_CFs, desired_class=desired_class,
desired_range=desired_range, permitted_range=permitted_range)
@pytest.mark.parametrize("desired_class", [1])
def test_cfs_type_consistency(
self, desired_class, method,
sample_custom_query_1, sample_counterfactual_example_dummy,
custom_public_data_interface,
sklearn_binary_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
sample_custom_query = pd.concat([sample_custom_query_1, sample_custom_query_1])
cf_explanations = exp.generate_counterfactuals(
query_instances=sample_custom_query,
total_CFs=2,
desired_class=desired_class)
for col in sample_custom_query.columns:
assert cf_explanations.cf_examples_list[0].test_instance_df[col].dtype == sample_custom_query[col].dtype
if cf_explanations.cf_examples_list[0].final_cfs_df is not None:
assert cf_explanations.cf_examples_list[0].final_cfs_df[col].dtype == sample_custom_query[col].dtype
if cf_explanations.cf_examples_list[0].final_cfs_df_sparse is not None:
assert cf_explanations.cf_examples_list[0].final_cfs_df_sparse[col].dtype == sample_custom_query[col].dtype
@pytest.mark.parametrize("method", ["genetic"])
def test_genetic_private_data(method):
d = private_data_object()
query = sample_adult_income_custom_query_11()
model = _load_adult_income_binary_model()
m = dice_ml.Model(model=model, backend='sklearn')
exp = dice_ml.Dice(d, m, method=method)
return exp.generate_counterfactuals(
query_instances=query,
total_CFs=1,
desired_class="opposite",
initialization="random")
@pytest.mark.parametrize("method", ['random', 'genetic', 'kdtree'])
class TestExplainerBaseMultiClassClassification:
@pytest.mark.parametrize("desired_class", [1])
def test_zero_totalcfs(
self, desired_class, method, sample_custom_query_1,
custom_public_data_interface,
sklearn_multiclass_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_multiclass_classification_model_interface,
method=method)
with pytest.raises(UserConfigValidationException):
exp.generate_counterfactuals(
query_instances=[sample_custom_query_1],
total_CFs=0,
desired_class=desired_class)
# Testing that the counterfactuals are in the desired class
@pytest.mark.parametrize(("desired_class", "total_CFs"), [(2, 2)])
@pytest.mark.parametrize("genetic_initialization", ['kdtree', 'random'])
def test_desired_class(
self, desired_class, total_CFs, method, genetic_initialization,
sample_custom_query_2,
custom_public_data_interface,
sklearn_multiclass_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_multiclass_classification_model_interface,
method=method)
if method != 'genetic':
ans = exp.generate_counterfactuals(
query_instances=sample_custom_query_2,
total_CFs=total_CFs, desired_class=desired_class)
else:
ans = exp.generate_counterfactuals(
query_instances=sample_custom_query_2,
total_CFs=total_CFs, desired_class=desired_class,
initialization=genetic_initialization)
assert ans is not None
if method != 'kdtree':
assert all(
ans.cf_examples_list[0].final_cfs_df[exp.data_interface.outcome_name].values == [desired_class] * total_CFs)
else:
assert all(
ans.cf_examples_list[0].final_cfs_df_sparse[exp.data_interface.outcome_name].values ==
[desired_class] * total_CFs)
assert all(i == desired_class for i in exp.cfs_preds)
exp.serialize_explainer(method + '.pkl')
new_exp = ExplainerBase.deserialize_explainer(method + '.pkl')
if method != 'genetic':
ans = new_exp.generate_counterfactuals(
query_instances=sample_custom_query_2,
total_CFs=total_CFs, desired_class=desired_class)
else:
ans = new_exp.generate_counterfactuals(
query_instances=sample_custom_query_2,
total_CFs=total_CFs, desired_class=desired_class,
initialization=genetic_initialization)
assert ans is not None
if method != 'kdtree':
assert all(
ans.cf_examples_list[0].final_cfs_df[
new_exp.data_interface.outcome_name].values == [desired_class] * total_CFs)
else:
assert all(
ans.cf_examples_list[0].final_cfs_df_sparse[new_exp.data_interface.outcome_name].values ==
[desired_class] * total_CFs)
assert all(i == desired_class for i in new_exp.cfs_preds)
# When no elements in the desired_class are present in the training data
@pytest.mark.parametrize(("desired_class", "total_CFs"), [(100, 3), ('opposite', 3)])
def test_unsupported_multiclass(
self, desired_class, total_CFs, method, sample_custom_query_4,
custom_public_data_interface,
sklearn_multiclass_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_multiclass_classification_model_interface,
method=method)
with pytest.raises(UserConfigValidationException) as ucve:
exp.generate_counterfactuals(query_instances=sample_custom_query_4, total_CFs=total_CFs,
desired_class=desired_class)
if desired_class == 100:
assert "Desired class not present in training data!" in str(ucve)
else:
assert "Desired class cannot be opposite if the number of classes is more than 2." in str(ucve)
# Testing for 0 CFs needed
@pytest.mark.parametrize(("features_to_vary", "desired_class", "desired_range", "total_CFs", "permitted_range"),
[("all", 0, None, 0, None)])
def test_zero_cfs_internal(
self, method, features_to_vary, desired_class, desired_range, sample_custom_query_2, total_CFs,
permitted_range, custom_public_data_interface, sklearn_multiclass_classification_model_interface):
if method == 'genetic':
pytest.skip('DiceGenetic explainer does not handle the total counterfactuals as zero')
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_multiclass_classification_model_interface,
method=method)
features_to_vary = exp.setup(features_to_vary, None, sample_custom_query_2, "inverse_mad")
exp._generate_counterfactuals(features_to_vary=features_to_vary, query_instance=sample_custom_query_2,
total_CFs=total_CFs, desired_class=desired_class,
desired_range=desired_range, permitted_range=permitted_range)
@pytest.mark.parametrize("desired_class", [1])
def test_cfs_type_consistency(
self, desired_class, method, sample_custom_query_1,
custom_public_data_interface,
sklearn_multiclass_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_multiclass_classification_model_interface,
method=method)
cf_explanations = exp.generate_counterfactuals(
query_instances=[sample_custom_query_1],
total_CFs=2,
desired_class=desired_class)
for col in sample_custom_query_1.columns:
assert cf_explanations.cf_examples_list[0].test_instance_df[col].dtype == sample_custom_query_1[col].dtype
if cf_explanations.cf_examples_list[0].final_cfs_df is not None:
assert cf_explanations.cf_examples_list[0].final_cfs_df[col].dtype == sample_custom_query_1[col].dtype
if cf_explanations.cf_examples_list[0].final_cfs_df_sparse is not None:
assert cf_explanations.cf_examples_list[0].final_cfs_df_sparse[col].dtype == sample_custom_query_1[col].dtype
class TestExplainerBaseRegression:
@pytest.mark.parametrize(("desired_range", "regression_exp_object"),
[([10, 100], 'random'), ([10, 100], 'genetic'), ([10, 100], 'kdtree')],
indirect=['regression_exp_object'])
def test_zero_totalcfs(self, desired_range, regression_exp_object, sample_custom_query_1):
exp = regression_exp_object # explainer object
with pytest.raises(UserConfigValidationException):
exp.generate_counterfactuals(
query_instances=[sample_custom_query_1],
total_CFs=0,
desired_range=desired_range)
@pytest.mark.parametrize(("desired_range", "method"),
[([3, 5], 'random')])
def test_numeric_categories(self, desired_range, method):
x_train, x_test, y_train, y_test, feature_names = \
create_housing_data()
x_train = pd.DataFrame(data=x_train, columns=feature_names)
x_test = pd.DataFrame(data=x_test, columns=feature_names)
rfc = RandomForestRegressor(n_estimators=10, max_depth=4,
random_state=777)
model = rfc.fit(x_train, y_train)
dataset_train = x_train.copy()
dataset_train['Outcome'] = y_train
d = dice_ml.Data(dataframe=dataset_train, continuous_features=feature_names, outcome_name='Outcome')
m = dice_ml.Model(model=model, backend='sklearn', model_type='regressor')
exp = dice_ml.Dice(d, m, method=method)
cf_explanation = exp.generate_counterfactuals(
query_instances=x_test.iloc[0:1],
total_CFs=10,
desired_range=desired_range)
assert cf_explanation is not None
exp.serialize_explainer("explainer.pkl")
new_exp = ExplainerBase.deserialize_explainer("explainer.pkl")
cf_explanation = new_exp.generate_counterfactuals(
query_instances=x_test.iloc[0:1],
total_CFs=10,
desired_range=desired_range)
assert cf_explanation is not None
class TestExplainerBase:
def test_instantiating_explainer_base(self, public_data_object):
with pytest.raises(TypeError):
ExplainerBase(data_interface=public_data_object)
@pytest.mark.parametrize("method", ['random', 'genetic', 'kdtree'])
class TestExplainerBaseUserConfigValidations:
@pytest.mark.parametrize('explainer_function',
['generate_counterfactuals', 'local_feature_importance',
'feature_importance', 'global_feature_importance'])
def test_generate_counterfactuals_user_config_validations(
self, method, sample_custom_query_2,
custom_public_data_interface,
sklearn_binary_classification_model_interface,
explainer_function):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
explainer_function = getattr(exp, explainer_function)
regex_pattern = re.escape(
'The query instance(s) should not have any missing values. '
'Please impute the missing values and try again.')
query_instances_missing_values_numerical = pd.DataFrame({'Categorical': ['a'], 'Numerical': [np.nan]})
with pytest.raises(
UserConfigValidationException,
match=regex_pattern):
explainer_function(
query_instances=query_instances_missing_values_numerical, desired_class='opposite',
total_CFs=10)
query_instances_missing_values_categorical = pd.DataFrame({'Categorical': [np.nan], 'Numerical': [1]})
with pytest.raises(
UserConfigValidationException,
match=regex_pattern):
explainer_function(
query_instances=query_instances_missing_values_categorical, desired_class='opposite',
total_CFs=10)
with pytest.raises(
UserConfigValidationException,
match=r"The number of counterfactuals generated per query instance \(total_CFs\) "
"should be a positive integer."):
explainer_function(query_instances=sample_custom_query_2,
total_CFs=-10, desired_class='opposite')
with pytest.raises(
UserConfigValidationException,
match=r"The number of counterfactuals generated per query instance \(total_CFs\) "
"should be a positive integer."):
explainer_function(
query_instances=sample_custom_query_2,
total_CFs=0,
desired_class="opposite")
with pytest.raises(
UserConfigValidationException,
match=r"The posthoc_sparsity_algorithm should be linear or binary and not random"):
explainer_function(query_instances=sample_custom_query_2,
total_CFs=10,
posthoc_sparsity_algorithm='random')
with pytest.raises(
UserConfigValidationException,
match=r"The posthoc_sparsity_algorithm should be linear or binary and not random"):
explainer_function(query_instances=sample_custom_query_2,
total_CFs=10,
posthoc_sparsity_algorithm='random')
with pytest.raises(
UserConfigValidationException,
match=r'The stopping_threshold should lie between 0.0 and 1.0'):
explainer_function(query_instances=sample_custom_query_2,
total_CFs=10,
stopping_threshold=-10.0)
with pytest.raises(
UserConfigValidationException,
match=r'The posthoc_sparsity_param should lie between 0.0 and 1.0'):
explainer_function(query_instances=sample_custom_query_2,
total_CFs=10,
posthoc_sparsity_param=-10.0)
with pytest.raises(
UserConfigValidationException,
match=r'The desired_range parameter should not be set for classification task'):
explainer_function(query_instances=sample_custom_query_2,
total_CFs=10, desired_range=[0, 10])
with pytest.raises(
UserConfigValidationException,
match=r'Some features need to be varied for generating counterfactuals.'):
explainer_function(query_instances=sample_custom_query_2,
total_CFs=10, features_to_vary=[])
@pytest.mark.parametrize('explainer_function',
['generate_counterfactuals', 'local_feature_importance',
'feature_importance', 'global_feature_importance'])
def test_generate_counterfactuals_user_config_validations_regression(
self, regression_exp_object, sample_custom_query_1,
method, explainer_function):
explainer_function = getattr(regression_exp_object, explainer_function)
with pytest.raises(
UserConfigValidationException,
match=r'The desired_range parameter should be set for regression task'):
explainer_function(query_instances=sample_custom_query_1,
total_CFs=10)
with pytest.raises(
UserConfigValidationException,
match=r'The parameter desired_range needs to have two numbers in ascending order.'):
explainer_function(query_instances=sample_custom_query_1,
total_CFs=10, desired_range=[1, 3, 4])
with pytest.raises(
UserConfigValidationException,
match=r'The range provided in desired_range should be in ascending order.'):
explainer_function(query_instances=sample_custom_query_1,
total_CFs=10, desired_range=[4, 3])
@pytest.mark.parametrize("method", ['random', 'genetic', 'kdtree'])
class TestExplainerBaseDataValidations:
def test_global_feature_importance_error_conditions_with_insufficient_query_points(
self, method,
sample_custom_query_1,
custom_public_data_interface,
sklearn_binary_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
cf_explanations = exp.generate_counterfactuals(
query_instances=sample_custom_query_1,
total_CFs=15)
with pytest.raises(
UserConfigValidationException,
match="The number of points for which counterfactuals generated should be "
"greater than or equal to 10 "
"to compute global feature importance"):
exp.global_feature_importance(
query_instances=None,
cf_examples_list=cf_explanations.cf_examples_list)
with pytest.raises(
UserConfigValidationException,
match="The number of query instances should be greater than or equal to 10 "
"to compute global feature importance over all query points"):
exp.global_feature_importance(
query_instances=sample_custom_query_1,
total_CFs=15)
@pytest.mark.skip(reason="Need to fix this test")
def test_global_feature_importance_error_conditions_with_insufficient_cfs_per_query_point(
self, method,
sample_custom_query_10,
custom_public_data_interface,
sklearn_binary_classification_model_interface):
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
cf_explanations = exp.generate_counterfactuals(
query_instances=sample_custom_query_10,
total_CFs=1)
with pytest.raises(
UserConfigValidationException,
match="The number of counterfactuals generated per query instance should be "
"greater than or equal to 10 "
"to compute global feature importance over all query points"):
exp.global_feature_importance(
query_instances=None,
cf_examples_list=cf_explanations.cf_examples_list)
with pytest.raises(
UserConfigValidationException,
match="The number of counterfactuals requested per query instance should be greater "
"than or equal to 10 "
"to compute global feature importance over all query points"):
exp.global_feature_importance(
query_instances=sample_custom_query_10,
total_CFs=1)
def test_local_feature_importance_error_conditions_with_insufficient_cfs_per_query_point(
self, method,
sample_custom_query_1,
custom_public_data_interface,
sklearn_binary_classification_model_interface):
if method == 'genetic':
pytest.skip('Skipping this test for genetic explainer')
exp = dice_ml.Dice(
custom_public_data_interface,
sklearn_binary_classification_model_interface,
method=method)
cf_explanations = exp.generate_counterfactuals(
query_instances=sample_custom_query_1,
total_CFs=1)
with pytest.raises(
UserConfigValidationException,
match="The number of counterfactuals generated per query instance should be "
"greater than or equal to 10 to compute feature importance for all query points"):
exp.local_feature_importance(
query_instances=None,
cf_examples_list=cf_explanations.cf_examples_list)
with pytest.raises(
UserConfigValidationException,
match="The number of counterfactuals requested per "
"query instance should be greater than or equal to 10 "
"to compute feature importance for all query points"):
exp.local_feature_importance(
query_instances=sample_custom_query_1,
total_CFs=1)