ins-pricing 0.4.4__py3-none-any.whl → 0.5.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ins_pricing/README.md +74 -56
- ins_pricing/__init__.py +142 -90
- ins_pricing/cli/BayesOpt_entry.py +52 -50
- ins_pricing/cli/BayesOpt_incremental.py +832 -898
- ins_pricing/cli/Explain_Run.py +31 -23
- ins_pricing/cli/Explain_entry.py +532 -579
- ins_pricing/cli/Pricing_Run.py +31 -23
- ins_pricing/cli/bayesopt_entry_runner.py +1440 -1438
- ins_pricing/cli/utils/cli_common.py +256 -256
- ins_pricing/cli/utils/cli_config.py +375 -375
- ins_pricing/cli/utils/import_resolver.py +382 -365
- ins_pricing/cli/utils/notebook_utils.py +340 -340
- ins_pricing/cli/watchdog_run.py +209 -201
- ins_pricing/frontend/README.md +573 -419
- ins_pricing/frontend/__init__.py +10 -10
- ins_pricing/frontend/config_builder.py +1 -0
- ins_pricing/frontend/example_workflows.py +1 -1
- ins_pricing/governance/__init__.py +20 -20
- ins_pricing/governance/release.py +159 -159
- ins_pricing/modelling/README.md +67 -0
- ins_pricing/modelling/__init__.py +147 -92
- ins_pricing/modelling/bayesopt/README.md +59 -0
- ins_pricing/modelling/{core/bayesopt → bayesopt}/__init__.py +64 -102
- ins_pricing/modelling/{core/bayesopt → bayesopt}/config_preprocess.py +562 -550
- ins_pricing/modelling/{core/bayesopt → bayesopt}/core.py +965 -962
- ins_pricing/modelling/{core/bayesopt → bayesopt}/model_explain_mixin.py +296 -296
- ins_pricing/modelling/{core/bayesopt → bayesopt}/model_plotting_mixin.py +482 -548
- ins_pricing/modelling/{core/bayesopt → bayesopt}/models/__init__.py +27 -27
- ins_pricing/modelling/{core/bayesopt → bayesopt}/models/model_ft_trainer.py +915 -913
- ins_pricing/modelling/{core/bayesopt → bayesopt}/models/model_gnn.py +788 -785
- ins_pricing/modelling/{core/bayesopt → bayesopt}/models/model_resn.py +448 -446
- ins_pricing/modelling/bayesopt/trainers/__init__.py +19 -0
- ins_pricing/modelling/{core/bayesopt → bayesopt}/trainers/trainer_base.py +1308 -1308
- ins_pricing/modelling/{core/bayesopt → bayesopt}/trainers/trainer_ft.py +3 -3
- ins_pricing/modelling/{core/bayesopt → bayesopt}/trainers/trainer_glm.py +197 -198
- ins_pricing/modelling/{core/bayesopt → bayesopt}/trainers/trainer_gnn.py +344 -344
- ins_pricing/modelling/{core/bayesopt → bayesopt}/trainers/trainer_resn.py +283 -283
- ins_pricing/modelling/{core/bayesopt → bayesopt}/trainers/trainer_xgb.py +346 -347
- ins_pricing/modelling/bayesopt/utils/__init__.py +67 -0
- ins_pricing/modelling/bayesopt/utils/constants.py +21 -0
- ins_pricing/modelling/bayesopt/utils/io_utils.py +7 -0
- ins_pricing/modelling/bayesopt/utils/losses.py +27 -0
- ins_pricing/modelling/bayesopt/utils/metrics_and_devices.py +17 -0
- ins_pricing/modelling/{core/bayesopt → bayesopt}/utils/torch_trainer_mixin.py +623 -623
- ins_pricing/modelling/{core/evaluation.py → evaluation.py} +113 -104
- ins_pricing/modelling/explain/__init__.py +55 -55
- ins_pricing/modelling/explain/metrics.py +27 -174
- ins_pricing/modelling/explain/permutation.py +237 -237
- ins_pricing/modelling/plotting/__init__.py +40 -36
- ins_pricing/modelling/plotting/compat.py +228 -0
- ins_pricing/modelling/plotting/curves.py +572 -572
- ins_pricing/modelling/plotting/diagnostics.py +163 -163
- ins_pricing/modelling/plotting/geo.py +362 -362
- ins_pricing/modelling/plotting/importance.py +121 -121
- ins_pricing/pricing/__init__.py +27 -27
- ins_pricing/production/__init__.py +35 -25
- ins_pricing/production/{predict.py → inference.py} +140 -57
- ins_pricing/production/monitoring.py +8 -21
- ins_pricing/reporting/__init__.py +11 -11
- ins_pricing/setup.py +1 -1
- ins_pricing/tests/production/test_inference.py +90 -0
- ins_pricing/utils/__init__.py +116 -83
- ins_pricing/utils/device.py +255 -255
- ins_pricing/utils/features.py +53 -0
- ins_pricing/utils/io.py +72 -0
- ins_pricing/{modelling/core/bayesopt/utils → utils}/losses.py +125 -129
- ins_pricing/utils/metrics.py +158 -24
- ins_pricing/utils/numerics.py +76 -0
- ins_pricing/utils/paths.py +9 -1
- {ins_pricing-0.4.4.dist-info → ins_pricing-0.5.0.dist-info}/METADATA +55 -35
- ins_pricing-0.5.0.dist-info/RECORD +131 -0
- ins_pricing/CHANGELOG.md +0 -272
- ins_pricing/RELEASE_NOTES_0.2.8.md +0 -344
- ins_pricing/docs/LOSS_FUNCTIONS.md +0 -78
- ins_pricing/docs/modelling/BayesOpt_USAGE.md +0 -945
- ins_pricing/docs/modelling/README.md +0 -34
- ins_pricing/frontend/QUICKSTART.md +0 -152
- ins_pricing/modelling/core/BayesOpt.py +0 -146
- ins_pricing/modelling/core/__init__.py +0 -1
- ins_pricing/modelling/core/bayesopt/PHASE2_REFACTORING_SUMMARY.md +0 -449
- ins_pricing/modelling/core/bayesopt/PHASE3_REFACTORING_SUMMARY.md +0 -406
- ins_pricing/modelling/core/bayesopt/REFACTORING_SUMMARY.md +0 -247
- ins_pricing/modelling/core/bayesopt/trainers/__init__.py +0 -19
- ins_pricing/modelling/core/bayesopt/utils/__init__.py +0 -86
- ins_pricing/modelling/core/bayesopt/utils/constants.py +0 -183
- ins_pricing/modelling/core/bayesopt/utils/io_utils.py +0 -126
- ins_pricing/modelling/core/bayesopt/utils/metrics_and_devices.py +0 -555
- ins_pricing/modelling/core/bayesopt/utils.py +0 -105
- ins_pricing/modelling/core/bayesopt/utils_backup.py +0 -1503
- ins_pricing/tests/production/test_predict.py +0 -233
- ins_pricing-0.4.4.dist-info/RECORD +0 -137
- /ins_pricing/modelling/{core/bayesopt → bayesopt}/config_components.py +0 -0
- /ins_pricing/modelling/{core/bayesopt → bayesopt}/models/model_ft_components.py +0 -0
- /ins_pricing/modelling/{core/bayesopt → bayesopt}/utils/distributed_utils.py +0 -0
- {ins_pricing-0.4.4.dist-info → ins_pricing-0.5.0.dist-info}/WHEEL +0 -0
- {ins_pricing-0.4.4.dist-info → ins_pricing-0.5.0.dist-info}/top_level.txt +0 -0
|
@@ -1,904 +1,838 @@
|
|
|
1
|
-
"""Incremental training harness built on top of ``ins_pricing.bayesopt
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
--
|
|
13
|
-
--
|
|
14
|
-
--
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
"""Incremental training harness built on top of ``ins_pricing.bayesopt``.
|
|
2
|
+
|
|
3
|
+
This utility lets you append new observations to an existing dataset,
|
|
4
|
+
reuse previously tuned hyperparameters and retrain a subset of models
|
|
5
|
+
without re-running the full Optuna search. It can operate on a directory
|
|
6
|
+
of per-model incremental CSVs or a single incremental file when updating
|
|
7
|
+
one dataset.
|
|
8
|
+
|
|
9
|
+
Example:
|
|
10
|
+
python ins_pricing/cli/BayesOpt_incremental.py \
|
|
11
|
+
--config-json examples/config_incremental_template.json \
|
|
12
|
+
--incremental-dir ./incremental_batches \
|
|
13
|
+
--merge-keys policy_id vehicle_id \
|
|
14
|
+
--model-keys glm xgb resn --plot-curves
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
20
19
|
from pathlib import Path
|
|
20
|
+
import importlib.util
|
|
21
21
|
import sys
|
|
22
22
|
|
|
23
23
|
if __package__ in {None, ""}:
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
import
|
|
30
|
-
|
|
31
|
-
from
|
|
32
|
-
from
|
|
33
|
-
|
|
24
|
+
if importlib.util.find_spec("ins_pricing") is None:
|
|
25
|
+
repo_root = Path(__file__).resolve().parents[2]
|
|
26
|
+
if str(repo_root) not in sys.path:
|
|
27
|
+
sys.path.insert(0, str(repo_root))
|
|
28
|
+
|
|
29
|
+
import argparse
|
|
30
|
+
import json
|
|
31
|
+
from dataclasses import asdict
|
|
32
|
+
from datetime import datetime
|
|
33
|
+
from typing import Any, Dict, List, Optional, Sequence, Tuple
|
|
34
|
+
|
|
34
35
|
import pandas as pd
|
|
35
36
|
|
|
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
|
-
default=None,
|
|
148
|
-
help="
|
|
149
|
-
)
|
|
150
|
-
parser.add_argument(
|
|
151
|
-
"--
|
|
152
|
-
|
|
153
|
-
default=
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
"
|
|
159
|
-
|
|
160
|
-
default=
|
|
161
|
-
help="
|
|
162
|
-
)
|
|
163
|
-
parser.add_argument(
|
|
164
|
-
"--
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
help="
|
|
168
|
-
)
|
|
169
|
-
parser.add_argument(
|
|
170
|
-
"--
|
|
171
|
-
|
|
172
|
-
help="
|
|
173
|
-
)
|
|
174
|
-
parser.add_argument(
|
|
175
|
-
"--
|
|
176
|
-
|
|
177
|
-
default=None,
|
|
178
|
-
help="
|
|
179
|
-
)
|
|
180
|
-
parser.add_argument(
|
|
181
|
-
"--
|
|
182
|
-
|
|
183
|
-
default=
|
|
184
|
-
help="
|
|
185
|
-
)
|
|
186
|
-
parser.add_argument(
|
|
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
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
]
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
self.
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
self.
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
self.
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
if
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
if
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
if
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
"
|
|
714
|
-
"
|
|
715
|
-
"
|
|
716
|
-
"
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
model.
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
"
|
|
787
|
-
"
|
|
788
|
-
"
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
"
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
new_rows = 0 if inc_df is None else len(inc_df)
|
|
839
|
-
_log(f"{model_name}: {len(base_df)} base rows, {new_rows} incremental rows.")
|
|
840
|
-
merged_df = self._merge_frames(base_df, inc_df)
|
|
841
|
-
merged_df.fillna(0, inplace=True)
|
|
842
|
-
|
|
843
|
-
if self.args.update_base_data and not self.args.dry_run:
|
|
844
|
-
self._write_dataset(merged_df, base_path, "update_base_data")
|
|
845
|
-
if self.args.persist_merged_dir and not self.args.dry_run:
|
|
846
|
-
suffix = base_path.suffix or ".csv"
|
|
847
|
-
dest = Path(self.args.persist_merged_dir).resolve() / f"{model_name}{suffix}"
|
|
848
|
-
self._write_dataset(merged_df, dest, "persist_merged_dir")
|
|
849
|
-
|
|
850
|
-
if not self._should_train(new_rows):
|
|
851
|
-
_log(f"{model_name}: below min_new_rows ({self.args.min_new_rows}); skipping retrain.")
|
|
852
|
-
self.summary_records.append({
|
|
853
|
-
"model_name": model_name,
|
|
854
|
-
"status": "skipped_no_incremental",
|
|
855
|
-
"new_rows": new_rows,
|
|
856
|
-
"total_rows": len(merged_df),
|
|
857
|
-
})
|
|
858
|
-
return 0
|
|
859
|
-
|
|
860
|
-
try:
|
|
861
|
-
train_result = self._train_single_model(model_name, merged_df, new_rows, inc_path)
|
|
862
|
-
except Exception as exc:
|
|
863
|
-
_log(f"Training failed for {model_name}: {exc}")
|
|
864
|
-
self.summary_records.append({
|
|
865
|
-
"model_name": model_name,
|
|
866
|
-
"status": "failed",
|
|
867
|
-
"error": str(exc),
|
|
868
|
-
"new_rows": new_rows,
|
|
869
|
-
"total_rows": len(merged_df),
|
|
870
|
-
})
|
|
871
|
-
return 0
|
|
872
|
-
|
|
873
|
-
executed = train_result["executed_keys"]
|
|
874
|
-
param_sources = train_result["param_sources"]
|
|
875
|
-
model = train_result["model"]
|
|
876
|
-
status = "dry_run" if self.args.dry_run else "trained"
|
|
877
|
-
|
|
878
|
-
summary = {
|
|
879
|
-
"model_name": model_name,
|
|
880
|
-
"status": status,
|
|
881
|
-
"trained_models": executed,
|
|
882
|
-
"param_sources": param_sources,
|
|
883
|
-
"new_rows": new_rows,
|
|
884
|
-
"total_rows": len(merged_df),
|
|
885
|
-
"incremental_path": str(inc_path) if inc_path else None,
|
|
886
|
-
}
|
|
887
|
-
self.summary_records.append(summary)
|
|
888
|
-
|
|
889
|
-
if not self.args.dry_run and self.plot_requested and executed:
|
|
890
|
-
_plot_curves_for_model(model, executed, self.cfg)
|
|
891
|
-
|
|
892
|
-
return 1 if executed else 0
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
def main() -> None:
|
|
896
|
-
if configure_run_logging:
|
|
897
|
-
configure_run_logging(prefix="bayesopt_incremental")
|
|
898
|
-
args = _parse_args()
|
|
899
|
-
runner = IncrementalUpdateRunner(args)
|
|
900
|
-
runner.process()
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
if __name__ == "__main__":
|
|
904
|
-
main()
|
|
37
|
+
from ins_pricing.cli.utils.import_resolver import resolve_imports, setup_sys_path
|
|
38
|
+
|
|
39
|
+
setup_sys_path()
|
|
40
|
+
_imports = resolve_imports()
|
|
41
|
+
|
|
42
|
+
ropt = _imports.bayesopt
|
|
43
|
+
if ropt is None: # pragma: no cover
|
|
44
|
+
raise ImportError("Failed to resolve ins_pricing.bayesopt for incremental CLI.")
|
|
45
|
+
|
|
46
|
+
PLOT_MODEL_LABELS = _imports.PLOT_MODEL_LABELS
|
|
47
|
+
PYTORCH_TRAINERS = _imports.PYTORCH_TRAINERS
|
|
48
|
+
build_model_names = _imports.build_model_names
|
|
49
|
+
dedupe_preserve_order = _imports.dedupe_preserve_order
|
|
50
|
+
load_dataset = _imports.load_dataset
|
|
51
|
+
parse_model_pairs = _imports.parse_model_pairs
|
|
52
|
+
resolve_data_path = _imports.resolve_data_path
|
|
53
|
+
resolve_path = _imports.resolve_path
|
|
54
|
+
split_train_test = _imports.split_train_test
|
|
55
|
+
|
|
56
|
+
add_config_json_arg = _imports.add_config_json_arg
|
|
57
|
+
resolve_and_load_config = _imports.resolve_and_load_config
|
|
58
|
+
resolve_data_config = _imports.resolve_data_config
|
|
59
|
+
resolve_split_config = _imports.resolve_split_config
|
|
60
|
+
resolve_runtime_config = _imports.resolve_runtime_config
|
|
61
|
+
resolve_output_dirs = _imports.resolve_output_dirs
|
|
62
|
+
|
|
63
|
+
configure_run_logging = _imports.configure_run_logging
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _log(message: str) -> None:
|
|
67
|
+
print(f"[Incremental] {message}")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _parse_args() -> argparse.Namespace:
|
|
71
|
+
parser = argparse.ArgumentParser(
|
|
72
|
+
description="Incrementally retrain BayesOpt models using new batches of data."
|
|
73
|
+
)
|
|
74
|
+
add_config_json_arg(
|
|
75
|
+
parser,
|
|
76
|
+
help_text="Path to the JSON config that cli/BayesOpt_entry.py uses.",
|
|
77
|
+
)
|
|
78
|
+
parser.add_argument(
|
|
79
|
+
"--model-names",
|
|
80
|
+
nargs="+",
|
|
81
|
+
default=None,
|
|
82
|
+
help="Optional subset of dataset names to update (defaults to model_list/model_categories Cartesian product)."
|
|
83
|
+
)
|
|
84
|
+
parser.add_argument(
|
|
85
|
+
"--model-keys",
|
|
86
|
+
nargs="+",
|
|
87
|
+
default=["glm", "xgb", "resn", "ft"],
|
|
88
|
+
choices=["glm", "xgb", "resn", "ft", "gnn", "all"],
|
|
89
|
+
help="Which trainers to run for each dataset."
|
|
90
|
+
)
|
|
91
|
+
parser.add_argument(
|
|
92
|
+
"--incremental-dir",
|
|
93
|
+
type=Path,
|
|
94
|
+
default=None,
|
|
95
|
+
help="Directory containing <model_name> incremental CSVs."
|
|
96
|
+
)
|
|
97
|
+
parser.add_argument(
|
|
98
|
+
"--incremental-file",
|
|
99
|
+
type=Path,
|
|
100
|
+
default=None,
|
|
101
|
+
help="Single incremental CSV (requires --model-names with exactly one entry)."
|
|
102
|
+
)
|
|
103
|
+
parser.add_argument(
|
|
104
|
+
"--incremental-template",
|
|
105
|
+
default="{model_name}_incremental.csv",
|
|
106
|
+
help="Filename template when --incremental-dir is provided."
|
|
107
|
+
)
|
|
108
|
+
parser.add_argument(
|
|
109
|
+
"--merge-keys",
|
|
110
|
+
nargs="+",
|
|
111
|
+
default=None,
|
|
112
|
+
help="Column(s) used to drop duplicate rows after merging base and incremental data."
|
|
113
|
+
)
|
|
114
|
+
parser.add_argument(
|
|
115
|
+
"--dedupe-keep",
|
|
116
|
+
choices=["first", "last"],
|
|
117
|
+
default="last",
|
|
118
|
+
help="How pandas.drop_duplicates resolves conflicts on merge keys."
|
|
119
|
+
)
|
|
120
|
+
parser.add_argument(
|
|
121
|
+
"--timestamp-col",
|
|
122
|
+
default=None,
|
|
123
|
+
help="Optional column used to sort rows before deduplication."
|
|
124
|
+
)
|
|
125
|
+
parser.add_argument(
|
|
126
|
+
"--timestamp-descending",
|
|
127
|
+
action="store_true",
|
|
128
|
+
help="Sort timestamp column in descending order before deduplication."
|
|
129
|
+
)
|
|
130
|
+
parser.add_argument(
|
|
131
|
+
"--min-new-rows",
|
|
132
|
+
type=int,
|
|
133
|
+
default=1,
|
|
134
|
+
help="Skip training if fewer new rows than this arrive (unless --train-without-incremental)."
|
|
135
|
+
)
|
|
136
|
+
parser.add_argument(
|
|
137
|
+
"--train-without-incremental",
|
|
138
|
+
action="store_true",
|
|
139
|
+
help="Always retrain even when no incremental file is present."
|
|
140
|
+
)
|
|
141
|
+
parser.add_argument(
|
|
142
|
+
"--strict-incremental",
|
|
143
|
+
action="store_true",
|
|
144
|
+
help="Raise an error when a dataset is missing its incremental CSV instead of skipping it."
|
|
145
|
+
)
|
|
146
|
+
parser.add_argument(
|
|
147
|
+
"--tag-new-column",
|
|
148
|
+
default=None,
|
|
149
|
+
help="If set, store 1 for incremental rows and 0 for historical rows in this column."
|
|
150
|
+
)
|
|
151
|
+
parser.add_argument(
|
|
152
|
+
"--max-evals",
|
|
153
|
+
type=int,
|
|
154
|
+
default=25,
|
|
155
|
+
help="Optuna trial count when retuning is required."
|
|
156
|
+
)
|
|
157
|
+
parser.add_argument(
|
|
158
|
+
"--retune-missing",
|
|
159
|
+
dest="retune_missing",
|
|
160
|
+
action="store_true",
|
|
161
|
+
default=True,
|
|
162
|
+
help="Retune models whose best-params CSV is unavailable (default)."
|
|
163
|
+
)
|
|
164
|
+
parser.add_argument(
|
|
165
|
+
"--skip-retune-missing",
|
|
166
|
+
dest="retune_missing",
|
|
167
|
+
action="store_false",
|
|
168
|
+
help="Do not retune when best params are missing; such models are skipped."
|
|
169
|
+
)
|
|
170
|
+
parser.add_argument(
|
|
171
|
+
"--force-retune",
|
|
172
|
+
action="store_true",
|
|
173
|
+
help="Run Optuna tuning even if historical best params exist."
|
|
174
|
+
)
|
|
175
|
+
parser.add_argument(
|
|
176
|
+
"--prop-test",
|
|
177
|
+
type=float,
|
|
178
|
+
default=None,
|
|
179
|
+
help="Override the test split proportion defined in the config file."
|
|
180
|
+
)
|
|
181
|
+
parser.add_argument(
|
|
182
|
+
"--rand-seed",
|
|
183
|
+
type=int,
|
|
184
|
+
default=None,
|
|
185
|
+
help="Override the random seed defined in the config."
|
|
186
|
+
)
|
|
187
|
+
parser.add_argument(
|
|
188
|
+
"--epochs",
|
|
189
|
+
type=int,
|
|
190
|
+
default=None,
|
|
191
|
+
help="Override the epoch count from the config."
|
|
192
|
+
)
|
|
193
|
+
parser.add_argument(
|
|
194
|
+
"--output-dir",
|
|
195
|
+
type=Path,
|
|
196
|
+
default=None,
|
|
197
|
+
help="Override the BayesOpt output root (models/results/plots)."
|
|
198
|
+
)
|
|
199
|
+
parser.add_argument(
|
|
200
|
+
"--update-base-data",
|
|
201
|
+
action="store_true",
|
|
202
|
+
help="Overwrite the base CSVs with the merged dataset after a successful update."
|
|
203
|
+
)
|
|
204
|
+
parser.add_argument(
|
|
205
|
+
"--persist-merged-dir",
|
|
206
|
+
type=Path,
|
|
207
|
+
default=None,
|
|
208
|
+
help="Optional directory to store the merged dataset snapshots."
|
|
209
|
+
)
|
|
210
|
+
parser.add_argument(
|
|
211
|
+
"--summary-json",
|
|
212
|
+
type=Path,
|
|
213
|
+
default=None,
|
|
214
|
+
help="Write a JSON summary of processed datasets to this path."
|
|
215
|
+
)
|
|
216
|
+
parser.add_argument(
|
|
217
|
+
"--plot-curves",
|
|
218
|
+
action="store_true",
|
|
219
|
+
help="Run one-way/lift plots after training (config plot settings also apply)."
|
|
220
|
+
)
|
|
221
|
+
parser.add_argument(
|
|
222
|
+
"--dry-run",
|
|
223
|
+
action="store_true",
|
|
224
|
+
help="Merge and report counts but skip training, saving and plotting."
|
|
225
|
+
)
|
|
226
|
+
args = parser.parse_args()
|
|
227
|
+
|
|
228
|
+
if args.incremental_file and args.incremental_dir:
|
|
229
|
+
parser.error("Use either --incremental-dir or --incremental-file, not both.")
|
|
230
|
+
if args.incremental_file and args.model_names and len(args.model_names) != 1:
|
|
231
|
+
parser.error("--incremental-file can only be used when updating exactly one model.")
|
|
232
|
+
if (not args.incremental_dir and not args.incremental_file) and not args.train_without_incremental:
|
|
233
|
+
parser.error(
|
|
234
|
+
"Provide --incremental-dir/--incremental-file or enable --train-without-incremental."
|
|
235
|
+
)
|
|
236
|
+
return args
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def _plot_curves_for_model(model: ropt.BayesOptModel, trained: List[str], cfg: Dict[str, Any]) -> None:
|
|
240
|
+
plot_cfg = cfg.get("plot", {})
|
|
241
|
+
legacy_flags = {
|
|
242
|
+
"glm": cfg.get("plot_lift_glm", False),
|
|
243
|
+
"xgb": cfg.get("plot_lift_xgb", False),
|
|
244
|
+
"resn": cfg.get("plot_lift_resn", False),
|
|
245
|
+
"ft": cfg.get("plot_lift_ft", False),
|
|
246
|
+
}
|
|
247
|
+
plot_enabled = plot_cfg.get("enable", any(legacy_flags.values()))
|
|
248
|
+
if not plot_enabled:
|
|
249
|
+
return
|
|
250
|
+
|
|
251
|
+
n_bins = int(plot_cfg.get("n_bins", 10))
|
|
252
|
+
oneway_enabled = plot_cfg.get("oneway", True)
|
|
253
|
+
available = dedupe_preserve_order([k for k in trained if k in PLOT_MODEL_LABELS])
|
|
254
|
+
|
|
255
|
+
lift_models = plot_cfg.get("lift_models")
|
|
256
|
+
if lift_models is None:
|
|
257
|
+
lift_models = [m for m, flag in legacy_flags.items() if flag]
|
|
258
|
+
if not lift_models:
|
|
259
|
+
lift_models = available
|
|
260
|
+
lift_models = dedupe_preserve_order([m for m in lift_models if m in available])
|
|
261
|
+
|
|
262
|
+
if oneway_enabled:
|
|
263
|
+
oneway_pred = bool(plot_cfg.get("oneway_pred", False))
|
|
264
|
+
oneway_pred_models = plot_cfg.get("oneway_pred_models")
|
|
265
|
+
pred_plotted = False
|
|
266
|
+
if oneway_pred:
|
|
267
|
+
if oneway_pred_models is None:
|
|
268
|
+
oneway_pred_models = lift_models or available
|
|
269
|
+
oneway_pred_models = dedupe_preserve_order(
|
|
270
|
+
[m for m in oneway_pred_models if m in available]
|
|
271
|
+
)
|
|
272
|
+
for model_key in oneway_pred_models:
|
|
273
|
+
label, pred_nme = PLOT_MODEL_LABELS[model_key]
|
|
274
|
+
if pred_nme not in model.train_data.columns:
|
|
275
|
+
print(
|
|
276
|
+
f"[Oneway] Missing prediction column '{pred_nme}'; skip.",
|
|
277
|
+
flush=True,
|
|
278
|
+
)
|
|
279
|
+
continue
|
|
280
|
+
model.plot_oneway(
|
|
281
|
+
n_bins=n_bins,
|
|
282
|
+
pred_col=pred_nme,
|
|
283
|
+
pred_label=label,
|
|
284
|
+
plot_subdir="oneway/post",
|
|
285
|
+
)
|
|
286
|
+
pred_plotted = True
|
|
287
|
+
if not oneway_pred or not pred_plotted:
|
|
288
|
+
model.plot_oneway(n_bins=n_bins, plot_subdir="oneway/post")
|
|
289
|
+
if not available:
|
|
290
|
+
return
|
|
291
|
+
|
|
292
|
+
for key in lift_models:
|
|
293
|
+
label, pred_nme = PLOT_MODEL_LABELS[key]
|
|
294
|
+
model.plot_lift(model_label=label, pred_nme=pred_nme, n_bins=n_bins)
|
|
295
|
+
|
|
296
|
+
if not plot_cfg.get("double_lift", True) or len(available) < 2:
|
|
297
|
+
return
|
|
298
|
+
|
|
299
|
+
raw_pairs = plot_cfg.get("double_lift_pairs")
|
|
300
|
+
if raw_pairs:
|
|
301
|
+
pairs = [
|
|
302
|
+
(a, b)
|
|
303
|
+
for a, b in parse_model_pairs(raw_pairs)
|
|
304
|
+
if a in available and b in available and a != b
|
|
305
|
+
]
|
|
306
|
+
else:
|
|
307
|
+
pairs = [(a, b) for i, a in enumerate(available) for b in available[i + 1 :]]
|
|
308
|
+
for first, second in pairs:
|
|
309
|
+
model.plot_dlift([first, second], n_bins=n_bins)
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
def _coerce_scalar(value: Any) -> Any:
|
|
313
|
+
if isinstance(value, str):
|
|
314
|
+
lowered = value.strip().lower()
|
|
315
|
+
if lowered in {"", "none", "nan"}:
|
|
316
|
+
return None
|
|
317
|
+
if lowered in {"true", "false"}:
|
|
318
|
+
return lowered == "true"
|
|
319
|
+
return value
|
|
320
|
+
if hasattr(value, "item"):
|
|
321
|
+
try:
|
|
322
|
+
return value.item()
|
|
323
|
+
except Exception:
|
|
324
|
+
return value
|
|
325
|
+
return value
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def _infer_format_from_path(path: Path) -> str:
|
|
329
|
+
suffix = path.suffix.lower()
|
|
330
|
+
if suffix in {".parquet", ".pq"}:
|
|
331
|
+
return "parquet"
|
|
332
|
+
if suffix in {".feather", ".ft"}:
|
|
333
|
+
return "feather"
|
|
334
|
+
return "csv"
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def _load_best_params(model: ropt.BayesOptModel, trainer, silent: bool = False) -> Optional[Dict[str, Any]]:
|
|
338
|
+
label = trainer.label.lower()
|
|
339
|
+
result_dir = Path(model.output_manager.result_dir)
|
|
340
|
+
path = result_dir / f"{model.model_nme}_bestparams_{label}.csv"
|
|
341
|
+
if not path.exists():
|
|
342
|
+
if not silent:
|
|
343
|
+
_log(f"No historical params found for {model.model_nme}/{label} at {path}.")
|
|
344
|
+
return None
|
|
345
|
+
try:
|
|
346
|
+
params_raw = ropt.IOUtils.load_params_file(str(path))
|
|
347
|
+
except Exception:
|
|
348
|
+
return None
|
|
349
|
+
return {
|
|
350
|
+
key: _coerce_scalar(val)
|
|
351
|
+
for key, val in (params_raw or {}).items()
|
|
352
|
+
if not pd.isna(val)
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def _to_serializable(obj: Any) -> Any:
|
|
357
|
+
if isinstance(obj, dict):
|
|
358
|
+
return {k: _to_serializable(v) for k, v in obj.items()}
|
|
359
|
+
if isinstance(obj, list):
|
|
360
|
+
return [_to_serializable(v) for v in obj]
|
|
361
|
+
if hasattr(obj, "item"):
|
|
362
|
+
try:
|
|
363
|
+
return obj.item()
|
|
364
|
+
except Exception:
|
|
365
|
+
return str(obj)
|
|
366
|
+
return obj
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
class IncrementalUpdateRunner:
|
|
370
|
+
def __init__(self, args: argparse.Namespace) -> None:
|
|
371
|
+
self.args = args
|
|
372
|
+
script_dir = Path(__file__).resolve().parents[1]
|
|
373
|
+
self.config_path, self.cfg = resolve_and_load_config(
|
|
374
|
+
args.config_json,
|
|
375
|
+
script_dir,
|
|
376
|
+
required_keys=[
|
|
377
|
+
"data_dir",
|
|
378
|
+
"model_list",
|
|
379
|
+
"model_categories",
|
|
380
|
+
"target",
|
|
381
|
+
"weight",
|
|
382
|
+
"feature_list",
|
|
383
|
+
"categorical_features",
|
|
384
|
+
],
|
|
385
|
+
)
|
|
386
|
+
data_dir, data_format, data_path_template, dtype_map = resolve_data_config(
|
|
387
|
+
self.cfg,
|
|
388
|
+
self.config_path,
|
|
389
|
+
create_data_dir=True,
|
|
390
|
+
)
|
|
391
|
+
self.data_dir = data_dir
|
|
392
|
+
self.data_format = data_format
|
|
393
|
+
self.data_path_template = data_path_template
|
|
394
|
+
self.dtype_map = dtype_map
|
|
395
|
+
split_cfg = resolve_split_config(self.cfg)
|
|
396
|
+
runtime_cfg = resolve_runtime_config(self.cfg)
|
|
397
|
+
output_cfg = resolve_output_dirs(
|
|
398
|
+
self.cfg,
|
|
399
|
+
self.config_path,
|
|
400
|
+
output_override=args.output_dir,
|
|
401
|
+
)
|
|
402
|
+
self.runtime_cfg = runtime_cfg
|
|
403
|
+
self.prop_test = args.prop_test if args.prop_test is not None else split_cfg["prop_test"]
|
|
404
|
+
self.rand_seed = args.rand_seed if args.rand_seed is not None else self.cfg.get("rand_seed", 13)
|
|
405
|
+
self.epochs = args.epochs if args.epochs is not None else self.cfg.get("epochs", 50)
|
|
406
|
+
self.split_strategy = split_cfg["split_strategy"]
|
|
407
|
+
self.split_group_col = split_cfg["split_group_col"]
|
|
408
|
+
self.split_time_col = split_cfg["split_time_col"]
|
|
409
|
+
self.split_time_ascending = split_cfg["split_time_ascending"]
|
|
410
|
+
self.cv_strategy = split_cfg["cv_strategy"]
|
|
411
|
+
self.cv_group_col = split_cfg["cv_group_col"]
|
|
412
|
+
self.cv_time_col = split_cfg["cv_time_col"]
|
|
413
|
+
self.cv_time_ascending = split_cfg["cv_time_ascending"]
|
|
414
|
+
self.cv_splits = split_cfg["cv_splits"]
|
|
415
|
+
self.ft_oof_folds = split_cfg["ft_oof_folds"]
|
|
416
|
+
self.ft_oof_strategy = split_cfg["ft_oof_strategy"]
|
|
417
|
+
self.ft_oof_shuffle = split_cfg["ft_oof_shuffle"]
|
|
418
|
+
self.save_preprocess = runtime_cfg["save_preprocess"]
|
|
419
|
+
self.preprocess_artifact_path = runtime_cfg["preprocess_artifact_path"]
|
|
420
|
+
self.bo_sample_limit = runtime_cfg["bo_sample_limit"]
|
|
421
|
+
self.cache_predictions = runtime_cfg["cache_predictions"]
|
|
422
|
+
self.prediction_cache_dir = runtime_cfg["prediction_cache_dir"]
|
|
423
|
+
self.prediction_cache_format = runtime_cfg["prediction_cache_format"]
|
|
424
|
+
self.plot_path_style = runtime_cfg["plot_path_style"]
|
|
425
|
+
self.xgb_max_depth_max = runtime_cfg["xgb_max_depth_max"]
|
|
426
|
+
self.xgb_n_estimators_max = runtime_cfg["xgb_n_estimators_max"]
|
|
427
|
+
self.optuna_storage = runtime_cfg["optuna_storage"]
|
|
428
|
+
self.optuna_study_prefix = runtime_cfg["optuna_study_prefix"]
|
|
429
|
+
self.best_params_files = runtime_cfg["best_params_files"]
|
|
430
|
+
self.reuse_best_params = runtime_cfg["reuse_best_params"]
|
|
431
|
+
self.plot_requested = bool(args.plot_curves or self.cfg.get("plot_curves", False))
|
|
432
|
+
self.model_names = self._resolve_model_names(args.model_names)
|
|
433
|
+
self.merge_keys = list(args.merge_keys or [])
|
|
434
|
+
self.timestamp_col = args.timestamp_col
|
|
435
|
+
self.timestamp_ascending = not args.timestamp_descending
|
|
436
|
+
self.output_root = output_cfg["output_dir"]
|
|
437
|
+
|
|
438
|
+
self.incremental_dir = None
|
|
439
|
+
if args.incremental_dir is not None:
|
|
440
|
+
self.incremental_dir = args.incremental_dir
|
|
441
|
+
if not self.incremental_dir.is_absolute():
|
|
442
|
+
self.incremental_dir = (self.config_path.parent / self.incremental_dir).resolve()
|
|
443
|
+
else:
|
|
444
|
+
self.incremental_dir = self.incremental_dir.resolve()
|
|
445
|
+
self.incremental_file = None
|
|
446
|
+
if args.incremental_file is not None:
|
|
447
|
+
self.incremental_file = args.incremental_file
|
|
448
|
+
if not self.incremental_file.is_absolute():
|
|
449
|
+
self.incremental_file = (self.config_path.parent / self.incremental_file).resolve()
|
|
450
|
+
else:
|
|
451
|
+
self.incremental_file = self.incremental_file.resolve()
|
|
452
|
+
self.summary_records: List[Dict[str, Any]] = []
|
|
453
|
+
self.binary_resp = self.cfg.get("binary_resp_nme") or self.cfg.get("binary_target")
|
|
454
|
+
|
|
455
|
+
if self.incremental_file and len(self.model_names) != 1:
|
|
456
|
+
raise ValueError("--incremental-file can only be used when exactly one model name is targeted.")
|
|
457
|
+
|
|
458
|
+
def _resolve_model_names(self, override: Optional[Sequence[str]]) -> List[str]:
|
|
459
|
+
if override:
|
|
460
|
+
return dedupe_preserve_order([str(item) for item in override])
|
|
461
|
+
prefixes = self.cfg["model_list"]
|
|
462
|
+
suffixes = self.cfg["model_categories"]
|
|
463
|
+
return build_model_names(prefixes, suffixes)
|
|
464
|
+
|
|
465
|
+
def _load_incremental_df(self, model_name: str) -> Tuple[Optional[pd.DataFrame], Optional[Path]]:
|
|
466
|
+
path: Optional[Path] = None
|
|
467
|
+
if self.incremental_file:
|
|
468
|
+
path = self.incremental_file
|
|
469
|
+
elif self.incremental_dir:
|
|
470
|
+
rel = self.args.incremental_template.format(model_name=model_name)
|
|
471
|
+
path = (self.incremental_dir / rel).resolve()
|
|
472
|
+
if not path or not path.exists():
|
|
473
|
+
return None, None
|
|
474
|
+
try:
|
|
475
|
+
df = load_dataset(
|
|
476
|
+
path,
|
|
477
|
+
data_format="auto",
|
|
478
|
+
dtype_map=self.dtype_map,
|
|
479
|
+
low_memory=False,
|
|
480
|
+
)
|
|
481
|
+
except pd.errors.EmptyDataError:
|
|
482
|
+
_log(f"Incremental file {path} is empty; treating as no-op.")
|
|
483
|
+
return None, path
|
|
484
|
+
except Exception as exc:
|
|
485
|
+
_log(f"Failed to load incremental file {path}: {exc}")
|
|
486
|
+
return None, path
|
|
487
|
+
return df, path
|
|
488
|
+
|
|
489
|
+
def _merge_frames(self, base_df: pd.DataFrame, inc_df: Optional[pd.DataFrame]) -> pd.DataFrame:
|
|
490
|
+
if inc_df is None or inc_df.empty:
|
|
491
|
+
merged = base_df.copy(deep=True)
|
|
492
|
+
return merged.reset_index(drop=True)
|
|
493
|
+
frames = []
|
|
494
|
+
tag = self.args.tag_new_column
|
|
495
|
+
if tag:
|
|
496
|
+
base_part = base_df.copy(deep=True)
|
|
497
|
+
base_part[tag] = 0
|
|
498
|
+
inc_part = inc_df.copy(deep=True)
|
|
499
|
+
inc_part[tag] = 1
|
|
500
|
+
frames = [base_part, inc_part]
|
|
501
|
+
else:
|
|
502
|
+
frames = [base_df, inc_df]
|
|
503
|
+
merged = pd.concat(frames, ignore_index=True, sort=False)
|
|
504
|
+
if self.timestamp_col and self.timestamp_col in merged.columns:
|
|
505
|
+
merged = merged.sort_values(
|
|
506
|
+
self.timestamp_col,
|
|
507
|
+
ascending=self.timestamp_ascending,
|
|
508
|
+
kind="mergesort",
|
|
509
|
+
)
|
|
510
|
+
if self.merge_keys:
|
|
511
|
+
missing = [col for col in self.merge_keys if col not in merged.columns]
|
|
512
|
+
if missing:
|
|
513
|
+
raise KeyError(f"Merge keys {missing} not found in merged frame for {self.merge_keys}.")
|
|
514
|
+
merged = merged.drop_duplicates(subset=self.merge_keys, keep=self.args.dedupe_keep)
|
|
515
|
+
return merged.reset_index(drop=True)
|
|
516
|
+
|
|
517
|
+
def _should_train(self, new_rows: int) -> bool:
|
|
518
|
+
if self.args.train_without_incremental:
|
|
519
|
+
return True
|
|
520
|
+
min_needed = max(0, self.args.min_new_rows)
|
|
521
|
+
return new_rows >= min_needed
|
|
522
|
+
|
|
523
|
+
def _write_dataset(self, df: pd.DataFrame, dest: Path, reason: str) -> None:
|
|
524
|
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
525
|
+
fmt = str(self.data_format or "csv").lower()
|
|
526
|
+
if fmt == "auto":
|
|
527
|
+
fmt = _infer_format_from_path(dest)
|
|
528
|
+
if fmt == "parquet":
|
|
529
|
+
df.to_parquet(dest, index=False)
|
|
530
|
+
elif fmt == "feather":
|
|
531
|
+
df.reset_index(drop=True).to_feather(dest)
|
|
532
|
+
else:
|
|
533
|
+
df.to_csv(dest, index=False)
|
|
534
|
+
_log(f"Wrote {len(df)} rows to {dest} ({reason}).")
|
|
535
|
+
|
|
536
|
+
def _prepare_splits(self, merged: pd.DataFrame) -> Tuple[pd.DataFrame, pd.DataFrame]:
|
|
537
|
+
if not 0 < self.prop_test < 1:
|
|
538
|
+
raise ValueError(f"prop_test must fall in (0, 1); got {self.prop_test}.")
|
|
539
|
+
if len(merged) < 2:
|
|
540
|
+
raise ValueError("Need at least two rows to form a train/test split.")
|
|
541
|
+
train_df, test_df = split_train_test(
|
|
542
|
+
merged,
|
|
543
|
+
holdout_ratio=self.prop_test,
|
|
544
|
+
strategy=self.split_strategy,
|
|
545
|
+
group_col=self.split_group_col,
|
|
546
|
+
time_col=self.split_time_col,
|
|
547
|
+
time_ascending=self.split_time_ascending,
|
|
548
|
+
rand_seed=self.rand_seed,
|
|
549
|
+
reset_index_mode="always",
|
|
550
|
+
ratio_label="prop_test",
|
|
551
|
+
validate_ratio=False,
|
|
552
|
+
)
|
|
553
|
+
return train_df, test_df
|
|
554
|
+
|
|
555
|
+
def _requested_model_keys(self, trainer_map: Dict[str, Any]) -> List[str]:
|
|
556
|
+
requested = self.args.model_keys
|
|
557
|
+
if "all" in requested:
|
|
558
|
+
requested = ["glm", "xgb", "resn", "ft", "gnn"]
|
|
559
|
+
requested = dedupe_preserve_order(requested)
|
|
560
|
+
missing = [key for key in requested if key not in trainer_map]
|
|
561
|
+
for key in missing:
|
|
562
|
+
_log(f"Trainer '{key}' is not available for this context and will be skipped.")
|
|
563
|
+
return [key for key in requested if key in trainer_map]
|
|
564
|
+
|
|
565
|
+
def _train_single_model(
|
|
566
|
+
self,
|
|
567
|
+
model_name: str,
|
|
568
|
+
merged: pd.DataFrame,
|
|
569
|
+
new_rows: int,
|
|
570
|
+
incremental_path: Optional[Path],
|
|
571
|
+
) -> Dict[str, Any]:
|
|
572
|
+
merged = merged.copy(deep=True)
|
|
573
|
+
merged.fillna(0, inplace=True)
|
|
574
|
+
train_df, test_df = self._prepare_splits(merged)
|
|
575
|
+
model = ropt.BayesOptModel(
|
|
576
|
+
train_df,
|
|
577
|
+
test_df,
|
|
578
|
+
model_name,
|
|
579
|
+
self.cfg["target"],
|
|
580
|
+
self.cfg["weight"],
|
|
581
|
+
self.cfg["feature_list"],
|
|
582
|
+
task_type=self.cfg.get("task_type", "regression"),
|
|
583
|
+
binary_resp_nme=self.binary_resp,
|
|
584
|
+
cate_list=self.cfg.get("categorical_features"),
|
|
585
|
+
prop_test=self.prop_test,
|
|
586
|
+
rand_seed=self.rand_seed,
|
|
587
|
+
epochs=self.epochs,
|
|
588
|
+
use_gpu=bool(self.cfg.get("use_gpu", True)),
|
|
589
|
+
use_resn_data_parallel=self.cfg.get("use_resn_data_parallel", False),
|
|
590
|
+
use_ft_data_parallel=self.cfg.get("use_ft_data_parallel", True),
|
|
591
|
+
use_gnn_data_parallel=self.cfg.get("use_gnn_data_parallel", False),
|
|
592
|
+
use_resn_ddp=self.cfg.get("use_resn_ddp", False),
|
|
593
|
+
use_ft_ddp=self.cfg.get("use_ft_ddp", False),
|
|
594
|
+
use_gnn_ddp=self.cfg.get("use_gnn_ddp", False),
|
|
595
|
+
output_dir=str(self.output_root) if self.output_root else None,
|
|
596
|
+
xgb_max_depth_max=self.xgb_max_depth_max,
|
|
597
|
+
xgb_n_estimators_max=self.xgb_n_estimators_max,
|
|
598
|
+
resn_weight_decay=self.cfg.get("resn_weight_decay"),
|
|
599
|
+
final_ensemble=bool(self.cfg.get("final_ensemble", False)),
|
|
600
|
+
final_ensemble_k=int(self.cfg.get("final_ensemble_k", 3)),
|
|
601
|
+
final_refit=bool(self.cfg.get("final_refit", True)),
|
|
602
|
+
optuna_storage=self.optuna_storage,
|
|
603
|
+
optuna_study_prefix=self.optuna_study_prefix,
|
|
604
|
+
best_params_files=self.best_params_files,
|
|
605
|
+
reuse_best_params=self.reuse_best_params,
|
|
606
|
+
gnn_use_approx_knn=self.cfg.get("gnn_use_approx_knn", True),
|
|
607
|
+
gnn_approx_knn_threshold=self.cfg.get("gnn_approx_knn_threshold", 50000),
|
|
608
|
+
gnn_graph_cache=self.cfg.get("gnn_graph_cache"),
|
|
609
|
+
gnn_max_gpu_knn_nodes=self.cfg.get("gnn_max_gpu_knn_nodes", 200000),
|
|
610
|
+
gnn_knn_gpu_mem_ratio=self.cfg.get("gnn_knn_gpu_mem_ratio", 0.9),
|
|
611
|
+
gnn_knn_gpu_mem_overhead=self.cfg.get("gnn_knn_gpu_mem_overhead", 2.0),
|
|
612
|
+
region_province_col=self.cfg.get("region_province_col"),
|
|
613
|
+
region_city_col=self.cfg.get("region_city_col"),
|
|
614
|
+
region_effect_alpha=self.cfg.get("region_effect_alpha"),
|
|
615
|
+
geo_feature_nmes=self.cfg.get("geo_feature_nmes"),
|
|
616
|
+
geo_token_hidden_dim=self.cfg.get("geo_token_hidden_dim"),
|
|
617
|
+
geo_token_layers=self.cfg.get("geo_token_layers"),
|
|
618
|
+
geo_token_dropout=self.cfg.get("geo_token_dropout"),
|
|
619
|
+
geo_token_k_neighbors=self.cfg.get("geo_token_k_neighbors"),
|
|
620
|
+
geo_token_learning_rate=self.cfg.get("geo_token_learning_rate"),
|
|
621
|
+
geo_token_epochs=self.cfg.get("geo_token_epochs"),
|
|
622
|
+
ft_role=str(self.cfg.get("ft_role", "model")),
|
|
623
|
+
ft_feature_prefix=str(self.cfg.get("ft_feature_prefix", "ft_emb")),
|
|
624
|
+
ft_num_numeric_tokens=self.cfg.get("ft_num_numeric_tokens"),
|
|
625
|
+
infer_categorical_max_unique=int(self.cfg.get("infer_categorical_max_unique", 50)),
|
|
626
|
+
infer_categorical_max_ratio=float(self.cfg.get("infer_categorical_max_ratio", 0.05)),
|
|
627
|
+
cv_strategy=self.cv_strategy or self.split_strategy,
|
|
628
|
+
cv_group_col=self.cv_group_col or self.split_group_col,
|
|
629
|
+
cv_time_col=self.cv_time_col or self.split_time_col,
|
|
630
|
+
cv_time_ascending=self.cv_time_ascending,
|
|
631
|
+
cv_splits=self.cv_splits,
|
|
632
|
+
ft_oof_folds=self.ft_oof_folds,
|
|
633
|
+
ft_oof_strategy=self.ft_oof_strategy,
|
|
634
|
+
ft_oof_shuffle=self.ft_oof_shuffle,
|
|
635
|
+
save_preprocess=self.save_preprocess,
|
|
636
|
+
preprocess_artifact_path=self.preprocess_artifact_path,
|
|
637
|
+
plot_path_style=self.plot_path_style,
|
|
638
|
+
bo_sample_limit=self.bo_sample_limit,
|
|
639
|
+
cache_predictions=self.cache_predictions,
|
|
640
|
+
prediction_cache_dir=self.prediction_cache_dir,
|
|
641
|
+
prediction_cache_format=self.prediction_cache_format,
|
|
642
|
+
)
|
|
643
|
+
|
|
644
|
+
if self.plot_requested and not self.args.dry_run:
|
|
645
|
+
plot_cfg = self.cfg.get("plot", {})
|
|
646
|
+
legacy_flags = {
|
|
647
|
+
"glm": self.cfg.get("plot_lift_glm", False),
|
|
648
|
+
"xgb": self.cfg.get("plot_lift_xgb", False),
|
|
649
|
+
"resn": self.cfg.get("plot_lift_resn", False),
|
|
650
|
+
"ft": self.cfg.get("plot_lift_ft", False),
|
|
651
|
+
}
|
|
652
|
+
plot_enabled = plot_cfg.get("enable", any(legacy_flags.values()))
|
|
653
|
+
if plot_enabled and plot_cfg.get("pre_oneway", False) and plot_cfg.get("oneway", True):
|
|
654
|
+
n_bins = int(plot_cfg.get("n_bins", 10))
|
|
655
|
+
model.plot_oneway(n_bins=n_bins, plot_subdir="oneway/pre")
|
|
656
|
+
|
|
657
|
+
requested_keys = self._requested_model_keys(model.trainers)
|
|
658
|
+
executed_keys: List[str] = []
|
|
659
|
+
param_sources: Dict[str, str] = {}
|
|
660
|
+
|
|
661
|
+
if self.args.dry_run:
|
|
662
|
+
_log(f"Dry run: would train {requested_keys} for {model_name}.")
|
|
663
|
+
return {
|
|
664
|
+
"executed_keys": executed_keys,
|
|
665
|
+
"param_sources": param_sources,
|
|
666
|
+
"model": model,
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
if self.args.force_retune and self.args.max_evals <= 0:
|
|
670
|
+
raise ValueError("force_retune requires --max-evals > 0.")
|
|
671
|
+
|
|
672
|
+
force_retune = bool(self.args.force_retune)
|
|
673
|
+
if force_retune:
|
|
674
|
+
model.config.reuse_best_params = False
|
|
675
|
+
model.config.best_params_files = {}
|
|
676
|
+
|
|
677
|
+
ft_role = str(getattr(model.config, "ft_role", "model"))
|
|
678
|
+
if ft_role != "model" and "ft" in requested_keys:
|
|
679
|
+
requested_keys = ["ft"] + [k for k in requested_keys if k != "ft"]
|
|
680
|
+
|
|
681
|
+
for key in requested_keys:
|
|
682
|
+
trainer = model.trainers[key]
|
|
683
|
+
|
|
684
|
+
if force_retune:
|
|
685
|
+
trainer.best_params = None
|
|
686
|
+
trainer.best_trial = None
|
|
687
|
+
param_sources[key] = "retune"
|
|
688
|
+
else:
|
|
689
|
+
best_params = _load_best_params(model, trainer, silent=True)
|
|
690
|
+
if best_params:
|
|
691
|
+
trainer.best_params = best_params
|
|
692
|
+
trainer.best_trial = None
|
|
693
|
+
param_sources[key] = "loaded"
|
|
694
|
+
else:
|
|
695
|
+
if not self.args.retune_missing:
|
|
696
|
+
_log(
|
|
697
|
+
f"Skipping {model_name}/{key}: no best params and retuning disabled."
|
|
698
|
+
)
|
|
699
|
+
continue
|
|
700
|
+
param_sources[key] = "retune"
|
|
701
|
+
|
|
702
|
+
if (trainer.best_params is None) and self.args.max_evals <= 0:
|
|
703
|
+
raise ValueError("--max-evals must be positive when retuning is requested.")
|
|
704
|
+
|
|
705
|
+
model.optimize_model(key, max_evals=self.args.max_evals)
|
|
706
|
+
trainer.save()
|
|
707
|
+
executed_keys.append(key)
|
|
708
|
+
if key in PYTORCH_TRAINERS:
|
|
709
|
+
ropt.free_cuda()
|
|
710
|
+
|
|
711
|
+
snapshot = {
|
|
712
|
+
"mode": "incremental_train",
|
|
713
|
+
"model_name": model_name,
|
|
714
|
+
"model_key": key,
|
|
715
|
+
"timestamp": datetime.now().isoformat(),
|
|
716
|
+
"param_source": param_sources[key],
|
|
717
|
+
"best_params": _to_serializable(trainer.best_params or {}),
|
|
718
|
+
"incremental_rows": new_rows,
|
|
719
|
+
"train_rows": len(model.train_data),
|
|
720
|
+
"test_rows": len(model.test_data),
|
|
721
|
+
"incremental_path": str(incremental_path) if incremental_path else None,
|
|
722
|
+
"config": asdict(model.config),
|
|
723
|
+
}
|
|
724
|
+
model.version_manager.save(f"{model_name}_{key}_incremental", snapshot)
|
|
725
|
+
|
|
726
|
+
if not executed_keys:
|
|
727
|
+
_log(f"No trainers executed for {model_name}.")
|
|
728
|
+
|
|
729
|
+
return {
|
|
730
|
+
"executed_keys": executed_keys,
|
|
731
|
+
"param_sources": param_sources,
|
|
732
|
+
"model": model,
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
def process(self) -> None:
|
|
736
|
+
total_trained = 0
|
|
737
|
+
for model_name in self.model_names:
|
|
738
|
+
total_trained += self._process_single_model(model_name)
|
|
739
|
+
if self.args.summary_json and self.summary_records:
|
|
740
|
+
summary_path = self.args.summary_json.resolve()
|
|
741
|
+
summary_path.parent.mkdir(parents=True, exist_ok=True)
|
|
742
|
+
summary_payload = _to_serializable(self.summary_records)
|
|
743
|
+
summary_path.write_text(json.dumps(summary_payload, indent=2, ensure_ascii=False), encoding="utf-8")
|
|
744
|
+
_log(f"Summary written to {summary_path}.")
|
|
745
|
+
_log(f"Finished incremental update for {total_trained} dataset(s).")
|
|
746
|
+
|
|
747
|
+
def _process_single_model(self, model_name: str) -> int:
|
|
748
|
+
base_path = resolve_data_path(
|
|
749
|
+
self.data_dir,
|
|
750
|
+
model_name,
|
|
751
|
+
data_format=self.data_format,
|
|
752
|
+
path_template=self.data_path_template,
|
|
753
|
+
)
|
|
754
|
+
if not base_path.exists():
|
|
755
|
+
_log(f"Base dataset {base_path} not found; skipping {model_name}.")
|
|
756
|
+
self.summary_records.append({
|
|
757
|
+
"model_name": model_name,
|
|
758
|
+
"status": "missing_base",
|
|
759
|
+
})
|
|
760
|
+
return 0
|
|
761
|
+
|
|
762
|
+
base_df = load_dataset(
|
|
763
|
+
base_path,
|
|
764
|
+
data_format=self.data_format,
|
|
765
|
+
dtype_map=self.dtype_map,
|
|
766
|
+
low_memory=False,
|
|
767
|
+
)
|
|
768
|
+
inc_df, inc_path = self._load_incremental_df(model_name)
|
|
769
|
+
if inc_df is None and self.incremental_dir and self.args.strict_incremental and not self.args.train_without_incremental:
|
|
770
|
+
raise FileNotFoundError(f"Missing incremental file for {model_name} under {self.incremental_dir}.")
|
|
771
|
+
|
|
772
|
+
new_rows = 0 if inc_df is None else len(inc_df)
|
|
773
|
+
_log(f"{model_name}: {len(base_df)} base rows, {new_rows} incremental rows.")
|
|
774
|
+
merged_df = self._merge_frames(base_df, inc_df)
|
|
775
|
+
merged_df.fillna(0, inplace=True)
|
|
776
|
+
|
|
777
|
+
if self.args.update_base_data and not self.args.dry_run:
|
|
778
|
+
self._write_dataset(merged_df, base_path, "update_base_data")
|
|
779
|
+
if self.args.persist_merged_dir and not self.args.dry_run:
|
|
780
|
+
suffix = base_path.suffix or ".csv"
|
|
781
|
+
dest = Path(self.args.persist_merged_dir).resolve() / f"{model_name}{suffix}"
|
|
782
|
+
self._write_dataset(merged_df, dest, "persist_merged_dir")
|
|
783
|
+
|
|
784
|
+
if not self._should_train(new_rows):
|
|
785
|
+
_log(f"{model_name}: below min_new_rows ({self.args.min_new_rows}); skipping retrain.")
|
|
786
|
+
self.summary_records.append({
|
|
787
|
+
"model_name": model_name,
|
|
788
|
+
"status": "skipped_no_incremental",
|
|
789
|
+
"new_rows": new_rows,
|
|
790
|
+
"total_rows": len(merged_df),
|
|
791
|
+
})
|
|
792
|
+
return 0
|
|
793
|
+
|
|
794
|
+
try:
|
|
795
|
+
train_result = self._train_single_model(model_name, merged_df, new_rows, inc_path)
|
|
796
|
+
except Exception as exc:
|
|
797
|
+
_log(f"Training failed for {model_name}: {exc}")
|
|
798
|
+
self.summary_records.append({
|
|
799
|
+
"model_name": model_name,
|
|
800
|
+
"status": "failed",
|
|
801
|
+
"error": str(exc),
|
|
802
|
+
"new_rows": new_rows,
|
|
803
|
+
"total_rows": len(merged_df),
|
|
804
|
+
})
|
|
805
|
+
return 0
|
|
806
|
+
|
|
807
|
+
executed = train_result["executed_keys"]
|
|
808
|
+
param_sources = train_result["param_sources"]
|
|
809
|
+
model = train_result["model"]
|
|
810
|
+
status = "dry_run" if self.args.dry_run else "trained"
|
|
811
|
+
|
|
812
|
+
summary = {
|
|
813
|
+
"model_name": model_name,
|
|
814
|
+
"status": status,
|
|
815
|
+
"trained_models": executed,
|
|
816
|
+
"param_sources": param_sources,
|
|
817
|
+
"new_rows": new_rows,
|
|
818
|
+
"total_rows": len(merged_df),
|
|
819
|
+
"incremental_path": str(inc_path) if inc_path else None,
|
|
820
|
+
}
|
|
821
|
+
self.summary_records.append(summary)
|
|
822
|
+
|
|
823
|
+
if not self.args.dry_run and self.plot_requested and executed:
|
|
824
|
+
_plot_curves_for_model(model, executed, self.cfg)
|
|
825
|
+
|
|
826
|
+
return 1 if executed else 0
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
def main() -> None:
|
|
830
|
+
if configure_run_logging:
|
|
831
|
+
configure_run_logging(prefix="bayesopt_incremental")
|
|
832
|
+
args = _parse_args()
|
|
833
|
+
runner = IncrementalUpdateRunner(args)
|
|
834
|
+
runner.process()
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
if __name__ == "__main__":
|
|
838
|
+
main()
|