Repository navigation
Expand file tree
/
Copy pathparser.py
More file actions
55 lines (43 loc) · 1.71 KB
/
Copy pathparser.py
File metadata and controls
55 lines (43 loc) · 1.71 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
"""Output parser for ml-clustering-algorithm.
Training feedback: TRAIN_METRICS stage=... or metric=value lines
Evaluation: TEST_METRICS ari=... nmi=... silhouette=...
Leaderboard metrics: ari_<env>, nmi_<env>, silhouette_<env>
"""
import re
import sys
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent
sys.path.insert(0, str(PROJECT_ROOT / "src"))
from mlsbench.agent.parsers import OutputParser, ParseResult
class Parser(OutputParser):
"""Parser for ml-clustering-algorithm task."""
def parse(self, cmd_label: str, raw_output: str) -> ParseResult:
feedback_parts = []
metrics = {}
# --- TRAIN_METRICS (feedback only) ---
train_lines = [
l.strip()
for l in raw_output.splitlines()
if l.strip().startswith("TRAIN_METRICS")
]
if train_lines:
feedback_parts.append(
f"Training progress ({cmd_label}):\n"
+ "\n".join(train_lines[-5:])
)
# --- TEST_METRICS (feedback + leaderboard) ---
for line in raw_output.splitlines():
line = line.strip()
if line.startswith("TEST_METRICS"):
for match in re.finditer(r"(\w+)=([\d.eE+-]+)", line):
key, val = match.group(1), float(match.group(2))
metric_key = f"{key}_{cmd_label}"
metrics[metric_key] = val
feedback_parts.append(f"{metric_key}: {val:.6f}")
# --- Fallback ---
if not feedback_parts:
feedback_parts.append(raw_output[-3000:])
return ParseResult(
feedback="\n".join(feedback_parts),
metrics=metrics,
)