diff --git a/copycat/copycat.py b/copycat/copycat.py index 9f77ccc..ade6a4d 100644 --- a/copycat/copycat.py +++ b/copycat/copycat.py @@ -42,6 +42,8 @@ class Copycat(object): self.reporter.report_coderack(self.coderack) self.reporter.report_temperature(self.temperature) self.reporter.report_workspace(self.workspace) + if (self.showgui): + self.gui.update(self) def update_workspace(self, currentTime): self.workspace.updateEverything() @@ -60,7 +62,7 @@ class Copycat(object): self.step() if self.showgui: - self.gui.update(self) + self.gui.refresh() def runTrial(self): """Run a trial of the copycat algorithm""" @@ -91,6 +93,8 @@ class Copycat(object): 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'] diff --git a/copycat/gui/control.py b/copycat/gui/control.py index 83dc33a..2e48959 100644 --- a/copycat/gui/control.py +++ b/copycat/gui/control.py @@ -10,10 +10,10 @@ class Control(GridFrame): self.paused = True self.steps = 0 - self.playbutton = tk.Button(self, text='Play', command=lambda : self.toggle(), background='black', foreground='white', activebackground='black', activeforeground='blue') + self.playbutton = tk.Button(self, bd=0, text='Play', command=lambda : self.toggle(), background='black', foreground='white', activebackground='black', activeforeground='blue') self.add(self.playbutton, 0, 0) - self.stepbutton= tk.Button(self, text='Step', command=lambda : self.step(), background='black', foreground='white', activebackground='black', activeforeground='blue') + self.stepbutton= tk.Button(self, bd=0, text='Step', command=lambda : self.step(), background='black', foreground='white', activebackground='black', activeforeground='blue') self.add(self.stepbutton, 1, 0) def toggle(self): diff --git a/copycat/gui/gridframe.py b/copycat/gui/gridframe.py index 773c847..d5218b9 100644 --- a/copycat/gui/gridframe.py +++ b/copycat/gui/gridframe.py @@ -1,9 +1,9 @@ import tkinter as tk import tkinter.ttk as ttk -class GridFrame(ttk.Frame): +class GridFrame(tk.Frame): def __init__(self, parent, *args, **kwargs): - ttk.Frame.__init__(self, parent, *args, **kwargs) + tk.Frame.__init__(self, parent, *args, **kwargs, bd=0) def add(self, element, x, y, xspan=1, yspan=1): element.grid(column=x, row=y, columnspan=xspan, rowspan=yspan, sticky=tk.N+tk.E+tk.S+tk.W) diff --git a/copycat/gui/gui.py b/copycat/gui/gui.py index b9672a7..efa52f6 100755 --- a/copycat/gui/gui.py +++ b/copycat/gui/gui.py @@ -11,6 +11,8 @@ from .status import Status, StatusFrame from .gridframe import GridFrame from .primary import Primary +from .plot import plot_imbedded + font1Size = 32 font2Size = 16 font1 = ('Helvetica', str(font1Size)) @@ -30,22 +32,20 @@ class MainApplication(GridFrame): self.primary = Primary(self, *args, **kwargs) self.add(self.primary, 0, 0) self.create_widgets() + + self.iterations = 0 def create_widgets(self): - tempLabel = ttk.Label(self, text='', **style, padding=30) - #tempLabel.grid(column=1, row=0, sticky=tk.N+tk.S+tk.E+tk.W) - self.widgets['temp'] = tempLabel - - slipList = tk.Listbox(self, **style) + slipList = tk.Listbox(self, **style, bd=0) self.add(slipList, 0, 1) self.widgets['sliplist'] = slipList - codeletList = tk.Listbox(self, **style) + codeletList = tk.Listbox(self, **style, bd=0) self.add(codeletList, 1, 1) self.widgets['codeletlist'] = codeletList - l = ttk.Label(self, text='temp', **style, padding=30) + l = ttk.Label(self, text='', **style, padding=30) self.add(l, 2, 1) self.graph1 = Status() @@ -56,14 +56,18 @@ class MainApplication(GridFrame): self.add(sframe2, 2, 0) def update(self, copycat): + self.iterations += 1 self.primary.update(copycat) temp = copycat.temperature.value() + self.graph1.x += [self.iterations] + self.graph1.y += [temp] + #self.widgets['temp']['text'] = 'Temp:\n{}'.format(round(temp, 2)) + slipnodes = copycat.slipnet.slipnodes codelets = copycat.coderack.codelets slipList = self.widgets['sliplist'] slipList.delete(0, slipList.size()) - self.widgets['temp']['text'] = 'Temp:\n{}'.format(round(temp, 2)) slipnodes = sorted(slipnodes, key=lambda s:s.activation, reverse=True) for item in slipnodes: listStr = '{}: {}'.format(item.name, round(item.activation, 2)) @@ -88,9 +92,16 @@ class GUI(object): self.lastUpdated = time.time() self.updateInterval = updateInterval - def update(self, copycat): + def add_answers(self, answers): + def modifier(status): + plot_imbedded(answers, status) + self.app.graph2.modifier = modifier + + def refresh(self): self.root.update_idletasks() self.root.update() + + def update(self, copycat): current = time.time() if current - self.lastUpdated > self.updateInterval: self.app.update(copycat) diff --git a/copycat/gui/plot.py b/copycat/gui/plot.py new file mode 100644 index 0000000..8583ff7 --- /dev/null +++ b/copycat/gui/plot.py @@ -0,0 +1,17 @@ +import matplotlib.pyplot as plt; plt.rcdefaults() +import numpy as np +import matplotlib.pyplot as plt + +def plot_imbedded(answers, status): + answers = sorted(answers.items(), key=lambda kv : kv[1]['count']) + objects = [t[0] for t in answers] + yvalues = [t[1]['count'] for t in answers] + + y_pos = np.arange(len(objects)) + + status.subplot.clear() + status.subplot.bar(y_pos, yvalues, align='center', alpha=0.5) + status.subplot.set_xticks(y_pos) + status.subplot.set_xticklabels(tuple(objects)) + status.subplot.set_ylabel('Count') + status.subplot.set_title('Answers') diff --git a/copycat/gui/primary.py b/copycat/gui/primary.py index a41cb98..fd5672e 100644 --- a/copycat/gui/primary.py +++ b/copycat/gui/primary.py @@ -24,6 +24,8 @@ def create_main_canvas(root, initial, final, new, guess): def add_sequences(sequences, x, y): for sequence in sequences: x += padding + if sequence is None: + sequence = '' for char in sequence: canvas.create_text(x, y, text=char, anchor=tk.NW, font=font1, fill='white') x += font1Size diff --git a/copycat/gui/status.py b/copycat/gui/status.py index 00b77e6..9a68d5b 100644 --- a/copycat/gui/status.py +++ b/copycat/gui/status.py @@ -38,12 +38,17 @@ class Status(object): self.subplot = self.figure.add_subplot(111) self.x = [] self.y = [] + + def modifier(status): + status.subplot.plot(status.x, status.y) + + self.modifier = modifier self.update_plots(0) def update_plots(self, i): self.subplot.clear() with plt.style.context(('dark_background')): - self.subplot.plot(self.x, self.y) + self.modifier(self) if __name__ == '__main__': app = tk.Tk() diff --git a/copycat/plot.py b/copycat/plot.py index 1e33cae..7e6ec71 100644 --- a/copycat/plot.py +++ b/copycat/plot.py @@ -16,4 +16,3 @@ def plot_answers(answers): plt.ylabel('Count') plt.title('Answers') plt.show() - diff --git a/main.py b/main.py index df59cae..36b5f0c 100755 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ import argparse import logging -from copycat import Copycat, Reporter, plot_answers +from copycat import Copycat, Reporter class SimpleReporter(Reporter): def report_answer(self, answer): @@ -27,7 +27,7 @@ def main(): for answer, d in sorted(iter(answers.items()), key=lambda kv: kv[1]['avgtemp']): print('%s: %d (avg time %.1f, avg temp %.1f)' % (answer, d['count'], d['avgtime'], d['avgtemp'])) - plot_answers(answers) + #plot_answers(answers) if __name__ == '__main__': main()