def test_create_dict_string(self): from UnitTesting.create_dict_string import create_dict_string from mpmath import mpf, mpc calculated_dict = {} self.assertEqual("{}", create_dict_string(calculated_dict)) calculated_dict = {'a': 0} self.assertEqual("{'a': 0}", create_dict_string(calculated_dict)) calculated_dict = {'b': mpf('1.0')} self.assertEqual("{'b': mpf('1.0')}", create_dict_string(calculated_dict)) calculated_dict = {'c': mpc(real='1.0', imag='2.0')} self.assertEqual("{'c': mpc(real='1.0', imag='2.0')}", create_dict_string(calculated_dict)) calculated_dict = {'alpha': 4, 'beta': mpf('0.2'), 'gamma': mpc(real='3.14', imag='6.28')} self.assertEqual("{'alpha': 4, 'beta': mpf('0.2'), 'gamma': mpc(real='3.14', imag='6.28')}", create_dict_string(calculated_dict)) calculated_dict = {'beta': mpf('0.2'), 'gamma': mpc(real='3.14', imag='6.28'), 'alpha': 4} self.assertEqual("{'alpha': 4, 'beta': mpf('0.2'), 'gamma': mpc(real='3.14', imag='6.28')}", create_dict_string(calculated_dict)) calculated_dict = {'AZ': mpf('2.4287654'), 'ab': mpc(real='0.0', imag='123.1234123412341234')} self.assertEqual("{'ab': mpc(real='0.0', imag='123.1234123412341234'), 'AZ': mpf('2.4287654')}", create_dict_string(calculated_dict)) calculated_dict = {'betaU[0]': mpf('1.0'), 'betaU[1]': mpf('0.0'), 'betaU[2]': mpf('-0.733235010089164696012176136719')} self.assertEqual("{'betaU[0]': mpf('1.0'), 'betaU[1]': mpf('0.0'), 'betaU[2]': mpf('-0.733235010089164696012176136719')}", create_dict_string(calculated_dict)) logging.info(' All create_dict_string tests passed.')
def first_time_print(self, write=True): logging.error(''' Module: {} Please copy the following code between the ##### and paste it into your trusted_values_dict.py file for this module: ##### # Generated on: {} trusted_values_dict['{}'] = {} ##### '''.format(self.module_name, date.today(), self.trusted_values_dict_name, create_dict_string(self.calculated_dict))) # If [write] is [True], write to [trusted_values_dict] if write: logging.debug( ' Writing trusted_values_dict entry to trusted_values_dict.py...') fw = open(os.path.join(self.path, 'trusted_values_dict.py'), 'a') fw.write(''' # Generated on: {} trusted_values_dict['{}'] = {} '''.format(date.today(), self.trusted_values_dict_name, self.calculated_dict)) fw.close() logging.debug(' ...Success: entry written to trusted_values_dict.py\n')
def first_time_print(self, write=True): dict_name = self.trusted_values_dict_name output_string = r""" # Generated on: """ + str(date.today()) + r""" trusted_values_dict['""" + dict_name + r"""'] = """ + \ str(create_dict_string(self.calculated_dict)) error = r""" Module: """ + self.module_name + r""" Please copy the following code between the ##### and paste it into your trusted_values_dict.py file for this module: ##### """ + output_string + """ ##### """ logging.error(error) # If [write] is [True], write to [trusted_values_dict] if write: logging.debug( ' Writing trusted_values_dict entry to trusted_values_dict.py...') with open(os.path.join(self.path, 'trusted_values_dict.py'), 'a') as file: file.write(output_string) logging.debug(' ...Success: entry written to trusted_values_dict.py\n')
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 == []