Skip to content

Commit 2587f55

Browse files
authored
Merge pull request #14 from lambda-feedback/feature/atol-rtol-rename
Feature/atol rtol rename
2 parents 48766a8 + 86be986 commit 2587f55

5 files changed

Lines changed: 61 additions & 32 deletions

File tree

app/docs/dev.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
This simple evaluation function checks if the supplied response is within a tolerance range defined in `params`. Works exactly like the [numpy.isclose](https://numpy.org/doc/stable/reference/generated/numpy.isclose.html#numpy.isclose) function.
44

5-
Valid params include `atol` and `rtol`, which can be used in combination, or alone. As the comparison made is the following:
5+
Valid params include `absolute_tolerance` and `relative_tolerance`, which can be used in combination, or alone. As the comparison made is the following:
66

77
```python
8-
is_correct = abs(res - ans) <= (atol + rtol*abs(ans))
8+
is_correct = abs(res - ans) <= (absolute_tolerance + relative_tolerance*abs(ans))
99
```
1010

1111
## Inputs
@@ -15,17 +15,17 @@ is_correct = abs(res - ans) <= (atol + rtol*abs(ans))
1515
"response": "<number>",
1616
"answer": "<number>",
1717
"params": {
18-
"atol": "<number>",
19-
"rtol": "<number>"
18+
"absolute_tolerance": "<number>",
19+
"relative_tolerance": "<number>"
2020
}
2121
}
2222
```
2323

24-
### `atol`
24+
### `absolute_tolerance`
2525

2626
Absolute tolerance parameter
2727

28-
### `rtol`
28+
### `relative_tolerance`
2929

3030
Relative tolerance parameter
3131

@@ -42,7 +42,7 @@ Relative tolerance parameter
4242
Real difference between the given answer and response
4343

4444
### `allowed_diff`
45-
Allowed difference between answer and response, calculated using the supplied `atol` and `rtol` parameters
45+
Allowed difference between answer and response, calculated using the supplied `absolute_tolerance` and `relative_tolerance` parameters
4646

4747

4848
## Examples

app/docs/user.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ Use this evaluation function to check if a student's response is within a tolera
55
A response is accepted if:
66

77
```
8-
|response - answer| ≤ atol + rtol × |answer|
8+
|response - answer| ≤ absolute_tolerance + relative_tolerance × |answer|
99
```
1010

11-
The left-hand side is the absolute difference between the student's response and the correct answer. The right-hand side is the total allowed difference, made up of a fixed part (`atol`) and a part that scales with the size of the answer (`rtol × |answer|`). A response is marked correct whenever the actual difference does not exceed the allowed difference.
11+
The left-hand side is the absolute difference between the student's response and the correct answer. The right-hand side is the total allowed difference, made up of a fixed part (`absolute_tolerance`) and a part that scales with the size of the answer (`relative_tolerance × |answer|`). A response is marked correct whenever the actual difference does not exceed the allowed difference.
1212

1313
## Parameters
1414

1515
Both parameters default to `0` (exact match required) and can be used individually or together.
1616

17-
### `atol` — Absolute tolerance
17+
### `absolute_tolerance` — Absolute tolerance
1818

1919
Specifies a fixed margin around the answer, regardless of its magnitude. Use this when you know the acceptable error in the same units as the answer.
2020

21-
### `rtol` — Relative tolerance
21+
### `relative_tolerance` — Relative tolerance
2222

2323
Specifies an acceptable error as a fraction of the answer's magnitude. Use this when the answer is very large or very small and a percentage-based margin makes more sense than a fixed one.
2424

@@ -31,26 +31,26 @@ No params needed. The student must enter exactly `42` (floating-point precision
3131
### Absolute tolerance
3232

3333
```json
34-
{ "atol": 0.05 }
34+
{ "absolute_tolerance": 0.05 }
3535
```
3636

3737
With answer `9.81`, accepts any response in the range **9.76 – 9.86**. Good for physical measurements where the acceptable error is known in the same units.
3838

3939
### Relative tolerance
4040

4141
```json
42-
{ "rtol": 0.01 }
42+
{ "relative_tolerance": 0.01 }
4343
```
4444

4545
With answer `6.674e-11`, accepts any response within **1%** of the answer. Good for very large or very small values where a fixed margin would be impractical.
4646

4747
### Combined tolerances
4848

4949
```json
50-
{ "atol": 0.01, "rtol": 0.005 }
50+
{ "absolute_tolerance": 0.01, "relative_tolerance": 0.005 }
5151
```
5252

53-
Both tolerances contribute: with answer `9.81`, the allowed difference is `0.01 + 0.005 × 9.81 ≈ 0.059`. Useful when you want a minimum floor (`atol`) plus a proportional allowance (`rtol`).
53+
Both tolerances contribute: with answer `9.81`, the allowed difference is `0.01 + 0.005 × 9.81 ≈ 0.059`. Useful when you want a minimum floor (`absolute_tolerance`) plus a proportional allowance (`relative_tolerance`).
5454

5555
## Notes
5656

app/evaluation.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,30 @@ def evaluation_function(response, answer, params) -> dict:
2121
to output the grading response.
2222
"""
2323

24-
rtol = params.get("rtol", 0)
25-
atol = params.get("atol", 0)
26-
27-
is_correct = None
28-
real_diff = None
24+
relative_tolerance = params.get("relative_tolerance", params.get("rtol", 0))
25+
absolute_tolerance = params.get("absolute_tolerance", params.get("atol", 0))
2926

3027
if not (isinstance(answer, int) or isinstance(answer, float)):
3128
raise Exception("Answer must be a number.")
3229

33-
real_diff = None
34-
allowed_diff = atol + rtol * abs(answer)
30+
allowed_diff = absolute_tolerance + relative_tolerance * abs(answer)
3531
allowed_diff += spacing(answer)
36-
is_correct = False
37-
feedback = ""
32+
3833
if not (isinstance(response, int) or isinstance(response, float)):
39-
feedback = "Please enter a number."
40-
else:
41-
real_diff = abs(response - answer)
42-
allowed_diff = atol + rtol * abs(answer)
43-
allowed_diff += spacing(answer)
44-
is_correct = bool(real_diff <= allowed_diff)
34+
return {
35+
"is_correct": False,
36+
"real_diff": None,
37+
"allowed_diff": allowed_diff,
38+
"feedback": "Please enter a number.",
39+
}
40+
41+
42+
real_diff = abs(response - answer)
43+
is_correct = bool(real_diff <= allowed_diff)
4544

4645
return {
4746
"is_correct": is_correct,
4847
"real_diff": real_diff,
4948
"allowed_diff": allowed_diff,
50-
"feedback": feedback,
49+
"feedback": "",
5150
}

app/evaluation_test.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,5 +244,35 @@ def test_response_is_not_number(self):
244244
self.assertEqual(response.get("is_correct"), False)
245245
self.assertEqual("Please enter a number." in response.get("feedback"), True)
246246

247+
def test_full_param_correct(self):
248+
body = {
249+
"response": 1e6,
250+
"answer": 1e7,
251+
"params": {
252+
"relative_tolerance": 1e-1,
253+
"absolute_tolerance": 8.2e6
254+
},
255+
}
256+
257+
response = evaluation_function(body['response'], body['answer'],
258+
body.get('params', {}))
259+
260+
self.assertEqual(response.get("is_correct"), True)
261+
262+
def test_full_param_incorrect(self):
263+
body = {
264+
"response": 1e6,
265+
"answer": 2e7,
266+
"params": {
267+
"relative_tolerance": 1e-1,
268+
"absolute_tolerance": 8.2e6
269+
},
270+
}
271+
272+
response = evaluation_function(body['response'], body['answer'],
273+
body.get('params', {}))
274+
275+
self.assertEqual(response.get("is_correct"), False)
276+
247277
if __name__ == "__main__":
248278
unittest.main()

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# IsSimilar
22

3-
This function checks whether a student's numeric response is within an acceptable tolerance of the correct answer, using absolute (`atol`) and relative (`rtol`) tolerance parameters. The comparison follows the formula: `|response - answer| ≤ atol + rtol × |answer|`. By default both tolerances are 0, requiring an exact match (within floating-point precision).
3+
This function checks whether a student's numeric response is within an acceptable tolerance of the correct answer, using absolute (`absolute_tolerance`) and relative (`relative_tolerance`) tolerance parameters. The comparison follows the formula: `|response - answer| ≤ absolute_tolerance + relative_tolerance × |answer|`. By default both tolerances are 0, requiring an exact match (within floating-point precision).
44

55
For more information, look at the docs in `app/docs/`.
66

0 commit comments

Comments
 (0)