Skip to content

Added parsing of atol and rtol when they are not numbers #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ def evaluation_function(response, answer, params) -> dict:
rtol = params.get("rtol", 0)
atol = params.get("atol", 0)

if isinstance(rtol, str):
try:
rtol = float(rtol)
except Exception as e:
raise Exception("Relative tolerance must be given as a number.") from e

if isinstance(atol, str):
try:
atol = float(atol)
except Exception as e:
raise Exception("Absolute tolerance must be given as a number.") from e

is_correct = None
real_diff = None

Expand Down
60 changes: 60 additions & 0 deletions app/evaluation_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,65 @@ def test_response_is_not_number(self):
self.assertEqual(response.get("is_correct"), False)
self.assertEqual("Please enter a number." in response.get("feedback"), True)

def test_atol_is_parseable_string(self):
body = {
"response": 2,
"answer": 2.1,
"params": {
"atol": "0.2"
},
}

response = evaluation_function(body['response'], body['answer'],
body.get('params', {}))
self.assertEqual(response.get("is_correct"), True)

def test_atol_is_not_parseable_string(self):
body = {
"response": 2,
"answer": 2.1,
"params": {
"atol": "nonsense"
},
}

self.assertRaises(
Exception,
evaluation_function,
body["response"],
body["answer"],
body["params"],
)

def test_rtol_is_parseable_string(self):
body = {
"response": 2,
"answer": 2.1,
"params": {
"rtol": "0.1"
},
}

response = evaluation_function(body['response'], body['answer'],
body.get('params', {}))
self.assertEqual(response.get("is_correct"), True)

def test_rtol_is_not_parseable_string(self):
body = {
"response": 2,
"answer": 2.1,
"params": {
"rtol": "nonsense"
},
}

self.assertRaises(
Exception,
evaluation_function,
body["response"],
body["answer"],
body["params"],
)

if __name__ == "__main__":
unittest.main()