-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptim_combine.py
More file actions
73 lines (61 loc) · 2.3 KB
/
optim_combine.py
File metadata and controls
73 lines (61 loc) · 2.3 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
import pickle
import re
from pathlib import Path
import calendar
result_dir = Path('C:/Users/Vincent/Downloads/train/optim/') # dossier des .pkl
# ---- pick your month(s) to EXCLUDE here ----
exclude_months_input = ['08'] # accepts: 8, '08', 'Aug', 'August', 'août', 'aout', etc.
# -------------------------------------------
# Build a lookup for month names/abbr (EN) + a couple FR variants
MONTH_LOOKUP = {name.lower(): i for i, name in enumerate(calendar.month_name) if name}
MONTH_LOOKUP.update({name.lower(): i for i, name in enumerate(calendar.month_abbr) if name})
MONTH_LOOKUP.update({'août': 8, 'aout': 8})
def to_month_int(x):
if isinstance(x, int):
if 1 <= x <= 12:
return x
raise ValueError(f"Invalid month int: {x}")
s = str(x).strip().lower()
if s.isdigit():
m = int(s)
if 1 <= m <= 12:
return m
raise ValueError(f"Invalid month number: {x}")
if s in MONTH_LOOKUP:
return MONTH_LOOKUP[s]
raise ValueError(f"Unknown month label: {x}")
exclude_months = {to_month_int(m) for m in exclude_months_input}
all_results = []
all_pkls = sorted(result_dir.glob('gridsearch_results_*_*.pkl'))
def extract_month_from_name(name: str):
"""Return month (1-12) parsed from first YYYYMMDD in filename; None if not found."""
m = re.search(r'(\d{8})', name) # find YYYYMMDD
if not m:
return None
yyyymmdd = m.group(1)
return int(yyyymmdd[4:6])
selected_pkls, skipped_pkls = [], []
for p in all_pkls:
mon = extract_month_from_name(p.name)
if mon is not None and mon in exclude_months:
skipped_pkls.append(p)
else:
selected_pkls.append(p)
for pkl_file in selected_pkls:
with open(pkl_file, 'rb') as f:
try:
data = pickle.load(f)
if isinstance(data, list):
all_results.extend(data)
else:
all_results.append(data)
except Exception as e:
print(f"Erreur lecture {pkl_file}: {e}")
print(f"Fichiers pris: {len(selected_pkls)} | ignorés (mois exclus): {len(skipped_pkls)}")
if skipped_pkls:
for p in skipped_pkls:
pass
# print(f"- {p.name}")
print(f"Fusion terminée! {len(all_results)} runs.")
with open('ALL_gridsearch_results_1.pkl', 'wb') as f:
pickle.dump(all_results, f)