-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathconftest.py
More file actions
206 lines (161 loc) · 6.41 KB
/
Copy pathconftest.py
File metadata and controls
206 lines (161 loc) · 6.41 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
from collections import OrderedDict
import pandas as pd
import pytest
from sklearn.datasets import load_boston, load_iris
from sklearn.model_selection import train_test_split
import dice_ml
from dice_ml.utils import helpers
@pytest.fixture(scope='session')
def custom_public_data_interface_binary_out_of_order():
dataset = helpers.load_outcome_not_last_column_dataset()
d = dice_ml.Data(dataframe=dataset, continuous_features=['Numerical'], outcome_name='Outcome')
return d
@pytest.fixture(scope='session')
def custom_public_data_interface_binary():
dataset = helpers.load_custom_testing_dataset_binary()
d = dice_ml.Data(dataframe=dataset, continuous_features=['Numerical'], outcome_name='Outcome')
return d
@pytest.fixture(scope='session')
def custom_public_data_interface_multicalss():
dataset = helpers.load_custom_testing_dataset_multiclass()
d = dice_ml.Data(dataframe=dataset, continuous_features=['Numerical'], outcome_name='Outcome')
return d
@pytest.fixture(scope='session')
def custom_public_data_interface_regression():
dataset = helpers.load_custom_testing_dataset_regression()
d = dice_ml.Data(dataframe=dataset, continuous_features=['Numerical'], outcome_name='Outcome')
return d
@pytest.fixture(scope='session')
def sklearn_binary_classification_model_interface():
ML_modelpath = helpers.get_custom_dataset_modelpath_pipeline_binary()
m = dice_ml.Model(model_path=ML_modelpath, backend='sklearn', model_type='classifier')
return m
@pytest.fixture(scope='session')
def sklearn_multiclass_classification_model_interface():
ML_modelpath = helpers.get_custom_dataset_modelpath_pipeline_multiclass()
m = dice_ml.Model(model_path=ML_modelpath, backend='sklearn', model_type='classifier')
return m
@pytest.fixture(scope='session')
def sklearn_regression_model_interface():
ML_modelpath = helpers.get_custom_dataset_modelpath_pipeline_regression()
m = dice_ml.Model(model_path=ML_modelpath, backend='sklearn', model_type='regressor')
return m
@pytest.fixture
def public_data_object():
"""
Returns a public data object for the adult income dataset
"""
dataset = helpers.load_adult_income_dataset()
return dice_ml.Data(dataframe=dataset, continuous_features=['age', 'hours_per_week'], outcome_name='income')
@pytest.fixture
def private_data_object():
"""
Returns a private data object containing meta information about the adult income dataset.
Providing an OrderedDict to make it work for Python<=3.6
"""
features_dict = OrderedDict(
[('age', [17, 90]),
('workclass', ['Government', 'Other/Unknown', 'Private', 'Self-Employed']),
('education', ['Assoc', 'Bachelors', 'Doctorate', 'HS-grad', 'Masters', 'Prof-school', 'School', 'Some-college']),
('marital_status', ['Divorced', 'Married', 'Separated', 'Single', 'Widowed']),
('occupation', ['Blue-Collar', 'Other/Unknown', 'Professional', 'Sales', 'Service', 'White-Collar']),
('race', ['Other', 'White']),
('gender', ['Female', 'Male']),
('hours_per_week', [1, 99])]
)
return dice_ml.Data(features=features_dict, outcome_name='income')
@pytest.fixture
def sample_adultincome_query():
"""
Returns a sample query instance for adult income dataset
"""
return {'age': 22, 'workclass': 'Private', 'education': 'HS-grad', 'marital_status': 'Single', 'occupation': 'Service',
'race': 'White', 'gender': 'Female', 'hours_per_week': 45}
@pytest.fixture
def sample_custom_query_1():
"""
Returns a sample query instance for the custom dataset
"""
return pd.DataFrame({'Categorical': ['a'], 'Numerical': [25]})
@pytest.fixture
def sample_custom_query_2():
"""
Returns a sample query instance for the custom dataset
"""
return pd.DataFrame({'Categorical': ['b'], 'Numerical': [25]})
@pytest.fixture
def sample_custom_query_3():
"""
Returns a sample query instance for the custom dataset
"""
return pd.DataFrame({'Categorical': ['d'], 'Numerical': [1000000]})
@pytest.fixture
def sample_custom_query_4():
"""
Returns a sample query instance for the custom dataset
"""
return pd.DataFrame({'Categorical': ['c'], 'Numerical': [13]})
@pytest.fixture
def sample_custom_query_5():
"""
Returns a sample query instance for the custom dataset
"""
return pd.DataFrame({'X': ['d'], 'Numerical': [25]})
@pytest.fixture
def sample_custom_query_6():
"""
Returns a sample query instance for the custom dataset including Outcome
"""
return pd.DataFrame({'Categorical': ['c'], 'Numerical': [13], 'Outcome': 0})
@pytest.fixture
def sample_custom_query_index():
"""
Returns a sample query instance for the custom dataset
"""
return pd.DataFrame({'Categorical': ['a'], 'Numerical': [88]})
@pytest.fixture
def sample_custom_query_10():
"""
Returns a sample query instance for the custom dataset
"""
return pd.DataFrame(
{
'Categorical': ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a'],
'Numerical': [25, 50, 75, 100, 125, 150, 175, 200, 225, 250]
}
)
@pytest.fixture
def sample_counterfactual_example_dummy():
"""
Returns a sample counterfactual example
"""
return pd.DataFrame(
{
'Categorical': ['a', 'b', 'c', 'a', 'b',
'c', 'a', 'b', 'c', 'a',
'a', 'b', 'c', 'c', 'c'],
'Numerical': [25, 50, 75, 100, 125,
150, 175, 200, 225, 250,
150, 175, 200, 225, 250],
'Outcome': [1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1]
}
)
@pytest.fixture
def create_iris_data():
iris = load_iris()
x_train, x_test, y_train, y_test = train_test_split(
iris.data, iris.target, test_size=0.2, random_state=0)
feature_names = iris.feature_names
classes = iris.target_names
return x_train, x_test, y_train, y_test, feature_names, classes
@pytest.fixture
def create_boston_data():
boston = load_boston()
x_train, x_test, y_train, y_test = train_test_split(
boston.data, boston.target,
test_size=0.2, random_state=7)
x_train = pd.DataFrame(data=x_train, columns=boston.feature_names)
x_test = pd.DataFrame(data=x_test, columns=boston.feature_names)
return x_train, x_test, y_train, y_test, boston.feature_names.tolist()