-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy path_logparser.py
173 lines (147 loc) · 5.81 KB
/
_logparser.py
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
import re
import fractions
from collections import namedtuple
LabelScore = namedtuple("LabelScore", "match model ref precision recall f1")
class TrainLogParser:
def __init__(self):
self.state = None
self.featgen_percent = -2
self.featgen_num_features = None
self.featgen_seconds = None
self.training_seconds = None
self.storing_seconds = None
self.iterations = []
self.last_iteration = None
self.log = []
self.events = []
def feed(self, line):
# if line != '\n':
self.log.append(line)
if self.state is None:
self.state = "STARTING"
self.handle_STARTING(line)
self.events.append(("start", 0, len(self.log)))
return "start"
event = getattr(self, "handle_" + self.state)(line)
if event is not None:
start, end = self.events[-1][2], len(self.log)
if event in ("prepared", "optimization_end"):
end -= 1
self.events.append((event, start, end))
return event
@property
def last_log(self):
event, start, end = self.events[-1]
return "".join(self.log[start:end])
def handle_STARTING(self, line):
if line.startswith("Feature generation"):
self.state = "FEATGEN"
def handle_FEATGEN(self, line):
if line in "0123456789.10":
self.featgen_percent += 2
return "featgen_progress"
m = re.match(r"Number of features: (\d+)", line)
if m:
self.featgen_num_features = int(m.group(1))
return None
if self._seconds(line) is not None:
self.featgen_seconds = self._seconds(line)
self.state = "AFTER_FEATGEN"
return "featgen_end"
def handle_AFTER_FEATGEN(self, line):
if self._iteration_head(line) is not None:
self.state = "ITERATION"
self.handle_ITERATION(line)
return "prepared"
if "terminated with error" in line:
self.state = "AFTER_ITERATION"
return "prepare_error"
def handle_ITERATION(self, line):
if self._iteration_head(line) is not None:
self.last_iteration = {
"num": self._iteration_head(line),
"scores": {},
}
self.iterations.append(self.last_iteration)
elif line == "\n":
self.state = "AFTER_ITERATION"
return "iteration"
def add_re(key, pattern, typ):
m = re.match(pattern, line)
if m:
self.last_iteration[key] = typ(m.group(1))
add_re("loss", r"Loss: (\d+\.\d+)", float)
add_re("feature_norm", r"Feature norm: (\d+\.\d+)", float)
add_re("error_norm", r"Error norm: (\d+\.\d+)", float)
add_re("active_features", r"Active features: (\d+)", int)
add_re("linesearch_trials", r"Line search trials: (\d+)", int)
add_re("linesearch_step", r"Line search step: (\d+\.\d+)", float)
add_re("time", r"Seconds required for this iteration: (\d+\.\d+)", float)
m = re.match(
r"Macro-average precision, recall, F1: \((\d\.\d+), (\d\.\d+), (\d\.\d+)\)",
line,
)
if m:
self.last_iteration["avg_precision"] = float(m.group(1))
self.last_iteration["avg_recall"] = float(m.group(2))
self.last_iteration["avg_f1"] = float(m.group(3))
m = re.match(r"Item accuracy: (\d+) / (\d+)", line)
if m:
acc = fractions.Fraction(int(m.group(1)), int(m.group(2)))
self.last_iteration["item_accuracy"] = acc
self.last_iteration["item_accuracy_float"] = float(acc)
m = re.match(r"Instance accuracy: (\d+) / (\d+)", line)
if m:
acc = fractions.Fraction(int(m.group(1)), int(m.group(2)))
self.last_iteration["instance_accuracy"] = acc
self.last_iteration["instance_accuracy_float"] = float(acc)
m = re.match(
r"\s{4}(.+): \((\d+), (\d+), (\d+)\) \((\d\.\d+), (\d\.\d+), (\d\.\d+)\)",
line,
)
if m:
self.last_iteration["scores"][m.group(1)] = LabelScore(
**{
"match": int(m.group(2)),
"model": int(m.group(3)),
"ref": int(m.group(4)),
"precision": float(m.group(5)),
"recall": float(m.group(6)),
"f1": float(m.group(7)),
}
)
m = re.match(r"\s{4}(.+): \(0, 0, 0\) \(\*{6}, \*{6}, \*{6}\)", line)
if m:
self.last_iteration["scores"][m.group(1)] = LabelScore(
**{
"match": 0,
"model": 0,
"ref": 0,
"precision": None,
"recall": None,
"f1": None,
}
)
def handle_AFTER_ITERATION(self, line):
if self._iteration_head(line) is not None:
self.state = "ITERATION"
return self.handle_ITERATION(line)
m = re.match(r"Total seconds required for training: (\d+\.\d+)", line)
if m:
self.training_seconds = float(m.group(1))
if line.startswith("Storing the model"):
self.state = "STORING"
return "optimization_end"
def handle_STORING(self, line):
if line == "\n":
return "end"
elif self._seconds(line):
self.storing_seconds = self._seconds(line)
def _iteration_head(self, line):
m = re.match(r"\*{5} (?:Iteration|Epoch) #(\d+) \*{5}\n", line)
if m:
return int(m.group(1))
def _seconds(self, line):
m = re.match(r"Seconds required: (\d+\.\d+)", line)
if m:
return float(m.group(1))