Adds live temperature and answer graphs
This commit is contained in:
@ -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']
|
||||
|
||||
@ -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):
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
17
copycat/gui/plot.py
Normal file
17
copycat/gui/plot.py
Normal file
@ -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')
|
||||
@ -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
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -16,4 +16,3 @@ def plot_answers(answers):
|
||||
plt.ylabel('Count')
|
||||
plt.title('Answers')
|
||||
plt.show()
|
||||
|
||||
|
||||
4
main.py
4
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()
|
||||
|
||||
Reference in New Issue
Block a user