Skip to content

Commit d3c18e2

Browse files
committed
Fix more flake errors
Signed-off-by: Gaurav Gupta <gaugup@microsoft.com>
1 parent 93f9eb7 commit d3c18e2

3 files changed

Lines changed: 38 additions & 37 deletions

File tree

tests/test_dice_interface/test_dice_KD.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def _initiate_exp_object(self, KD_binary_classification_exp_object):
4747
self.data_df_copy = self.exp.data_interface.data_df.copy()
4848

4949
# When a query's feature value is not within the permitted range and the feature is not allowed to vary
50-
@pytest.mark.parametrize("desired_range, desired_class, total_CFs, features_to_vary, permitted_range",
50+
@pytest.mark.parametrize(("desired_range", "desired_class", "total_CFs", "features_to_vary", "permitted_range"),
5151
[(None, 0, 4, ['Numerical'], {'Categorical': ['b', 'c']})])
5252
def test_invalid_query_instance(self, desired_range, desired_class, sample_custom_query_1, total_CFs,
5353
features_to_vary, permitted_range):
@@ -59,20 +59,20 @@ def test_invalid_query_instance(self, desired_range, desired_class, sample_custo
5959
features_to_vary=features_to_vary, permitted_range=permitted_range)
6060

6161
# Verifying the output of the KD tree
62-
@pytest.mark.parametrize("desired_class, total_CFs", [(0, 1)])
63-
@pytest.mark.parametrize('posthoc_sparsity_algorithm', ['linear', 'binary', None])
62+
@pytest.mark.parametrize(("desired_class", "total_CFs"), [(0, 1)])
63+
@pytest.mark.parametrize(("posthoc_sparsity_algorithm"), ['linear', 'binary', None])
6464
def test_KD_tree_output(self, desired_class, sample_custom_query_1, total_CFs, posthoc_sparsity_algorithm):
6565
self.exp._generate_counterfactuals(query_instance=sample_custom_query_1, desired_class=desired_class,
6666
total_CFs=total_CFs,
6767
posthoc_sparsity_algorithm=posthoc_sparsity_algorithm)
6868
self.exp.final_cfs_df.Numerical = self.exp.final_cfs_df.Numerical.astype(int)
6969
expected_output = self.exp.data_interface.data_df
7070

71-
assert all(self.exp.final_cfs_df.Numerical == expected_output.Numerical[0]) and \
72-
all(self.exp.final_cfs_df.Categorical == expected_output.Categorical[0])
71+
assert all(self.exp.final_cfs_df.Numerical == expected_output.Numerical[0])
72+
assert all(self.exp.final_cfs_df.Categorical == expected_output.Categorical[0])
7373

7474
# Verifying the output of the KD tree
75-
@pytest.mark.parametrize("desired_class, total_CFs", [(0, 1)])
75+
@pytest.mark.parametrize(("desired_class", "total_CFs"), [(0, 1)])
7676
def test_KD_tree_counterfactual_explanations_output(self, desired_class, sample_custom_query_1, total_CFs):
7777
counterfactual_explanations = self.exp.generate_counterfactuals(
7878
query_instances=sample_custom_query_1, desired_class=desired_class,
@@ -81,35 +81,35 @@ def test_KD_tree_counterfactual_explanations_output(self, desired_class, sample_
8181
assert counterfactual_explanations is not None
8282

8383
# Testing that the features_to_vary argument actually varies only the features that you wish to vary
84-
@pytest.mark.parametrize("desired_class, total_CFs, features_to_vary", [(0, 1, ["Numerical"])])
84+
@pytest.mark.parametrize(("desired_class", "total_CFs", "features_to_vary"), [(0, 1, ["Numerical"])])
8585
def test_features_to_vary(self, desired_class, sample_custom_query_2, total_CFs, features_to_vary):
8686
self.exp._generate_counterfactuals(query_instance=sample_custom_query_2, desired_class=desired_class,
8787
total_CFs=total_CFs, features_to_vary=features_to_vary)
8888
self.exp.final_cfs_df.Numerical = self.exp.final_cfs_df.Numerical.astype(int)
8989
expected_output = self.exp.data_interface.data_df
9090

91-
assert all(self.exp.final_cfs_df.Numerical == expected_output.Numerical[1]) and \
92-
all(self.exp.final_cfs_df.Categorical == expected_output.Categorical[1])
91+
assert all(self.exp.final_cfs_df.Numerical == expected_output.Numerical[1])
92+
assert all(self.exp.final_cfs_df.Categorical == expected_output.Categorical[1])
9393

9494
# Testing that the permitted_range argument actually varies the features only within the permitted_range
95-
@pytest.mark.parametrize("desired_class, total_CFs, permitted_range", [(0, 1, {'Numerical': [1000, 10000]})])
95+
@pytest.mark.parametrize(("desired_class", "total_CFs", "permitted_range"), [(0, 1, {'Numerical': [1000, 10000]})])
9696
def test_permitted_range(self, desired_class, sample_custom_query_2, total_CFs, permitted_range):
9797
self.exp._generate_counterfactuals(query_instance=sample_custom_query_2, desired_class=desired_class,
9898
total_CFs=total_CFs, permitted_range=permitted_range)
9999
self.exp.final_cfs_df.Numerical = self.exp.final_cfs_df.Numerical.astype(int)
100100
expected_output = self.exp.data_interface.data_df
101-
assert all(self.exp.final_cfs_df.Numerical == expected_output.Numerical[1]) and \
102-
all(self.exp.final_cfs_df.Categorical == expected_output.Categorical[1])
101+
assert all(self.exp.final_cfs_df.Numerical == expected_output.Numerical[1])
102+
assert all(self.exp.final_cfs_df.Categorical == expected_output.Categorical[1])
103103

104104
# Testing if you can provide permitted_range for categorical variables
105-
@pytest.mark.parametrize("desired_class, total_CFs, permitted_range", [(0, 4, {'Categorical': ['b', 'c']})])
105+
@pytest.mark.parametrize(("desired_class", "total_CFs", "permitted_range"), [(0, 4, {'Categorical': ['b', 'c']})])
106106
def test_permitted_range_categorical(self, desired_class, sample_custom_query_2, total_CFs, permitted_range):
107107
self.exp._generate_counterfactuals(query_instance=sample_custom_query_2, desired_class=desired_class,
108108
total_CFs=total_CFs, permitted_range=permitted_range)
109109
assert all(i in permitted_range["Categorical"] for i in self.exp.final_cfs_df.Categorical.values)
110110

111111
# Ensuring that there are no duplicates in the resulting counterfactuals even if the dataset has duplicates
112-
@pytest.mark.parametrize("desired_class, total_CFs", [(0, 2)])
112+
@pytest.mark.parametrize(("desired_class", "total_CFs"), [(0, 2)])
113113
def test_duplicates(self, desired_class, sample_custom_query_4, total_CFs):
114114
self.exp._generate_counterfactuals(query_instance=sample_custom_query_4, total_CFs=total_CFs,
115115
desired_class=desired_class)
@@ -123,8 +123,8 @@ def test_duplicates(self, desired_class, sample_custom_query_4, total_CFs):
123123
assert all(self.exp.final_cfs_df == expected_output)
124124

125125
# Testing for index returned
126-
@pytest.mark.parametrize("desired_class, total_CFs", [(0, 1)])
127-
@pytest.mark.parametrize('posthoc_sparsity_algorithm', ['linear', 'binary', None])
126+
@pytest.mark.parametrize(("desired_class", "total_CFs"), [(0, 1)])
127+
@pytest.mark.parametrize(("posthoc_sparsity_algorithm"), ['linear', 'binary', None])
128128
def test_index(self, desired_class, sample_custom_query_index, total_CFs, posthoc_sparsity_algorithm):
129129
self.exp._generate_counterfactuals(query_instance=sample_custom_query_index, total_CFs=total_CFs,
130130
desired_class=desired_class,
@@ -139,8 +139,8 @@ def _initiate_exp_object(self, KD_multi_classification_exp_object):
139139
self.data_df_copy = self.exp_multi.data_interface.data_df.copy()
140140

141141
# Testing that the output of multiclass classification lies in the desired_class
142-
@pytest.mark.parametrize("desired_class, total_CFs", [(2, 3)])
143-
@pytest.mark.parametrize('posthoc_sparsity_algorithm', ['linear', 'binary', None])
142+
@pytest.mark.parametrize(("desired_class", "total_CFs"), [(2, 3)])
143+
@pytest.mark.parametrize(("posthoc_sparsity_algorithm"), ['linear', 'binary', None])
144144
def test_KD_tree_output(self, desired_class, sample_custom_query_2, total_CFs,
145145
posthoc_sparsity_algorithm):
146146
self.exp_multi._generate_counterfactuals(query_instance=sample_custom_query_2, total_CFs=total_CFs,
@@ -156,9 +156,9 @@ def _initiate_exp_object(self, KD_regression_exp_object):
156156
self.data_df_copy = self.exp_regr.data_interface.data_df.copy()
157157

158158
# Testing that the output of regression lies in the desired_range
159-
@pytest.mark.parametrize("desired_range, total_CFs", [([1, 2.8], 6)])
160-
@pytest.mark.parametrize("version", ['2.0', '1.0'])
161-
@pytest.mark.parametrize('posthoc_sparsity_algorithm', ['linear', 'binary', None])
159+
@pytest.mark.parametrize(("desired_range", "total_CFs"), [([1, 2.8], 6)])
160+
@pytest.mark.parametrize(("version"), ['2.0', '1.0'])
161+
@pytest.mark.parametrize(("posthoc_sparsity_algorithm"), ['linear', 'binary', None])
162162
def test_KD_tree_output(self, desired_range, sample_custom_query_2, total_CFs, version, posthoc_sparsity_algorithm):
163163
cf_examples = self.exp_regr._generate_counterfactuals(query_instance=sample_custom_query_2, total_CFs=total_CFs,
164164
desired_range=desired_range,
@@ -173,7 +173,7 @@ def test_KD_tree_output(self, desired_range, sample_custom_query_2, total_CFs, v
173173
assert recovered_cf_examples is not None
174174
assert cf_examples == recovered_cf_examples
175175

176-
@pytest.mark.parametrize("desired_range, total_CFs", [([1, 2.8], 6)])
176+
@pytest.mark.parametrize(("desired_range", "total_CFs"), [([1, 2.8], 6)])
177177
def test_KD_tree_counterfactual_explanations_output(self, desired_range, sample_custom_query_2,
178178
total_CFs):
179179
counterfactual_explanations = self.exp_regr.generate_counterfactuals(
@@ -189,7 +189,7 @@ def test_KD_tree_counterfactual_explanations_output(self, desired_range, sample_
189189
assert counterfactual_explanations == recovered_counterfactual_explanations
190190

191191
# Testing for 0 CFs needed
192-
@pytest.mark.parametrize("desired_class, desired_range, total_CFs", [(0, [1, 2.8], 0)])
192+
@pytest.mark.parametrize(("desired_class", "desired_range", "total_CFs"), [(0, [1, 2.8], 0)])
193193
def test_zero_cfs(self, desired_class, desired_range, sample_custom_query_4, total_CFs):
194194
self.exp_regr._generate_counterfactuals(query_instance=sample_custom_query_4, total_CFs=total_CFs,
195195
desired_range=desired_range)

tests/test_dice_interface/test_dice_genetic.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,21 @@ def _initiate_exp_object(self, genetic_binary_classification_exp_object):
4444
self.exp = genetic_binary_classification_exp_object # explainer object
4545

4646
# When invalid desired_class is given
47-
@pytest.mark.parametrize("desired_class, total_CFs", [(7, 3)])
47+
@pytest.mark.parametrize(("desired_class", "total_CFs"), [(7, 3)])
4848
def test_no_cfs(self, desired_class, sample_custom_query_1, total_CFs):
4949
with pytest.raises(UserConfigValidationException):
5050
self.exp.generate_counterfactuals(query_instances=sample_custom_query_1, total_CFs=total_CFs,
5151
desired_class=desired_class)
5252

5353
# When a query's feature value is not within the permitted range and the feature is not allowed to vary
54-
@pytest.mark.parametrize("features_to_vary, permitted_range, feature_weights",
54+
@pytest.mark.parametrize(("features_to_vary", "permitted_range", "feature_weights"),
5555
[(['Numerical'], {'Categorical': ['b', 'c']}, "inverse_mad")])
5656
def test_invalid_query_instance(self, sample_custom_query_1, features_to_vary, permitted_range, feature_weights):
5757
with pytest.raises(ValueError):
5858
self.exp.setup(features_to_vary, permitted_range, sample_custom_query_1, feature_weights)
5959

6060
# Testing that the features_to_vary argument actually varies only the features that you wish to vary
61-
@pytest.mark.parametrize("desired_class, total_CFs, features_to_vary, initialization",
61+
@pytest.mark.parametrize(("desired_class", "total_CFs", "features_to_vary", "initialization"),
6262
[(1, 2, ["Numerical"], "kdtree"), (1, 2, ["Numerical"], "random")])
6363
def test_features_to_vary(self, desired_class, sample_custom_query_2, total_CFs, features_to_vary, initialization):
6464
ans = self.exp.generate_counterfactuals(query_instances=sample_custom_query_2,
@@ -74,7 +74,7 @@ def test_features_to_vary(self, desired_class, sample_custom_query_2, total_CFs,
7474
range(total_CFs))
7575

7676
# Testing that the permitted_range argument actually varies the features only within the permitted_range
77-
@pytest.mark.parametrize("desired_class, total_CFs, features_to_vary, permitted_range, initialization",
77+
@pytest.mark.parametrize(("desired_class", "total_CFs", "features_to_vary", "permitted_range", "initialization"),
7878
[(1, 2, "all", {'Numerical': [10, 15]}, "kdtree"),
7979
(1, 2, "all", {'Numerical': [10, 15]}, "random")])
8080
def test_permitted_range(self, desired_class, sample_custom_query_2, total_CFs, features_to_vary, permitted_range,
@@ -92,7 +92,7 @@ def test_permitted_range(self, desired_class, sample_custom_query_2, total_CFs,
9292
in range(total_CFs))
9393

9494
# Testing if you can provide permitted_range for categorical variables
95-
@pytest.mark.parametrize("desired_class, total_CFs, features_to_vary, permitted_range, initialization",
95+
@pytest.mark.parametrize(("desired_class", "total_CFs", "features_to_vary", "permitted_range", "initialization"),
9696
[(1, 2, "all", {'Categorical': ['a', 'c']}, "kdtree"),
9797
(1, 2, "all", {'Categorical': ['a', 'c']}, "random")])
9898
def test_permitted_range_categorical(self, desired_class, total_CFs, features_to_vary, sample_custom_query_2,
@@ -118,7 +118,7 @@ def test_query_instance_with_target_column(self, sample_custom_query_6):
118118
assert "present in query instance" in str(ve)
119119

120120
# Testing if only valid cfs are found after maxiterations
121-
@pytest.mark.parametrize("desired_class, total_CFs, initialization, maxiterations",
121+
@pytest.mark.parametrize(("desired_class", "total_CFs", "initialization", "maxiterations"),
122122
[(0, 7, "kdtree", 0), (0, 7, "random", 0)])
123123
def test_maxiter(self, desired_class, sample_custom_query_2, total_CFs, initialization, maxiterations):
124124
ans = self.exp.generate_counterfactuals(query_instances=sample_custom_query_2,
@@ -129,7 +129,7 @@ def test_maxiter(self, desired_class, sample_custom_query_2, total_CFs, initiali
129129
assert i == desired_class
130130

131131
# Testing the custom predict function
132-
@pytest.mark.parametrize("desired_class", [2])
132+
@pytest.mark.parametrize(("desired_class"), [2])
133133
def test_predict_custom(self, desired_class, sample_custom_query_2, mocker):
134134
self.exp.data_interface.set_continuous_feature_indexes(query_instance=sample_custom_query_2)
135135
self.exp.yloss_type = 'hinge_loss'
@@ -145,7 +145,7 @@ def _initiate_exp_object(self, genetic_multi_classification_exp_object):
145145
self.exp = genetic_multi_classification_exp_object # explainer object
146146

147147
# Testing if only valid cfs are found after maxiterations
148-
@pytest.mark.parametrize("desired_class, total_CFs, initialization, maxiterations",
148+
@pytest.mark.parametrize(("desired_class", "total_CFs", "initialization", "maxiterations"),
149149
[(2, 7, "kdtree", 0), (2, 7, "random", 0)])
150150
def test_maxiter(self, desired_class, sample_custom_query_2, total_CFs, initialization, maxiterations):
151151
ans = self.exp.generate_counterfactuals(query_instances=sample_custom_query_2,
@@ -156,7 +156,7 @@ def test_maxiter(self, desired_class, sample_custom_query_2, total_CFs, initiali
156156
assert i == desired_class
157157

158158
# Testing the custom predict function
159-
@pytest.mark.parametrize("desired_class", [2])
159+
@pytest.mark.parametrize(("desired_class"), [2])
160160
def test_predict_custom(self, desired_class, sample_custom_query_2, mocker):
161161
self.exp.data_interface.set_continuous_feature_indexes(query_instance=sample_custom_query_2)
162162
self.exp.yloss_type = 'hinge_loss'
@@ -172,7 +172,7 @@ def _initiate_exp_object(self, genetic_regression_exp_object):
172172
self.exp = genetic_regression_exp_object # explainer object
173173

174174
# features_range
175-
@pytest.mark.parametrize("desired_range, total_CFs, initialization",
175+
@pytest.mark.parametrize(("desired_range", "total_CFs", "initialization"),
176176
[([1, 2.8], 2, "kdtree"), ([1, 2.8], 2, "random")])
177177
def test_desired_range(self, desired_range, sample_custom_query_2, total_CFs, initialization):
178178
ans = self.exp.generate_counterfactuals(query_instances=sample_custom_query_2,
@@ -181,11 +181,12 @@ def test_desired_range(self, desired_range, sample_custom_query_2, total_CFs, in
181181
for cfs_example in ans.cf_examples_list:
182182
assert all(
183183
[desired_range[0]] * total_CFs <= cfs_example.final_cfs_df[
184-
self.exp.data_interface.outcome_name].values) and all(
184+
self.exp.data_interface.outcome_name].values)
185+
assert all(
185186
cfs_example.final_cfs_df[self.exp.data_interface.outcome_name].values <= [desired_range[1]] * total_CFs)
186187

187188
# Testing if only valid cfs are found after maxiterations
188-
@pytest.mark.parametrize("desired_range, total_CFs, initialization, maxiterations",
189+
@pytest.mark.parametrize(("desired_range", "total_CFs", "initialization", "maxiterations"),
189190
[([1, 2.8], 7, "kdtree", 0), ([1, 2.8], 7, "random", 0)])
190191
def test_maxiter(self, desired_range, sample_custom_query_2, total_CFs, initialization, maxiterations):
191192
ans = self.exp.generate_counterfactuals(query_instances=sample_custom_query_2,

tests/test_dice_interface/test_dice_pytorch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _initiate_exp_object(self, pyt_exp_object, sample_adultincome_query):
4040
weights = np.random.rand(len(self.exp.data_interface.ohe_encoded_feature_names))
4141
self.exp.feature_weights_list = torch.tensor(weights)
4242

43-
@pytest.mark.parametrize("yloss, output", [("hinge_loss", 10.8443), ("l2_loss", 0.9999), ("log_loss", 9.8443)])
43+
@pytest.mark.parametrize(("yloss", "output"), [("hinge_loss", 10.8443), ("l2_loss", 0.9999), ("log_loss", 9.8443)])
4444
def test_yloss(self, yloss, output):
4545
self.exp.yloss_type = yloss
4646
loss1 = self.exp.compute_yloss()
@@ -52,7 +52,7 @@ def test_proximity_loss(self):
5252
# proximity loss computed for given query instance and feature weights.
5353
assert pytest.approx(loss2.data.detach().numpy(), abs=1e-4) == 0.0068
5454

55-
@pytest.mark.parametrize("diversity_loss, output", [("dpp_style:inverse_dist", 0.0104), ("avg_dist", 0.1743)])
55+
@pytest.mark.parametrize(("diversity_loss", "output"), [("dpp_style:inverse_dist", 0.0104), ("avg_dist", 0.1743)])
5656
def test_diversity_loss(self, diversity_loss, output):
5757
self.exp.diversity_loss_type = diversity_loss
5858
loss3 = self.exp.compute_diversity_loss()

0 commit comments

Comments
 (0)