Skip to content

Commit 388d297

Browse files
authored
Feature/rename atol rtol (#226)
* Added __init__ to tests to enable support of IDE test features * Added relative_tolerance and absolute_tolerance as alternatives for rtol and atol respectively * Updated user docs * Removed pull_request trigger from staging-deploy workflow * Added a workflow for testing and linitng on PR
1 parent 02201d0 commit 388d297

File tree

6 files changed

+103
-9
lines changed

6 files changed

+103
-9
lines changed

.github/workflows/staging-deploy.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ on:
44
push:
55
branches:
66
- main
7-
pull_request:
8-
branches:
9-
- main
107
workflow_dispatch:
118

129
jobs:

.github/workflows/test-lint.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Test and Lint
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
test:
8+
name: Test
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: read
12+
actions: read
13+
checks: write
14+
pull-requests: write
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
python-version: ["3.8"]
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v4
22+
- name: Set up Python ${{ matrix.python-version }}
23+
id: python-setup
24+
uses: actions/setup-python@v5
25+
with:
26+
python-version: ${{ matrix.python-version }}
27+
28+
- name: Install dependencies
29+
run: |
30+
python -m venv .venv
31+
.venv/bin/pip install pytest
32+
.venv/bin/pip install -r app/requirements.txt
33+
34+
- name: Lint with flake8
35+
run: |
36+
source .venv/bin/activate
37+
.venv/bin/pip install flake8
38+
# stop the build if there are Python syntax errors or undefined names
39+
flake8 ./app --count --select=E9,F63,F7,F82 --show-source --statistics
40+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
41+
flake8 ./app --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
42+
43+
- name: Run tests
44+
if: always()
45+
run: |
46+
source .venv/bin/activate
47+
pytest --junit-xml=./reports/pytest.xml --tb=auto -v
48+
49+
- name: Upload test results
50+
uses: actions/upload-artifact@v4
51+
if: always()
52+
with:
53+
name: test-results-${{ matrix.python-version }}
54+
path: ./reports/pytest.xml
55+
if-no-files-found: warn

app/docs/user.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Note that this function is designed to handle comparisons of mathematical expres
88

99
### Optional parameters
1010

11-
There are 15 optional parameters that can be set: `atol`, `complexNumbers`, `convention`, `criteria`, `elementary_functions`, `feedback_for_incorrect_response`, `multiple_answers_criteria`, `physical_quantity`, `plus_minus`/`minus_plus`, `rtol`, `specialFunctions`, `strict_syntax`, `strictness`, `symbol_assumptions`.
11+
There are 15 optional parameters that can be set: `absolute_tolerance`, `complexNumbers`, `convention`, `criteria`, `elementary_functions`, `feedback_for_incorrect_response`, `multiple_answers_criteria`, `physical_quantity`, `plus_minus`/`minus_plus`, `rtol`, `specialFunctions`, `strict_syntax`, `strictness`, `symbol_assumptions`.
1212

13-
#### `atol`
14-
Sets the absolute tolerance, $e_a$, i.e. if the answer, $x$, and response, $\tilde{x}$, are numerical values then the response is considered equal to the answer if $|x-\tilde{x}| \leq e_aBy default `atol` is set to `0`, which means the comparison will be done with as high accuracy as possible. If either the answer or the response aren't numerical expressions this parameter is ignored.
13+
#### `absolute_tolerance` (`atol`)
14+
Sets the absolute tolerance, $e_a$, i.e. if the answer, $x$, and response, $\tilde{x}$, are numerical values then the response is considered equal to the answer if $|x-\tilde{x}| \leq e_aBy default `absolute_tolerance` is set to `0`, which means the comparison will be done with as high accuracy as possible. If either the answer or the response aren't numerical expressions this parameter is ignored.
1515

1616
#### `complexNumbers`
1717

@@ -76,8 +76,8 @@ When `physical_quantity` the evaluation function will generate feedback based on
7676

7777
**TODO:** Generate new flowchart for updated physical quantity feedback generation procedure.
7878

79-
#### `rtol`
80-
Sets the relative tolerance, $e_r$, i.e. if the answer, $x$, and response, $\tilde{x}$, are numerical values then the response is considered equal to the answer if $\left|\frac{x-\tilde{x}}{x}\right| \leq e_r$. By default `rtol` is set to `0`, which means the comparison will be done with as high accuracy as possible. If either the answer or the response aren't numerical expressions this parameter is ignored.
79+
#### `relative_tolerance` (`rtol`)
80+
Sets the relative tolerance, $e_r$, i.e. if the answer, $x$, and response, $\tilde{x}$, are numerical values then the response is considered equal to the answer if $\left|\frac{x-\tilde{x}}{x}\right| \leq e_r$. By default `relative_tolerance` is set to `0`, which means the comparison will be done with as high accuracy as possible. If either the answer or the response aren't numerical expressions this parameter is ignored.
8181

8282
#### `strictness`
8383

app/evaluation.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ def evaluation_function(response, answer, params, include_test_data=False) -> di
233233
- if set to True, use basic dimensional analysis functionality.
234234
"""
235235

236+
if "relative_tolerance" in params:
237+
params["rtol"] = params["relative_tolerance"]
238+
239+
if "absolute_tolerance" in params:
240+
params["atol"] = params["absolute_tolerance"]
241+
236242
evaluation_result = EvaluationResult()
237243
evaluation_result.is_correct = False
238244

@@ -318,7 +324,6 @@ def evaluation_function(response, answer, params, include_test_data=False) -> di
318324
"reserved_expressions": reserved_expressions_parsed,
319325
"criteria": criteria,
320326
"disabled_evaluation_nodes": parameters.get("disabled_evaluation_nodes", set()),
321-
"evaluation_result": evaluation_result,
322327
"parsing_parameters": parsing_parameters,
323328
"evaluation_result": evaluation_result,
324329
"syntactical_comparison": parameters.get("syntactical_comparison", False),

app/tests/physical_quantity_evaluation_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,18 @@ def test_physical_quantity_with_rtol(self):
337337
result = evaluation_function(res, ans, params, include_test_data=True)
338338
assert result["is_correct"] is True
339339

340+
def test_physical_quantity_with_rel_tol(self):
341+
ans = "7500 m/s"
342+
res = "7504.1 m/s"
343+
params = {
344+
'relative_tolerance': 0.05,
345+
'strict_syntax': False,
346+
'physical_quantity': True,
347+
'elementary_functions': True,
348+
}
349+
result = evaluation_function(res, ans, params, include_test_data=True)
350+
assert result["is_correct"] is True
351+
340352
def test_physical_quantity_with_atol(self):
341353
ans = "7500 m/s"
342354
res = "7504.1 m/s"
@@ -349,6 +361,18 @@ def test_physical_quantity_with_atol(self):
349361
result = evaluation_function(res, ans, params, include_test_data=True)
350362
assert result["is_correct"] is True
351363

364+
def test_physical_quantity_with_abs_tol(self):
365+
ans = "7500 m/s"
366+
res = "7504.1 m/s"
367+
params = {
368+
'absolute_tolerance': 5,
369+
'strict_syntax': False,
370+
'physical_quantity': True,
371+
'elementary_functions': True,
372+
}
373+
result = evaluation_function(res, ans, params, include_test_data=True)
374+
assert result["is_correct"] is True
375+
352376
def test_tolerance_given_as_string(self):
353377
ans = "4.52 kg"
354378
res = "13.74 kg"

app/tests/symbolic_evaluation_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,19 @@ def test_pi_with_rtol(self):
928928
result = evaluation_function(response, answer, params)
929929
assert result["is_correct"] is True
930930

931+
def test_pi_with_rel_tol(self):
932+
answer = "pi"
933+
response = "3.14"
934+
params = {
935+
"strict_syntax": False,
936+
"relative_tolerance": 0.05,
937+
"symbols": {
938+
"pi": {"aliases": ["Pi", "PI", "π"], "latex": "\\(\\pi\\)"},
939+
}
940+
}
941+
result = evaluation_function(response, answer, params)
942+
assert result["is_correct"] is True
943+
931944
@pytest.mark.parametrize(
932945
"response,outcome",
933946
[

0 commit comments

Comments
 (0)