Skip to content

Commit 026a08c

Browse files
Fixed that mathematical expressions were not properly type set in feedback messages.
1 parent 8584beb commit 026a08c

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

app/evaluation.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,24 @@
2222
def feedback_not_dimensionless(groups):
2323
groups = list(groups)
2424
if len(groups) == 1:
25-
return f"The group {str(groups[0])} is not dimensionless."
25+
return f"The group {convert_to_latex(groups[0])} is not dimensionless."
2626
else:
27-
return "The groups "+", ".join([str(g) for g in groups[0:-1]])+" and "+str(groups[-1])+" are not dimensionless."
27+
return "The groups "+", ".join([convert_to_latex(g) for g in groups[0:-1]])+" and "+convert_to_latex(groups[-1])+" are not dimensionless."
2828

29+
def convert_to_latex(expr):
30+
if isinstance(expr, str):
31+
return expr
32+
else:
33+
return "$"+latex(expr)+"$"
2934

3035
buckingham_pi_feedback_responses = {
3136
"VALID_CANDIDATE_SET": "",
3237
"NOT_DIMENSIONLESS": feedback_not_dimensionless,
3338
"MORE_GROUPS_THAN_REFERENCE_SET": "Response has more groups than necessary.",
3439
"CANDIDATE_GROUPS_NOT_INDEPENDENT": lambda r, n: f"Groups in response are not independent. It has {r} independent group(s) and contains {n} groups.",
3540
"TOO_FEW_INDEPENDENT_GROUPS": lambda name, r, n: f"{name} contains too few independent groups. It has {r} independent group(s) and needs at least {n} independent groups.",
36-
"UNKNOWN_SYMBOL": lambda symbols: "Unknown symbol(s): "+", ".join([str(s) for s in symbols])+".",
37-
"SUM_WITH_INDEPENDENT_TERMS": lambda s: f"Sum in {s} group contains more independent terms that there are groups in total. Group expressions should ideally be written as a comma-separated list where each item is an entry of the form `q_1**c_1*q_2**c_2*...*q_n**c_n`."
41+
"UNKNOWN_SYMBOL": lambda symbols: "Unknown symbol(s): "+", ".join([convert_to_latex(s) for s in symbols])+".",
42+
"SUM_WITH_INDEPENDENT_TERMS": lambda s: f"Sum in {convert_to_latex(s)} contains more independent terms that there are groups in total. Group expressions should ideally be written as a comma-separated list where each item is an entry of the form `q_1**c_1*q_2**c_2*...*q_n**c_n`."
3843
}
3944

4045
feedback_responses_list = [parsing_feedback_responses, buckingham_pi_feedback_responses]
@@ -320,7 +325,7 @@ def wrapped_function(*args):
320325
# Check that answers are dimensionless
321326
for k, dimension in enumerate(answer_dimensions):
322327
if not dimension.is_constant():
323-
raise Exception(buckingham_pi_feedback_responses["NOT_DIMENSIONLESS"]("$"+latex(answer_groups[k])+"$"))
328+
raise Exception(buckingham_pi_feedback_responses["NOT_DIMENSIONLESS"](answer_groups[k]))
324329

325330
# Check that there is a sufficient number of independent groups in the answer
326331
answer_matrix = get_exponent_matrix(answer_groups, answer_symbols)

app/evaluation_tests.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,9 @@ def test_buckingham_pi_unknown_symbols(self):
509509
self.assertEqual_input_variations(response, answer, params, False)
510510
result = evaluation_function(response, answer, params)
511511
self.assertEqual(
512-
buckingham_pi_feedback_responses["UNKNOWN_SYMBOL"](["p", "q"]) in result["feedback"]
512+
buckingham_pi_feedback_responses["UNKNOWN_SYMBOL"](["$p$", "$q$"]) in result["feedback"]
513513
or
514-
buckingham_pi_feedback_responses["UNKNOWN_SYMBOL"](["q", "p"]) in result["feedback"],
514+
buckingham_pi_feedback_responses["UNKNOWN_SYMBOL"](["$q$", "$p$"]) in result["feedback"],
515515
True
516516
)
517517

