def __init__(self): super(FSM, self).__init__(show_conditions=True, auto_transitions=False, title="Projected GUI state machine") states = [ 'initial', 'calibrate_projectors', 'waiting_for_user', 'waiting_for_user_calibration', 'program_selection', 'learning', 'running' ] self.add_states(states) logger.setLevel(logging.DEBUG) # trigger, source, destination self.add_transition('tr_start', 'initial', 'calibrate_projectors', after='cb_start_calibration') self.add_transition('tr_projectors_calibrated', 'calibrate_projectors', 'waiting_for_user', after='cb_waiting_for_user') self.add_transition('tr_projectors_calibrated', 'initial', 'waiting_for_user', after='cb_waiting_for_user') self.add_transition('tr_user_present', 'waiting_for_user', 'waiting_for_user_calibration', after="cb_waiting_for_user_calibration") self.add_transition('tr_user_calibrated', 'waiting_for_user_calibration', 'program_selection', after='cb_program_selection') self.add_transition('tr_program_edit', 'program_selection', 'learning', after="cb_learning") self.add_transition('tr_program_selected', 'program_selection', 'running', after="cb_running") self.add_transition('tr_program_learned', 'learning', 'program_selection', after='cb_program_selection') self.add_transition('tr_running', '*', 'running', after="cb_running") self.add_transition('tr_program_finished', 'running', 'program_selection', after='cb_program_selection')
def __init__(self): super(FSM, self).__init__(show_conditions=True, auto_transitions=False, title="Projected GUI state machine") states = ['initial', 'calibrate', 'waiting_for_user', 'waiting_for_user_calibration', 'program_selection', 'learning', 'running'] self.add_states(states) logger.setLevel(logging.DEBUG) # trigger, source, destination self.add_transition('tr_start', 'initial', 'calibrate', after='cb_start_calibration') self.add_transition('tr_calibrated', 'calibrate', 'waiting_for_user', after='cb_waiting_for_user') self.add_transition('tr_user_present', 'waiting_for_user', 'waiting_for_user_calibration', after="cb_waiting_for_user_calibration") self.add_transition('tr_user_calibrated', 'waiting_for_user_calibration', 'program_selection', after='cb_program_selection') self.add_transition('tr_program_selected', 'program_selection', 'learning', conditions='is_template', after="cb_learning") self.add_transition('tr_program_selected', 'program_selection', 'running', unless='is_template') self.add_transition('tr_program_learned', 'learning', 'running', after="cb_running") self.add_transition('tr_running', '*', 'running', after="cb_running")
def log2yaml(filename, presvec): # construct the YAML string # 2nd version, using FSM (Finite State Machine) states = ['none', 'transient', 'nonlinear', 'linear', 'l_setup', 'l_solutiontime', 'l_iteration'] transitions = [ { 'trigger': 'n2t', 'source': 'none', 'dest': 'transient' }, { 'trigger': 't2n', 'source': 'transient', 'dest': 'none' }, { 'trigger': 't2nl', 'source': 'transient', 'dest': 'nonlinear' }, { 'trigger': 'nl2t', 'source': 'nonlinear', 'dest': 'transient' }, { 'trigger': 'nl2l', 'source': 'nonlinear', 'dest': 'linear' }, { 'trigger': 'l2l_setup', 'source': 'linear', 'dest': 'l_setup' }, { 'trigger': 'l_setup2l', 'source': 'l_setup', 'dest': 'linear' }, { 'trigger': 'l2l_solutiontime', 'source': 'linear', 'dest': 'l_solutiontime' }, { 'trigger': 'l_solutiontime2l_iteration', 'source': 'l_solutiontime', 'dest': 'l_iteration'}, { 'trigger': 'l_iteration2t', 'source': 'l_iteration', 'dest': 'transient' }, { 'trigger': 'n2nl', 'source': 'none', 'dest': 'nonlinear' }, { 'trigger': 'n2l_setup', 'source': 'none', 'dest': 'l_setup' } ] logger.setLevel(logging.INFO) machine = Drekar() M = Machine(model=machine, states=states, transitions=transitions, initial='none') mode = 'drekar' start_stepper = 'Entering Rythmos::.*::advanceStepperToTime' end_stepper = 'Leaving Rythmos::.*::advanceStepperToTime' start_c_step = 'Entering Rythmos::.*::takeStep' end_c_step = 'Leaving Rythmos::.*::takeStep' start_nl_step = '(?<=Nonlinear Solver Step )\d*' end_nl_step_good = '\(Converged!\)' end_nl_step_bad = '\(Failed!\)' start_l_step = 'CALCULATING FORCING TERM' start_residual_aztecoo = 'Problem: Thyra::DefaultBlockedLinearOp' start_setuptime = '(?<=TimeMonitor results over )\d*' end_setuptime = '(?<=MueLu setup time )\d*' end_simulation = '(?<=Drekar run completed.)' start_solutiontime = '(?<=Solution time: ).*(?= \(sec.\))' end_l_step = '(?<=total iterations: )\d*' close_file = 'CLOSEFILE' #end_residual_belos_good = '(?<=returned a solve status of "SOLVE_STATUS_CONVERGED" in )\d*' #end_residual_belos_bad = '(?<=returned a solve status of "SOLVE_STATUS_UNCONVERGED" in )\d*' #mid_timers = '.* \(.*\)\s*$' #end_timers = '===========' timer_names = [] timer_times = {} timer_calls = {} timer_serial = None nl_step = 0 t_step = 0 lineno = 0 l_setuptimes = [] cur_setup_time = 0.0 yaml_string = '{"scheme":"' + mode + '","Steps":{' with open(filename) as f: try: for line in f: lineno += 1 if re.search(start_stepper, line) != None: if machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) t_step = 0 elif re.search(end_stepper, line) != None: if machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) elif re.search(start_c_step, line) != None: if machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) machine.n2t() if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"c_step_' + str(t_step) + '":{' elif re.search(end_c_step, line) != None: if machine.state != 'transient': raise RuntimeError('Wrong state: ' + machine.state) machine.t2n() yaml_string += ', "nl_its":' + str(nl_step-1) + '}' nl_step = 0 t_step += 1 # start NONLINEAR state elif re.search(start_nl_step, line) != None: print 'start nonlinear state...' if machine.state != 'transient' and machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) if machine.state == 'none': machine.n2nl() elif machine.state == 'transient': machine.t2nl() if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"nl_step_' + str(nl_step) + '":{' nl_step += 1 elif re.search(end_nl_step_good, line) != None or re.search(end_nl_step_bad, line) != None: print 'end nonlinear state...' if machine.state != 'nonlinear': raise RuntimeError('Wrong state: ' + machine.state) machine.nl2t() print yaml_string # Get rid of ",nl_step_?:{} i = 1 while (yaml_string[-i] != ','): i += 1 yaml_string = yaml_string[:-i] print yaml_string # start LINEAR state elif re.search(start_l_step, line) != None: print 'start linear state...' if machine.state != 'nonlinear': raise RuntimeError('Wrong state: ' + machine.state) machine.nl2l() l_setuptimes[:] = [] # empty list of setup times # Collect setup time data for MueLu elif re.search(start_setuptime, line) != None: print 'collect setup time...' if machine.state == 'none' or machine.state == 'transient': continue if machine.state != 'linear' and machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) machine.l2l_setup() elif re.search(end_setuptime, line) != None: print 'collect setup time done...' if machine.state != 'l_setup': raise RuntimeError('Wrong state: ' + machine.state) m = re.search(end_setuptime, line) if m != None: tt = m.group() else: m = re.search(end_timers_aztecoo, line) tt = m.group() cur_setup_time = line.split(" ")[6] l_setuptimes.append(float(cur_setup_time)) machine.l_setup2l() # extract linear solution time elif re.search(start_solutiontime, line) != None: print 'extract solution time...' if machine.state != 'linear': raise RuntimeError('Wrong state: ' + machine.state) machine.l2l_solutiontime() m = re.search(start_solutiontime, line) tt = m.group() yaml_string += ' "solve_time":' + str(tt) machine.l_solutiontime2l_iteration() # extract linear iterations # end LINEAR state elif re.search(end_l_step, line) != None: print 'extract iterations...' if machine.state != 'l_iteration': raise RuntimeError('Wrong state: ' + machine.state) m = re.search(end_l_step, line) tt = m.group() yaml_string += ', "its":' + str(tt) yaml_string += ', "setup_time":' + str(sum(l_setuptimes)) + '}' l_setuptimes[:] = [] machine.l_iteration2t() print 'end linear state...' elif re.search(end_simulation, line) != None: print 'end simulation' elif re.search(close_file, line) != None: if machine.state == 'linear': yaml_string += ' "its": -1' + ' }' machine.l2t() if machine.state == 'nonlinear': machine.nl2t() # Get rid of ",nl_step_?:{} i = 1 while (yaml_string[-i] != ','): i += 1 yaml_string = yaml_string[:-i] if machine.state == 'transient': i = 1 while (yaml_string[-i] != ','): i += 1 yaml_string = yaml_string[:-i] yaml_string += ', "nl_its":' + str(nl_step) + '}' nl_step = 0 machine.t2n() except RuntimeError as e: raise RuntimeError("Caught an error while parsing on line " + str(lineno) + ": " + e.args[0]) if timer_serial == None: # We did not encounter any timers yaml_string += '}}' print yaml_string print yaml.load(yaml_string) try: yaml_data = yaml.load(yaml_string) except yaml.parser.ParserError: raise RuntimeError('Could not parse YAML out. Did you select the right mode?') return yaml_data
def run(self, filename): """We skip all timer outputs until the last one""" logger.setLevel(logging.DEBUG) machine = self M = Machine(model=machine, states=self.states, transitions=self.transitions, initial='none') # construct the YAML string start_residual = '\*\*\*\*\* Belos Iterative Solver: ' mid_residual = 'Iter.*, \[.*\] :\s*.*' end_residual = '(?<=Number of iterations performed for this solve: )\d*' start_timers = '(?<=TimeMonitor results over )\d*' mid_timers = '.* \(.*\)\s*$' end_timers = '===========' nprocs = -1 timer_names = [] timer_times = {} timer_calls = {} timer_serial = None through_residual = False yaml_string = '{"scheme":"muelu","Steps":{"c_step_0":{"nl_step_0":{' with open(filename) as f: try: for line in f: if re.search(start_residual, line) != None: if machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) machine.n2r() through_residual = True if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"res_hist":[' elif re.search(mid_residual, line) != None: if machine.state != 'residual': raise RuntimeError('Wrong state: ' + machine.state) m = re.search('[^\s]*$', line) res = m.group() if yaml_string[-1] != '[': yaml_string += ',' yaml_string += res elif re.search(end_residual, line) != None: if machine.state != 'residual': raise RuntimeError('Wrong state: ' + machine.state) machine.r2n() m = re.search(end_residual, line) its = m.group() yaml_string += '], "its":' + its + '}}' elif re.search(start_timers, line) != None: if machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) machine.n2t() # Reset timers timer_names = [] timer_times = {} timer_calls = {} m = re.search(start_timers, line) nprocs = m.group() if nprocs == "1": timer_serial = True else: timer_serial = False elif re.search(end_timers, line) != None: if machine.state != 'timers': # there could be other ======== lines continue machine.t2n() elif re.search(mid_timers, line) != None: if machine.state != 'timers': # there could be other matching lines continue if re.search('Timer Name', line) != None: # Skip header (in serial it matches the pattern) continue splits = line.split() if timer_serial == True: name = ' '.join(splits[0:-2]) name = name.replace('"', '\\"') timer_names.append(name) timer_times[name] = {} timer_times[name]['MinOverProcs'] = splits[-2] timer_times[name]['MeanOverProcs'] = splits[-2] timer_times[name]['MaxOverProcs'] = splits[-2] timer_times[name]['MeanOverCallCounts'] = splits[ -2] timer_calls[name] = {} timer_calls[name]['MinOverProcs'] = splits[-1][ 1:-1] timer_calls[name]['MeanOverProcs'] = splits[-1][ 1:-1] timer_calls[name]['MaxOverProcs'] = splits[-1][ 1:-1] timer_calls[name]['MeanOverCallCounts'] = splits[ -1][1:-1] else: name = ' '.join(splits[0:-8]) name = name.replace('"', '\\"') timer_names.append(name) timer_times[name] = {} timer_times[name]['MinOverProcs'] = splits[-8] timer_times[name]['MeanOverProcs'] = splits[-6] timer_times[name]['MaxOverProcs'] = splits[-4] timer_times[name]['MeanOverCallCounts'] = splits[ -2] timer_calls[name] = {} timer_calls[name]['MinOverProcs'] = splits[-7][ 1:-1] timer_calls[name]['MeanOverProcs'] = splits[-5][ 1:-1] timer_calls[name]['MaxOverProcs'] = splits[-3][ 1:-1] timer_calls[name]['MeanOverCallCounts'] = splits[ -1][1:-1] if not through_residual: # Somebody may have given a truncated log with # just timers yaml_string += '"res_hist":[], "its":0}}' if not timer_serial == None: # We did encounter some timers # Finalize stepping yaml_string += '}' yaml_string += ',"Number of processes":' + nprocs yaml_string += ',"Time unit":s' yaml_string += ',"Statistics collected":["MinOverProcs","MeanOverProcs","MaxOverProcs","MeanOverCallCounts"]' # Timer names yaml_string += ',"Timer names":[' for name in timer_names: if yaml_string[-1] != '[': yaml_string += ',' yaml_string += '"' + name + '"' yaml_string += ']' # Total times yaml_string += ',"Total times":{' for name in timer_names: if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"' + name + '":{' yaml_string += '"MinOverProcs":' + timer_times[name][ 'MinOverProcs'] yaml_string += ',"MeanOverProcs":' + timer_times[name][ 'MeanOverProcs'] yaml_string += ',"MaxOverProcs":' + timer_times[name][ 'MaxOverProcs'] yaml_string += ',"MeanOverCallCounts":' + timer_times[ name]['MeanOverCallCounts'] yaml_string += '}' yaml_string += '}' # Call counts yaml_string += ',"Call counts":{' for name in timer_calls: if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"' + name + '":{' yaml_string += '"MinOverProcs":' + timer_times[name][ 'MinOverProcs'] yaml_string += ',"MeanOverProcs":' + timer_times[name][ 'MeanOverProcs'] yaml_string += ',"MaxOverProcs":' + timer_times[name][ 'MaxOverProcs'] yaml_string += ',"MeanOverCallCounts":' + timer_times[ name]['MeanOverCallCounts'] yaml_string += '}' yaml_string += '}' yaml_string += '}' except RuntimeError as e: raise RuntimeError("Caught an error while parsing on line " + str(lineno) + ": " + e.args[0]) if timer_serial == None: # We did not encounter any timers yaml_string += '}}' try: yaml_data = yaml.load(yaml_string) except yaml.parser.ParserError: print('Badly formatted YAML string:\n', yaml_string) raise RuntimeError('Did you select the right mode?') return yaml_data
def run(self, filename): logger.setLevel(logging.INFO) machine = self M = Machine(model=machine, states=self.states, transitions=self.transitions, initial='none') start_stepper = 'Entering Rythmos::.*::advanceStepperToTime' end_stepper = 'Leaving Rythmos::.*::advanceStepperToTime' start_c_step = 'Entering Rythmos::.*::takeStep' end_c_step = 'Leaving Rythmos::.*::takeStep' start_nl_step = '(?<=Nonlinear Solver Step )\d*' end_nl_step_good = '\(Converged!\)' end_nl_step_bad = '\(Failed!\)' start_residual_belos = '\*\*\*\*\* Belos Iterative Solver: ' mid_residual = 'Iter.*, \[.*\] :\s*.*' end_residual_belos_good = '(?<=returned a solve status of "SOLVE_STATUS_CONVERGED" in )\d*' end_residual_belos_bad = '(?<=returned a solve status of "SOLVE_STATUS_UNCONVERGED" in )\d*' start_timers = '(?<=TimeMonitor results over )\d*' mid_timers = '.* \(.*\)\s*$' end_timers = '===========' timer_names = [] timer_times = {} timer_calls = {} timer_serial = None nl_step = 0 t_step = 0 lineno = 0 yaml_string = '{"scheme":"drekar","Steps":{' with open(filename) as f: try: for line in f: lineno += 1 if re.search(start_stepper, line) != None: if machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) t_step = 0 elif re.search(end_stepper, line) != None: if machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) elif re.search(start_c_step, line) != None: if machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) machine.n2t() if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"c_step_' + str(t_step) + '":{' elif re.search(end_c_step, line) != None: if machine.state != 'transient': raise RuntimeError('Wrong state: ' + machine.state) machine.t2n() yaml_string += ', "nl_its":' + str(nl_step) + '}' nl_step = 0 t_step += 1 elif re.search(start_nl_step, line) != None: if machine.state != 'transient': raise RuntimeError('Wrong state: ' + machine.state) machine.t2nl() if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"nl_step_' + str(nl_step) + '":{' nl_step += 1 elif re.search(end_nl_step_good, line) != None or re.search( end_nl_step_bad, line) != None: if machine.state != 'nonlinear': raise RuntimeError('Wrong state: ' + machine.state) machine.nl2t() # Get rid of ",nl_step_?:{} i = 1 while (yaml_string[-i] != ','): i += 1 yaml_string = yaml_string[:-i] elif re.search(start_residual_belos, line) != None: if machine.state != 'nonlinear': raise RuntimeError('Wrong state: ' + machine.state) machine.nl2l() if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"res_hist":[' elif re.search(end_residual_belos_good, line) != None or re.search( end_residual_belos_bad, line) != None: if machine.state != 'linear': raise RuntimeError('Wrong state: ' + machine.state) machine.l2t() m = re.search(end_residual_belos_good, line) if m != None: its = m.group() else: m = re.search(end_residual_belos_bad, line) its = m.group() yaml_string += '], "its":' + its m = re.search( '(?<=with total CPU time of ).*(?=\ sec)', line) belos_time = m.group() yaml_string += ', "solve_time":' + belos_time + '}' elif re.search(mid_residual, line) != None: if machine.state != 'linear': raise RuntimeError('Wrong state: ' + machine.state) m = re.search('[^\s]*$', line) res = m.group() if yaml_string[-1] != '[': yaml_string += ',' yaml_string += res elif re.search(start_timers, line) != None: if machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) machine.n2m() m = re.search(start_timers, line) nprocs = m.group() if nprocs == "1": timer_serial = True else: timer_serial = False # Finalize stepping yaml_string += '}' yaml_string += ',"Number of processes":' + nprocs yaml_string += ',"Time unit":s' yaml_string += ',"Statistics collected":["MinOverProcs","MeanOverProcs","MaxOverProcs","MeanOverCallCounts"]' elif re.search(end_timers, line) != None: if machine.state != 'timers': # there could be other ======== lines continue machine.m2n() # Timer names yaml_string += ',"Timer names":[' for name in timer_names: if yaml_string[-1] != '[': yaml_string += ',' yaml_string += '"' + name + '"' yaml_string += ']' # Total times yaml_string += ',"Total times":{' for name in timer_names: if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"' + name + '":{' yaml_string += '"MinOverProcs":' + timer_times[ name]['MinOverProcs'] yaml_string += ',"MeanOverProcs":' + timer_times[ name]['MeanOverProcs'] yaml_string += ',"MaxOverProcs":' + timer_times[ name]['MaxOverProcs'] yaml_string += ',"MeanOverCallCounts":' + timer_times[ name]['MeanOverCallCounts'] yaml_string += '}' yaml_string += '}' # Call counts yaml_string += ',"Call counts":{' for name in timer_calls: if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"' + name + '":{' yaml_string += '"MinOverProcs":' + timer_times[ name]['MinOverProcs'] yaml_string += ',"MeanOverProcs":' + timer_times[ name]['MeanOverProcs'] yaml_string += ',"MaxOverProcs":' + timer_times[ name]['MaxOverProcs'] yaml_string += ',"MeanOverCallCounts":' + timer_times[ name]['MeanOverCallCounts'] yaml_string += '}' yaml_string += '}' yaml_string += '}' elif re.search(mid_timers, line) != None: if machine.state != 'timers': # there could be other matching lines continue if re.search('Timer Name', line) != None: # Skip header (in serial it matches the pattern) continue splits = line.split() if timer_serial == True: name = ' '.join(splits[0:-2]) name = name.replace('"', '\\"') timer_names.append(name) timer_times[name] = {} timer_times[name]['MinOverProcs'] = splits[-2] timer_times[name]['MeanOverProcs'] = splits[-2] timer_times[name]['MaxOverProcs'] = splits[-2] timer_times[name]['MeanOverCallCounts'] = splits[ -2] timer_calls[name] = {} timer_calls[name]['MinOverProcs'] = splits[-1][ 1:-1] timer_calls[name]['MeanOverProcs'] = splits[-1][ 1:-1] timer_calls[name]['MaxOverProcs'] = splits[-1][ 1:-1] timer_calls[name]['MeanOverCallCounts'] = splits[ -1][1:-1] else: name = ' '.join(splits[0:-8]) name = name.replace('"', '\\"') timer_names.append(name) timer_times[name] = {} timer_times[name]['MinOverProcs'] = splits[-8] timer_times[name]['MeanOverProcs'] = splits[-6] timer_times[name]['MaxOverProcs'] = splits[-4] timer_times[name]['MeanOverCallCounts'] = splits[ -2] timer_calls[name] = {} timer_calls[name]['MinOverProcs'] = splits[-7][ 1:-1] timer_calls[name]['MeanOverProcs'] = splits[-5][ 1:-1] timer_calls[name]['MaxOverProcs'] = splits[-3][ 1:-1] timer_calls[name]['MeanOverCallCounts'] = splits[ -1][1:-1] except RuntimeError as e: raise RuntimeError("Caught an error while parsing on line " + str(lineno) + ": " + e.args[0]) if timer_serial == None: # We did not encounter any timers yaml_string += '}}' try: yaml_data = yaml.load(yaml_string) except yaml.parser.ParserError: raise RuntimeError( 'Could not parse YAML out. Did you select the right mode?') return yaml_data
import cv import numpy as np from PyQt4 import QtGui from PyQt4 import QtCore import ipdb import traceback import ArenaController from FishTrackerWidget import FishTrackerWidget from utility_widgets import PathSelectorWidget from utility_widgets import LabeledSpinBox from transitions import Machine # Set up state machine logging to std out import logging from transitions import logger logger.setLevel(logging.INFO) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) logger.addHandler(ch) from itertools import tee, izip def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = tee(iterable) next(b, None) return izip(a, b) class Side: S1 = 1
import logging from transitions import logger, Machine, State logger.setLevel(logging.INFO) states = ['liquid', 'solid', 'gas', 'plasma'] transitions = [{ 'trigger': 'melt', 'source': 'solid', 'dest': 'liquid' }, { 'trigger': 'evaporate', 'source': 'liquid', 'dest': 'gas' }, { 'trigger': 'ionize', 'source': 'gas', 'dest': 'plasma' }] # Business as usual machine = Machine(states=states, transitions=transitions, initial='solid')
def log2yaml_fsm(filename): # construct the YAML string # 2nd version, using FSM (Finite State Machine) states = ['none', 'transient', 'nonlinear', 'linear', 'timers'] transitions = [ { 'trigger': 'n2m', 'source': 'none', 'dest': 'timers' }, { 'trigger': 'n2t', 'source': 'none', 'dest': 'transient' }, { 'trigger': 't2n', 'source': 'transient', 'dest': 'none' }, { 'trigger': 't2nl', 'source': 'transient', 'dest': 'nonlinear' }, { 'trigger': 'nl2t', 'source': 'nonlinear', 'dest': 'transient' }, { 'trigger': 'nl2l', 'source': 'nonlinear', 'dest': 'linear' }, { 'trigger': 'l2nl', 'source': 'linear', 'dest': 'nonlinear' }, { 'trigger': 'l2t', 'source': 'linear', 'dest': 'transient' }, { 'trigger': 'm2n', 'source': 'timers', 'dest': 'none' } ] logger.setLevel(logging.INFO) machine = Drekar() M = Machine(model=machine, states=states, transitions=transitions, initial='none') mode = 'drekar' start_stepper = 'Entering Rythmos::.*::advanceStepperToTime' end_stepper = 'Leaving Rythmos::.*::advanceStepperToTime' start_c_step = 'Entering Rythmos::.*::takeStep' end_c_step = 'Leaving Rythmos::.*::takeStep' start_nl_step = '(?<=Nonlinear Solver Step )\d*' end_nl_step = '\(Converged!\)' start_residual_belos = '\*\*\*\*\* Belos Iterative Solver: ' mid_residual = 'Iter.*, \[.*\] :\s*.*' end_residual_belos = '(?<=returned a solve status of "SOLVE_STATUS_CONVERGED" in )\d*' start_timers = '(?<=TimeMonitor results over )\d*' mid_timers = '.* \(.*\)\s*$' end_timers = '===========' timer_names = [] timer_times = {} timer_calls = {} timer_serial = None nl_step = 0 t_step = 0 yaml_string = '{"scheme":"' + mode + '","Steps":{' with open(filename) as f: for line in f: if re.search(start_stepper, line) != None: if machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) t_step = 0 elif re.search(end_stepper, line) != None: if machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) elif re.search(start_c_step, line) != None: if machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) machine.n2t() if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"c_step_' + str(t_step) + '":{' elif re.search(end_c_step, line) != None: if machine.state != 'transient': raise RuntimeError('Wrong state: ' + machine.state) machine.t2n() yaml_string += ', "nl_its":' + str(nl_step) + '}' nl_step = 0 t_step += 1 elif re.search(start_nl_step, line) != None: if machine.state != 'transient': raise RuntimeError('Wrong state: ' + machine.state) machine.t2nl() if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"nl_step_' + str(nl_step) + '":{' nl_step += 1 elif re.search(end_nl_step, line) != None: if machine.state != 'nonlinear': raise RuntimeError('Wrong state: ' + machine.state) machine.nl2t() # Get rid of ",nl_step_?:{} i = 1 while (yaml_string[-i] != ','): i += 1 yaml_string = yaml_string[:-i] elif re.search(start_residual_belos, line) != None: if machine.state != 'nonlinear': raise RuntimeError('Wrong state: ' + machine.state) machine.nl2l() if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"res_hist":[' elif re.search(end_residual_belos, line) != None: if machine.state != 'linear': raise RuntimeError('Wrong state: ' + machine.state) if mode == 'drekar': machine.l2t() elif mode == 'albany': machine.l2nl() m = re.search(end_residual_belos, line) its = m.group() yaml_string += '], "its":' + its m = re.search('(?<=with total CPU time of ).*(?=\ sec)', line) belos_time = m.group() yaml_string += ', "solve_time":' + belos_time + '}' elif re.search(mid_residual, line) != None: if machine.state != 'linear': raise RuntimeError('Wrong state: ' + machine.state) m = re.search('[^\s]*$', line) res = m.group() if yaml_string[-1] != '[': yaml_string += ',' yaml_string += res elif re.search(start_timers, line) != None: if machine.state != 'none': raise RuntimeError('Wrong state: ' + machine.state) machine.n2m() m = re.search(start_timers, line) nprocs = m.group() if nprocs == "1": timer_serial = True else: timer_serial = False # Finalize stepping yaml_string += '}' yaml_string += ',"Number of processes":' + nprocs yaml_string += ',"Time unit":s' yaml_string += ',"Statistics collected":["MinOverProcs","MeanOverProcs","MaxOverProcs","MeanOverCallCounts"]' elif re.search(end_timers, line) != None: if machine.state != 'timers': # there could be other ======== lines continue machine.m2n() # Timer names yaml_string += ',"Timer names":[' for name in timer_names: if yaml_string[-1] != '[': yaml_string += ',' yaml_string += '"' + name + '"' yaml_string += ']' # Total times yaml_string += ',"Total times":{' for name in timer_names: if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"' + name + '":{' yaml_string += '"MinOverProcs":' + timer_times[name]['MinOverProcs'] yaml_string += ',"MeanOverProcs":' + timer_times[name]['MeanOverProcs'] yaml_string += ',"MaxOverProcs":' + timer_times[name]['MaxOverProcs'] yaml_string += ',"MeanOverCallCounts":' + timer_times[name]['MeanOverCallCounts'] yaml_string += '}' yaml_string += '}' # Call counts yaml_string += ',"Call counts":{' for name in timer_calls: if yaml_string[-1] != '{': yaml_string += ',' yaml_string += '"' + name + '":{' yaml_string += '"MinOverProcs":' + timer_times[name]['MinOverProcs'] yaml_string += ',"MeanOverProcs":' + timer_times[name]['MeanOverProcs'] yaml_string += ',"MaxOverProcs":' + timer_times[name]['MaxOverProcs'] yaml_string += ',"MeanOverCallCounts":' + timer_times[name]['MeanOverCallCounts'] yaml_string += '}' yaml_string += '}' yaml_string += '}' elif re.search(mid_timers, line) != None: if machine.state != 'timers': # there could be other matching lines continue if re.search('Timer Name', line) != None: # Skip header (in serial it matches the pattern) continue splits = line.split() if timer_serial == True: name = ' '.join(splits[0:-2]) name = name.replace('"', '\\"') timer_names.append(name) timer_times[name] = {} timer_times[name]['MinOverProcs'] = splits[-2]; timer_times[name]['MeanOverProcs'] = splits[-2]; timer_times[name]['MaxOverProcs'] = splits[-2]; timer_times[name]['MeanOverCallCounts'] = splits[-2]; timer_calls[name] = {} timer_calls[name]['MinOverProcs'] = splits[-1][1:-1]; timer_calls[name]['MeanOverProcs'] = splits[-1][1:-1]; timer_calls[name]['MaxOverProcs'] = splits[-1][1:-1]; timer_calls[name]['MeanOverCallCounts'] = splits[-1][1:-1]; else: name = ' '.join(splits[0:-8]) name = name.replace('"', '\\"') timer_names.append(name) timer_times[name] = {} timer_times[name]['MinOverProcs'] = splits[-8]; timer_times[name]['MeanOverProcs'] = splits[-6]; timer_times[name]['MaxOverProcs'] = splits[-4]; timer_times[name]['MeanOverCallCounts'] = splits[-2]; timer_calls[name] = {} timer_calls[name]['MinOverProcs'] = splits[-7][1:-1]; timer_calls[name]['MeanOverProcs'] = splits[-5][1:-1]; timer_calls[name]['MaxOverProcs'] = splits[-3][1:-1]; timer_calls[name]['MeanOverCallCounts'] = splits[-1][1:-1]; if timer_serial == None: # We did not encounter any timers yaml_string += '}}' try: yaml_data = yaml.load(yaml_string) except yaml.parser.ParserError: raise RuntimeError('Could not parse YAML out. Did you select the right mode?') return yaml_data