Reja1 Claude Opus 4.8 commited on
Commit
b314e44
·
1 Parent(s): dd2d72e

fix: compare INTEGER answers by value, not string

Browse files

Numerical answers (INTEGER/INTEGER_2) were scored with exact string
equality, so equal-but-differently-formatted values were marked wrong
(e.g. '9.8' != '9.80', '5' != '5.00'). Add integer_answers_match() which
falls back to a float comparison with a tight tolerance, used in both the
JEE Main and JEE Advanced INTEGER scoring paths. Range answers are
unaffected (they already use is_within_range/float).

Adds 4 tests covering the trailing-zero cases plus a false-positive guard
(9.79 != 9.80).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Files changed (2) hide show
  1. src/evaluation.py +21 -2
  2. tests/test_evaluation.py +43 -0
src/evaluation.py CHANGED
@@ -1,4 +1,5 @@
1
  import logging
 
2
  import re
3
  from typing import List, Optional, Union, Dict, Any
4
 
@@ -33,6 +34,24 @@ def is_within_range(predicted_value_str: str, lower_bound_str: str, upper_bound_
33
  return lower_bound <= predicted_value <= upper_bound
34
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def calculate_single_question_score_details(result_item: Dict[str, Any]) -> Dict[str, Any]:
37
  """
38
  Calculates marks_awarded and evaluation_status for a single question result.
@@ -121,7 +140,7 @@ def calculate_single_question_score_details(result_item: Dict[str, Any]) -> Dict
121
  is_correct = False
122
  if len(pred_set) == 1:
123
  predicted_answer_str = list(pred_set)[0]
124
- if predicted_answer_str in truth_processed: # Check against single string truths
125
  is_correct = True
126
 
127
  if is_correct:
@@ -144,7 +163,7 @@ def calculate_single_question_score_details(result_item: Dict[str, Any]) -> Dict
144
  is_correct = True
145
  break # Found a matching range, no need to check others
146
  elif isinstance(gt_entry, str): # This is an exact integer match
147
- if predicted_answer_str == gt_entry: # gt_entry is already uppercase
148
  is_correct = True
149
  break # Found an exact match, no need to check others
150
 
 
1
  import logging
2
+ import math
3
  import re
4
  from typing import List, Optional, Union, Dict, Any
5
 
 
34
  return lower_bound <= predicted_value <= upper_bound
35
 
36
 
37
+ def integer_answers_match(predicted_value_str: str, truth_str: str) -> bool:
38
+ """
39
+ Checks whether a predicted numerical answer equals an exact ground-truth value.
40
+
41
+ Falls back to a numeric comparison so that differently-formatted but equal
42
+ values match (e.g. '9.8' == '9.80', '5' == '5.00'). Exact string equality is
43
+ tried first to handle any non-numeric tokens gracefully.
44
+ """
45
+ if predicted_value_str == truth_str:
46
+ return True
47
+ try:
48
+ # abs_tol is tiny: real JEE numerical keys differ by >= 0.01, so this only
49
+ # collapses formatting differences (trailing zeros), never distinct values.
50
+ return math.isclose(float(predicted_value_str), float(truth_str), rel_tol=0.0, abs_tol=1e-9)
51
+ except (ValueError, TypeError):
52
+ return False
53
+
54
+
55
  def calculate_single_question_score_details(result_item: Dict[str, Any]) -> Dict[str, Any]:
56
  """
57
  Calculates marks_awarded and evaluation_status for a single question result.
 
140
  is_correct = False
141
  if len(pred_set) == 1:
142
  predicted_answer_str = list(pred_set)[0]
143
+ if any(integer_answers_match(predicted_answer_str, t) for t in truth_processed): # Check against single string truths
144
  is_correct = True
145
 
146
  if is_correct:
 
163
  is_correct = True
164
  break # Found a matching range, no need to check others
165
  elif isinstance(gt_entry, str): # This is an exact integer match
166
+ if integer_answers_match(predicted_answer_str, gt_entry): # gt_entry is already uppercase
167
  is_correct = True
168
  break # Found an exact match, no need to check others
169
 
tests/test_evaluation.py CHANGED
@@ -182,6 +182,49 @@ class TestSingleQuestionScoring:
182
  assert result["marks_awarded"] == 0
183
  assert result["evaluation_status"] == "incorrect"
184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  def test_unexpected_prediction_type_no_penalty(self):
186
  """Unexpected pred type is a code/data bug, not a deliberate wrong answer — no penalty."""
187
  result = calculate_single_question_score_details({
 
182
  assert result["marks_awarded"] == 0
183
  assert result["evaluation_status"] == "incorrect"
184
 
185
+ def test_jee_advanced_integer_trailing_zero_equivalent(self):
186
+ """'9.8' must match the key '9.80' — same numeric value, different formatting."""
187
+ result = calculate_single_question_score_details({
188
+ "question_id": "JA_FMT", "exam_name": "JEE_ADVANCED",
189
+ "question_type": "INTEGER",
190
+ "ground_truth": ["9.80"], "predicted_answer": ["9.8"],
191
+ "api_call_successful": True,
192
+ })
193
+ assert result["marks_awarded"] == 4
194
+ assert result["evaluation_status"] == "correct"
195
+
196
+ def test_jee_advanced_integer_half_equivalent(self):
197
+ """'0.5' must match the key '0.50'."""
198
+ result = calculate_single_question_score_details({
199
+ "question_id": "JA_FMT2", "exam_name": "JEE_ADVANCED",
200
+ "question_type": "INTEGER",
201
+ "ground_truth": ["0.50"], "predicted_answer": ["0.5"],
202
+ "api_call_successful": True,
203
+ })
204
+ assert result["marks_awarded"] == 4
205
+ assert result["evaluation_status"] == "correct"
206
+
207
+ def test_jee_advanced_integer_numeric_mismatch_still_wrong(self):
208
+ """A genuinely different value must still score 0 (no false positives)."""
209
+ result = calculate_single_question_score_details({
210
+ "question_id": "JA_FMT3", "exam_name": "JEE_ADVANCED",
211
+ "question_type": "INTEGER",
212
+ "ground_truth": ["9.80"], "predicted_answer": ["9.79"],
213
+ "api_call_successful": True,
214
+ })
215
+ assert result["marks_awarded"] == 0
216
+ assert result["evaluation_status"] == "incorrect"
217
+
218
+ def test_jee_main_integer_trailing_zero_equivalent(self):
219
+ """Numeric-equivalence fix also applies to JEE Main INTEGER scoring."""
220
+ result = calculate_single_question_score_details({
221
+ "question_id": "JM_FMT", "exam_name": "JEE_MAIN",
222
+ "question_type": "INTEGER",
223
+ "ground_truth": ["5.00"], "predicted_answer": ["5"],
224
+ "api_call_successful": True,
225
+ })
226
+ assert result["marks_awarded"] == 4
227
+
228
  def test_unexpected_prediction_type_no_penalty(self):
229
  """Unexpected pred type is a code/data bug, not a deliberate wrong answer — no penalty."""
230
  result = calculate_single_question_score_details({