-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysical_quantity_evaluation_test.py
More file actions
421 lines (388 loc) · 18.5 KB
/
physical_quantity_evaluation_test.py
File metadata and controls
421 lines (388 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import pytest
import os
# Import necessary data and reference cases for tests
from .slr_quantity_test import slr_strict_si_syntax_test_cases, slr_natural_si_syntax_test_cases
from ..evaluation import evaluation_function
from ..utility.unit_system_conversions import (
set_of_SI_prefixes,
set_of_SI_base_unit_dimensions,
set_of_derived_SI_units_in_SI_base_units,
set_of_common_units_in_SI, set_of_very_common_units_in_SI,
set_of_imperial_units
)
class TestEvaluationFunction():
"""
TestCase Class used to test the algorithm.
---
Tests are used here to check that the algorithm written
is working as it should.
These tests are organised in classes to ensure that the same
calling conventions can be used for tests using unittest and
tests using pytest.
Read the docs on how to use unittest and pytest here:
https://docs.python.org/3/library/unittest.html
https://docs.pytest.org/en/7.2.x/
Use evaluation_function() to call the evaluation function.
"""
# Import tests that makes sure that physical quantity parsing works as expected
from .slr_quantity_test import TestEvaluationFunction as TestStrictSLRSyntax
log_details = True
def log_details_to_file(self, details, filename):
if self.log_details:
f = open(filename, "w")
f.write(details)
f.close()
return
@pytest.mark.parametrize("string,value,unit,content,value_latex,unit_latex,criteria", slr_strict_si_syntax_test_cases)
def test_strict_syntax_cases(self, string, value, unit, content, value_latex, unit_latex, criteria):
params = {
"strict_syntax": False,
"physical_quantity": True,
"units_string": "SI",
"strictness": "strict",
"elementary_functions": True
}
answer = string
response = string
result = evaluation_function(response, answer, params)
assert result["is_correct"] is True
assert result["response_latex"] == "~".join([latex for latex in [value_latex, unit_latex] if latex is not None])
@pytest.mark.parametrize("string,value,unit,content,value_latex,unit_latex,criteria", slr_natural_si_syntax_test_cases)
def test_natural_syntax_cases(self, string, value, unit, content, value_latex, unit_latex, criteria):
params = {
"strict_syntax": False,
"physical_quantity": True,
"units_string": "SI",
"strictness": "natural",
"elementary_functions": True
}
answer = string
response = string
result = evaluation_function(response, answer, params)
assert result["is_correct"] is True
assert result["response_latex"] == "~".join([latex for latex in [value_latex, unit_latex] if latex is not None])
@pytest.mark.skip("Too resource intensive")
def test_slow_quantity_alternative_names_natural_syntax(self):
params = {"strict_syntax": False, "physical_quantity": True, "units_string": "SI common imperial", "strictness": "natural"}
units = set_of_SI_base_unit_dimensions | set_of_derived_SI_units_in_SI_base_units | set_of_common_units_in_SI | set_of_very_common_units_in_SI | set_of_imperial_units
incorrect = []
errors = []
n = 0
k = 0
for prefix in set_of_SI_prefixes:
for u1 in units:
for u2 in units:
if u1 is not u2:
answer = prefix[0]+u1[0]+" "+u2[0]
for u1_alt in (u1[0],)+u1[3]:
for u2_alt in (u2[0],)+u2[3]+u2[4]:
n += 1
response = prefix[0]+u1_alt+u2_alt
try:
result = evaluation_function(response, answer, params)
except Exception:
errors.append((answer, response))
continue
if result["is_correct"] is False:
incorrect.append((answer, response, result["response_latex"]))
print("Incorrect: "+str((answer, response, result["response_latex"])))
if n-k > 0:
print(k)
k += 1000
m = len(errors)+len(incorrect)
details = "Total: "+str(m)+"/"+str(n)+"\nIncorrect:\n"+"".join([str(x)+"\n" for x in incorrect])+"\nErrors:\n"+"".join([str(x)+"\n" for x in errors])
self.log_details_to_file(details, "test_quantity_alternative_names_natural_syntax_log.txt")
# Current number of collisions caused by concatenating two units, e.g. "barnewton" has "barn" as substring, "aremole" has "rem" as substring etc.
assert len(errors)+len(incorrect) <= 144
@pytest.mark.skip("Too resource intensive")
def test_slow_quantity_short_forms_natural_syntax(self):
params = {"strict_syntax": False, "physical_quantity": True, "units_string": "SI common imperial", "strictness": "natural"}
units = set_of_SI_base_unit_dimensions | set_of_derived_SI_units_in_SI_base_units | set_of_common_units_in_SI | set_of_very_common_units_in_SI | set_of_imperial_units
incorrect = []
errors = []
n = 0
k = 0
for prefix in set_of_SI_prefixes:
for u1 in units:
for u2 in units:
if u1 is not u2 and (prefix[1]+u1[1] not in [u[1] for u in units]):
answer = prefix[0]+u1[0]+" "+u2[0]
response = prefix[1]+u1[1]+u2[1]
n += 1
try:
result = evaluation_function(response, answer, params)
assert result["is_correct"]
except Exception:
errors.append((answer, response, result["response_latex"]))
continue
if result["is_correct"] is False:
incorrect.append((answer, response, result["response_latex"]))
if n-k > 0:
print(k)
k += 1000
m = len(errors)+len(incorrect)
details = "Total: "+str(m)+"/"+str(n)+"\nIncorrect:\n"+"".join([str(x)+"\n" for x in incorrect])+"\nErrors:\n"+"".join([str(x)+"\n" for x in errors])
self.log_details_to_file(details, "test_quantity_short_forms_natural_syntax_units_log.txt")
# Current number of collisions caused by concatenating short forms for units, e.g. 'femtometre inch' and 'femtominute' both have short form 'fmin'
assert len(errors)+len(incorrect) <= 551
@pytest.mark.parametrize(
"value,unit,small_diff,large_diff",
[
("10.5", "kg m/s^2", 0.04, 0.06),
("10.55", "kg m/s^2", 0.004, 0.006),
("0.105", "kg m/s^2", 0.0004, 0.0006),
("0.0010", "kg m/s^2", 0.00004, 0.00006),
("100", "kg m/s^2", 0.4, 0.6),
("100*10**10", "kg m/s^2", 4*10**9, 6*10**9)
]
)
def test_compute_relative_tolerance_from_significant_digits(self, value, unit, small_diff, large_diff):
ans = value+" "+unit
res_correct_under = str(eval(value)-small_diff)+" "+unit
res_correct_over = str(eval(value)+small_diff)+" "+unit
res_incorrect_under = str(eval(value)-large_diff)+" "+unit
res_incorrect_over = str(eval(value)+large_diff)+" "+unit
params = {"strict_syntax": False, "physical_quantity": True, "units_string": "SI", "strictness": "strict"}
assert evaluation_function(res_correct_under, ans, params)["is_correct"] is True
assert evaluation_function(res_correct_over, ans, params)["is_correct"] is True
assert evaluation_function(res_incorrect_under, ans, params)["is_correct"] is False
assert evaluation_function(res_incorrect_over, ans, params)["is_correct"] is False
@pytest.mark.parametrize(
"ans,res",
[
("-10500 g m/s^2", "-10.5 kg m/s^2"),
("-10.5 mm^2", "-0.0000105 m^2"),
("5 GN", "5000000000 metre kilogram second^(-2)"),
("10 pint", "5682.6 centimetre^3"),
("1 kg", "2.204 lb")
]
)
def test_convert_units(self, ans, res):
params = {"strict_syntax": False, "physical_quantity": True, "units_string": "SI common imperial", "strictness": "strict"}
result = evaluation_function(res, ans, params)
assert result["is_correct"]
@pytest.mark.parametrize(
"ans,res,tag",
[
("-10.5 kg m/s^2", "kg m/s^2", "response matches answer_MISSING_VALUE"),
("-10.5 kg m/s^2", "-10.5", "response matches answer_MISSING_UNIT"),
("kg m/s^2", "-10.5 kg m/s^2", "response matches answer_UNEXPECTED_VALUE"),
("-10.5", "-10.5 kg m/s^2", "response matches answer_UNEXPECTED_UNIT")
]
)
def test_si_units_check_tag(self, ans, res, tag):
params = {"strict_syntax": False, "physical_quantity": True, "units_string": "SI", "strictness": "strict"}
result = evaluation_function(res, ans, params, include_test_data=True)
assert tag in result["tags"]
assert result["is_correct"] is False
def test_si_units_parse_error(self):
ans = "-10.5 kg m/s^2"
res = "-10.5 kg m/s^"
params = {"strict_syntax": False, "physical_quantity": True, "units_string": "SI", "strictness": "strict"}
result = evaluation_function(res, ans, params, include_test_data=True)
assert "PARSE_ERROR_response" in result["tags"]
assert result["is_correct"] is False
@pytest.mark.parametrize(
"res,is_correct,tag",
[
("-10.5 kilogram metre/second^2", True, "response matches answer_TRUE"),
("-10.5 kilogram m/s^2", True, "response matches answer_TRUE"),
("-10.5 kg m/s^2", True, "response matches answer_TRUE"),
("-0.5 kg m/s^2+10 kg m/s^2", False, "response_REVERTED_UNIT_3"),
("-10500 g m/s^2", True, "response matches answer_UNIT_COMPARISON_PREFIX_IS_SMALL"),
("-10.46 kg m/s^2", True, "response matches answer_TRUE"),
("-10.54 kg m/s^2", True, "response matches answer_TRUE"),
("-10.44 kg m/s^2", False, "response matches answer_FALSE"),
("-10.56 kg m/s^2", False, "response matches answer_FALSE"),
("-10.5", False, "response matches answer_MISSING_UNIT"),
("kg m/s^2", False, "response matches answer_MISSING_VALUE"),
("-sin(pi/2)*sqrt(441)^(0.77233) kg m/s^2", True, "response matches answer_TRUE"),
]
)
def test_demo_si_units_demo_a(self, res, is_correct, tag):
ans = "-10.5 kilogram metre/second^2"
params = {
"strict_syntax": False,
"physical_quantity": True,
"units_string": "SI",
"strictness": "strict",
"elementary_functions": True,
}
result = evaluation_function(res, ans, params, include_test_data=True)
assert tag in result["tags"]
assert result["is_correct"] is is_correct
@pytest.mark.parametrize(
"res,ans,is_correct,tag,latex",
[
("-10.5", "-10.5", True, "response matches answer_TRUE", r"-10.5"),
("-10.5 kg m/s^2", "-10.5", False, "response matches answer_UNEXPECTED_UNIT", r"-10.5~\mathrm{kilogram}~\frac{\mathrm{metre}}{\mathrm{second}^{2}}"),
("kg m/s^2", "kg m/s^2", True, "response matches answer_TRUE", r"\mathrm{kilogram}~\frac{\mathrm{metre}}{\mathrm{second}^{2}}"),
("-10.5 kg m/s^2", "kg m/s^2", False, "response matches answer_UNEXPECTED_VALUE", r"-10.5~\mathrm{kilogram}~\frac{\mathrm{metre}}{\mathrm{second}^{2}}"),
]
)
def test_demo_si_units_demo_b(self, res, ans, is_correct, tag, latex):
params = {"strict_syntax": False, "physical_quantity": True, "units_string": "SI", "strictness": "strict"}
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["response_latex"] == latex
assert tag in result["tags"]
assert result["is_correct"] == is_correct
def test_MECH60001_dynamic_signals_error_with_dB(self):
ans = "48 dB"
res = "48 dB"
params = {
"strict_syntax": False,
"physical_quantity": True,
"elementary functions": True
}
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
def test_quantity_with_multiple_of_positive_value(self):
ans = "5 Hz"
res = "10 Hz"
params = {
"strict_syntax": False,
"physical_quantity": True,
"elementary functions": True,
"criteria": "response > answer"
}
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
def test_radians_to_frequency(self):
ans = "2*pi*f radian/second"
res = "f Hz"
params = {
"strict_syntax": False,
"physical_quantity": True,
"elementary functions": True
}
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
def test_print_floating_point_approximation_of_very_large_numbers(self):
ans = "2.47*10^4 kg/s"
res = "2.47**10^4 kg/s" # This number is large enough than attempting to turn it into a string will cause an error
params = {
'rtol': 0.005,
'comparison': 'expression',
'strict_syntax': False,
'physical_quantity': True,
}
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is False
def test_legacy_strictness(self):
ans = "100*kilo*pascal*ohm"
res = "100 kilopascal ohm"
params = {
'strict_syntax': False,
'physical_quantity': True,
'strictness': 'legacy',
}
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
ans = "8650*watt"
res = "8.65kW"
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
res = "8650W"
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
res = "8650*W"
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
res = "8.65 k W"
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
res = "8.65 k*W"
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
res = "(8.65)kW"
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
res = "(8650)W"
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
def test_physical_quantity_with_rtol(self):
ans = "7500 m/s"
res = "7504.1 m/s"
params = {
'rtol': 0.05,
'strict_syntax': False,
'physical_quantity': True,
'elementary_functions': True,
}
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
def test_physical_quantity_with_rel_tol(self):
ans = "7500 m/s"
res = "7504.1 m/s"
params = {
'relative_tolerance': 0.05,
'strict_syntax': False,
'physical_quantity': True,
'elementary_functions': True,
}
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
def test_physical_quantity_with_atol(self):
ans = "7500 m/s"
res = "7504.1 m/s"
params = {
'atol': 5,
'strict_syntax': False,
'physical_quantity': True,
'elementary_functions': True,
}
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
def test_physical_quantity_with_abs_tol(self):
ans = "7500 m/s"
res = "7504.1 m/s"
params = {
'absolute_tolerance': 5,
'strict_syntax': False,
'physical_quantity': True,
'elementary_functions': True,
}
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is True
def test_tolerance_given_as_string(self):
ans = "4.52 kg"
res = "13.74 kg"
params = {
'rtol': '0.015',
'strict_syntax': False,
'physical_quantity': True,
'elementary_functions': True,
}
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is False
def test_answer_zero_value(self):
ans = "0 m"
res = "1 m"
params = {
'rtol': 0,
'atol': 0,
'strict_syntax': False,
'physical_quantity': True,
'elementary_functions': True,
}
result = evaluation_function(res, ans, params, include_test_data=True)
assert result["is_correct"] is False
@pytest.mark.parametrize(
"ans,res",
[
("10 ohm", "10 Ω"),
("10 microA", "10 μA"),
("10 microA", "10 μ A"),
("30 degree", "30 °"),
]
)
def test_greek_letter_units(self, ans, res):
params = {
'strict_syntax': False,
'physical_quantity': True,
'elementary_functions': True,
}
result = evaluation_function(res, ans, params)
assert result["is_correct"] is True
if __name__ == "__main__":
pytest.main(['-xk not slow', "--no-header", os.path.abspath(__file__)])