From f67ef0a09465c202c50781673c01470b02d56182 Mon Sep 17 00:00:00 2001 From: KarlLundengaard Date: Mon, 13 Jan 2025 03:04:09 +0000 Subject: [PATCH] Added parsing of atol and rtol when they are not numbers --- app/evaluation.py | 12 +++++++++ app/evaluation_tests.py | 60 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/app/evaluation.py b/app/evaluation.py index d0629d7..1c8c616 100644 --- a/app/evaluation.py +++ b/app/evaluation.py @@ -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 diff --git a/app/evaluation_tests.py b/app/evaluation_tests.py index a792d54..68fc098 100644 --- a/app/evaluation_tests.py +++ b/app/evaluation_tests.py @@ -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()