-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment_monolithic_models_test.py
325 lines (287 loc) · 16.8 KB
/
experiment_monolithic_models_test.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
import os
import tempfile
import typing
import numpy as np
import pandas as pd
from sklearn import ensemble, tree
import const
import poisoning
from . import base, dataset_generator_test as dg_test, experiment_common as common, experiment_common_test as common_test, \
experiment_monolithic_models as exp
def build_quality_metrics(info: base.ExpInfo, multiplier: typing.Optional[float] = None) -> pd.Series:
if multiplier is None:
multiplier = 1
return info.prepend_to(pd.Series(
np.concatenate([np.ones(len(base.METRICS_NAME)) * multiplier, np.zeros(len(base.METRICS_NAME))]),
index=[f'{const.PREFIX_AVG}({col})' for col in base.METRICS_NAME] + [
f'{const.PREFIX_STD}({col})' for col in base.METRICS_NAME]
))
HardcodedResultsType: typing.TypeAlias = typing.Dict[str, typing.Dict[str, typing.Dict[str,
# typing.Union[typing.Dict[str, base.ExpInfo], typing.Dict[common.TestSetType, pd.DataFrame]]
typing.Union[base.ExpInfo, typing.Dict[common.TestSetType, pd.DataFrame]]
]]]
def col_to_drop_for_delta(hardcoded_results: HardcodedResultsType, model_name_: str, test_set_type_: common.TestSetType):
# these columns are not used in delta.
return [col for col in hardcoded_results[model_name_]['0']['model_quality'][test_set_type_].index
if const.PREFIX_STD in col] + list(const.INFO_KEYS_SET)
def build_info_column_for_df(hardcoded_results: HardcodedResultsType,
df_: pd.DataFrame, model_name_: str, test_set_type_: common.TestSetType):
# this function can be called both for delta_self and delta_ref. When we use delta_ref,
# we have an additional column upfront, which is the one with eps=0.
# These columns are always there.
rows = [hardcoded_results[model_name_]['1'], hardcoded_results[model_name_]['2'],
hardcoded_results[model_name_]['3']]
if len(df_) == len(rows) + 1:
rows = [hardcoded_results[model_name_]['0']] + rows
df_[const.KEY_PERC_DATA_POINTS] = [row['model_quality'][test_set_type_][const.KEY_PERC_DATA_POINTS]
for row in rows]
df_[const.KEY_PERC_FEATURES] = [row['model_quality'][test_set_type_][const.KEY_PERC_FEATURES]
for row in rows]
return df_
def build_df_from_delta_diff(hardcoded_results: HardcodedResultsType,
diffs_, model_name_: str, test_set_type_: common.TestSetType):
df_ = pd.DataFrame(diffs_)
df_ = df_.rename(lambda col: f'{model_name_}_{const.PREFIX_DELTA}({col})', axis='columns')
df_ = build_info_column_for_df(df_=df_, model_name_=model_name_, test_set_type_=test_set_type_,
hardcoded_results=hardcoded_results)
return df_
def build_expected_df_model_quality(hardcoded_results: HardcodedResultsType,
model_name_, test_set_type_: common.TestSetType):
# the first row is repeated twice because the first tme refers to the clean results while
# the second time to the first poisoned results
return pd.DataFrame([
# hardcoded_results[model_name_]['0']['model_quality'][test_set_type_].drop(const.KEY_PIPELINE_NAME),
hardcoded_results[model_name_]['0']['model_quality'][test_set_type_].drop(const.KEY_PIPELINE_NAME),
hardcoded_results[model_name_]['1']['model_quality'][test_set_type_].drop(const.KEY_PIPELINE_NAME),
hardcoded_results[model_name_]['2']['model_quality'][test_set_type_].drop(const.KEY_PIPELINE_NAME),
hardcoded_results[model_name_]['3']['model_quality'][test_set_type_].drop(const.KEY_PIPELINE_NAME)]
).rename(
lambda col: f'{model_name_}_{col}' if col not in const.INFO_KEY_LIST else col, axis='columns')
def build_expected_df_delta_self(hardcoded_results: HardcodedResultsType,
model_name_, test_set_type_: common.TestSetType):
# these columns are not used in delta.
to_drop = col_to_drop_for_delta(hardcoded_results=hardcoded_results,
model_name_=model_name_, test_set_type_=test_set_type_)
diffs = [row['model_quality'][test_set_type_].drop(to_drop) -
hardcoded_results[model_name_]['0']['model_quality'][test_set_type_].drop(to_drop)
for row in [hardcoded_results[model_name_]['1'],
hardcoded_results[model_name_]['2'],
hardcoded_results[model_name_]['3']]]
# now, we add the percentages once again.
df = build_df_from_delta_diff(hardcoded_results=hardcoded_results,
diffs_=diffs, model_name_=model_name_, test_set_type_=test_set_type_)
return df
def build_expected_df_delta_ref(hardcoded_results: HardcodedResultsType,
model_name_: str, test_set_type_: common.TestSetType):
# these columns are not used in delta.
# Note that in this delta_ref we just the very same results.
to_drop = col_to_drop_for_delta(hardcoded_results=hardcoded_results,
model_name_=model_name_, test_set_type_=test_set_type_)
diffs = [row['model_quality'][test_set_type_].drop(to_drop) - row['model_quality'][test_set_type_].drop(to_drop)
for row in [hardcoded_results[model_name_]['0'],
hardcoded_results[model_name_]['1'],
hardcoded_results[model_name_]['2'],
hardcoded_results[model_name_]['3']]]
# now, we add the percentages once again.
df = build_df_from_delta_diff(diffs_=diffs, model_name_=model_name_, test_set_type_=test_set_type_,
hardcoded_results=hardcoded_results, )
return df
def _merge(a, b):
expected_df = a.merge(b, on=[const.KEY_PERC_DATA_POINTS, const.KEY_PERC_FEATURES])
expected_df = expected_df.reindex(sorted(expected_df.columns), axis=1)
return expected_df
def _rearrange(to_arrange: dict):
"""
We return a dict: [test_set_type: DataFrame]
:return:
"""
expected_ = {}
# we now merge at model level, but first we have to separate at test set level,
# so we do some arrangements.
values_separated_test_set_type = {t: [] for t in common.TestSetType}
for model_name, values_for_model in to_arrange.items():
for test_set_type, values_for_test_set_type in values_for_model.items():
values_separated_test_set_type[test_set_type].append(values_for_test_set_type)
# now, we can merge
for test_set_type, values_for_test_set_type in values_separated_test_set_type.items():
# now we can merge
expected_[test_set_type] = _merge(*values_for_test_set_type)
return expected_
def test_analyzed_results():
model_name_rf = 'rf'
model_name_svm = 'svm'
info_rf_0 = base.ExpInfo(perc_features=0, perc_points=0, pipeline_name=model_name_rf)
info_rf_1 = base.ExpInfo(perc_features=0, perc_points=10, pipeline_name=model_name_rf)
info_rf_2 = base.ExpInfo(perc_features=0, perc_points=20, pipeline_name=model_name_rf)
info_rf_3 = base.ExpInfo(perc_features=0, perc_points=30, pipeline_name=model_name_rf)
info_svm_0 = base.ExpInfo(perc_features=0, perc_points=0.0, pipeline_name=model_name_svm)
info_svm_1 = base.ExpInfo(perc_features=0, perc_points=10, pipeline_name=model_name_svm)
info_svm_2 = base.ExpInfo(perc_features=0, perc_points=20, pipeline_name=model_name_svm)
info_svm_3 = base.ExpInfo(perc_features=0, perc_points=30, pipeline_name=model_name_svm)
hardcoded_results = {
model_name_rf: {
'0': {
'model_quality': {
common.TestSetType.CLEAN_TEST_SET: build_quality_metrics(info_rf_0, 1),
common.TestSetType.CLEAN_TRAINING_SET: build_quality_metrics(info_rf_0, 1)
},
'info': info_rf_0
},
'1': {
'model_quality': {
common.TestSetType.CLEAN_TEST_SET:build_quality_metrics(info_rf_1, 0.99),
common.TestSetType.CLEAN_TRAINING_SET: build_quality_metrics(info_rf_1, 1)
},
'info': info_rf_1
},
'2': {
'model_quality': {
common.TestSetType.CLEAN_TEST_SET:build_quality_metrics(info_rf_2, 0.95),
common.TestSetType.CLEAN_TRAINING_SET: build_quality_metrics(info_rf_2, 1)
},
'info': info_rf_2
},
'3': {
'model_quality': {
common.TestSetType.CLEAN_TEST_SET:build_quality_metrics(info_rf_3, 0.9),
common.TestSetType.CLEAN_TRAINING_SET: build_quality_metrics(info_rf_3, 1)
},
'info': info_rf_3
}
},
model_name_svm: {
'0': {
'model_quality': {
common.TestSetType.CLEAN_TEST_SET:build_quality_metrics(info_svm_0, 0.97),
common.TestSetType.CLEAN_TRAINING_SET: build_quality_metrics(info_svm_0, 1)
},
'info': info_svm_0
},
'1': {
'model_quality': {
common.TestSetType.CLEAN_TEST_SET:build_quality_metrics(info_svm_1, 0.85),
common.TestSetType.CLEAN_TRAINING_SET: build_quality_metrics(info_svm_1, 1)
},
'info': info_svm_1
},
'2': {
'model_quality': {
common.TestSetType.CLEAN_TEST_SET:build_quality_metrics(info_svm_2, 0.85),
common.TestSetType.CLEAN_TRAINING_SET: build_quality_metrics(info_svm_2, 1)
},
'info': info_svm_2
},
'3': {
'model_quality': {
common.TestSetType.CLEAN_TEST_SET:build_quality_metrics(info_svm_3, 0.8),
common.TestSetType.CLEAN_TRAINING_SET: build_quality_metrics(info_svm_3, 1)
},
'info': info_svm_3
}
}
}
results = [
common.CleanPoisonedOutputPair(
pipeline_name=info_rf_0.pipeline_name,
clean=common.TrainSingleOutputMonolithicWithRep(**hardcoded_results[model_name_rf]['0']),
poisoned=
[
# results for the random forest
common.TrainSingleOutputMonolithicWithRep(**hardcoded_results[model_name_rf]['1'],),
common.TrainSingleOutputMonolithicWithRep(**hardcoded_results[model_name_rf]['2']),
common.TrainSingleOutputMonolithicWithRep(**hardcoded_results[model_name_rf]['3']),
],
),
common.CleanPoisonedOutputPair(
pipeline_name=info_svm_0.pipeline_name,
clean=common.TrainSingleOutputMonolithicWithRep(**hardcoded_results[model_name_svm]['0']),
poisoned=
[
# results for the svm
common.TrainSingleOutputMonolithicWithRep(**hardcoded_results[model_name_svm]['1']),
common.TrainSingleOutputMonolithicWithRep(**hardcoded_results[model_name_svm]['2'],),
common.TrainSingleOutputMonolithicWithRep(**hardcoded_results[model_name_svm]['3']),
])
]
got = exp.AnalyzedResultMonolithicModels.from_results(results_vanilla=results,
results_oracled=results, )
ORACLE, VANILLA = 'oracle', 'vanilla'
df_model_quality = {
source: {m: {t: build_expected_df_model_quality(model_name_=m, test_set_type_=t,
hardcoded_results=hardcoded_results) for t in common.TestSetType}
for m in [model_name_rf, model_name_svm]}
for source in [ORACLE, VANILLA]
}
df_delta_self = {
source: {m: {t: build_expected_df_delta_self(model_name_=m, test_set_type_=t,
hardcoded_results=hardcoded_results) for t in common.TestSetType}
for m in [model_name_rf, model_name_svm]}
for source in [ORACLE, VANILLA]
}
df_delta_ref = {m: {t: build_expected_df_delta_ref(model_name_=m, test_set_type_=t,
hardcoded_results=hardcoded_results) for t in common.TestSetType}
for m in [model_name_rf, model_name_svm]}
expected_model_quality_oracle = _rearrange(df_model_quality[ORACLE])
expected_model_quality_vanilla = _rearrange(df_model_quality[VANILLA])
expected_delta_self_oracle = _rearrange(df_delta_self[ORACLE])
expected_delta_self_vanilla = _rearrange(df_delta_self[VANILLA])
expected_delta_ref = _rearrange(df_delta_ref)
expected = exp.AnalyzedResultMonolithicModels(model_quality_oracle=expected_model_quality_oracle,
model_quality_vanilla=expected_model_quality_vanilla,
delta_self_oracle=expected_delta_self_oracle,
delta_self_vanilla=expected_delta_self_vanilla,
delta_ref_vanilla_oracle=expected_delta_ref)
for t in common.TestSetType:
common_test.check_and_compare_df(got=got.model_quality_oracle[t], expected=expected_model_quality_oracle[t], )
common_test.check_and_compare_df(got=got.model_quality_oracle[t], expected=expected.model_quality_oracle[t])
common_test.check_and_compare_df(got=got.model_quality_vanilla[t], expected=expected.model_quality_vanilla[t])
common_test.check_and_compare_df(got=got.delta_self_oracle[t], expected=expected.delta_self_oracle[t])
common_test.check_and_compare_df(got=got.delta_self_vanilla[t], expected=expected.delta_self_vanilla[t])
common_test.check_and_compare_df(got=got.delta_ref_vanilla_oracle[t], expected=expected.delta_ref_vanilla_oracle[t])
# now, we export results.
with tempfile.TemporaryDirectory() as temp_dir:
export_config = exp.ExportConfigBaseModels(base_directory=temp_dir,
exists_ok=True)
got.export(config=export_config)
expected_file_list = [f'{f}_{t.prefix()}.csv'
for f in [base.FILE_NAME_EXPORT_MONO_VANILLA_QUALITY, base.FILE_NAME_EXPORT_MONO_ORACLED_QUALITY,
base.FILE_NAME_EXPORT_MONO_VANILLA_DELTA_SELF, base.FILE_NAME_EXPORT_MONO_ORACLED_DELTA_SELF,
base.FILE_NAME_EXPORT_MONO_VANILLA_DELTA_REF_AGAINST_MONO_ORACLED]
for t in common.TestSetType]
assert sorted(expected_file_list) == sorted(os.listdir(temp_dir))
def get_exp_from_dg(poisoning_generation_input: poisoning.PoisoningGenerationInput,
base_models, rep):
dg = dg_test.get_dg(poisoning_generation_input=poisoning_generation_input)
exp_ = exp.ExperimentMonolithicModels.from_dataset_generator(
monolithic_models=base_models, dg=dg, repetitions=rep)
return dg, exp_
def test_train_model_on_all_datasets():
estimator = ('dt', tree.DecisionTreeClassifier())
dg, exp_ = get_exp_from_dg(poisoning_generation_input=poisoning.PoisoningGenerationInput(
perc_data_points=[10.0, 15.0],
performer=poisoning.PerformerLabelFlippingMonoDirectional(),
selector=poisoning.SelectorRandom(),
perform_info_kwargs={'from_label': 1, 'to_label': 0},
perform_info_clazz=poisoning.PerformInfoMonoDirectional,
selection_info_kwargs={},
selection_info_clazz=poisoning.SelectionInfoEmpty
), base_models=[estimator], rep=2)
result = exp_.train_model_on_all_datasets(estimator=estimator)
# +1 because we want results on the clean dataset as well.
assert len(result.poisoned) == len(dg)
assert result.pipeline_name == estimator[0]
def test_do():
estimators = [('dt', tree.DecisionTreeClassifier()),
('rf', ensemble.RandomForestClassifier(n_estimators=10))]
dg, exp_ = get_exp_from_dg(poisoning_generation_input=poisoning.PoisoningGenerationInput(
perc_data_points=[10.0, 15.0],
performer=poisoning.PerformerLabelFlippingMonoDirectional(),
selector=poisoning.SelectorRandom(),
perform_info_kwargs={'from_label': 1, 'to_label': 0},
perform_info_clazz=poisoning.PerformInfoMonoDirectional,
selection_info_kwargs={},
selection_info_clazz=poisoning.SelectionInfoEmpty
), base_models=estimators, rep=2)
result = exp_.do()
assert len(result[0]) == len(estimators)
assert len(result[1]) == len(estimators)