Skip to content

Commit

Permalink
Merge pull request #3 from TDycores-Project/glenn/logging-refactor
Browse files Browse the repository at this point in the history
Glenn/logging refactor
  • Loading branch information
Glenn Hammond committed Nov 26, 2019
2 parents 9efd484 + ac3a7f9 commit fe4b4dc
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 651 deletions.
53 changes: 38 additions & 15 deletions qa_solution_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,25 @@
from qa_common import *
from qa_test_error import *
from qa_solution import QASolutionReader
from qa_test_doc import *

class QASolutionComparison(object):

def __init__(self,solution_dictionary,output_options,mapped_simulator_names,template,run_number):
def __init__(self,solution_dictionary,output_options,
mapped_simulator_names,template,run_number,doc_run):
debug_push('QACompareSolutions init')

self.solution_dictionary = solution_dictionary
self.output_options = output_options
self.mapped_simulator_names = mapped_simulator_names
self.template = template
self.run_number = run_number
self.doc_run = doc_run

debug_pop()

def process_opt_file(self):
debug_push('QACompareSolutions process_opt_file')

if debug_verbose():
print(self.solution_dictionary)

Expand Down Expand Up @@ -105,13 +107,21 @@ def plot_time_slice(self,times,plot_error,print_error):

all_stat_files=[]
for time in times:
plot_time_units = ''
converted_time = -999.
if time < 0.:
# time < 0 indicates steady state
if len(times) > 1:
print_err_msg('QACompareSolutions: Negative time in times '
'array indicates steady state. Yet, there '
'is more than one time.')
else:
plot_time_units = self.output_options['plot_time_units']
sec_over_tunits = unit_conversion(plot_time_units)
converted_time = time/sec_over_tunits
doc_slice = QATestDocTimeSlice(converted_time,plot_time_units)
for variable in self.variables:
doc_var = QATestDocVariable(variable)
x_min = 1e20
x_max = -1.e20
y_min = 1e20
Expand Down Expand Up @@ -267,10 +277,6 @@ def plot_time_slice(self,times,plot_error,print_error):
plot_time_units = ''
temp_title = self.title
if not time < 0.:
plot_time_units = self.output_options['plot_time_units']
sec_over_tunits = unit_conversion(plot_time_units)
#TODO(rcl): move to function in qa_common for use elsewhere, too.
converted_time = time/sec_over_tunits
if converted_time == 0:
temp_title += ' @ {} {}'.format(converted_time,
plot_time_units)
Expand All @@ -297,8 +303,9 @@ def plot_time_slice(self,times,plot_error,print_error):
prefix = 'ss'
if not time < 0.:
prefix = '{}'.format(converted_time)
filename = './{}_{}_{}_run{}.png'.format(prefix,variable,
filename = '{}_{}_{}_run{}.png'.format(prefix,variable,
self.template,self.run_number)
doc_var.add_solution_png(filename)
plt.title(variable,fontsize=18)
plt.savefig(filename)
if self.plot_to_screen==True:
Expand All @@ -309,18 +316,28 @@ def plot_time_slice(self,times,plot_error,print_error):

error = QATestError(prefix,variable,self.template,self.run_number,self.plot_to_screen,self.error_units,False,self.plot_dimension)
if plot_error == True:
error.plot_error(x_loc[0],y_loc[0],z_loc[0],solutions[0],x_loc[1],y_loc[1],z_loc[1],solutions[1],self.x_string_time_slice,self.y_string_time_slice)
filename = error.plot_error(x_loc[0],y_loc[0],z_loc[0],solutions[0],x_loc[1],y_loc[1],z_loc[1],solutions[1],self.x_string_time_slice,self.y_string_time_slice)
doc_var.add_error_png(filename)
if print_error == True:
filename=error.print_error(x_loc[0],y_loc[0],z_loc[0],solutions[0],x_loc[1],y_loc[1],z_loc[1],solutions[1])
filename = error.print_error(x_loc[0],y_loc[0],z_loc[0],solutions[0],x_loc[1],y_loc[1],z_loc[1],solutions[1])
all_stat_files.append(filename)
doc_var.set_error_stat(filename)
doc_slice.add_variable(doc_var)
self.doc_run.add_time_slice(doc_slice)
if print_error == True:
error.calc_error_metrics_over_all_times(all_stat_files,plot_time_units)
filename = error.calc_error_metrics_over_all_times(all_stat_files,plot_time_units)
# self.doc_run.add_time_slice_variable(doc_var)

debug_pop()

def plot_observation_file(self,locations,plot_error,print_error):
debug_push('QACompareSolutions plot_observatoin_file')
debug_push('QACompareSolutions plot_observation_file')
for location in locations:
location_string = '{}, {}, {}'.format(location[0],location[1],
location[2])
doc_obs = QATestDocObservation(location_string)
for variable in self.variables:
doc_var = QATestDocVariable(variable)
t_min = 1e20
t_max = -1e20
s_min = 1e20
Expand Down Expand Up @@ -389,15 +406,17 @@ def plot_observation_file(self,locations,plot_error,print_error):
plt.ylabel(self.y_string_observation, fontsize=16)

ax.tick_params(labelsize=14)
temp_title= self.title+' {}, {}, {}'.format(location[0],location[1],location[2])
temp_title= self.title+' '+location_string

plt.annotate(temp_title,
xy=(.03, .990),
xycoords='figure fraction',
horizontalalignment='left',
verticalalignment='top',fontsize=14)
filename = './{}_{}_{}_{}_{}_run{}.png'.format(
location[0],location[1],location[2],variable,self.template,self.run_number)
filename = '{}_{}_{}_{}_{}_run{}.png'.format(
location[0],location[1],location[2],variable,
self.template,self.run_number)
doc_var.add_solution_png(filename)
plt.savefig(filename)
if self.plot_to_screen == True:
plt.show()
Expand All @@ -406,9 +425,13 @@ def plot_observation_file(self,locations,plot_error,print_error):
#######CHANGE
error = QATestError(location,variable,self.template,self.run_number,self.plot_to_screen,self.error_units,observation=True)
if plot_error == True:
error.plot_error_1D(times[0],solutions[0],times[1],solutions[1],self.x_string_observation)
filename = error.plot_error_1D(times[0],solutions[0],times[1],solutions[1],self.x_string_observation)
doc_var.add_error_png(filename)
if print_error == True:
filename = error.print_error_1D(times[0],solutions[0],times[1],solutions[1],time_unit)
doc_var.set_error_stat(filename)
doc_obs.add_variable(doc_var)
self.doc_run.add_observation(doc_obs)

debug_pop()

Expand Down
51 changes: 32 additions & 19 deletions qa_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,26 @@
from qa_debug import *
from qa_common import *
from qa_solution_comparison import *
from qa_test_doc import *

class QATest(object):
"""
Class to collect data about a test, run the test, and plot results
"""
_wall_time_re = re.compile(r"Time \(seconds\)")

def __init__(self,name,section_dict=None):
def __init__(self,name,root_dir,section_dict=None):
debug_push('QATest init')
print_header('*',name)
self._txtwrap = textwrap.TextWrapper(width=78, subsequent_indent=4*" ")
self._timeout = 300.
self.name = name
self._section_dict = section_dict
self.name = name
self.root_dir = root_dir
try:
self.title = self._section_dict['title']
except:
self.title = self.name
self._simulators = []
self._mapped_simulator_names = []
self._max_attempts = 1 # 1 is default
Expand All @@ -48,7 +54,7 @@ def __init__(self,name,section_dict=None):
self.tough_options={}
self._process_opt_file()
self.swap_dict = {}

self._template = self._section_dict['template']

debug_pop()

Expand Down Expand Up @@ -97,9 +103,6 @@ def _section_from_opt_file(self,config,key):
pass
return xx_options

def _get_template(self):
return self._section_dict['template']

def _process_swap_options(self):
debug_push('QATest _process_swap_options')
number_dict = {}
Expand Down Expand Up @@ -199,10 +202,17 @@ def run(self,available_simulators):

i_attempt = 0
passed = False
# list_of_swap_dict = self._list_of_swap_dict

cwd = os.getcwd()
print(cwd)
print(self.root_dir)
print(cwd.replace(self.root_dir,''))
doc = QATestDoc(cwd,cwd.replace(self.root_dir,''))
doc.set_title(self.title)
doc.set_template(self._template)
for i in range(len(list_of_swap_dict)):
run_number = i+1
doc_run = QATestDocRun(run_number)

swap_dict = None
if len(list_of_swap_dict) > 0:
Expand All @@ -217,38 +227,41 @@ def run(self,available_simulators):
isimulator = 0

for simulator in self._simulators:
mapped_name = self._mapped_simulator_names[isimulator]
print_header('-',mapped_name)
filename = self._swap(mapped_name,simulator.get_suffix(),
mapped_simulator_name = self._mapped_simulator_names[isimulator]
if run_number == 1:
doc.add_simulator(mapped_simulator_name)
print_header('-',mapped_simulator_name)
filename = self._swap(mapped_simulator_name,simulator.get_suffix(),
run_number,swap_dict)

doc_run.set_input_filename(mapped_simulator_name,filename)
if len(self.map_options) > 0:
simulator.update_dict(self.map_options)
if len(self.tough_options) > 0 and mapped_name == 'tough3':
if len(self.tough_options) > 0 and mapped_simulator_name == 'tough3':
simulator.process_tough_options(self.tough_options,self._output_options)
solutions[mapped_name] = \
solutions[mapped_simulator_name] = \
simulator.run(filename,annotation)
isimulator += 1
#self._compare_solutions(solutions)
template = self._get_template()
##pass in template and run number
compare_solutions = \
QASolutionComparison(solutions,self._output_options,
self._mapped_simulator_names,template,
run_number)
self._mapped_simulator_names,
self._template,run_number,
doc_run)
compare_solutions.process_opt_file()
doc.add_run(doc_run)
doc.write()
debug_pop()


def _swap(self,simulator_mapped_name,simulator_suffix,run_number,
swap_dict=None):
debug_push('QATest _swap')
template = self._get_template()
in_filename = template+'.'+simulator_mapped_name
in_filename = self._template+'.'+simulator_mapped_name
#run_number_string = ''
#if run_number > 0:
run_number_string = '_run{}'.format(run_number)
out_filename = template+'_'+simulator_mapped_name+ \
out_filename = self._template+'_'+simulator_mapped_name+ \
run_number_string+simulator_suffix
if os.path.isfile(in_filename):
if debug_verbose():
Expand Down
Loading

0 comments on commit fe4b4dc

Please sign in to comment.