ins-pricing 0.4.5__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 +48 -22
- ins_pricing/__init__.py +142 -90
- ins_pricing/cli/BayesOpt_entry.py +52 -50
- ins_pricing/cli/BayesOpt_incremental.py +39 -105
- 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 +11 -9
- 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/__init__.py +10 -10
- 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/__init__.py +147 -92
- ins_pricing/modelling/{core/bayesopt → bayesopt}/README.md +2 -2
- ins_pricing/modelling/{core/bayesopt → bayesopt}/__init__.py +64 -102
- ins_pricing/modelling/{core/bayesopt → bayesopt}/config_preprocess.py +562 -562
- ins_pricing/modelling/{core/bayesopt → bayesopt}/core.py +965 -964
- 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.5.dist-info → ins_pricing-0.5.0.dist-info}/METADATA +182 -182
- ins_pricing-0.5.0.dist-info/RECORD +131 -0
- ins_pricing/modelling/core/BayesOpt.py +0 -146
- ins_pricing/modelling/core/__init__.py +0 -1
- 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.5.dist-info/RECORD +0 -130
- /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.5.dist-info → ins_pricing-0.5.0.dist-info}/WHEEL +0 -0
- {ins_pricing-0.4.5.dist-info → ins_pricing-0.5.0.dist-info}/top_level.txt +0 -0
ins_pricing/cli/Explain_entry.py
CHANGED
|
@@ -1,586 +1,539 @@
|
|
|
1
|
-
"""Config-driven explain runner for trained BayesOpt models."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
1
|
+
"""Config-driven explain runner for trained BayesOpt models."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
5
|
from pathlib import Path
|
|
6
|
+
import importlib.util
|
|
6
7
|
import sys
|
|
7
8
|
|
|
8
9
|
if __package__ in {None, ""}:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
import
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
if importlib.util.find_spec("ins_pricing") is None:
|
|
11
|
+
repo_root = Path(__file__).resolve().parents[2]
|
|
12
|
+
if str(repo_root) not in sys.path:
|
|
13
|
+
sys.path.insert(0, str(repo_root))
|
|
14
|
+
|
|
15
|
+
import argparse
|
|
16
|
+
import json
|
|
17
|
+
from typing import Any, Dict, List, Optional, Sequence
|
|
18
|
+
|
|
17
19
|
import numpy as np
|
|
18
20
|
import pandas as pd
|
|
19
21
|
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
"reuse_best_params": runtime_cfg["reuse_best_params"],
|
|
539
|
-
"plot_path_style": plot_path_style or "nested",
|
|
540
|
-
})
|
|
541
|
-
config_payload = {k: v for k, v in config_payload.items() if v is not None}
|
|
542
|
-
config = ropt.BayesOptConfig(**config_payload)
|
|
543
|
-
model = ropt.BayesOptModel(train_df, test_df, config=config)
|
|
544
|
-
|
|
545
|
-
output_overrides = resolve_explain_output_overrides(
|
|
546
|
-
explain_cfg,
|
|
547
|
-
model_name=model_name,
|
|
548
|
-
base_dir=config_path.parent,
|
|
549
|
-
)
|
|
550
|
-
model_dir_override = output_overrides.get("model_dir")
|
|
551
|
-
if model_dir_override is not None:
|
|
552
|
-
model.output_manager.model_dir = model_dir_override
|
|
553
|
-
result_dir_override = output_overrides.get("result_dir")
|
|
554
|
-
if result_dir_override is not None:
|
|
555
|
-
model.output_manager.result_dir = result_dir_override
|
|
556
|
-
plot_dir_override = output_overrides.get("plot_dir")
|
|
557
|
-
if plot_dir_override is not None:
|
|
558
|
-
model.output_manager.plot_dir = plot_dir_override
|
|
559
|
-
|
|
560
|
-
save_dir = resolve_explain_save_dir(
|
|
561
|
-
save_root,
|
|
562
|
-
result_dir=model.output_manager.result_dir,
|
|
563
|
-
)
|
|
564
|
-
save_dir.mkdir(parents=True, exist_ok=True)
|
|
565
|
-
|
|
566
|
-
print(f"\n=== Explain model {model_name} ===")
|
|
567
|
-
_explain_for_model(
|
|
568
|
-
model,
|
|
569
|
-
model_name=model_name,
|
|
570
|
-
model_keys=model_keys,
|
|
571
|
-
methods=methods,
|
|
572
|
-
on_train=on_train,
|
|
573
|
-
save_dir=save_dir,
|
|
574
|
-
explain_cfg=explain_cfg,
|
|
575
|
-
)
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
def main() -> None:
|
|
579
|
-
if configure_run_logging:
|
|
580
|
-
configure_run_logging(prefix="explain_entry")
|
|
581
|
-
args = _parse_args()
|
|
582
|
-
explain_from_config(args)
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
if __name__ == "__main__":
|
|
586
|
-
main()
|
|
22
|
+
from ins_pricing.cli.utils.import_resolver import resolve_imports, setup_sys_path
|
|
23
|
+
|
|
24
|
+
setup_sys_path()
|
|
25
|
+
_imports = resolve_imports()
|
|
26
|
+
|
|
27
|
+
ropt = _imports.bayesopt
|
|
28
|
+
if ropt is None: # pragma: no cover
|
|
29
|
+
raise ImportError("Failed to resolve ins_pricing.bayesopt for explain CLI.")
|
|
30
|
+
|
|
31
|
+
build_model_names = _imports.build_model_names
|
|
32
|
+
dedupe_preserve_order = _imports.dedupe_preserve_order
|
|
33
|
+
load_dataset = _imports.load_dataset
|
|
34
|
+
resolve_data_path = _imports.resolve_data_path
|
|
35
|
+
coerce_dataset_types = _imports.coerce_dataset_types
|
|
36
|
+
split_train_test = _imports.split_train_test
|
|
37
|
+
|
|
38
|
+
add_config_json_arg = _imports.add_config_json_arg
|
|
39
|
+
add_output_dir_arg = _imports.add_output_dir_arg
|
|
40
|
+
resolve_and_load_config = _imports.resolve_and_load_config
|
|
41
|
+
resolve_data_config = _imports.resolve_data_config
|
|
42
|
+
resolve_explain_output_overrides = _imports.resolve_explain_output_overrides
|
|
43
|
+
resolve_explain_save_dir = _imports.resolve_explain_save_dir
|
|
44
|
+
resolve_explain_save_root = _imports.resolve_explain_save_root
|
|
45
|
+
resolve_model_path_value = _imports.resolve_model_path_value
|
|
46
|
+
resolve_split_config = _imports.resolve_split_config
|
|
47
|
+
resolve_runtime_config = _imports.resolve_runtime_config
|
|
48
|
+
resolve_output_dirs = _imports.resolve_output_dirs
|
|
49
|
+
|
|
50
|
+
configure_run_logging = _imports.configure_run_logging
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
_SUPPORTED_METHODS = {"permutation", "shap", "integrated_gradients"}
|
|
54
|
+
_METHOD_ALIASES = {
|
|
55
|
+
"ig": "integrated_gradients",
|
|
56
|
+
"integrated": "integrated_gradients",
|
|
57
|
+
"intgrad": "integrated_gradients",
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _safe_name(value: str) -> str:
|
|
62
|
+
return "".join(ch if ch.isalnum() or ch in "-_." else "_" for ch in str(value))
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _load_dataset(
|
|
66
|
+
path: Path,
|
|
67
|
+
*,
|
|
68
|
+
data_format: str,
|
|
69
|
+
dtype_map: Optional[Dict[str, Any]],
|
|
70
|
+
) -> pd.DataFrame:
|
|
71
|
+
raw = load_dataset(
|
|
72
|
+
path,
|
|
73
|
+
data_format=data_format,
|
|
74
|
+
dtype_map=dtype_map,
|
|
75
|
+
low_memory=False,
|
|
76
|
+
)
|
|
77
|
+
return coerce_dataset_types(raw)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _normalize_methods(raw: Sequence[str]) -> List[str]:
|
|
81
|
+
methods: List[str] = []
|
|
82
|
+
for item in raw:
|
|
83
|
+
key = str(item).strip().lower()
|
|
84
|
+
if not key:
|
|
85
|
+
continue
|
|
86
|
+
key = _METHOD_ALIASES.get(key, key)
|
|
87
|
+
if key not in _SUPPORTED_METHODS:
|
|
88
|
+
raise ValueError(f"Unsupported explain method: {item}")
|
|
89
|
+
methods.append(key)
|
|
90
|
+
return dedupe_preserve_order(methods)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _save_series(series: pd.Series, path: Path) -> None:
|
|
94
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
95
|
+
series.to_frame(name="importance").to_csv(path, index=True)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _save_df(df: pd.DataFrame, path: Path) -> None:
|
|
99
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
100
|
+
df.to_csv(path, index=False)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _shap_importance(values: Any, feature_names: Sequence[str]) -> pd.Series:
|
|
104
|
+
if isinstance(values, list):
|
|
105
|
+
values = values[0]
|
|
106
|
+
arr = np.asarray(values)
|
|
107
|
+
if arr.ndim == 3:
|
|
108
|
+
arr = arr[0]
|
|
109
|
+
scores = np.mean(np.abs(arr), axis=0)
|
|
110
|
+
return pd.Series(scores, index=list(feature_names)).sort_values(ascending=False)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _parse_args() -> argparse.Namespace:
|
|
114
|
+
parser = argparse.ArgumentParser(
|
|
115
|
+
description="Run explainability (permutation/SHAP/IG) on trained models."
|
|
116
|
+
)
|
|
117
|
+
add_config_json_arg(
|
|
118
|
+
parser,
|
|
119
|
+
help_text="Path to config.json (same schema as training).",
|
|
120
|
+
)
|
|
121
|
+
parser.add_argument(
|
|
122
|
+
"--model-keys",
|
|
123
|
+
nargs="+",
|
|
124
|
+
default=None,
|
|
125
|
+
choices=["glm", "xgb", "resn", "ft", "gnn", "all"],
|
|
126
|
+
help="Model keys to load for explanation (default from config.explain.model_keys).",
|
|
127
|
+
)
|
|
128
|
+
parser.add_argument(
|
|
129
|
+
"--methods",
|
|
130
|
+
nargs="+",
|
|
131
|
+
default=None,
|
|
132
|
+
help="Explain methods: permutation, shap, integrated_gradients (default from config.explain.methods).",
|
|
133
|
+
)
|
|
134
|
+
add_output_dir_arg(
|
|
135
|
+
parser,
|
|
136
|
+
help_text="Override output root for loading models/results.",
|
|
137
|
+
)
|
|
138
|
+
parser.add_argument(
|
|
139
|
+
"--eval-path",
|
|
140
|
+
default=None,
|
|
141
|
+
help="Override validation CSV path (supports {model_name}).",
|
|
142
|
+
)
|
|
143
|
+
parser.add_argument(
|
|
144
|
+
"--on-train",
|
|
145
|
+
action="store_true",
|
|
146
|
+
help="Explain on train split instead of validation/test.",
|
|
147
|
+
)
|
|
148
|
+
parser.add_argument(
|
|
149
|
+
"--save-dir",
|
|
150
|
+
default=None,
|
|
151
|
+
help="Override output directory for explanation artifacts.",
|
|
152
|
+
)
|
|
153
|
+
return parser.parse_args()
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _explain_for_model(
|
|
157
|
+
model: ropt.BayesOptModel,
|
|
158
|
+
*,
|
|
159
|
+
model_name: str,
|
|
160
|
+
model_keys: List[str],
|
|
161
|
+
methods: List[str],
|
|
162
|
+
on_train: bool,
|
|
163
|
+
save_dir: Path,
|
|
164
|
+
explain_cfg: Dict[str, Any],
|
|
165
|
+
) -> None:
|
|
166
|
+
perm_cfg = dict(explain_cfg.get("permutation") or {})
|
|
167
|
+
shap_cfg = dict(explain_cfg.get("shap") or {})
|
|
168
|
+
ig_cfg = dict(explain_cfg.get("integrated_gradients") or {})
|
|
169
|
+
|
|
170
|
+
perm_metric = perm_cfg.get("metric", explain_cfg.get("metric", "auto"))
|
|
171
|
+
perm_repeats = int(perm_cfg.get("n_repeats", 5))
|
|
172
|
+
perm_max_rows = perm_cfg.get("max_rows", 5000)
|
|
173
|
+
perm_random_state = perm_cfg.get("random_state", None)
|
|
174
|
+
|
|
175
|
+
shap_background = int(shap_cfg.get("n_background", 500))
|
|
176
|
+
shap_samples = int(shap_cfg.get("n_samples", 200))
|
|
177
|
+
shap_save_values = bool(shap_cfg.get("save_values", False))
|
|
178
|
+
|
|
179
|
+
ig_steps = int(ig_cfg.get("steps", 50))
|
|
180
|
+
ig_batch_size = int(ig_cfg.get("batch_size", 256))
|
|
181
|
+
ig_target = ig_cfg.get("target", None)
|
|
182
|
+
ig_baseline = ig_cfg.get("baseline", None)
|
|
183
|
+
ig_baseline_num = ig_cfg.get("baseline_num", None)
|
|
184
|
+
ig_baseline_geo = ig_cfg.get("baseline_geo", None)
|
|
185
|
+
ig_save_values = bool(ig_cfg.get("save_values", False))
|
|
186
|
+
|
|
187
|
+
for key in model_keys:
|
|
188
|
+
trainer = model.trainers.get(key)
|
|
189
|
+
if trainer is None:
|
|
190
|
+
print(f"[Explain] Skip {model_name}/{key}: trainer not available.")
|
|
191
|
+
continue
|
|
192
|
+
model.load_model(key)
|
|
193
|
+
trained_model = getattr(model, f"{key}_best", None)
|
|
194
|
+
if trained_model is None:
|
|
195
|
+
print(f"[Explain] Skip {model_name}/{key}: model not loaded.")
|
|
196
|
+
continue
|
|
197
|
+
|
|
198
|
+
if key == "ft" and str(model.config.ft_role) != "model":
|
|
199
|
+
print(f"[Explain] Skip {model_name}/ft: ft_role != 'model'.")
|
|
200
|
+
continue
|
|
201
|
+
|
|
202
|
+
for method in methods:
|
|
203
|
+
if method == "permutation" and key not in {"xgb", "resn", "ft"}:
|
|
204
|
+
print(f"[Explain] Skip permutation for {model_name}/{key}.")
|
|
205
|
+
continue
|
|
206
|
+
if method == "shap" and key not in {"glm", "xgb", "resn", "ft"}:
|
|
207
|
+
print(f"[Explain] Skip shap for {model_name}/{key}.")
|
|
208
|
+
continue
|
|
209
|
+
if method == "integrated_gradients" and key not in {"resn", "ft"}:
|
|
210
|
+
print(f"[Explain] Skip integrated gradients for {model_name}/{key}.")
|
|
211
|
+
continue
|
|
212
|
+
|
|
213
|
+
if method == "permutation":
|
|
214
|
+
try:
|
|
215
|
+
result = model.compute_permutation_importance(
|
|
216
|
+
key,
|
|
217
|
+
on_train=on_train,
|
|
218
|
+
metric=perm_metric,
|
|
219
|
+
n_repeats=perm_repeats,
|
|
220
|
+
max_rows=perm_max_rows,
|
|
221
|
+
random_state=perm_random_state,
|
|
222
|
+
)
|
|
223
|
+
except Exception as exc:
|
|
224
|
+
print(f"[Explain] permutation failed for {model_name}/{key}: {exc}")
|
|
225
|
+
continue
|
|
226
|
+
out_path = save_dir / f"{_safe_name(model_name)}_{key}_permutation.csv"
|
|
227
|
+
_save_df(result, out_path)
|
|
228
|
+
print(f"[Explain] Saved permutation -> {out_path}")
|
|
229
|
+
|
|
230
|
+
if method == "shap":
|
|
231
|
+
try:
|
|
232
|
+
if key == "glm":
|
|
233
|
+
shap_result = model.compute_shap_glm(
|
|
234
|
+
n_background=shap_background,
|
|
235
|
+
n_samples=shap_samples,
|
|
236
|
+
on_train=on_train,
|
|
237
|
+
)
|
|
238
|
+
elif key == "xgb":
|
|
239
|
+
shap_result = model.compute_shap_xgb(
|
|
240
|
+
n_background=shap_background,
|
|
241
|
+
n_samples=shap_samples,
|
|
242
|
+
on_train=on_train,
|
|
243
|
+
)
|
|
244
|
+
elif key == "resn":
|
|
245
|
+
shap_result = model.compute_shap_resn(
|
|
246
|
+
n_background=shap_background,
|
|
247
|
+
n_samples=shap_samples,
|
|
248
|
+
on_train=on_train,
|
|
249
|
+
)
|
|
250
|
+
else:
|
|
251
|
+
shap_result = model.compute_shap_ft(
|
|
252
|
+
n_background=shap_background,
|
|
253
|
+
n_samples=shap_samples,
|
|
254
|
+
on_train=on_train,
|
|
255
|
+
)
|
|
256
|
+
except Exception as exc:
|
|
257
|
+
print(f"[Explain] shap failed for {model_name}/{key}: {exc}")
|
|
258
|
+
continue
|
|
259
|
+
|
|
260
|
+
shap_values = shap_result.get("shap_values")
|
|
261
|
+
X_explain = shap_result.get("X_explain")
|
|
262
|
+
feature_names = (
|
|
263
|
+
list(X_explain.columns)
|
|
264
|
+
if isinstance(X_explain, pd.DataFrame)
|
|
265
|
+
else list(model.factor_nmes)
|
|
266
|
+
)
|
|
267
|
+
importance = _shap_importance(shap_values, feature_names)
|
|
268
|
+
out_path = save_dir / f"{_safe_name(model_name)}_{key}_shap_importance.csv"
|
|
269
|
+
_save_series(importance, out_path)
|
|
270
|
+
print(f"[Explain] Saved SHAP importance -> {out_path}")
|
|
271
|
+
|
|
272
|
+
if shap_save_values:
|
|
273
|
+
values_path = save_dir / f"{_safe_name(model_name)}_{key}_shap_values.npy"
|
|
274
|
+
np.save(values_path, np.array(shap_values, dtype=object), allow_pickle=True)
|
|
275
|
+
if isinstance(X_explain, pd.DataFrame):
|
|
276
|
+
x_path = save_dir / f"{_safe_name(model_name)}_{key}_shap_X.csv"
|
|
277
|
+
_save_df(X_explain, x_path)
|
|
278
|
+
meta_path = save_dir / f"{_safe_name(model_name)}_{key}_shap_meta.json"
|
|
279
|
+
meta = {
|
|
280
|
+
"base_value": shap_result.get("base_value"),
|
|
281
|
+
"n_samples": int(len(X_explain)) if X_explain is not None else None,
|
|
282
|
+
}
|
|
283
|
+
meta_path.write_text(json.dumps(meta, indent=2), encoding="utf-8")
|
|
284
|
+
|
|
285
|
+
if method == "integrated_gradients":
|
|
286
|
+
try:
|
|
287
|
+
if key == "resn":
|
|
288
|
+
ig_result = model.compute_integrated_gradients_resn(
|
|
289
|
+
on_train=on_train,
|
|
290
|
+
baseline=ig_baseline,
|
|
291
|
+
steps=ig_steps,
|
|
292
|
+
batch_size=ig_batch_size,
|
|
293
|
+
target=ig_target,
|
|
294
|
+
)
|
|
295
|
+
series = ig_result.get("importance")
|
|
296
|
+
if isinstance(series, pd.Series):
|
|
297
|
+
out_path = save_dir / f"{_safe_name(model_name)}_{key}_ig_importance.csv"
|
|
298
|
+
_save_series(series, out_path)
|
|
299
|
+
print(f"[Explain] Saved IG importance -> {out_path}")
|
|
300
|
+
if ig_save_values and "attributions" in ig_result:
|
|
301
|
+
attr_path = save_dir / f"{_safe_name(model_name)}_{key}_ig_attributions.npy"
|
|
302
|
+
np.save(attr_path, ig_result.get("attributions"))
|
|
303
|
+
else:
|
|
304
|
+
ig_result = model.compute_integrated_gradients_ft(
|
|
305
|
+
on_train=on_train,
|
|
306
|
+
baseline_num=ig_baseline_num,
|
|
307
|
+
baseline_geo=ig_baseline_geo,
|
|
308
|
+
steps=ig_steps,
|
|
309
|
+
batch_size=ig_batch_size,
|
|
310
|
+
target=ig_target,
|
|
311
|
+
)
|
|
312
|
+
series_num = ig_result.get("importance_num")
|
|
313
|
+
series_geo = ig_result.get("importance_geo")
|
|
314
|
+
if isinstance(series_num, pd.Series):
|
|
315
|
+
out_path = save_dir / f"{_safe_name(model_name)}_{key}_ig_num_importance.csv"
|
|
316
|
+
_save_series(series_num, out_path)
|
|
317
|
+
print(f"[Explain] Saved IG num importance -> {out_path}")
|
|
318
|
+
if isinstance(series_geo, pd.Series):
|
|
319
|
+
out_path = save_dir / f"{_safe_name(model_name)}_{key}_ig_geo_importance.csv"
|
|
320
|
+
_save_series(series_geo, out_path)
|
|
321
|
+
print(f"[Explain] Saved IG geo importance -> {out_path}")
|
|
322
|
+
if ig_save_values:
|
|
323
|
+
if ig_result.get("attributions_num") is not None:
|
|
324
|
+
attr_path = save_dir / f"{_safe_name(model_name)}_{key}_ig_num_attributions.npy"
|
|
325
|
+
np.save(attr_path, ig_result.get("attributions_num"))
|
|
326
|
+
if ig_result.get("attributions_geo") is not None:
|
|
327
|
+
attr_path = save_dir / f"{_safe_name(model_name)}_{key}_ig_geo_attributions.npy"
|
|
328
|
+
np.save(attr_path, ig_result.get("attributions_geo"))
|
|
329
|
+
except Exception as exc:
|
|
330
|
+
print(f"[Explain] integrated gradients failed for {model_name}/{key}: {exc}")
|
|
331
|
+
continue
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def explain_from_config(args: argparse.Namespace) -> None:
|
|
335
|
+
script_dir = Path(__file__).resolve().parents[1]
|
|
336
|
+
config_path, cfg = resolve_and_load_config(
|
|
337
|
+
args.config_json,
|
|
338
|
+
script_dir,
|
|
339
|
+
required_keys=["data_dir", "model_list", "model_categories", "target", "weight"],
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
data_dir, data_format, data_path_template, dtype_map = resolve_data_config(
|
|
343
|
+
cfg,
|
|
344
|
+
config_path,
|
|
345
|
+
create_data_dir=True,
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
runtime_cfg = resolve_runtime_config(cfg)
|
|
349
|
+
output_cfg = resolve_output_dirs(
|
|
350
|
+
cfg,
|
|
351
|
+
config_path,
|
|
352
|
+
output_override=args.output_dir,
|
|
353
|
+
)
|
|
354
|
+
output_dir = output_cfg["output_dir"]
|
|
355
|
+
|
|
356
|
+
split_cfg = resolve_split_config(cfg)
|
|
357
|
+
prop_test = split_cfg["prop_test"]
|
|
358
|
+
rand_seed = runtime_cfg["rand_seed"]
|
|
359
|
+
split_strategy = split_cfg["split_strategy"]
|
|
360
|
+
split_group_col = split_cfg["split_group_col"]
|
|
361
|
+
split_time_col = split_cfg["split_time_col"]
|
|
362
|
+
split_time_ascending = split_cfg["split_time_ascending"]
|
|
363
|
+
|
|
364
|
+
explain_cfg = dict(cfg.get("explain") or {})
|
|
365
|
+
|
|
366
|
+
model_keys = args.model_keys or explain_cfg.get("model_keys") or ["xgb"]
|
|
367
|
+
if "all" in model_keys:
|
|
368
|
+
model_keys = ["glm", "xgb", "resn", "ft", "gnn"]
|
|
369
|
+
model_keys = dedupe_preserve_order([str(x) for x in model_keys])
|
|
370
|
+
|
|
371
|
+
method_list = args.methods or explain_cfg.get("methods") or ["permutation"]
|
|
372
|
+
methods = _normalize_methods([str(x) for x in method_list])
|
|
373
|
+
|
|
374
|
+
on_train = bool(args.on_train or explain_cfg.get("on_train", False))
|
|
375
|
+
|
|
376
|
+
model_names = build_model_names(cfg["model_list"], cfg["model_categories"])
|
|
377
|
+
if not model_names:
|
|
378
|
+
raise ValueError("No model names generated from model_list/model_categories.")
|
|
379
|
+
|
|
380
|
+
save_root = resolve_explain_save_root(
|
|
381
|
+
args.save_dir or explain_cfg.get("save_dir"),
|
|
382
|
+
config_path.parent,
|
|
383
|
+
)
|
|
384
|
+
|
|
385
|
+
for model_name in model_names:
|
|
386
|
+
train_path = resolve_model_path_value(
|
|
387
|
+
explain_cfg.get("train_path"),
|
|
388
|
+
model_name=model_name,
|
|
389
|
+
base_dir=config_path.parent,
|
|
390
|
+
data_dir=data_dir,
|
|
391
|
+
)
|
|
392
|
+
if train_path is None:
|
|
393
|
+
train_path = resolve_data_path(
|
|
394
|
+
data_dir,
|
|
395
|
+
model_name,
|
|
396
|
+
data_format=data_format,
|
|
397
|
+
path_template=data_path_template,
|
|
398
|
+
)
|
|
399
|
+
if not train_path.exists():
|
|
400
|
+
raise FileNotFoundError(f"Missing training dataset: {train_path}")
|
|
401
|
+
|
|
402
|
+
validation_override = args.eval_path or explain_cfg.get("validation_path") or explain_cfg.get("eval_path")
|
|
403
|
+
validation_path = resolve_model_path_value(
|
|
404
|
+
validation_override,
|
|
405
|
+
model_name=model_name,
|
|
406
|
+
base_dir=config_path.parent,
|
|
407
|
+
data_dir=data_dir,
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
raw = _load_dataset(
|
|
411
|
+
train_path,
|
|
412
|
+
data_format=data_format,
|
|
413
|
+
dtype_map=dtype_map,
|
|
414
|
+
)
|
|
415
|
+
if validation_path is not None:
|
|
416
|
+
if not validation_path.exists():
|
|
417
|
+
raise FileNotFoundError(f"Missing validation dataset: {validation_path}")
|
|
418
|
+
train_df = raw
|
|
419
|
+
test_df = _load_dataset(
|
|
420
|
+
validation_path,
|
|
421
|
+
data_format=data_format,
|
|
422
|
+
dtype_map=dtype_map,
|
|
423
|
+
)
|
|
424
|
+
else:
|
|
425
|
+
if float(prop_test) <= 0:
|
|
426
|
+
train_df = raw
|
|
427
|
+
test_df = raw.copy()
|
|
428
|
+
else:
|
|
429
|
+
train_df, test_df = split_train_test(
|
|
430
|
+
raw,
|
|
431
|
+
holdout_ratio=prop_test,
|
|
432
|
+
strategy=split_strategy,
|
|
433
|
+
group_col=split_group_col,
|
|
434
|
+
time_col=split_time_col,
|
|
435
|
+
time_ascending=split_time_ascending,
|
|
436
|
+
rand_seed=rand_seed,
|
|
437
|
+
reset_index_mode="time_group",
|
|
438
|
+
ratio_label="prop_test",
|
|
439
|
+
include_strategy_in_ratio_error=True,
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
binary_target = cfg.get("binary_target") or cfg.get("binary_resp_nme")
|
|
443
|
+
feature_list = cfg.get("feature_list")
|
|
444
|
+
categorical_features = cfg.get("categorical_features")
|
|
445
|
+
plot_path_style = runtime_cfg["plot_path_style"]
|
|
446
|
+
|
|
447
|
+
config_fields = getattr(ropt.BayesOptConfig, "__dataclass_fields__", {})
|
|
448
|
+
allowed_config_keys = set(config_fields.keys())
|
|
449
|
+
config_payload = {k: v for k, v in cfg.items() if k in allowed_config_keys}
|
|
450
|
+
config_payload.update({
|
|
451
|
+
"model_nme": model_name,
|
|
452
|
+
"resp_nme": cfg["target"],
|
|
453
|
+
"weight_nme": cfg["weight"],
|
|
454
|
+
"factor_nmes": feature_list,
|
|
455
|
+
"task_type": str(cfg.get("task_type", "regression")),
|
|
456
|
+
"binary_resp_nme": binary_target,
|
|
457
|
+
"cate_list": categorical_features,
|
|
458
|
+
"prop_test": prop_test,
|
|
459
|
+
"rand_seed": rand_seed,
|
|
460
|
+
"epochs": int(runtime_cfg["epochs"]),
|
|
461
|
+
"use_gpu": bool(cfg.get("use_gpu", True)),
|
|
462
|
+
"output_dir": output_dir,
|
|
463
|
+
"xgb_max_depth_max": runtime_cfg["xgb_max_depth_max"],
|
|
464
|
+
"xgb_n_estimators_max": runtime_cfg["xgb_n_estimators_max"],
|
|
465
|
+
"resn_weight_decay": cfg.get("resn_weight_decay"),
|
|
466
|
+
"final_ensemble": bool(cfg.get("final_ensemble", False)),
|
|
467
|
+
"final_ensemble_k": int(cfg.get("final_ensemble_k", 3)),
|
|
468
|
+
"final_refit": bool(cfg.get("final_refit", True)),
|
|
469
|
+
"optuna_storage": runtime_cfg["optuna_storage"],
|
|
470
|
+
"optuna_study_prefix": runtime_cfg["optuna_study_prefix"],
|
|
471
|
+
"best_params_files": runtime_cfg["best_params_files"],
|
|
472
|
+
"gnn_use_approx_knn": cfg.get("gnn_use_approx_knn", True),
|
|
473
|
+
"gnn_approx_knn_threshold": cfg.get("gnn_approx_knn_threshold", 50000),
|
|
474
|
+
"gnn_graph_cache": cfg.get("gnn_graph_cache"),
|
|
475
|
+
"gnn_max_gpu_knn_nodes": cfg.get("gnn_max_gpu_knn_nodes", 200000),
|
|
476
|
+
"gnn_knn_gpu_mem_ratio": cfg.get("gnn_knn_gpu_mem_ratio", 0.9),
|
|
477
|
+
"gnn_knn_gpu_mem_overhead": cfg.get("gnn_knn_gpu_mem_overhead", 2.0),
|
|
478
|
+
"region_province_col": cfg.get("region_province_col"),
|
|
479
|
+
"region_city_col": cfg.get("region_city_col"),
|
|
480
|
+
"region_effect_alpha": cfg.get("region_effect_alpha"),
|
|
481
|
+
"geo_feature_nmes": cfg.get("geo_feature_nmes"),
|
|
482
|
+
"geo_token_hidden_dim": cfg.get("geo_token_hidden_dim"),
|
|
483
|
+
"geo_token_layers": cfg.get("geo_token_layers"),
|
|
484
|
+
"geo_token_dropout": cfg.get("geo_token_dropout"),
|
|
485
|
+
"geo_token_k_neighbors": cfg.get("geo_token_k_neighbors"),
|
|
486
|
+
"geo_token_learning_rate": cfg.get("geo_token_learning_rate"),
|
|
487
|
+
"geo_token_epochs": cfg.get("geo_token_epochs"),
|
|
488
|
+
"ft_role": str(cfg.get("ft_role", "model")),
|
|
489
|
+
"ft_feature_prefix": str(cfg.get("ft_feature_prefix", "ft_emb")),
|
|
490
|
+
"ft_num_numeric_tokens": cfg.get("ft_num_numeric_tokens"),
|
|
491
|
+
"reuse_best_params": runtime_cfg["reuse_best_params"],
|
|
492
|
+
"plot_path_style": plot_path_style or "nested",
|
|
493
|
+
})
|
|
494
|
+
config_payload = {k: v for k, v in config_payload.items() if v is not None}
|
|
495
|
+
config = ropt.BayesOptConfig(**config_payload)
|
|
496
|
+
model = ropt.BayesOptModel(train_df, test_df, config=config)
|
|
497
|
+
|
|
498
|
+
output_overrides = resolve_explain_output_overrides(
|
|
499
|
+
explain_cfg,
|
|
500
|
+
model_name=model_name,
|
|
501
|
+
base_dir=config_path.parent,
|
|
502
|
+
)
|
|
503
|
+
model_dir_override = output_overrides.get("model_dir")
|
|
504
|
+
if model_dir_override is not None:
|
|
505
|
+
model.output_manager.model_dir = model_dir_override
|
|
506
|
+
result_dir_override = output_overrides.get("result_dir")
|
|
507
|
+
if result_dir_override is not None:
|
|
508
|
+
model.output_manager.result_dir = result_dir_override
|
|
509
|
+
plot_dir_override = output_overrides.get("plot_dir")
|
|
510
|
+
if plot_dir_override is not None:
|
|
511
|
+
model.output_manager.plot_dir = plot_dir_override
|
|
512
|
+
|
|
513
|
+
save_dir = resolve_explain_save_dir(
|
|
514
|
+
save_root,
|
|
515
|
+
result_dir=model.output_manager.result_dir,
|
|
516
|
+
)
|
|
517
|
+
save_dir.mkdir(parents=True, exist_ok=True)
|
|
518
|
+
|
|
519
|
+
print(f"\n=== Explain model {model_name} ===")
|
|
520
|
+
_explain_for_model(
|
|
521
|
+
model,
|
|
522
|
+
model_name=model_name,
|
|
523
|
+
model_keys=model_keys,
|
|
524
|
+
methods=methods,
|
|
525
|
+
on_train=on_train,
|
|
526
|
+
save_dir=save_dir,
|
|
527
|
+
explain_cfg=explain_cfg,
|
|
528
|
+
)
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
def main() -> None:
|
|
532
|
+
if configure_run_logging:
|
|
533
|
+
configure_run_logging(prefix="explain_entry")
|
|
534
|
+
args = _parse_args()
|
|
535
|
+
explain_from_config(args)
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
if __name__ == "__main__":
|
|
539
|
+
main()
|