@@ -633,7 +633,7 @@ def test_buckingham_pi_two_groups_with_quantities_not_dimensionless(self):
633633
answer = "U*L/nu, f*L/U"
634634
response = "U*L/nu, U*nu/(f*L**2)"
635635
result = evaluation_function(response, answer, params)
636-
self.assertEqual(buckingham_pi_feedback_responses["NOT_DIMENSIONLESS"]({"L*U/nu", }) in result["feedback"], True)
636+
self.assertEqual(buckingham_pi_feedback_responses["NOT_DIMENSIONLESS"]({"$\frac{LU}{\nu}", }) in result["feedback"], True)
637637

638638
def test_buckingham_pi_two_groups_with_quantities_too_few_independent_groups_in_answer(self):
639639
params = {"comparison": "buckinghamPi",
@@ -905,7 +905,7 @@ def test_buckingham_pi_example_in_examples_module(self):
905905
response = "q*U*L/nu"
906906
result = evaluation_function(response, answer, params)
907907
self.assertEqual(result["is_correct"], False)
908-
self.assertEqual(buckingham_pi_feedback_responses["UNKNOWN_SYMBOL"]({"q"}) in result["feedback"], True)
908+
self.assertEqual(buckingham_pi_feedback_responses["UNKNOWN_SYMBOL"]({"$q$"}) in result["feedback"], True)
909909
# two dimensionless groups that are not independent
910910
response = "U*L/nu, nu/U/L"
911911
result = evaluation_function(response, answer, params)
@@ -916,7 +916,7 @@ def test_buckingham_pi_example_in_examples_module(self):
916916
response = "U*L"
917917
result = evaluation_function(response, answer, params)
918918
self.assertEqual(result["is_correct"], False)
919-
self.assertEqual(buckingham_pi_feedback_responses["NOT_DIMENSIONLESS"]({"L*U", }) in result["feedback"], True)
919+
self.assertEqual(buckingham_pi_feedback_responses["NOT_DIMENSIONLESS"]({"$L U$", }) in result["feedback"], True)
920920
with self.subTest(tag="Part b)"):
921921
params = {
922922
"comparison": "buckinghamPi",
@@ -950,14 +950,14 @@ def test_buckingham_pi_example_in_examples_module(self):
950950
response = "U*L/nu, f/U"
951951
result = evaluation_function(response, answer, params)
952952
self.assertEqual(result["is_correct"], False)
953-
self.assertEqual(buckingham_pi_feedback_responses["NOT_DIMENSIONLESS"]({"f/U"}) in result["feedback"], True)
953+
self.assertEqual(buckingham_pi_feedback_responses["NOT_DIMENSIONLESS"]({r"$\frac{f}{U}$"}) in result["feedback"], True)
954954
response = "L/nu, f/U"
955955
result = evaluation_function(response, answer, params)
956956
self.assertEqual(result["is_correct"], False)
957957
self.assertEqual(
958-
buckingham_pi_feedback_responses["NOT_DIMENSIONLESS"](["L/nu", "f/U"]) in result["feedback"]
958+
buckingham_pi_feedback_responses["NOT_DIMENSIONLESS"]([r"$\frac{L}{\nu}$", r"$\frac{f}{U}$"]) in result["feedback"]
959959
or
960-
buckingham_pi_feedback_responses["NOT_DIMENSIONLESS"](["f/U", "L/nu"]) in result["feedback"],
960+
buckingham_pi_feedback_responses["NOT_DIMENSIONLESS"]([r"$\frac{f}{U}$", r"$\frac{L}{\nu}$"]) in result["feedback"],
961961
True
962962
)
963963
with self.subTest(tag="Part c)"):

0 commit comments

Comments
 (0)