-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbdt_inference.py
355 lines (306 loc) · 14.2 KB
/
bdt_inference.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
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
import os
from pathlib import Path
import shutil
import time
import argparse
import yaml
import gc
import numpy as np
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split, StratifiedKFold
from logging import DEBUG, INFO, WARNING, ERROR
from utils import R3KLogger, ROCPlotterKFold, FeatureImportancePlotterKFold, ScorePlotterKFold, \
read_bdt_arrays, save_bdt_arrays, load_external_model, edit_filename, get_branches
def bdt_inference(dataset_params, model_params, output_params, args):
debug_n_evts = 100000 if args.debug else None
# configuration for outputs & logging
os.makedirs(output_params.output_dir, exist_ok=True)
base_filename = output_params.output_dir / 'measurement.root'
lgr = R3KLogger(
output_params.output_dir / output_params.log_file,
verbose=args.verbose,
append=True if args.cached_model else False,
)
with open(args.config, 'r') as f:
cfg_str = f'cfg_path: {args.config}\n{f.read()}'
if args.cached_model:
cached_filepath = output_params.output_dir / 'model.json' if isinstance(args.cached_model,bool) else args.cached_model
assert cached_filepath.is_file(), 'No Valid Model File Found'
# Load XGBoost model if stored in output x
model = XGBClassifier()
model.load_model(cached_filepath)
lgr.log(f'Running Additional Inference with Model {cached_filepath}')
else:
lgr.log(f'Configuration File:\n{cfg_str}')
# load data & mc arrays from input files
X_mc, cutvars_mc, weights_mc = read_bdt_arrays(
dataset_params.rare_file,
dataset_params.tree_name,
model_params.features,
model_params.sample_weights,
model_params.preselection,
(dataset_params.b_mass_branch, dataset_params.ll_mass_branch),
n_evts=debug_n_evts
)
X_data, cutvars_data, weights_data = read_bdt_arrays(
dataset_params.data_file,
dataset_params.tree_name,
model_params.features,
None,
model_params.preselection,
(dataset_params.b_mass_branch, dataset_params.ll_mass_branch),
n_evts=debug_n_evts
)
y_mc = np.ones(X_mc.shape[0])
y_data = np.zeros(X_data.shape[0])
# create array masks for training
mask_sig = np.logical_and(
cutvars_mc[dataset_params.ll_mass_branch] > 1.05,
cutvars_mc[dataset_params.ll_mass_branch] < 2.45
)
mask_bkg_lowq2 = np.logical_and(
cutvars_data[dataset_params.ll_mass_branch] > 1.05,
cutvars_data[dataset_params.ll_mass_branch] < 2.45
)
mask_bkg_sideband = np.logical_or(
np.logical_and(
cutvars_data[dataset_params.b_mass_branch] > 4.8,
cutvars_data[dataset_params.b_mass_branch] < 5.
),
np.logical_and(
cutvars_data[dataset_params.b_mass_branch] > 5.4,
cutvars_data[dataset_params.b_mass_branch] < 5.6
)
)
mask_bkg = np.logical_and(mask_bkg_lowq2, mask_bkg_sideband)
# load bdt from template file
model = load_external_model(model_params.template_file, debug=args.debug)
# set K-fold validation scheme for retraining model
skf = StratifiedKFold(n_splits=3)
# K-fold loop for data measurement
scores = np.empty(X_data.shape[0], dtype=np.float64)
event_idxs = np.empty(X_data.shape[0], dtype=np.int64)
last_idx = -1
# initialize plotters
if args.plot:
roc = ROCPlotterKFold(skf)
feat_imp = FeatureImportancePlotterKFold(skf, features=model_params.feature_labels)
score_dist = ScorePlotterKFold(skf)
lgr.log(f'Training Model for Data Measurement ({skf.get_n_splits()}-fold x-val)', just_print=True)
start = time.perf_counter()
for fold, (train_data, test_data) in enumerate(skf.split(X_data, y_data)):
# mask all training events (low-q2 + mass sidebands for data)
train_mask = np.concatenate((mask_sig, mask_bkg[train_data]))
# split events into training and validation sets from mc and data fold
split = train_test_split(
np.concatenate((X_mc, X_data[train_data]))[train_mask],
np.concatenate((y_mc, y_data[train_data]))[train_mask],
np.concatenate((weights_mc, weights_data[train_data]))[train_mask],
test_size=0.05, random_state=271996
)
X_train, X_val, y_train, y_val, weights_train, weights_val = split
eval_set = ((X_train, y_train), (X_val, y_val))
# fit model
model.fit(
X_train,
y_train,
sample_weight=weights_train,
eval_set=eval_set,
verbose=2 if args.verbose else 0
)
# predict bdt scores on data events designated for testing in fold
fill_idxs = slice(last_idx+1,last_idx+test_data.size+1)
scores[fill_idxs] = model.predict_proba(X_data[test_data])[:,1].astype(np.float64)
event_idxs[fill_idxs] = test_data
last_idx = last_idx+test_data.size
# add data to to plots for fold
if args.plot:
roc.add_fold(model, X_val, y_val)
feat_imp.add_fold(model)
score_dist.add_fold(model, X_train, y_train, X_val, y_val)
lgr.log(f'Finished fold {fold+1} of {skf.get_n_splits()}', just_print=True)
del split, train_mask, X_train, X_val, y_train, y_val, weights_train, \
weights_val, eval_set, train_data, test_data
gc.collect()
lgr.log(f'Elapsed Training/Inference Time on Data = {round(time.perf_counter() - start)}s', just_print=True)
# save plots
if args.plot:
plot_dir = output_params.output_dir / 'plots'
plot_dir.mkdir(exist_ok=True)
roc.save(plot_dir / 'roc.pdf', inset=True)
feat_imp.save(plot_dir / 'feature_importance.pdf')
score_dist.save(plot_dir / 'scores.pdf')
# save data measurement file
output_filename = edit_filename(base_filename, suffix='data')
output_branch_names = get_branches(output_params, ['common','data'])
save_bdt_arrays(
dataset_params.data_file,
dataset_params.tree_name,
output_filename,
dataset_params.tree_name,
output_branch_names,
output_params.score_branch,
scores,
event_idxs,
preselection=model_params.preselection,
n_evts=debug_n_evts,
)
lgr.log(f'Data Measurement File: {output_filename}')
del scores, event_idxs
gc.collect()
# # TODO down-sample background events
# downsample = False
# if downsample:
# pass
# K-fold loop for rare mc measurement
scores = np.empty(X_mc.shape[0], dtype=np.float64)
event_idxs = np.empty(X_mc.shape[0], dtype=np.int64)
last_idx = -1
lgr.log(f'Training Model for MC Measurement ({skf.get_n_splits()}-fold x-val)', just_print=True)
start = time.perf_counter()
for fold, (train_mc, test_mc) in enumerate(skf.split(X_mc, y_mc)):
# mask all training events (low-q2 + mass sidebands for data)
train_mask = np.concatenate((mask_sig[train_mc], mask_bkg))
# split events into training and validation sets from mc fold and data
split = train_test_split(
np.concatenate((X_mc[train_mc], X_data))[train_mask],
np.concatenate((y_mc[train_mc], y_data))[train_mask],
np.concatenate((weights_mc[train_mc], weights_data))[train_mask],
test_size=0.05, random_state=271996
)
X_train, X_val, y_train, y_val, weights_train, weights_val = split
eval_set = ((X_train, y_train), (X_val, y_val))
# fit model
model.fit(
X_train,
y_train,
sample_weight=weights_train,
eval_set=eval_set,
verbose=2 if args.verbose else 0
)
# predict bdt scores on data events designated for testing in fold
fill_idxs = slice(last_idx+1,last_idx+test_mc.size+1)
scores[fill_idxs] = model.predict_proba(X_mc[test_mc])[:,1].astype(np.float64)
event_idxs[fill_idxs] = test_mc
last_idx = last_idx+test_mc.size
lgr.log(f'Finished fold {fold+1} of {skf.get_n_splits()}', just_print=True)
del split, train_mask, X_train, X_val, y_train, y_val, weights_train, \
weights_val, eval_set, train_mc, test_mc
gc.collect()
lgr.log(f'Elapsed Training/Inference Time = {round(time.perf_counter() - start)}s', just_print=True)
# save rare mc measurement file
output_filename = edit_filename(base_filename, suffix='rare')
output_branch_names = get_branches(output_params, ['common','mc'])
save_bdt_arrays(
dataset_params.rare_file,
dataset_params.tree_name,
output_filename,
dataset_params.tree_name,
output_branch_names,
output_params.score_branch,
scores,
event_idxs,
preselection=model_params.preselection,
n_evts=debug_n_evts,
)
lgr.log(f'MC (Rare) Measurement File: {output_filename}')
del scores, event_idxs
gc.collect()
# Delete arrays needed for training, save memory
del X_mc, cutvars_mc, weights_mc, X_data, cutvars_data, weights_data, \
y_mc, y_data, mask_sig, mask_bkg_lowq2, mask_bkg_sideband
gc.collect()
# Save XGBoost model
model.save_model(output_params.output_dir / 'model.json')
# predict bdt scores for other data files in config
if dataset_params.other_data_files:
for data_name, data_file in dataset_params.other_data_files.items():
# load arrays from input file
X_data_extra, _, _ = read_bdt_arrays(
data_file,
dataset_params.tree_name,
model_params.features,
None,
model_params.preselection,
(dataset_params.b_mass_branch, dataset_params.ll_mass_branch),
n_evts=debug_n_evts,
)
# bdt inference
scores = model.predict_proba(X_data_extra)[:,1].astype(np.float64)
# save jpsi mc measurement file
output_filename = edit_filename(base_filename, suffix=data_name)
output_branch_names = get_branches(output_params, ['common','data'])
save_bdt_arrays(
data_file,
dataset_params.tree_name,
output_filename,
dataset_params.tree_name,
output_branch_names,
output_params.score_branch,
scores,
preselection=model_params.preselection,
n_evts=debug_n_evts,
)
lgr.log(f'Data ({data_name}) Measurement File: {output_filename}')
del X_data_extra, scores
gc.collect()
# predict bdt scores for other mc files in config
if dataset_params.other_mc_files:
for mc_name, mc_file in dataset_params.other_mc_files.items():
X_mc_extra, _, _ = read_bdt_arrays(
mc_file,
dataset_params.tree_name,
model_params.features,
model_params.sample_weights,
model_params.preselection,
(dataset_params.b_mass_branch, dataset_params.ll_mass_branch),
n_evts=debug_n_evts,
)
# bdt inference
scores = model.predict_proba(X_mc_extra)[:,1].astype(np.float64)
# save jpsi mc measurement file
output_filename = edit_filename(base_filename, suffix=mc_name)
output_branch_names = get_branches(output_params, ['common','mc'])
save_bdt_arrays(
mc_file,
dataset_params.tree_name,
output_filename,
dataset_params.tree_name,
output_branch_names,
output_params.score_branch,
scores,
preselection=model_params.preselection,
n_evts=debug_n_evts,
)
lgr.log(f'MC ({mc_name}) Measurement File: {output_filename}')
del X_mc_extra, scores
gc.collect()
def main(args):
with open(args.config, 'r') as f:
cfg = yaml.safe_load(f)
dataset_params = argparse.Namespace(**cfg['datasets'])
model_params = argparse.Namespace(**cfg['model'])
output_params = argparse.Namespace(**cfg['output'])
if args.debug:
args.verbose = True
output_params.output_dir = Path('outputs/tmp')
else:
output_params.output_dir = Path(output_params.output_dir)
bdt_inference(dataset_params, model_params ,output_params, args)
if args.debug:
if output_params.output_dir.exists() and output_params.output_dir.is_dir():
shutil.rmtree(output_params.output_dir)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', dest='config',
type=str, required=True, help='BDT configuration file (.yml)')
parser.add_argument('-np', '--no_plot', dest='plot',
action='store_false', help='dont add loss plot to output directory')
parser.add_argument('-v', '--verbose', dest='verbose',
action='store_true', help='print parameters and training progress to stdout')
parser.add_argument('-cm', '--cached_model', dest='cached_model', nargs='?',
const=True, default=False, help='only run prediction on extra samples with cached model, no retraining')
parser.add_argument('-db', '--debug', dest='debug',
action='store_true', help='debug mode: run in verbose mode with scaled-down model & datasets')
args, _ = parser.parse_known_args()
main(args)