From 2cf1320672ee0e500737e46a419b2380de60e4b7 Mon Sep 17 00:00:00 2001 From: LSaldyt Date: Wed, 25 Oct 2017 10:21:07 -0700 Subject: [PATCH] Creates a GUI specific runnable --- copycat/copycat.py | 29 ++++++++++++++++++++++++++++- copycat/gui/gui.py | 16 +++++++++++----- copycat/gui/primary.py | 2 +- copycat/workspace.py | 10 ++++++++++ gui.py | 24 ++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 7 deletions(-) create mode 100755 gui.py diff --git a/copycat/copycat.py b/copycat/copycat.py index 7f210de..8e14abc 100644 --- a/copycat/copycat.py +++ b/copycat/copycat.py @@ -92,14 +92,41 @@ class Copycat(object): self.reporter.report_answer(answer) return answer + def runGUI(self): + while not self.check_reset(): + self.gui.update(self) + self.gui.refresh() + answers = {} + while True: + if self.check_reset(): + answers = {} + answer = self.runTrial() + if self.showgui: + self.gui.app.log('Answered: {}'.format(answer['answer'])) + d = answers.setdefault(answer['answer'], { + 'count': 0, + 'sumtemp': 0, + 'sumtime': 0 + }) + d['count'] += 1 + d['sumtemp'] += answer['temp'] + d['sumtime'] += answer['time'] + if self.showgui: + self.gui.add_answers(answers) + + for answer, d in answers.items(): + d['avgtemp'] = d.pop('sumtemp') / d['count'] + d['avgtime'] = d.pop('sumtime') / d['count'] + def run(self, initial, modified, target, iterations): self.reset_with_strings(initial, modified, target) answers = {} for i in range(iterations): if self.check_reset(): answers = {} - answer = self.runTrial() + if self.showgui: + self.gui.app.log('Answered: {}'.format(answer['answer'])) d = answers.setdefault(answer['answer'], { 'count': 0, 'sumtemp': 0, diff --git a/copycat/gui/gui.py b/copycat/gui/gui.py index 850b78e..47dd9dc 100755 --- a/copycat/gui/gui.py +++ b/copycat/gui/gui.py @@ -38,6 +38,11 @@ class MainApplication(GridFrame): self.iterations = 0 + self.messages = [] + + def log(self, message): + self.messages.append(message) + def create_widgets(self): self.slipList = tk.Listbox(self, **style, bd=0) @@ -49,11 +54,8 @@ class MainApplication(GridFrame): self.objectList = tk.Listbox(self, **style, bd=0) self.add(self.objectList, 2, 1) - #l = ttk.Label(self, text='', **style, padding=30) - #self.add(l, 2, 1) - - self.log = tk.Label(self, text='[Logging]', **style) - self.add(self.log, 1, 0) + self.logBox = tk.Label(self, text='', **style, bd=1) + self.add(self.logBox, 1, 0) self.graph2 = Status() sframe2 = StatusFrame(self, self.graph2, 'graph 2') self.add(sframe2, 2, 0) @@ -85,6 +87,10 @@ class MainApplication(GridFrame): listStr = str(o) self.objectList.insert(tk.END, listStr) + self.logBox['text'] = '\n'.join(list(reversed(self.messages))[:10]) + if len(self.messages) > 10: + self.logBox['text'] += '\n...' + def reset_with_strings(self, initial, modified, target): self.primary.reset_with_strings(initial, modified, target) diff --git a/copycat/gui/primary.py b/copycat/gui/primary.py index 5b885e8..d552b2a 100644 --- a/copycat/gui/primary.py +++ b/copycat/gui/primary.py @@ -61,7 +61,7 @@ class Primary(GridFrame): self.add(self.control, 0, 2) def update(self, copycat): - answer = '' if copycat.workspace.rule is None else copycat.workspace.rule.buildTranslatedRule() + answer = '' if copycat.workspace.rule is None else copycat.workspace.rule.buildTranslatedRule() self.canvas = create_main_canvas(self, self.initial, self.modified, self.target, answer) self.add(self.canvas, 0, 0, xspan=2) diff --git a/copycat/workspace.py b/copycat/workspace.py index 3e5228a..ebc7c5d 100644 --- a/copycat/workspace.py +++ b/copycat/workspace.py @@ -19,6 +19,16 @@ class Workspace(object): self.intraStringUnhappiness = 0.0 self.interStringUnhappiness = 0.0 + # LSaldyt: default initializations for GUI entry + self.targetString = '' + self.initialString = '' + self.modifiedString = '' + self.finalAnswer = None + self.changedObject = None + self.objects = [] + self.structures = [] + self.rule = None + def __repr__(self): return '' % ( self.initialString, self.modifiedString, self.targetString, diff --git a/gui.py b/gui.py new file mode 100755 index 0000000..af8292d --- /dev/null +++ b/gui.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +import argparse +import logging + +from copycat import Copycat, Reporter + +class SimpleReporter(Reporter): + def report_answer(self, answer): + print('Answered %s (time %d, final temperature %.1f)' % ( + answer['answer'], answer['time'], answer['temp'], + )) + +def main(): + logging.basicConfig(level=logging.INFO, format='%(message)s', filename='./output/copycat.log', filemode='w') + + parser = argparse.ArgumentParser() + parser.add_argument('--seed', type=int, default=None, help='Provide a deterministic seed for the RNG.') + options = parser.parse_args() + + copycat = Copycat(reporter=SimpleReporter(), rng_seed=options.seed) + copycat.runGUI() + +if __name__ == '__main__': + main()