-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdirty_evaluate.py
More file actions
369 lines (309 loc) · 11.1 KB
/
dirty_evaluate.py
File metadata and controls
369 lines (309 loc) · 11.1 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
from tqdm import tqdm
from pathlib import Path
from typing import Optional
from concurrent.futures import ThreadPoolExecutor, as_completed
import time
import threading
import signal
import sys
from src.agent import init_pipeline
from src.backtesting.dataset import load_examples
from src.backtesting.metrics import (
soft_cross_entropy,
validate_directional,
score_stats,
)
from src.backtesting.metrics import brier_score as brier_score_fn
from src.scripts.evaluate import jsonify_eval_outputs
class TimeoutException(Exception):
pass
# Flag to track if Ctrl+C was pressed
ctrl_c_pressed = False
def signal_handler(sig, frame):
"""Handle SIGINT (Ctrl+C) signal."""
global ctrl_c_pressed
if not ctrl_c_pressed:
print(
"\nCtrl+C detected. Finishing current tasks and preparing to exit gracefully..."
)
ctrl_c_pressed = True
else:
# If Ctrl+C is pressed a second time, exit immediately
print("\nForced exit. Results may be incomplete.")
sys.exit(1)
def run_with_timeout(func, args=None, kwargs=None, timeout=None):
"""
Run a function with a timeout.
Args:
func: The function to run.
args: Arguments to pass to the function.
kwargs: Keyword arguments to pass to the function.
timeout: The timeout in seconds. If None, no timeout is applied.
Returns:
The result of the function.
Raises:
TimeoutException: If the function does not complete within the timeout.
"""
if args is None:
args = ()
if kwargs is None:
kwargs = {}
if timeout is None:
return func(*args, **kwargs)
result_container = []
exception_container = []
def worker():
try:
result_container.append(func(*args, **kwargs))
except Exception as e:
exception_container.append(e)
thread = threading.Thread(target=worker)
thread.daemon = True
thread.start()
thread.join(timeout)
if thread.is_alive():
raise TimeoutException("Function execution timed out")
if exception_container:
raise exception_container[0]
return result_container[0]
def process_example(args):
"""
Process a single example with timeout and error handling.
Returns a tuple (example, prediction, cross_entropy_score, directional_score, l1_score, status, error_msg, elapsed)
where status is one of "success", "timeout", or "error"
"""
example, predict_market, use_brier, timeout = args
error_msg = None
status = "success"
start_time = time.time()
try:
# Define a function that will process this example
def process_func():
prediction = predict_market(
question=example.question,
description=example.description,
current_date=example.current_date,
creatorUsername=example.creatorUsername,
comments=example.comments,
cutoff_date=example.cutoff_date,
)
if use_brier:
score = brier_score_fn(example, prediction)
else:
score = soft_cross_entropy(example, prediction)
directional_score = validate_directional(example, prediction)
return prediction, score, directional_score
# Run the function with a timeout
prediction, score, directional_score = run_with_timeout(
process_func, timeout=timeout
)
except TimeoutException:
prediction = None
score = None
directional_score = None
status = "timeout"
error_msg = "Evaluation timed out"
except Exception as e:
prediction = None
score = None
directional_score = None
status = "error"
error_msg = str(e)
elapsed = time.time() - start_time
if status == "timeout" and timeout is not None:
elapsed = timeout # Cap at timeout value
return (
example,
prediction,
score,
directional_score,
status,
error_msg,
elapsed,
)
def evaluate(
config_path: Path,
parquet_path: Path,
max_examples: Optional[int],
log_level: str,
num_threads: int,
trade_from_start: bool,
use_brier: bool,
min_num_trades: Optional[int],
timeout: Optional[int],
):
# Set up the signal handler for Ctrl+C
signal.signal(signal.SIGINT, signal_handler)
predict_market, logger, evalfile_name, cutoff_date, exclude_groups = init_pipeline(
config_path, log_level, "eval"
)
examples = load_examples(
parquet_path,
cutoff_date,
exclude_groups,
trade_from_start,
yes_no_resolution=use_brier,
max_examples=max_examples,
min_num_trades=min_num_trades,
)
predictions = []
scores = []
directional_scores = []
result_triples = []
timeouts_count = 0
errors_count = 0
processing_times = []
failed_examples = []
completed_count = 0
total_examples = len(examples)
# Prepare arguments for parallel processing
process_args = [
(example, predict_market, use_brier, timeout) for example in examples
]
# Use ThreadPoolExecutor for parallel processing
with ThreadPoolExecutor(max_workers=num_threads) as executor:
# Submit all tasks
futures_to_args = {
executor.submit(process_example, arg): arg for arg in process_args
}
# Track progress using tqdm
progress_bar = tqdm(total=total_examples, desc="Processing examples")
try:
# Process completed futures as they come in
for future in as_completed(futures_to_args):
if ctrl_c_pressed:
# Cancel any pending futures when Ctrl+C is pressed
for f in futures_to_args.keys():
if not f.done():
f.cancel()
logger.info("Ctrl+C detected. Processing partial results...")
break
(
example,
prediction,
score,
directional_score,
status,
error_msg,
elapsed,
) = future.result()
completed_count += 1
processing_times.append(elapsed)
progress_bar.update(1)
if status == "timeout":
timeouts_count += 1
logger.warning(
f"Example timed out after {timeout} seconds: {example.question[:50]}..."
)
failed_examples.append(
{
"question": example.question,
"status": status,
"error": error_msg,
"elapsed": elapsed,
}
)
continue
elif status == "error":
errors_count += 1
logger.error(
f"Example errored: {example.question[:50]}... Error: {error_msg}"
)
failed_examples.append(
{
"question": example.question,
"status": status,
"error": error_msg,
"elapsed": elapsed,
}
)
continue
predictions.append(prediction)
if score is not None:
scores.append(score)
if directional_score is not None:
directional_scores.append(directional_score)
if prediction is not None:
result_triples.append((example, prediction, score))
except Exception as e:
logger.error(f"Unexpected error: {e}")
finally:
progress_bar.close()
# Save results
logger.info(f"Saving results from {len(result_triples)} completed examples")
jsonify_eval_outputs(result_triples, evalfile_name)
logger.info("Evaluation results saved to %s", evalfile_name)
# Calculate statistics
if scores:
score_mean, score_confidence = score_stats(scores)
directional_mean, directional_confidence = score_stats(directional_scores)
# Save failed examples to a separate file
if failed_examples:
import json
from datetime import datetime
failed_file = (
Path(evalfile_name).parent
/ f"failed_examples_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
)
with open(failed_file, "w") as f:
json.dump(failed_examples, f, indent=2)
logger.info(f"Failed examples saved to {failed_file}")
# Log completion status
if ctrl_c_pressed:
logger.info(
f"Evaluation stopped early due to Ctrl+C. Processed {completed_count}/{total_examples} examples."
)
else:
logger.info(
f"Processed all {total_examples} examples with {num_threads} threads"
)
# Log results
logger.info(
f"Timed out: {timeouts_count} examples ({timeouts_count/completed_count*100:.2f}%)"
)
logger.info(
f"Errors: {errors_count} examples ({errors_count/completed_count*100:.2f}%)"
)
logger.info(
f"Successful: {completed_count - timeouts_count - errors_count} examples ({(completed_count - timeouts_count - errors_count)/completed_count*100:.2f}%)"
)
if processing_times:
logger.info(
f"Average processing time: {sum(processing_times)/len(processing_times):.2f} seconds"
)
if scores:
score_type = "Brier" if use_brier else "Cross-entropy"
logger.info(f"{score_type}: mean {score_mean}, 95% CI +-{score_confidence}")
logger.info(
f"Directional: mean {directional_mean}, 95% CI +-{directional_confidence}"
)
return result_triples
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--config_path", type=Path, required=True)
parser.add_argument(
"--parquet_path", type=Path, default="processed_data/test.parquet"
)
parser.add_argument("--max_examples", type=int, default=None)
parser.add_argument("--log_level", type=str, default="INFO")
parser.add_argument("--num_threads", type=int, default=1)
parser.add_argument("--timeout", type=int, default=None)
parser.add_argument("--random_snapshot", action="store_true")
parser.add_argument("--score_type", type=str, default="brier")
parser.add_argument("--min_num_trades", type=int, default=10)
args = parser.parse_args()
assert args.score_type in ["brier", "cross_entropy"], "Invalid score type"
evaluate(
args.config_path,
args.parquet_path,
args.max_examples,
args.log_level,
args.num_threads,
not args.random_snapshot,
args.score_type == "brier",
args.min_num_trades,
args.timeout,
)
if __name__ == "__main__":
main()