-
Notifications
You must be signed in to change notification settings - Fork 38
/
calc_error.py
94 lines (73 loc) · 3.99 KB
/
calc_error.py
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
import logging
from mpmath import log10,fabs, mp
from datetime import date
from UnitTesting.standard_constants import precision
from UnitTesting.create_dict_string import create_dict_string
# calc_error loops through each item in self.calculated_dict and self.trusted_values_dict_entry,
# and makes sure that the difference between each respective value's number of decimal in the dictionaries places is
# less than 1/2 of the precision. It returns a boolean representing whether or not any variables differed.
# Called by run_test
# Uses self.calculated_dict, self.trusted_values_dict_entry, self.module_name
def calc_error(self):
# Setting precision
mp.dps = precision
# Creating sets to easily compare the keys of calculated_dict and trusted_dict
calculated_set = set(self.calculated_dict)
trusted_set = set(self.trusted_values_dict_entry)
logging.debug(' Checking that calculated and trusted dicts contain the same variables...')
# If the sets differ, output the differing variables
if calculated_set != trusted_set:
logging.error(' {}: Calculated dictionary and trusted dictionary have different variables.'
.format(self.module_name))
calculated_minus_trusted = calculated_set - trusted_set
trusted_minus_calculated = trusted_set - calculated_set
if calculated_minus_trusted != set([]):
logging.error(' Calculated Dictionary variables not in Trusted Dictionary: ' +
str(sorted(calculated_minus_trusted)))
if trusted_minus_calculated != set([]):
logging.error(' Trusted Dictionary variables not in Calculated Dictionary: ' +
str(sorted(trusted_minus_calculated)))
# Automatically fail and don't proceed
return False
logging.debug(' ...Success: same variables in both dicts.\n')
# Initialize list of variables whose values differ
bad_var_list = []
logging.debug(' Comparing all calculated and trusted values...')
# Loop through all variables in sorted order
for var in sorted(self.calculated_dict):
# Values to compare
calculated_val = self.calculated_dict[var]
trusted_val = self.trusted_values_dict_entry[var]
# For each variable, print calculated and trusted values
logging.debug('\n' + self.module_name + ': ' + var + ': Calculated: ' + str(calculated_val) + '\n' + self.module_name + ': ' + var
+ ': Trusted: ' + str(trusted_val) + '\n')
# Calculate the error between both values
if trusted_val == 0:
log10_relative_error = log10(fabs(calculated_val))
elif calculated_val == 0:
log10_relative_error = log10(fabs(trusted_val))
else:
log10_relative_error = log10(fabs((trusted_val - calculated_val) / trusted_val))
# Boolean determining if their difference is within the tolerance we accept
good = (log10_relative_error < (precision / -2.0))
# Store all variables who are not 'good'
if not good:
bad_var_list.append(var)
# If we want to output and there exists at least one variable with error, print
if bad_var_list != []:
logging.error('''
\nVariable(s) {} in module {} failed. Please check values.
If you are confident that the newly calculated values are correct, comment out the old trusted values for
{} in your trusted_values_dict and copy the following code between the ##### into your trusted_values_dict.
Make sure to fill out the TODO comment describing why the values had to be changed. Then re-run test script.
#####
# Generated on: {}
# Reason for changing values: TODO
trusted_values_dict['{}'] = {}
#####
'''.format(bad_var_list, self.module_name, self.trusted_values_dict_name, date.today(),
self.trusted_values_dict_name, create_dict_string(self.calculated_dict)))
else:
logging.debug(' ...Success: all variables identical.\n')
# Return True if all variables are good, False otherwise
return bad_var_list == []