-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
297 lines (255 loc) · 13.3 KB
/
utils.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
# import contextlib
# import joblib
import numpy as np
import os
import json
import pickle
import torch.nn as nn
import re
import pandas as pd
from openxai.ML_Models.LR.model import LogisticRegression
import openxai.ML_Models.ANN.MLP as model_MLP
import datetime
# Make a numpy array of ints from 1 to N where each row is shuffled without replacement
def shuffled_indices(num_samples, num_feats):
return np.array([np.random.choice(num_feats, num_feats, replace=False) for _ in range(num_samples)])
def DefineModel(num_feats, model_name, dim_per_layer=None, activation_per_layer=None):
if 'ann' in model_name:
dim_per_layer = [num_feats] + dim_per_layer
model = model_MLP.MLP(dim_per_layer, activation_per_layer)
elif model_name == 'lr':
dim_per_layer = [num_feats] + dim_per_layer
model = LogisticRegression(dim_per_layer[0], dim_per_layer[1])
return model
def get_model_architecture(model_name):
dim_per_layer_per_MLP = {'ann_s': [16, 2],
'ann_m': [32, 16, 2],
'ann_l': [64, 32, 16, 2],
'ann_xl': [256, 128, 64, 32, 16, 2],
'lr': [2]
} # dimension for each layer for each network to train, ignoring input layer size
activation_per_layer_per_MLP = {'ann_s': [nn.ReLU(), None],
'ann_m': [nn.ReLU(), nn.ReLU(), None],
'ann_l': [nn.ReLU(), nn.ReLU(), nn.ReLU(), None],
'ann_xl': [nn.ReLU(), nn.ReLU(), nn.ReLU(), nn.ReLU(), nn.ReLU(), None],
'lr': [None]
} # ignore input layer size
return dim_per_layer_per_MLP[model_name], activation_per_layer_per_MLP[model_name]
def get_model_names(model_name, dataset_name, base_model_dir):
model_names = ['lr', 'ann_s', 'ann_m', 'ann_l', 'ann_xl', 'text_ann']
model_dirs = [base_model_dir + model_name.upper() + '/' for model_name in model_names]
model_dirs = dict(zip(model_names, model_dirs))
if 'ClassWeighted_scale_minmax' in base_model_dir:
compas_model_names = [
'20230629_0056_2__compas_lr_0.001_auc_roc_0.82.pt',
'20230629_0056_16_2__compas_ann_s_0.001_auc_roc_0.83.pt',
'20230629_0056_32_16_2__compas_ann_m_0.001_auc_roc_0.83.pt',
'20230629_0057_64_32_16_2__compas_ann_l_0.001_auc_roc_0.83.pt',
'20230629_0057_256_128_64_32_16_2__compas_ann_xl_0.001_auc_roc_0.82.pt',
'none_text_ann']
credit_model_names = [
'20230629_0101_2__credit_lr_0.001_auc_roc_0.81.pt',
'20230629_0105_16_2__credit_ann_s_0.001_auc_roc_0.81.pt',
'20230629_0109_32_16_2__credit_ann_m_0.001_auc_roc_0.81.pt',
'20230629_0111_64_32_16_2__credit_ann_l_0.001_auc_roc_0.81.pt',
'20230629_0113_256_128_64_32_16_2__credit_ann_xl_0.001_auc_roc_0.81.pt',
'none_text_ann']
adult_model_names = [
'20230629_0039_2__adult_lr_0.001_auc_roc_0.89.pt',
'20230629_0041_16_2__adult_ann_s_0.001_auc_roc_0.90.pt',
'20230629_0044_32_16_2__adult_ann_m_0.001_auc_roc_0.90.pt',
'20230629_0048_64_32_16_2__adult_ann_l_0.001_auc_roc_0.90.pt',
'20230629_0051_256_128_64_32_16_2__adult_ann_xl_0.001_auc_roc_0.90.pt',
'none_text_ann']
blood_model_names = [
'20230907_1208_2__blood_lr_0.001_auc_roc_0.66.pt',
'20230907_1208_16_2__blood_ann_s_0.001_auc_roc_0.73.pt',
'20230907_1208_32_16_2__blood_ann_m_0.001_auc_roc_0.74.pt',
'20230907_1208_64_32_16_2__blood_ann_l_0.001_auc_roc_0.75.pt',
'20230907_1208_256_128_64_32_16_2__blood_ann_xl_0.001_auc_roc_0.76.pt',
'non_text_ann']
heloc_model_names = [
'20230629_0058_2__heloc_lr_0.001_auc_roc_0.80.pt',
'',
'',
'20230629_0059_64_32_16_2__heloc_ann_l_0.001_auc_roc_0.81.pt',
'',
'none_text_ann']
model_file_names_data = {
'compas': dict(zip(model_names, compas_model_names)),
'adult': dict(zip(model_names, adult_model_names)),
'credit': dict(zip(model_names, credit_model_names)),
'blood': dict(zip(model_names, blood_model_names)),
'heloc': dict(zip(model_names, heloc_model_names))
}
elif 'ClassWeighted_scale_standard' in base_model_dir:
compas_model_names = ['20230713_1728_2__compas_lr_0.001_auc_roc_0.84.pt']
credit_model_names = ['20230713_1730_2__credit_lr_0.001_auc_roc_0.81.pt']
adult_model_names = ['20230713_1728_2__adult_lr_0.001_auc_roc_0.90.pt']
beauty_model_names = ['20240326_1629_2__beauty_lr_0.001_auc_roc_0.93.pt']
model_file_names_data = {
'compas': dict(zip(model_names, compas_model_names)),
'adult': dict(zip(model_names, adult_model_names)),
'credit': dict(zip(model_names, credit_model_names))
}
elif 'ClassWeighted' in base_model_dir:
beauty_model_names = ['20240326_1629_2__beauty_lr_0.001_auc_roc_0.93.pt',
'none_ann_s',
'none_ann_m',
'20240328_1159_64_32_16_2__beauty_ann_l_0.001_auc_roc_0.94.pt',
'none_ann_xl',
'none_text_ann']
amazon_1000_model_names = ['none_lr',
'none_ann_s',
'none_ann_m',
'none_ann_l',
'none_ann_xl',
'20240611_0417_None__amazon_1000_text_ann_0.001_auc_roc_0.85.pt']
yelp_model_names = ['none_lr',
'none_ann_s',
'none_ann_m',
'none_ann_l',
'none_ann_xl',
'20240611_0423_None__yelp_text_ann_0.001_auc_roc_0.73.pt']
imdb_model_names = ['none_lr',
'none_ann_s',
'none_ann_m',
'none_ann_l',
'none_ann_xl',
'20240613_0044_None__imdb_text_ann_0.001_auc_roc_0.81.pt']
model_file_names_data = {
'beauty': dict(zip(model_names, beauty_model_names)),
'amazon_1000': dict(zip(model_names, amazon_1000_model_names)),
'yelp': dict(zip(model_names, yelp_model_names)),
'imdb': dict(zip(model_names, imdb_model_names))
}
else:
raise NotImplementedError(f'Not implemented for {base_model_dir}')
model_dir = model_dirs[model_name]
model_file_name = model_file_names_data[dataset_name][model_name]
return model_dir, model_file_name
def append_k(ks, metrics):
#metrics is a list of strings denoting the metrics
#ks is a list of ints denoting the k values to append to each metric
# append -1, -3, and -5 (for each k in ks) to each metric name
appended_metrics = []
for k in ks:
temp = []
for metric in metrics:
temp.append(metric + '_' + str(k))
appended_metrics.append(temp)
#flatten the list
metrics = [item for sublist in appended_metrics for item in sublist]
return metrics
def _saveObjAsPkl(output_dir, file_name, data_to_save, extra_str=''):
# Save to .pkl
fpth = os.path.join(output_dir, file_name + extra_str + '.pkl')
file = open(fpth, "wb")
pickle.dump(data_to_save, file)
file.close()
# recursively take a dict and write it out to a file with proper indentation and newlines for readability
# but if it's long make sure it doesnt write out "..." for the middle of the dict
def _writeDictToFile(f, d, indent=0):
# f.write('\n')
# for key, value in d.items():
# f.write('\t' * indent + str(key) + ':')
# if isinstance(value, dict):
# _writeDictToFile(f, value, indent+1)
# f.write('\n')
# else:
# f.write('\t' + str(value) + '\n')
f.write('\n')
for key, value in d.items():
f.write('\t' * indent + str(key) + ':')
if isinstance(value, dict):
_writeDictToFile(f, value, indent+1)
f.write('\n')
elif isinstance(value, pd.DataFrame):
f.write('\n' + value.to_string() + '\n')
else:
f.write('\t' + str(value) + '\n')
def saveParameters(output_dir, file_name, params, extra_str=''):
# Save parameter dict items to a .txt file and .pkl
fpth = os.path.join(output_dir, file_name+extra_str+'.txt')
paramTxt = open(fpth, 'w')
_writeDictToFile(paramTxt, params)
paramTxt.close()
# Save to .pkl
_saveObjAsPkl(output_dir, file_name, params, extra_str)
return
def _load_config(config_file):
"""Load config from file"""
with open(config_file, 'r') as f:
config = json.load(f)
return config
def loadOpenAPIKeyFromFile(file_name):
# Load OpenAI API key from file and remove newline character
with open(file_name, 'r') as f:
API_KEY = f.readline().replace('\n', '')
return API_KEY
def mergeListOfStringIntoSingleString(LLM_topks):
# merge list of strings into a single string. (for post-processing analysis)
concated_LLM_topks = []
for l, LLM_topk in enumerate(LLM_topks):
concated_str = ''
for k, feat in enumerate(LLM_topk):
concated_str += feat
concated_LLM_topks.append(concated_str)
return concated_LLM_topks
def getExperimentID():
date_info = datetime.datetime.now()
testID = '%d%02d%02d_%02d%02d%02d' % (
date_info.year, date_info.month, date_info.day, date_info.hour, date_info.minute, date_info.second)
return testID
def getTotalCosts(folders):
total_costs = []
for folder in folders:
total_costs.append(pd.read_csv(folder + 'total_costs.csv')['total_cost'].values)
return np.array(total_costs)[:, 0]
def SaveExperimentInfo(config, folder_name_exp_id, n_shot, LLM, model_name, data_name, LLM_topks,
eval_min_idx, eval_max_idx, hidden_ys=None):
# (Pickle) Save all replies of top-k features for each test instance individually
output_dir = config['output_dir'] + folder_name_exp_id + '/'
output_file_name = 'n_shot_' + str(n_shot) + '_' + data_name + '_' + model_name + '_LLM_topK.pkl'
with open(output_dir + output_file_name, 'wb') as file:
pickle.dump(LLM_topks, file)
if hidden_ys is not None:
with open(output_dir + 'hidden_ys.pkl', 'wb') as file:
pickle.dump(hidden_ys, file)
# Save config file as json to output directory
saveParameters(output_dir, 'pipeline_config', config)
with open(f'{output_dir}/pipeline_config.json', 'w') as f:
json.dump(config, f)
# Write out all the final replies for each test sample to a single .txt file
concated_replies = mergeListOfStringIntoSingleString(LLM_topks)
file_name = LLM + '_' + model_name.upper() + '_' + data_name + '_Replies.txt'
paramTxt = open(output_dir + file_name, 'w')
for i in range(eval_min_idx, eval_max_idx):
paramTxt.write('Sample ' + str(i) + ':\t' + str(list(concated_replies[i])) + '\n')
paramTxt.close()
def get_k_words():
return ['', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve',
'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen',
'twenty', 'twenty-one', 'twenty-two', 'twenty-three', 'twenty-four', 'twenty-five', 'twenty-six',
'twenty-seven', 'twenty-eight', 'twenty-nine', 'thirty', 'thirty-one', 'thirty-two', 'thirty-three',
'thirty-four', 'thirty-five', 'thirty-six', 'thirty-seven', 'thirty-eight', 'thirty-nine', 'forty',
'forty-one', 'forty-two', 'forty-three', 'forty-four', 'forty-five', 'forty-six', 'forty-seven',
'forty-eight', 'forty-nine', 'fifty', 'fifty-one', 'fifty-two', 'fifty-three', 'fifty-four',
'fifty-five', 'fifty-six', 'fifty-seven', 'fifty-eight', 'fifty-nine', 'sixty', 'sixty-one',
'sixty-two', 'sixty-three', 'sixty-four', 'sixty-five', 'sixty-six', 'sixty-seven', 'sixty-eight',
'sixty-nine', 'seventy', 'seventy-one', 'seventy-two', 'seventy-three', 'seventy-four', 'seventy-five',
'seventy-six', 'seventy-seven', 'seventy-eight', 'seventy-nine', 'eighty', 'eighty-one', 'eighty-two',
'eighty-three', 'eighty-four', 'eighty-five', 'eighty-six', 'eighty-seven', 'eighty-eight',
'eighty-nine', 'ninety', 'ninety-one', 'ninety-two', 'ninety-three', 'ninety-four', 'ninety-five',
'ninety-six', 'ninety-seven', 'ninety-eight', 'ninety-nine', 'one hundred']
def convert_to_int(samples_text):
# Find all decimal numbers in the text
decimals = re.findall(r'(\d+\.\d+)', samples_text)
# Find the maximum number of decimal places
max_decimal_places = max([len(dec.split('.')[1]) for dec in decimals])
# Calculate multiplier factor
factor = 10 ** max_decimal_places
for dec in decimals:
# Replace the decimal number with its integer equivalent in the text
samples_text = samples_text.replace(dec, str(int(float(dec) * factor)), 1) # Replace only the first occurrence
return samples_text