-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathParetoDataLoader.py
More file actions
2768 lines (2393 loc) · 121 KB
/
ParetoDataLoader.py
File metadata and controls
2768 lines (2393 loc) · 121 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
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
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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
ParetoDataLoader - Loads and analyzes all_results.bin from Passivbot optimization runs
Provides comprehensive metrics, robustness scores, and parameter analysis
"""
import os
import heapq
import msgpack
import numpy as np
import pandas as pd
from typing import Dict, List, Tuple, Optional, Any, Iterator
from dataclasses import dataclass
import json
import glob
from pathlib import Path, PurePath
import re
import time
import hashlib
import traceback
from logging_helpers import human_log as _log
try:
import msgspec # type: ignore
except Exception:
msgspec = None
_HAS_MSGSPEC = (msgspec is not None) and (os.environ.get("PBG_DISABLE_MSGSPEC") != "1")
_MSGSPEC_MSGPACK_DECODER = msgspec.msgpack.Decoder() if _HAS_MSGSPEC else None
_DISABLE_SCAN_CACHE = os.environ.get("PBG_DISABLE_SCAN_CACHE") == "1"
_FULL_SCAN_CACHE = os.environ.get("PBG_FULL_SCAN_CACHE") == "1"
@dataclass
class ConfigMetrics:
"""Holds all metrics and analysis for a single config"""
config_index: int
config_hash: str
# Objectives
objectives: Dict[str, float]
constraint_violation: float
# Suite metrics (aggregated)
suite_metrics: Dict[str, float]
# Scenario-specific metrics
scenario_metrics: Dict[str, Dict[str, float]] # scenario -> metric -> value
# Robustness scores
robustness_scores: Dict[str, float] # metric -> robustness score (1/CV)
# Bot parameters
bot_params: Dict[str, Any]
# Optimize bounds (parameter search space)
bounds: Dict[str, Tuple[float, float]] = None
# Optimize settings (scoring, limits, etc)
optimize_settings: Dict[str, Any] = None
# Backtest scenarios details
scenario_details: List[Dict[str, Any]] = None
# Stats for each metric (mean, min, max, std across scenarios)
metric_stats: Dict[str, Dict[str, float]] = None
# Is this config on the Pareto front?
is_pareto: bool = False
# Cached overall robustness score [0, 1] used when `robustness_scores` isn't loaded.
overall_robustness: float = 0.0
# Lazy-loading flags (all_results.bin mode)
details_loaded: bool = True
bot_params_loaded: bool = True
def _extract_scoring_metric_names(scoring_list: list) -> List[str]:
"""Extract metric name strings from scoring entries.
PB7 < v7.9 stores scoring as ``["adg_w_usd", ...]``.
PB7 >= v7.9 stores scoring as ``[{"metric": "adg_w_usd", "goal": "max"}, ...]``.
This helper normalises both formats to a plain list of metric-name strings.
"""
names: List[str] = []
for entry in scoring_list:
if isinstance(entry, str):
names.append(entry)
elif isinstance(entry, dict):
m = entry.get('metric') or entry.get(b'metric')
if m:
names.append(m if isinstance(m, str) else m.decode('utf-8', errors='replace'))
elif isinstance(entry, bytes):
names.append(entry.decode('utf-8', errors='replace'))
return names
class ParetoDataLoader:
"""Loads and analyzes all_results.bin from optimize runs"""
def __init__(self, results_path: str):
"""
Initialize loader with results directory
Args:
results_path: Path to optimize_results directory (contains all_results.bin)
"""
self.results_path = results_path
self.all_results_path = os.path.join(results_path, "all_results.bin")
self.pareto_dir = os.path.join(results_path, "pareto")
self.configs: List[ConfigMetrics] = []
self.pareto_hashes: set = set()
self.scoring_metrics: List[str] = []
self.scenario_labels: List[str] = []
# Cache for pareto JSON configs (index -> full config)
self.pareto_configs_cache: Dict[int, Dict] = {}
# Cache for raw config_data from all_results.bin (config_index -> config_data)
# This avoids re-reading the entire file for each config
self.raw_configs_cache: Dict[int, Dict] = {}
# If a load fails for a known, user-facing reason, store it here.
# The UI can surface it without a stack trace.
self.last_error: Optional[str] = None
# Global optimization settings (same for all configs)
self.optimize_bounds: Dict[str, Tuple[float, float]] = {}
self.optimize_limits: List[Dict] = []
self.backtest_scenarios: List[Dict] = []
def _scan_cache_paths(self) -> Tuple[str, str]:
"""Return (meta_json_path, npz_path) for the persistent scan cache."""
base = self.all_results_path + ".scan_cache"
return base + ".meta.json", base + ".npz"
@staticmethod
def _normalize_optimize_bounds(bounds: Dict) -> Dict[str, Tuple[float, float]]:
"""Normalize PB7 optimize bounds to a simple (lower, upper) tuple per parameter.
PB7 may store bounds as e.g. [lower, upper, step].
This converts list/tuple/dict formats into (lower, upper) floats.
Unparseable entries are skipped.
"""
def _to_pair(v):
if v is None:
return None
if isinstance(v, dict):
for lo_key, hi_key in (("lower", "upper"), ("min", "max"), ("lo", "hi")):
if lo_key in v and hi_key in v:
try:
return float(v[lo_key]), float(v[hi_key])
except Exception:
return None
if "bounds" in v:
return _to_pair(v.get("bounds"))
return None
if isinstance(v, (list, tuple)):
if len(v) < 2:
return None
try:
return float(v[0]), float(v[1])
except Exception:
return None
return None
out: Dict[str, Tuple[float, float]] = {}
if not isinstance(bounds, dict):
return out
for k, v in bounds.items():
if not isinstance(k, str):
continue
pair = _to_pair(v)
if not pair:
continue
lo, hi = pair
if lo > hi:
lo, hi = hi, lo
out[k] = (lo, hi)
return out
def _is_scan_cache_valid(self) -> bool:
"""Check if scan cache exists and matches current all_results.bin (mtime+size)."""
meta_path, npz_path = self._scan_cache_paths()
try:
if not os.path.exists(meta_path) or not os.path.exists(npz_path):
return False
if not os.path.exists(self.all_results_path):
return False
st_bin = os.stat(self.all_results_path)
with open(meta_path, "r", encoding="utf-8") as f:
meta = json.load(f)
src = meta.get("source") or {}
return int(src.get("size") or -1) == int(st_bin.st_size) and float(src.get("mtime") or -1) == float(st_bin.st_mtime)
except Exception:
return False
def _load_scan_cache(self) -> Optional[Dict[str, Any]]:
"""Load scan cache (meta + numpy arrays). Returns dict or None on failure."""
meta_path, npz_path = self._scan_cache_paths()
try:
with open(meta_path, "r", encoding="utf-8") as f:
meta = json.load(f)
arrays = np.load(npz_path, allow_pickle=False)
return {"meta": meta, "arrays": arrays}
except Exception:
return None
def _write_scan_cache(self, meta: Dict[str, Any], arrays: Dict[str, np.ndarray]) -> None:
"""Write scan cache to disk (best-effort)."""
meta_path, npz_path = self._scan_cache_paths()
try:
os.makedirs(os.path.dirname(meta_path) or ".", exist_ok=True)
tmp_meta = meta_path + ".tmp"
# NOTE: np.savez appends ".npz" if the path doesn't end with it.
# Ensure the temp file ends with ".npz" so os.replace() targets the correct filename.
tmp_npz = npz_path + ".tmp.npz"
with open(tmp_meta, "w", encoding="utf-8") as f:
json.dump(meta, f)
np.savez(tmp_npz, **arrays)
os.replace(tmp_meta, meta_path)
os.replace(tmp_npz, npz_path)
except Exception:
# Cache is an optimization; never fail loading due to cache write.
try:
if os.path.exists(tmp_meta):
os.remove(tmp_meta)
if os.path.exists(tmp_npz):
os.remove(tmp_npz)
except Exception:
pass
def _is_legacy_results_format(self) -> bool:
"""Detect older optimize_results formats which this UI does not support.
Legacy signature (observed):
- `pareto/*.json` filenames start with a numeric score prefix like `001.2345_<hash>.json`.
- `pareto/*.json` content lacks `suite_metrics` (often contains `analyses*` keys instead).
"""
try:
if not os.path.exists(self.pareto_dir):
return False
json_files = glob.glob(os.path.join(self.pareto_dir, "*.json"))
if not json_files:
return False
sample_file = os.path.basename(sorted(json_files)[0])
if re.match(r"^\d+\.\d+_", sample_file):
return True
# Content-based fallback (cheap: inspect one file)
sample_path = sorted(json_files)[0]
with open(sample_path, "r", encoding="utf-8") as f:
obj = json.load(f)
if isinstance(obj, dict) and "suite_metrics" not in obj and ("analyses" in obj or "analyses_combined" in obj):
return True
return False
except Exception:
# Never block loading due to detection issues.
return False
def _decode_msgpack_object(self, payload: bytes) -> Any:
"""Decode a single msgpack object from a bytes slice.
Prefers msgspec if installed; falls back to msgpack.
"""
if _HAS_MSGSPEC and _MSGSPEC_MSGPACK_DECODER is not None:
return _MSGSPEC_MSGPACK_DECODER.decode(payload)
return msgpack.loads(payload, raw=False, strict_map_key=False)
def load(self, load_strategy: List[str] = None, max_configs: int = 2000, progress_callback=None) -> bool:
"""
Load all_results.bin and parse all configs
Args:
load_strategy: List of criteria to use for selecting top configs
Options: 'performance', 'robustness', 'sharpe', 'drawdown',
'calmar', 'sortino', 'omega', 'volatility', 'recovery'
Default: ['performance'] (Passivbot official)
max_configs: Maximum number of configs to keep (default: 2000)
progress_callback: Optional callback(current, total, message) for progress updates
Returns:
True if successful, False otherwise
"""
self.last_error = None
if self._is_legacy_results_format():
self.last_error = "Old format not supported"
return False
if load_strategy is None:
load_strategy = ['performance'] # Default: Passivbot official
if not os.path.exists(self.all_results_path):
self.last_error = f"File not found: {self.all_results_path}"
return False
t_total0 = time.perf_counter()
# Reset state
self.configs = []
self.raw_configs_cache = {}
self.pareto_configs_cache = {}
self.scoring_metrics = []
self.scenario_labels = []
self.optimize_bounds = {}
self.optimize_limits = []
self.backtest_scenarios = []
# Load pareto hashes to mark pareto configs
t0 = time.perf_counter()
self._load_pareto_hashes()
t_load_pareto_hashes = time.perf_counter() - t0
# Preload globals from first object (cheap) so the main scan can run in raw=True mode.
try:
with open(self.all_results_path, 'rb') as f:
u0 = msgpack.Unpacker(f, raw=False, strict_map_key=False, read_size=1024 * 1024)
first_obj = next(iter(u0))
if isinstance(first_obj, dict):
# populate scoring_metrics, optimize bounds/limits, scenarios, scenario_labels
try:
optimize_config = first_obj.get('optimize', {}) or {}
self.scoring_metrics = _extract_scoring_metric_names(optimize_config.get('scoring', []) or [])
self.optimize_bounds = self._normalize_optimize_bounds(optimize_config.get('bounds', {}) or {})
self.optimize_limits = optimize_config.get('limits', []) or []
except Exception:
pass
try:
if 'suite_metrics' in first_obj:
suite_metrics_block = first_obj.get('suite_metrics', {}) or {}
if not self.scenario_labels:
self.scenario_labels = suite_metrics_block.get('scenario_labels', []) or []
backtest_config = first_obj.get('backtest', {}) or {}
suite_config = backtest_config.get('suite', {}) or {}
if suite_config.get('enabled'):
self.backtest_scenarios = suite_config.get('scenarios', []) or []
except Exception:
pass
except Exception:
pass
# -------- Persistent scan cache fast-path --------
# If the cache is valid, avoid decoding the entire all_results.bin.
# We'll select indices from cached arrays and only unpack selected configs by offsets.
t_cache_load0 = time.perf_counter()
scan_cache = self._load_scan_cache() if (not _DISABLE_SCAN_CACHE and self._is_scan_cache_valid()) else None
t_cache_load = time.perf_counter() - t_cache_load0
if scan_cache is not None:
meta = scan_cache.get("meta") or {}
arrays = scan_cache.get("arrays")
try:
# Restore globals from cache
self.scoring_metrics = _extract_scoring_metric_names(meta.get("scoring_metrics") or [])
self.scenario_labels = meta.get("scenario_labels") or []
self.optimize_bounds = self._normalize_optimize_bounds(meta.get("optimize_bounds") or {})
self.optimize_limits = meta.get("optimize_limits") or []
self.backtest_scenarios = meta.get("backtest_scenarios") or []
# If cache doesn't include fields required by the current strategy, fall back.
strategy = list(load_strategy) if load_strategy else ['performance']
required_fields = {"offsets", "constraint_violation", "primary"}
if 'robustness' in strategy:
required_fields.add("overall_robustness")
if 'sharpe' in strategy:
required_fields.add("sharpe")
if 'drawdown' in strategy:
required_fields.add("drawdown")
if 'calmar' in strategy:
required_fields.add("calmar")
if 'sortino' in strategy:
required_fields.add("sortino")
if 'omega' in strategy:
required_fields.add("omega")
if 'volatility' in strategy:
required_fields.add("volatility")
if 'recovery' in strategy:
required_fields.add("recovery")
available_fields = set(getattr(arrays, "files", []) or [])
missing = required_fields - available_fields
if missing:
scan_cache = None
raise RuntimeError(f"scan cache missing fields: {sorted(missing)}")
offsets = arrays["offsets"]
constraint_v = arrays["constraint_violation"]
primary_v = arrays["primary"]
overall_rob_v = arrays["overall_robustness"] if "overall_robustness" in available_fields else None
sharpe_v = arrays["sharpe"] if "sharpe" in available_fields else None
drawdown_v = arrays["drawdown"] if "drawdown" in available_fields else None
calmar_v = arrays["calmar"] if "calmar" in available_fields else None
sortino_v = arrays["sortino"] if "sortino" in available_fields else None
omega_v = arrays["omega"] if "omega" in available_fields else None
volatility_v = arrays["volatility"] if "volatility" in available_fields else None
recovery_v = arrays["recovery"] if "recovery" in available_fields else None
total_parsed = int(len(offsets))
if total_parsed <= 0:
scan_cache = None
else:
idxs = np.arange(total_parsed, dtype=np.int32)
configs_per_criterion = max(1, int(max_configs // len(strategy))) if strategy else max_configs
def _topk_by(keys: Tuple[np.ndarray, ...], k: int) -> np.ndarray:
order = np.lexsort(keys)
return order[:k]
# Precompute full performance ordering (used for fill)
perf_order = _topk_by((idxs, primary_v, constraint_v), min(max_configs, total_parsed))
# Note: primary_v is stored as (-primary_metric) already? No; stored as positive.
# For performance we want constraint asc, -primary desc, idx asc => keys: (idx, -primary, constraint)
perf_order = np.lexsort((idxs, -primary_v, constraint_v))[: min(max_configs, total_parsed)]
selected_order: List[int] = []
selected_set: set = set()
for criterion in strategy:
if criterion == 'performance':
order = perf_order[: min(configs_per_criterion, total_parsed)]
elif criterion == 'robustness':
if overall_rob_v is None:
scan_cache = None
raise RuntimeError('scan cache missing overall_robustness')
order = np.lexsort((idxs, constraint_v, -overall_rob_v))[: min(configs_per_criterion, total_parsed)]
elif criterion == 'sharpe':
if sharpe_v is None:
scan_cache = None
raise RuntimeError('scan cache missing sharpe')
order = np.lexsort((idxs, -sharpe_v, constraint_v))[: min(configs_per_criterion, total_parsed)]
elif criterion == 'drawdown':
if drawdown_v is None:
scan_cache = None
raise RuntimeError('scan cache missing drawdown')
order = np.lexsort((idxs, -drawdown_v, constraint_v))[: min(configs_per_criterion, total_parsed)]
elif criterion == 'calmar':
if calmar_v is None:
scan_cache = None
raise RuntimeError('scan cache missing calmar')
order = np.lexsort((idxs, -calmar_v, constraint_v))[: min(configs_per_criterion, total_parsed)]
elif criterion == 'sortino':
if sortino_v is None:
scan_cache = None
raise RuntimeError('scan cache missing sortino')
order = np.lexsort((idxs, -sortino_v, constraint_v))[: min(configs_per_criterion, total_parsed)]
elif criterion == 'omega':
if omega_v is None:
scan_cache = None
raise RuntimeError('scan cache missing omega')
order = np.lexsort((idxs, -omega_v, constraint_v))[: min(configs_per_criterion, total_parsed)]
elif criterion == 'volatility':
if volatility_v is None:
scan_cache = None
raise RuntimeError('scan cache missing volatility')
order = np.lexsort((idxs, volatility_v, constraint_v))[: min(configs_per_criterion, total_parsed)]
elif criterion == 'recovery':
if recovery_v is None:
scan_cache = None
raise RuntimeError('scan cache missing recovery')
order = np.lexsort((idxs, recovery_v, constraint_v))[: min(configs_per_criterion, total_parsed)]
else:
order = perf_order[: min(configs_per_criterion, total_parsed)]
for cand in order.tolist():
if cand in selected_set:
continue
selected_set.add(int(cand))
selected_order.append(int(cand))
if len(selected_order) < max_configs:
for cand in perf_order.tolist():
if len(selected_order) >= max_configs:
break
if cand in selected_set:
continue
selected_set.add(int(cand))
selected_order.append(int(cand))
if len(selected_order) > max_configs:
selected_order = selected_order[:max_configs]
# Parse selected configs by offsets
if progress_callback:
progress_callback(0, max(1, len(selected_order)), "Parsing selected configs (offset cache)...")
t_parse_selected0 = time.perf_counter()
pairs = [(int(offsets[i]), int(i)) for i in selected_order if 0 <= int(i) < total_parsed]
pairs.sort(key=lambda x: x[0])
idx_to_metrics: Dict[int, ConfigMetrics] = {}
idx_to_raw: Dict[int, Dict] = {}
with open(self.all_results_path, 'rb') as f:
file_size = os.path.getsize(self.all_results_path)
for n_done, (off, i) in enumerate(pairs, 1):
try:
# Prefer decoding from a bounded bytes slice so msgspec can be used.
end = int(offsets[i + 1]) if (int(i) + 1) < total_parsed else int(file_size)
if end <= int(off):
raise ValueError("invalid offset range")
f.seek(int(off))
payload = f.read(int(end - int(off)))
config_data = self._decode_msgpack_object(payload)
# Reuse precomputed robustness from scan cache (if present).
pre_rob = None
if overall_rob_v is not None:
try:
pre_rob = float(overall_rob_v[i])
except Exception:
pre_rob = None
if isinstance(config_data, dict) and any(isinstance(k, bytes) for k in config_data.keys()):
metrics = self._parse_config_light_raw(i, config_data, precomputed_overall_robustness=pre_rob)
else:
metrics = self._parse_config_light(
i,
config_data,
precomputed_overall_robustness=pre_rob,
compute_overall_robustness=False,
)
idx_to_metrics[i] = metrics
idx_to_raw[i] = config_data
except Exception:
# Fall back to legacy per-object unpack
try:
f.seek(int(off))
unpacker = msgpack.Unpacker(f, raw=True, strict_map_key=False)
config_data = next(iter(unpacker))
pre_rob = None
if overall_rob_v is not None:
try:
pre_rob = float(overall_rob_v[i])
except Exception:
pre_rob = None
metrics = self._parse_config_light_raw(i, config_data, precomputed_overall_robustness=pre_rob)
idx_to_metrics[i] = metrics
idx_to_raw[i] = config_data
except Exception:
continue
if progress_callback and (n_done % 50 == 0 or n_done == len(pairs)):
progress_callback(n_done, len(pairs), f"Parsed {n_done}/{len(pairs)} selected configs")
self.configs = [idx_to_metrics[i] for i in selected_order if i in idx_to_metrics]
self.raw_configs_cache = idx_to_raw
t_parse_selected = time.perf_counter() - t_parse_selected0
# Compute Pareto front
t_pareto0 = time.perf_counter()
self._compute_pareto_front_fast()
t_pareto = time.perf_counter() - t_pareto0
t_total = time.perf_counter() - t_total0
self.load_stats = {
'total_parsed': total_parsed,
'selected_configs': len(self.configs),
'pareto_configs': sum(1 for c in self.configs if c.is_pareto),
'scenarios': self.scenario_labels,
'scoring_metrics': self.scoring_metrics,
'load_strategy': load_strategy,
'max_configs': max_configs,
'timings': {
'total': t_total,
'load_pareto_hashes': t_load_pareto_hashes,
'parse_all_results': t_cache_load + t_parse_selected,
'scan_all_results': 0.0,
'parse_selected_configs': t_parse_selected,
'select_top_configs': 0.0,
'compute_pareto_front': t_pareto,
},
}
return True
except Exception:
# Fall back to full scan
scan_cache = None
# Parse configs streaming from msgpack (avoid materializing entire file as a list)
if progress_callback:
progress_callback(0, os.path.getsize(self.all_results_path), "Loading/parsing all_results.bin...")
def _metrics_dict_from_config_data(config_data: Dict) -> Dict:
if 'suite_metrics' in config_data:
suite_metrics_block = config_data.get('suite_metrics', {}) or {}
if not self.scenario_labels:
self.scenario_labels = suite_metrics_block.get('scenario_labels', []) or []
return suite_metrics_block.get('metrics', {}) or {}
metrics_block = config_data.get('metrics', {}) or {}
return metrics_block.get('stats', {}) or {}
def _ensure_globals_from_config_data(config_data: Dict) -> None:
if 'optimize' in config_data:
optimize_config = config_data.get('optimize', {}) or {}
if not self.scoring_metrics:
self.scoring_metrics = _extract_scoring_metric_names(optimize_config.get('scoring', []) or [])
if not self.optimize_bounds:
self.optimize_bounds = self._normalize_optimize_bounds(optimize_config.get('bounds', {}) or {})
if not self.optimize_limits:
self.optimize_limits = optimize_config.get('limits', []) or []
if not self.backtest_scenarios and 'backtest' in config_data:
backtest_config = config_data.get('backtest', {}) or {}
suite_config = backtest_config.get('suite', {}) or {}
if suite_config.get('enabled'):
self.backtest_scenarios = suite_config.get('scenarios', []) or []
def _aggregated_metric_value(metric_data: Any) -> float:
if isinstance(metric_data, dict):
if 'aggregated' in metric_data:
try:
return float(metric_data.get('aggregated') or 0.0)
except Exception:
return 0.0
if 'mean' in metric_data:
try:
return float(metric_data.get('mean') or 0.0)
except Exception:
return 0.0
stats = metric_data.get('stats')
if isinstance(stats, dict):
try:
return float(stats.get('mean') or 0.0)
except Exception:
return 0.0
return 0.0
if isinstance(metric_data, (int, float)):
return float(metric_data)
return 0.0
def _mean_std_from_metric_data(metric_data: Any) -> Tuple[float, float]:
if not isinstance(metric_data, dict):
return 0.0, 0.0
if 'stats' in metric_data and isinstance(metric_data.get('stats'), dict):
stats = metric_data.get('stats', {}) or {}
mean = stats.get('mean', 0.0)
std = stats.get('std', 0.0)
else:
mean = metric_data.get('mean', 0.0)
std = metric_data.get('std', 0.0)
try:
return float(mean or 0.0), float(std or 0.0)
except Exception:
return 0.0, 0.0
parsed_count = 0
total_parsed = 0
strategy = list(load_strategy) if load_strategy else ['performance']
only_performance = len(strategy) == 1 and strategy[0] == 'performance'
configs_per_criterion = max_configs // len(strategy) if strategy else max_configs
configs_per_criterion = max(1, int(configs_per_criterion))
needs_robustness = 'robustness' in strategy
needs_sharpe = 'sharpe' in strategy
needs_drawdown = 'drawdown' in strategy
needs_calmar = 'calmar' in strategy
needs_sortino = 'sortino' in strategy
needs_omega = 'omega' in strategy
needs_volatility = 'volatility' in strategy
needs_recovery = 'recovery' in strategy
# Heaps store (priority_tuple, idx, key_tuple)
heaps: Dict[str, List[Tuple[Tuple[float, ...], int, Tuple[float, ...]]]] = {c: [] for c in strategy}
perf_fill_heap: List[Tuple[Tuple[float, ...], int, Tuple[float, ...]]] = []
def _push_candidate(heap: List, k: int, idx: int, key: Tuple[float, ...]):
priority = tuple(-float(x) for x in key)
item = (priority, int(idx), key)
if len(heap) < k:
heapq.heappush(heap, item)
return
if item[0] > heap[0][0]:
heapq.heapreplace(heap, item)
# For building a persistent scan cache
offsets_all: List[int] = []
constraint_all: List[float] = []
primary_all: List[float] = []
# Default: cache only fields required by the current strategy.
# Opt-in full cache (faster strategy switching, slower first scan) via PBG_FULL_SCAN_CACHE=1.
cache_full = bool(_FULL_SCAN_CACHE)
store_sharpe = cache_full or needs_sharpe
store_drawdown = cache_full or needs_drawdown
store_calmar = cache_full or needs_calmar
store_sortino = cache_full or needs_sortino
store_omega = cache_full or needs_omega
store_volatility = cache_full or needs_volatility
store_recovery = cache_full or needs_recovery
store_overall_rob = cache_full or needs_robustness
fast_perf_scan = bool(only_performance and not cache_full)
sharpe_all: Optional[List[float]] = [] if store_sharpe else None
drawdown_all: Optional[List[float]] = [] if store_drawdown else None
calmar_all: Optional[List[float]] = [] if store_calmar else None
sortino_all: Optional[List[float]] = [] if store_sortino else None
omega_all: Optional[List[float]] = [] if store_omega else None
volatility_all: Optional[List[float]] = [] if store_volatility else None
recovery_all: Optional[List[float]] = [] if store_recovery else None
overall_rob_all: Optional[List[float]] = [] if store_overall_rob else None
# Use raw=True for the scan to avoid decoding all strings on first load.
# We'll parse selected configs with raw=False afterwards.
primary_metric = self.scoring_metrics[0] if self.scoring_metrics else 'adg_w_usd'
primary_metric_b = primary_metric.encode('utf-8', errors='ignore')
def _get(d: Any, k: Any, default=None):
if isinstance(d, dict):
return d.get(k, default)
return default
def _metrics_dict_from_raw(obj: Dict) -> Dict:
if b'suite_metrics' in obj:
suite = _get(obj, b'suite_metrics', {}) or {}
return _get(suite, b'metrics', {}) or {}
m = _get(obj, b'metrics', {}) or {}
return _get(m, b'stats', {}) or {}
def _aggregated_metric_value_raw(metric_data: Any) -> float:
if isinstance(metric_data, dict):
if b'aggregated' in metric_data:
v = metric_data.get(b'aggregated')
try:
return float(v or 0.0)
except Exception:
return 0.0
if b'mean' in metric_data:
v = metric_data.get(b'mean')
try:
return float(v or 0.0)
except Exception:
return 0.0
stats = metric_data.get(b'stats')
if isinstance(stats, dict):
v = stats.get(b'mean')
try:
return float(v or 0.0)
except Exception:
return 0.0
return 0.0
if isinstance(metric_data, (int, float)):
return float(metric_data)
return 0.0
def _mean_std_from_metric_data_raw(metric_data: Any) -> Tuple[float, float]:
if not isinstance(metric_data, dict):
return 0.0, 0.0
if b'stats' in metric_data and isinstance(metric_data.get(b'stats'), dict):
stats = metric_data.get(b'stats', {}) or {}
mean = stats.get(b'mean', 0.0)
std = stats.get(b'std', 0.0)
else:
mean = metric_data.get(b'mean', 0.0)
std = metric_data.get(b'std', 0.0)
try:
return float(mean or 0.0), float(std or 0.0)
except Exception:
return 0.0, 0.0
t_scan0 = time.perf_counter()
try:
for idx, (offset, config_data) in enumerate(self._iter_binary_file(progress_callback=progress_callback, with_offsets=True, raw_mode=True)):
total_parsed += 1
offsets_all.append(int(offset))
# raw=True scan extraction
metrics_dict = _metrics_dict_from_raw(config_data)
metrics_block = _get(config_data, b'metrics', {}) or {}
try:
constraint_violation = float(metrics_block.get(b'constraint_violation', 0.0) or 0.0)
except Exception:
constraint_violation = 0.0
primary_val = _aggregated_metric_value_raw(metrics_dict.get(primary_metric_b))
if fast_perf_scan:
constraint_all.append(float(constraint_violation))
primary_all.append(float(primary_val))
sharpe_val = drawdown_val = calmar_val = sortino_val = omega_val = volatility_val = recovery_val = 0.0
overall_robustness = 0.0
else:
sharpe_val = _aggregated_metric_value_raw(metrics_dict.get(b'sharpe_ratio_usd')) if store_sharpe else 0.0
drawdown_val = _aggregated_metric_value_raw(metrics_dict.get(b'drawdown_worst_usd')) if store_drawdown else 0.0
calmar_val = _aggregated_metric_value_raw(metrics_dict.get(b'calmar_ratio_usd')) if store_calmar else 0.0
sortino_val = _aggregated_metric_value_raw(metrics_dict.get(b'sortino_ratio_usd')) if store_sortino else 0.0
omega_val = _aggregated_metric_value_raw(metrics_dict.get(b'omega_ratio_usd')) if store_omega else 0.0
volatility_val = _aggregated_metric_value_raw(metrics_dict.get(b'equity_volatility_usd')) if store_volatility else 0.0
recovery_val = _aggregated_metric_value_raw(metrics_dict.get(b'drawdown_recovery_hours_mean')) if store_recovery else 0.0
overall_robustness = 0.0
if store_overall_rob and metrics_dict:
# Match compute_overall_robustness() semantics without allocating dicts
score_sum = 0.0
score_n = 0
for metric_data in metrics_dict.values():
if not isinstance(metric_data, dict):
continue
mean, std = _mean_std_from_metric_data_raw(metric_data)
if abs(mean) > 1e-10:
cv = abs(std / mean)
score_sum += 1.0 / (1.0 + cv)
else:
score_sum += 1.0
score_n += 1
overall_robustness = (score_sum / score_n) if score_n else 0.0
constraint_all.append(float(constraint_violation))
primary_all.append(float(primary_val))
if sharpe_all is not None:
sharpe_all.append(float(sharpe_val))
if drawdown_all is not None:
drawdown_all.append(float(drawdown_val))
if calmar_all is not None:
calmar_all.append(float(calmar_val))
if sortino_all is not None:
sortino_all.append(float(sortino_val))
if omega_all is not None:
omega_all.append(float(omega_val))
if volatility_all is not None:
volatility_all.append(float(volatility_val))
if recovery_all is not None:
recovery_all.append(float(recovery_val))
if overall_rob_all is not None:
overall_rob_all.append(float(overall_robustness))
key_perf = (constraint_violation, -primary_val, int(idx))
# Always maintain performance heap for fill.
_push_candidate(perf_fill_heap, max_configs, idx, key_perf)
for criterion in strategy:
if criterion == 'performance':
key = key_perf
_push_candidate(heaps[criterion], configs_per_criterion, idx, key)
elif criterion == 'robustness':
key = (-overall_robustness, constraint_violation, int(idx))
_push_candidate(heaps[criterion], configs_per_criterion, idx, key)
elif criterion == 'sharpe':
key = (constraint_violation, -sharpe_val, int(idx))
_push_candidate(heaps[criterion], configs_per_criterion, idx, key)
elif criterion == 'drawdown':
key = (constraint_violation, -drawdown_val, int(idx))
_push_candidate(heaps[criterion], configs_per_criterion, idx, key)
elif criterion == 'calmar':
key = (constraint_violation, -calmar_val, int(idx))
_push_candidate(heaps[criterion], configs_per_criterion, idx, key)
elif criterion == 'sortino':
key = (constraint_violation, -sortino_val, int(idx))
_push_candidate(heaps[criterion], configs_per_criterion, idx, key)
elif criterion == 'omega':
key = (constraint_violation, -omega_val, int(idx))
_push_candidate(heaps[criterion], configs_per_criterion, idx, key)
elif criterion == 'volatility':
key = (constraint_violation, volatility_val, int(idx))
_push_candidate(heaps[criterion], configs_per_criterion, idx, key)
elif criterion == 'recovery':
key = (constraint_violation, recovery_val, int(idx))
_push_candidate(heaps[criterion], configs_per_criterion, idx, key)
else:
key = key_perf
_push_candidate(heaps[criterion], configs_per_criterion, idx, key)
parsed_count += 1
except Exception as e:
_log('ParetoDataLoader', f'Error during scan: {e}', level='ERROR',
meta={'traceback': traceback.format_exc()})
return False
t_scan = time.perf_counter() - t_scan0
if parsed_count == 0:
return False
# Determine selected indices in stable order (same semantics as _select_top_configs_by_strategy)
t_select0 = time.perf_counter()
selected_order: List[int] = []
selected_set: set = set()
for criterion in strategy:
heap_items = heaps.get(criterion) or []
# Sort by original key (ascending = best first)
for _, cand_idx, _ in sorted(heap_items, key=lambda x: x[2]):
if cand_idx in selected_set:
continue
selected_set.add(cand_idx)
selected_order.append(cand_idx)
# Fill remaining with best-by-performance
if len(selected_order) < max_configs:
for _, cand_idx, _ in sorted(perf_fill_heap, key=lambda x: x[2]):
if len(selected_order) >= max_configs:
break
if cand_idx in selected_set:
continue
selected_set.add(cand_idx)
selected_order.append(cand_idx)
# Ensure hard cap
if len(selected_order) > max_configs:
selected_order = selected_order[:max_configs]
selected_set = set(selected_order)
t_select = time.perf_counter() - t_select0
# Parse selected configs by offsets (no second full-file pass; no candidate dict caching)
if progress_callback:
progress_callback(0, max(1, len(selected_order)), "Parsing selected configs (offsets)...")
t_parse_selected0 = time.perf_counter()
pairs = [(int(offsets_all[i]), int(i)) for i in selected_order if 0 <= int(i) < len(offsets_all)]
pairs.sort(key=lambda x: x[0])
parsed_selected: Dict[int, ConfigMetrics] = {}
raw_selected: Dict[int, Dict] = {}
with open(self.all_results_path, 'rb') as f:
file_size = os.path.getsize(self.all_results_path)
for n_done, (off, idx) in enumerate(pairs, 1):
try:
# Prefer decoding from a bounded bytes slice so msgspec can be used.
end = int(offsets_all[idx + 1]) if (int(idx) + 1) < len(offsets_all) else int(file_size)
if end <= int(off):
raise ValueError("invalid offset range")
f.seek(int(off))
payload = f.read(int(end - int(off)))
config_data = self._decode_msgpack_object(payload)
# Use precomputed robustness from the scan phase (if available) to skip recomputation here.
pre_rob = None
if overall_rob_all is not None:
try:
pre_rob = float(overall_rob_all[idx])
except Exception:
pre_rob = None
if isinstance(config_data, dict) and any(isinstance(k, bytes) for k in config_data.keys()):
metrics = self._parse_config_light_raw(idx, config_data, precomputed_overall_robustness=pre_rob)
else:
metrics = self._parse_config_light(
idx,
config_data,
precomputed_overall_robustness=pre_rob,
compute_overall_robustness=False,
)
parsed_selected[idx] = metrics
raw_selected[idx] = config_data
except Exception:
# Fall back to legacy per-object unpack
try:
f.seek(int(off))
unpacker = msgpack.Unpacker(f, raw=True, strict_map_key=False)
config_data = next(iter(unpacker))
pre_rob = None
if overall_rob_all is not None:
try:
pre_rob = float(overall_rob_all[idx])
except Exception:
pre_rob = None
metrics = self._parse_config_light_raw(idx, config_data, precomputed_overall_robustness=pre_rob)
parsed_selected[idx] = metrics
raw_selected[idx] = config_data
except Exception:
continue
if progress_callback and (n_done % 50 == 0 or n_done == len(pairs)):
progress_callback(n_done, len(pairs), f"Parsed {n_done}/{len(pairs)} selected configs")
self.raw_configs_cache = raw_selected
self.configs = [parsed_selected[i] for i in selected_order if i in parsed_selected]
t_parse_selected = time.perf_counter() - t_parse_selected0
total_parsed = total_parsed
t_parse = t_scan + t_parse_selected
# Write persistent scan cache (best-effort)
if not _DISABLE_SCAN_CACHE:
try:
st_bin = os.stat(self.all_results_path)
cache_meta = {
"version": 2,
"source": {"size": int(st_bin.st_size), "mtime": float(st_bin.st_mtime)},
"scoring_metrics": self.scoring_metrics,
"scenario_labels": self.scenario_labels,
"optimize_bounds": self.optimize_bounds,
"optimize_limits": self.optimize_limits,
"backtest_scenarios": self.backtest_scenarios,
"has_overall_robustness": bool(store_overall_rob),
"cached_fields": [