-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_evaluation_postprocess.py
More file actions
441 lines (394 loc) · 17.5 KB
/
test_evaluation_postprocess.py
File metadata and controls
441 lines (394 loc) · 17.5 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
"""Tests for evaluation postprocess functionality."""
import pytest
from unittest.mock import Mock, patch
from eval_protocol.models import EvaluationRow, EvaluateResult, EvalMetadata, ExecutionMetadata, InputMetadata, Message
from eval_protocol.pytest.evaluation_test_postprocess import postprocess
from eval_protocol.stats.confidence_intervals import compute_fixed_set_mu_ci
class TestPostprocess:
"""Tests for postprocess function."""
def create_test_row(self, score: float, is_valid: bool = True) -> EvaluationRow:
"""Helper to create a test evaluation row."""
return EvaluationRow(
messages=[],
evaluation_result=EvaluateResult(score=score, is_score_valid=is_valid, reason="test"),
input_metadata=InputMetadata(completion_params={"model": "test-model"}),
execution_metadata=ExecutionMetadata(),
eval_metadata=EvalMetadata(
name="test",
description="test",
version="1.0",
status=None,
num_runs=1,
aggregation_method="mean",
passed_threshold=None,
passed=None,
),
)
@patch.dict("os.environ", {"EP_NO_UPLOAD": "1"}) # Disable uploads
def test_bootstrap_aggregation_with_valid_scores(self):
"""Test bootstrap aggregation with all valid scores and verify exact scores list."""
# Create test data: 2 runs with 2 rows each
all_results = [
[self.create_test_row(0.8), self.create_test_row(0.6)], # Run 1
[self.create_test_row(0.7), self.create_test_row(0.9)], # Run 2
]
mock_logger = Mock()
# Mock the aggregate function to capture the exact scores passed to it
with patch("eval_protocol.pytest.evaluation_test_postprocess.aggregate") as mock_aggregate:
mock_aggregate.return_value = 0.75 # Mock return value
postprocess(
all_results=all_results,
aggregation_method="bootstrap",
threshold=None,
active_logger=mock_logger,
mode="pointwise",
completion_params={"model": "test-model"},
test_func_name="test_bootstrap",
num_runs=2,
experiment_duration_seconds=10.0,
)
# Check that aggregate was called with all individual scores in order
mock_aggregate.assert_called_once_with([0.8, 0.6, 0.7, 0.9], "bootstrap")
# Should call logger.log for each row
assert mock_logger.log.call_count == 4
@patch.dict("os.environ", {"EP_NO_UPLOAD": "1"}) # Disable uploads
def test_bootstrap_aggregation_filters_invalid_scores(self):
"""Test that bootstrap aggregation excludes invalid scores and generates correct scores list."""
# Create test data with some invalid scores
all_results = [
[
self.create_test_row(0.8, is_valid=True),
self.create_test_row(0.0, is_valid=False), # Invalid - should be excluded
],
[
self.create_test_row(0.7, is_valid=True),
self.create_test_row(0.0, is_valid=False), # Invalid - should be excluded
],
]
mock_logger = Mock()
# Mock the aggregate function to capture the scores passed to it
with patch("eval_protocol.pytest.evaluation_test_postprocess.aggregate") as mock_aggregate:
mock_aggregate.return_value = 0.75 # Mock return value
postprocess(
all_results=all_results,
aggregation_method="bootstrap",
threshold=None,
active_logger=mock_logger,
mode="pointwise",
completion_params={"model": "test-model"},
test_func_name="test_bootstrap_invalid",
num_runs=2,
experiment_duration_seconds=10.0,
)
# Check that aggregate was called with only valid scores
mock_aggregate.assert_called_once_with([0.8, 0.7], "bootstrap")
# Should still call logger.log for all rows (including invalid ones)
assert mock_logger.log.call_count == 4
@patch.dict("os.environ", {"EP_NO_UPLOAD": "1"}) # Disable uploads
def test_mean_aggregation_with_valid_scores(self):
"""Test mean aggregation with all valid scores."""
all_results = [
[self.create_test_row(0.8), self.create_test_row(0.6)], # Run 1: mean = 0.7
[self.create_test_row(0.4), self.create_test_row(0.8)], # Run 2: mean = 0.6
]
mock_logger = Mock()
postprocess(
all_results=all_results,
aggregation_method="mean",
threshold=None,
active_logger=mock_logger,
mode="pointwise",
completion_params={"model": "test-model"},
test_func_name="test_mean",
num_runs=2,
experiment_duration_seconds=10.0,
)
# Should call logger.log for each row
assert mock_logger.log.call_count == 4
@patch.dict("os.environ", {"EP_NO_UPLOAD": "1"}) # Disable uploads
def test_mean_aggregation_filters_invalid_scores(self):
"""Test that mean aggregation excludes invalid scores from run averages."""
all_results = [
[
self.create_test_row(0.8, is_valid=True),
self.create_test_row(0.0, is_valid=False), # Invalid - excluded from run average
],
[
self.create_test_row(0.6, is_valid=True),
self.create_test_row(0.4, is_valid=True),
],
]
mock_logger = Mock()
postprocess(
all_results=all_results,
aggregation_method="mean",
threshold=None,
active_logger=mock_logger,
mode="pointwise",
completion_params={"model": "test-model"},
test_func_name="test_mean_invalid",
num_runs=2,
experiment_duration_seconds=10.0,
)
# Should call logger.log for all rows
assert mock_logger.log.call_count == 4
@patch.dict("os.environ", {"EP_NO_UPLOAD": "1"}) # Disable uploads
def test_empty_runs_are_skipped(self):
"""Test that runs with no valid scores are skipped."""
all_results = [
[self.create_test_row(0.8, is_valid=True)], # Run 1: has valid score
[self.create_test_row(0.0, is_valid=False)], # Run 2: no valid scores - should be skipped
]
mock_logger = Mock()
postprocess(
all_results=all_results,
aggregation_method="mean",
threshold=None,
active_logger=mock_logger,
mode="pointwise",
completion_params={"model": "test-model"},
test_func_name="test_empty_runs",
num_runs=2,
experiment_duration_seconds=10.0,
)
# Should still call logger.log for all rows
assert mock_logger.log.call_count == 2
@patch.dict("os.environ", {"EP_NO_UPLOAD": "1"}) # Disable uploads
def test_all_invalid_scores(self):
"""Test behavior when all scores are invalid."""
all_results = [
[self.create_test_row(0.0, is_valid=False), self.create_test_row(0.0, is_valid=False)],
]
mock_logger = Mock()
postprocess(
all_results=all_results,
aggregation_method="bootstrap",
threshold=None,
active_logger=mock_logger,
mode="pointwise",
completion_params={"model": "test-model"},
test_func_name="test_all_invalid",
num_runs=1,
experiment_duration_seconds=10.0,
)
# Should still call logger.log for all rows
assert mock_logger.log.call_count == 2
class TestComputeFixedSetMuCi:
"""Tests for compute_fixed_set_mu_ci function."""
@patch.dict("os.environ", {"EP_NO_UPLOAD": "1"}) # Disable uploads
def test_compute_fixed_set_mu_ci_with_flattened_results(self):
"""Test that postprocess correctly calls compute_fixed_set_mu_ci with flattened all_results structure."""
q1_run1 = EvaluationRow(
messages=[Message(role="user", content="What is 2+2?")],
evaluation_result=EvaluateResult(score=0.5, is_score_valid=True, reason="correct"),
input_metadata=InputMetadata(row_id="q1", completion_params={"model": "test"}),
execution_metadata=ExecutionMetadata(),
eval_metadata=EvalMetadata(
name="test",
description="test",
version="1.0",
status=None,
num_runs=3,
aggregation_method="mean",
passed_threshold=None,
passed=None,
),
)
q1_run2 = EvaluationRow(
messages=[Message(role="user", content="What is 2+2?")],
evaluation_result=EvaluateResult(score=0.4, is_score_valid=True, reason="incorrect"),
input_metadata=InputMetadata(row_id="q1", completion_params={"model": "test"}),
execution_metadata=ExecutionMetadata(),
eval_metadata=EvalMetadata(
name="test",
description="test",
version="1.0",
status=None,
num_runs=3,
aggregation_method="mean",
passed_threshold=None,
passed=None,
),
)
q1_run3 = EvaluationRow(
messages=[Message(role="user", content="What is 2+2?")],
evaluation_result=EvaluateResult(score=0.45, is_score_valid=True, reason="incorrect"),
input_metadata=InputMetadata(row_id="q1", completion_params={"model": "test"}),
execution_metadata=ExecutionMetadata(),
eval_metadata=EvalMetadata(
name="test",
description="test",
version="1.0",
status=None,
num_runs=3,
aggregation_method="mean",
passed_threshold=None,
passed=None,
),
)
q2_run1 = EvaluationRow(
messages=[Message(role="user", content="What is 3+3?")],
evaluation_result=EvaluateResult(score=0.8, is_score_valid=True, reason="incorrect"),
input_metadata=InputMetadata(row_id="q2", completion_params={"model": "test"}),
execution_metadata=ExecutionMetadata(),
eval_metadata=EvalMetadata(
name="test",
description="test",
version="1.0",
status=None,
num_runs=3,
aggregation_method="mean",
passed_threshold=None,
passed=None,
),
)
q2_run2 = EvaluationRow(
messages=[Message(role="user", content="What is 3+3?")],
evaluation_result=EvaluateResult(score=0.9, is_score_valid=True, reason="correct"),
input_metadata=InputMetadata(row_id="q2", completion_params={"model": "test"}),
execution_metadata=ExecutionMetadata(),
eval_metadata=EvalMetadata(
name="test",
description="test",
version="1.0",
status=None,
num_runs=3,
aggregation_method="mean",
passed_threshold=None,
passed=None,
),
)
q2_run3 = EvaluationRow(
messages=[Message(role="user", content="What is 3+3?")],
evaluation_result=EvaluateResult(score=0.95, is_score_valid=True, reason="correct"),
input_metadata=InputMetadata(row_id="q2", completion_params={"model": "test"}),
execution_metadata=ExecutionMetadata(),
eval_metadata=EvalMetadata(
name="test",
description="test",
version="1.0",
status=None,
num_runs=3,
aggregation_method="mean",
passed_threshold=None,
passed=None,
),
)
q3_run1 = EvaluationRow(
messages=[Message(role="user", content="What is 4+4?")],
evaluation_result=EvaluateResult(score=0.1, is_score_valid=True, reason="incorrect"),
input_metadata=InputMetadata(row_id="q3", completion_params={"model": "test"}),
execution_metadata=ExecutionMetadata(),
eval_metadata=EvalMetadata(
name="test",
description="test",
version="1.0",
status=None,
num_runs=3,
aggregation_method="mean",
passed_threshold=None,
passed=None,
),
)
q3_run2 = EvaluationRow(
messages=[Message(role="user", content="What is 4+4?")],
evaluation_result=EvaluateResult(score=0.2, is_score_valid=True, reason="correct"),
input_metadata=InputMetadata(row_id="q3", completion_params={"model": "test"}),
execution_metadata=ExecutionMetadata(),
eval_metadata=EvalMetadata(
name="test",
description="test",
version="1.0",
status=None,
num_runs=3,
aggregation_method="mean",
passed_threshold=None,
passed=None,
),
)
q3_run3_valid = EvaluationRow(
messages=[Message(role="user", content="What is 4+4?")],
evaluation_result=EvaluateResult(score=0.3, is_score_valid=True, reason="correct"),
input_metadata=InputMetadata(row_id="q3", completion_params={"model": "test"}),
execution_metadata=ExecutionMetadata(),
eval_metadata=EvalMetadata(
name="test",
description="test",
version="1.0",
status=None,
num_runs=3,
aggregation_method="mean",
passed_threshold=None,
passed=None,
),
)
q3_run3_invalid = EvaluationRow(
messages=[Message(role="user", content="What is 4+4?")],
evaluation_result=EvaluateResult(score=0.3, is_score_valid=False, reason="correct"),
input_metadata=InputMetadata(row_id="q3", completion_params={"model": "test"}),
execution_metadata=ExecutionMetadata(),
eval_metadata=EvalMetadata(
name="test",
description="test",
version="1.0",
status=None,
num_runs=3,
aggregation_method="mean",
passed_threshold=None,
passed=None,
),
)
rows = [[q1_run1, q2_run1, q3_run1], [q1_run2, q2_run2, q1_run3], [q2_run3, q3_run2, q3_run3_valid]]
rows_with_invalid_score = [
[q1_run1, q2_run1, q3_run1],
[q1_run2, q2_run2, q1_run3],
[q2_run3, q3_run2, q3_run3_invalid],
]
# Store results for assertions
first_result = None
second_result = None
# Test first case (all valid scores)
with patch("eval_protocol.pytest.evaluation_test_postprocess.compute_fixed_set_mu_ci") as mock_ci:
mock_ci.side_effect = lambda input_rows, **kwargs: compute_fixed_set_mu_ci(input_rows, **kwargs)
postprocess(
all_results=rows,
aggregation_method="mean",
threshold=None,
active_logger=Mock(),
mode="pointwise",
completion_params={"model": "test-model"},
test_func_name="test_ci_flattened",
num_runs=3,
experiment_duration_seconds=10.0,
)
first_result = mock_ci.return_value
# Test second case (with invalid score)
with patch("eval_protocol.pytest.evaluation_test_postprocess.compute_fixed_set_mu_ci") as mock_ci:
mock_ci.side_effect = lambda input_rows, **kwargs: compute_fixed_set_mu_ci(input_rows, **kwargs)
postprocess(
all_results=rows_with_invalid_score,
aggregation_method="mean",
threshold=None,
active_logger=Mock(),
mode="pointwise",
completion_params={"model": "test-model"},
test_func_name="test_ci_flattened_invalid",
num_runs=3,
experiment_duration_seconds=10.0,
)
second_result = mock_ci.return_value
# Assert exact values
# First case: (0.5111111111111111, 0.18101430525778583, 0.8412079169644363, 0.168416737680268)
if first_result and len(first_result) == 4:
mu_hat1, ci_low1, ci_high1, se1 = first_result
assert abs(mu_hat1 - 0.5111111111111111) < 1e-10
assert abs(ci_low1 - 0.18101430525778583) < 1e-10
assert abs(ci_high1 - 0.8412079169644363) < 1e-10
assert abs(se1 - 0.168416737680268) < 1e-10
# Second case: (0.49444444444444446, 0.13494616580367125, 0.8539427230852177, 0.18341748910243533)
if second_result and len(second_result) == 4:
mu_hat2, ci_low2, ci_high2, se2 = second_result
assert abs(mu_hat2 - 0.49444444444444446) < 1e-10
assert abs(ci_low2 - 0.13494616580367125) < 1e-10
assert abs(ci_high2 - 0.8539427230852177) < 1e-10
assert abs(se2 - 0.18341748910243533) < 1e-10