# Created by Leo from: C:\Python22\Tom\concious.leo # @path c:\python22\tom # << concious3 declarations >> #!/usr/bin/env python from wxPython.wx import * from wxPython.lib import colourdb import random import time import pickle import math # -- end -- << concious3 declarations >> # << concious3 methods >> (1 of 8) def findword(): num = random.Random() num.seed() mystring = '' wordflag = 0 while not wordflag: mynum = num.randrange(64, 90) if mynum == 64: if len(mystring) > 4: wordflag = 1 else: mystring = '' else: mystring = mystring + chr(mynum) return mystring # << concious3 methods >> (2 of 8) def plant(): g.seed() h.seed(g) return abs(h.random()- g.random()) # << concious3 methods >> (3 of 8) def rand(a, b): return (b-a)*random.random() + a # << concious3 methods >> (4 of 8) def makeMatrix(I, J, fill=0.0): m = [] for i in range(I): m.append([fill]*J) return m # << concious3 methods >> (5 of 8) class NN: # << class NN methods >> (1 of 6) def __init__(self, ni, nh, no): self.ni = ni + 1 self.nh = nh self.no = no self.NNerror = '' self.testpattern = [] self.ai = [1.0]*self.ni self.ah = [1.0]*self.nh self.ao = [1.0]*self.no self.wi = makeMatrix(self.ni, self.nh) self.wo = makeMatrix(self.nh, self.no) for i in range(self.ni): for j in range(self.nh): self.wi[i][j] = rand(-2.0, 2.0) for j in range(self.nh): for k in range(self.no): self.wo[j][k] = rand(-2.0, 2.0) self.ci = makeMatrix(self.ni, self.nh) self.co = makeMatrix(self.nh, self.no) # << class NN methods >> (2 of 6) def update(self, inputs): if len(inputs) != self.ni-1: raise ValueError, 'wrong number of inputs' for i in range(self.ni-1): self.ai[i] = inputs[i] for j in range(self.nh): sum = 0.0 for i in range(self.ni): sum = sum + self.ai[i] * self.wi[i][j] self.ah[j] = 1.0/(1.0+math.exp(-sum)) for k in range(self.no): sum = 0.0 for j in range(self.nh): sum = sum + self.ah[j] * self.wo[j][k] self.ao[k] = 1.0/(1.0+math.exp(-sum)) return self.ao[:] # << class NN methods >> (3 of 6) def backPropagate(self, targets, N, M): if len(targets) != self.no: raise ValueError, 'wrong number of target values' output_deltas = [0.0] * self.no for k in range(self.no): ao = self.ao[k] output_deltas[k] = ao*(1-ao)*(targets[k]-ao) hidden_deltas = [0.0] * self.nh for j in range(self.nh): sum = 0.0 for k in range(self.no): sum = sum + output_deltas[k]*self.wo[j][k] hidden_deltas[j] = self.ah[j]*(1-self.ah[j])*sum for j in range(self.nh): for k in range(self.no): change = output_deltas[k]*self.ah[j] self.wo[j][k] = self.wo[j][k] + N*change + M*self.co[j][k] self.co[j][k] = change for i in range(self.ni): for j in range(self.nh): change = hidden_deltas[j]*self.ai[i] self.wi[i][j] = self.wi[i][j] + N*change + M*self.ci[i][j] self.ci[i][j] = change error = 0.0 for k in range(len(targets)): error = error + 0.5*(targets[k]-self.ao[k])**2 return error # << class NN methods >> (4 of 6) def test(self, patterns): self.testpattern = [] for p in patterns: self.testpattern.append(self.update(p[0])) return self.testpattern # << class NN methods >> (5 of 6) def weights(self): print 'Input weights:' for i in range(self.ni): print self.wi[i] print print 'Output weights:' for j in range(self.nh): print self.wo[j] # << class NN methods >> (6 of 6) def train(self, patterns, iterations=2000, N=0.5, M=0.1): for i in xrange(iterations): error = 0.0 for p in patterns: inputs = p[0] targets = p[1] self.update(inputs) error = error + self.backPropagate(targets, N, M) if i % 100 == 0: self.NNerror = 'error %-14f' % error # -- end -- << class NN methods >> # << concious3 methods >> (6 of 8) class GraphWindow(wxScrolledWindow): # << class GraphWindow methods >> (1 of 6) def __init__(self, parent, ID, pos = wxPoint(0, 0), size = wxSize(1000,1000), Secconds = [], Nuralguess = []): wxScrolledWindow.__init__(self, parent, ID, pos , size , wxSUNKEN_BORDER) self.SetScrollbars(20, 20, 50, 50) self.nuralcounter = 0 self.NuralNet = NN(1, 7, 1) self.Testpattern = [] self.Trainpattern = [] for i in range(60): self.Testpattern.append([plant()]) self.Trainpattern.append([[plant()],[plant()]]) ID_Timer = wxNewId() self.timer = wxTimer(self, ID_Timer) EVT_TIMER(self, ID_Timer, self.OnTimer) EVT_PAINT(self, self.OnPaint) if Nuralguess == []: self.NuralGuess = range(86399) for i in range(86399): self.NuralGuess[i] = 0.0 else: self.NuralGuess = Nuralguess if Secconds == []: self.SeccondsinDay = range(86399) for i in range(86399): self.SeccondsinDay[i] = 0.0 else: self.SeccondsinDay = Secconds # << class GraphWindow methods >> (2 of 6) def TimeStart(self): self.timer.Start(1000) # << class GraphWindow methods >> (3 of 6) def TimeStop(self): self.timer.Stop() # << class GraphWindow methods >> (4 of 6) def OnTimer(self, event): self.nuralcounter = self.nuralcounter +1 pattern = [] timenow = time.localtime()[3]*3600 + time.localtime()[4] * 60 + time.localtime()[5] self.SeccondsinDay[timenow] = plant() self.Testpattern = self.NuralNet.test([ [ [ self.SeccondsinDay[timenow - 1] ], [0] ],[ [ self.SeccondsinDay[timenow] ], [0] ] ]) if (timenow + 60) < 86399: self.NuralGuess[timenow + 60] = self.Testpattern[1][0] if self.nuralcounter == 104: self.nuralcounter = 0 self.OnNuralTimer() # << class GraphWindow methods >> (5 of 6) def OnNuralTimer(self): self.Trainpattern = [] if time.localtime()[3] * 3600 + time.localtime()[4] * 60 + time.localtime()[5] - 60 >= 3600: for i in range(time.localtime()[3] * 3600 + time.localtime()[4] * 60 + time.localtime()[5] - 60, time.localtime()[3] * 3600 + time.localtime()[4] * 60 + time.localtime()[5]): self.Trainpattern.append([ [ self.SeccondsinDay[i - 3600] ], [ self.SeccondsinDay[i] ] ]) else: for i in range(3600,3660): self.Trainpattern.append([ [ self.SeccondsinDay[i - 3600] ], [ self.SeccondsinDay[i]] ]) self.NuralNet.train(self.Trainpattern) # << class GraphWindow methods >> (6 of 6) def OnPaint(self, event): dc = wxPaintDC(self) self.PrepareDC(dc) dc.BeginDrawing() dc.SetPen(wxPen(wxNamedColour('BLUE'), 4)) dc.DrawLine(0,50, 1000, 50) dc.SetFont(wxFont(14, wxSWISS, wxNORMAL, wxNORMAL)) dc.SetTextForeground(wxColour(0xFF, 0x20, 0xFF)) for i in range(23): dc.DrawText(str(i+1),(i +1) * 41, 20) for i in range(86399): if self.SeccondsinDay[i] != 0.0: if (self.SeccondsinDay[i] * 100) < 10: dc.SetPen(wxPen(wxNamedColour('RED'), 1)) elif (self.SeccondsinDay[i] * 100) < 20: dc.SetPen(wxPen(wxNamedColour('DARKORANGE1'), 1)) elif (self.SeccondsinDay[i] * 100) < 30: dc.SetPen(wxPen(wxNamedColour('GOLD1'), 1)) elif (self.SeccondsinDay[i] * 100) < 40: dc.SetPen(wxPen(wxNamedColour('GREEN1'), 1)) elif (self.SeccondsinDay[i] * 100) < 50: dc.SetPen(wxPen(wxNamedColour('BLUE1'), 1)) elif (self.SeccondsinDay[i] * 100) < 60: dc.SetPen(wxPen(wxNamedColour('PLUM'), 1)) elif (self.SeccondsinDay[i] * 100) < 70: dc.SetPen(wxPen(wxNamedColour('ORCHID'), 1)) elif (self.SeccondsinDay[i] * 100) < 80: dc.SetPen(wxPen(wxNamedColour('MEDIUMORCHID'), 1)) elif (self.SeccondsinDay[i] * 100) < 90: dc.SetPen(wxPen(wxNamedColour('DARKORCHID'), 1)) elif (self.SeccondsinDay[i] * 100) < 100: dc.SetPen(wxPen(wxNamedColour('BLUEVIOLET'), 1)) dc.DrawLine(int(i/86.4),50, int(i/86.4), 50 + (int(self.SeccondsinDay[i] * 100) )) dc.DrawLine(int(i/86.4),50, int(i/86.4), 50 - (int(self.NuralGuess[i]*10) )) dc.SetPen(wxPen(wxNamedColour('RED'), 1)) for i in range(time.localtime()[3] * 3600 + time.localtime()[4] * 60 + time.localtime()[5] - 60, time.localtime()[3] * 3600 + time.localtime()[4] * 60 + time.localtime()[5]): dc.DrawLine(int(i/86.4),50, int(i/86.4), 100) dc.EndDrawing() # -- end -- << class GraphWindow methods >> # << concious3 methods >> (7 of 8) class MyFrame(wxFrame): # << class MyFrame methods >> (1 of 10) def __init__(self, parent, id, title): wxFrame.__init__(self, parent, id, title, wxDefaultPosition, wxDefaultSize) EVT_CLOSE(self, self.OnCloseWindow) maintimerID = wxNewId() self.maintimer = wxTimer(self, maintimerID) EVT_TIMER(self, maintimerID, self.OnTimer) self.dayofweek = time.localtime()[6] PlotWindow_monID = wxNewId() PlotWindow_tuesID = wxNewId() PlotWindow_wedID = wxNewId() PlotWindow_thursID = wxNewId() PlotWindow_friID = wxNewId() PlotWindow_satID = wxNewId() PlotWindowID_sun = wxNewId() PlotWindowID_all = wxNewId() self.PlotWindow_mon = GraphWindow(self, PlotWindow_monID) self.PlotWindow_tues = GraphWindow(self, PlotWindow_tuesID) self.PlotWindow_wed = GraphWindow(self, PlotWindow_wedID) self.PlotWindow_thurs = GraphWindow(self, PlotWindow_thursID) self.PlotWindow_fri = GraphWindow(self, PlotWindow_friID) self.PlotWindow_sat = GraphWindow(self, PlotWindow_satID) self.PlotWindow_sun = GraphWindow(self, PlotWindowID_sun) self.PlotWindow_all = wxScrolledWindow(self, PlotWindowID_all, wxPoint(0, 0), wxSize(1000,1000), wxSUNKEN_BORDER) self.PlotWindow_all.SetScrollbars(20, 20, 50, 50) self.mainmenu = wxMenuBar() menu=wxMenu() exitID=wxNewId() menu.Append(exitID, '&Load', 'Load Data') EVT_MENU(self, exitID, self.OnLoadData) exitID=wxNewId() menu.Append(exitID, '&Save', 'Save Data') EVT_MENU(self, exitID, self.OnSaveData) exitID=wxNewId() menu.Append(exitID, 'Save &As', 'Save Data As') EVT_MENU(self, exitID, self.OnSaveAsData) exitID=wxNewId() menu.Append(exitID, '&Clear', 'Clear Data') EVT_MENU(self, exitID, self.OnClearData) self.mainmenu.Append (menu, '&Data') self.SetMenuBar (self.mainmenu) self.PlotWindow_mon.SetBackgroundColour(wxNamedColor("WHITE")) self.PlotWindow_tues.SetBackgroundColour(wxNamedColor("WHITE")) self.PlotWindow_wed.SetBackgroundColour(wxNamedColor("WHITE")) self.PlotWindow_thurs.SetBackgroundColour(wxNamedColor("WHITE")) self.PlotWindow_fri.SetBackgroundColour(wxNamedColor("WHITE")) self.PlotWindow_sat.SetBackgroundColour(wxNamedColor("WHITE")) self.PlotWindow_sun.SetBackgroundColour(wxNamedColor("WHITE")) self.PlotWindow_all.SetBackgroundColour(wxNamedColor("WHITE")) StartButtonID = wxNewId() self.StartButton = wxButton(self, StartButtonID, "Go", wxPoint(20, 20)) EVT_BUTTON(self, StartButtonID, self.OnGoClick) self.StartButton.SetBackgroundColour(wxBLUE) self.StartButton.SetForegroundColour(wxWHITE) self.StartButton.SetDefault() StopButtonID = wxNewId() self.StopButton = wxButton(self, StopButtonID, "Stop", wxPoint(260, 20)) EVT_BUTTON(self, StopButtonID, self.OnStopClick) self.StopButton.SetBackgroundColour(wxRED) self.StopButton.SetForegroundColour(wxWHITE) self.main_sizer = wxBoxSizer(wxVERTICAL) self.button_sizer = wxBoxSizer(wxHORIZONTAL) self.row1_sizer = wxBoxSizer(wxHORIZONTAL) self.row2_sizer = wxBoxSizer(wxHORIZONTAL) self.row1_sizer.Add(self.PlotWindow_mon, 1, wxEXPAND) self.row1_sizer.Add(self.PlotWindow_tues, 1, wxEXPAND) self.row1_sizer.Add(self.PlotWindow_wed, 1, wxEXPAND) self.row1_sizer.Add(self.PlotWindow_thurs, 1, wxEXPAND) self.row2_sizer.Add(self.PlotWindow_fri, 1, wxEXPAND) self.row2_sizer.Add(self.PlotWindow_sat, 1, wxEXPAND) self.row2_sizer.Add(self.PlotWindow_sun, 1, wxEXPAND) self.row2_sizer.Add(self.PlotWindow_all, 1, wxEXPAND) self.button_sizer.Add(self.StartButton, 1, wxEXPAND) self.button_sizer.Add(self.StopButton, 1, wxEXPAND) self.main_sizer.Add(self.button_sizer, 1, wxEXPAND) self.main_sizer.Add(self.row1_sizer, 5, wxEXPAND) self.main_sizer.Add(self.row2_sizer, 5, wxEXPAND) self.SetSizer(self.main_sizer) self.SetAutoLayout(1) self.main_sizer.Fit(self) # << class MyFrame methods >> (2 of 10) def OnTimer(self, event): if time.localtime()[6] != self.dayofweek: self.dayofweek = time.localtime()[6] self.OnStopClick(event) self.OnGoClick(event) self.OnPaintlines() # << class MyFrame methods >> (3 of 10) def OnSaveAsData(self, event): self.tom.close() dlg = wxFileDialog(self, "Data file name", ".", "", "*.txt", wxSAVE) if dlg.ShowModal() == wxID_OK: path = dlg.GetPath() self.tom = open(path,'w+') try: PlotWindow_data = [ self.PlotWindow_mon.SeccondsinDay, self.PlotWindow_tues.SeccondsinDay, self.PlotWindow_wed.SeccondsinDay, self.PlotWindow_thurs.SeccondsinDay, self.PlotWindow_fri.SeccondsinDay, self.PlotWindow_sat.SeccondsinDay, self.PlotWindow_sun.SeccondsinDay] pickle.dump(PlotWindow_data, self.tom) except: pass dlg.Destroy() # << class MyFrame methods >> (4 of 10) def OnClearData(self, event): Plotdata = [] for i in range(86399): Plotdata[i] = 0.0 self.PlotWindow_mon.SeccondsinDay = Plotdata self.PlotWindow_tues.SeccondsinDay = Plotdata self.PlotWindow_wed.SeccondsinDay = Plotdata self.PlotWindow_thurs.SeccondsinDay = Plotdata self.PlotWindow_fri.SeccondsinDay = Plotdata self.PlotWindow_sat.SeccondsinDay = Plotdata self.PlotWindow_sun.SeccondsinDay = Plotdata self.PlotWindow_mon.OnPaint() self.PlotWindow_tues.OnPaint() self.PlotWindow_wed.OnPaint() self.PlotWindow_thurs.OnPaint() self.PlotWindow_fri.OnPaint() self.PlotWindow_sat.OnPaint() self.PlotWindow_sun.OnPaint() # << class MyFrame methods >> (5 of 10) def OnLoadData(self, event): PlotWindow_data = [] dlg = wxFileDialog(self, "Data file name", ".", "", "*.txt", wxOPEN) if dlg.ShowModal() == wxID_OK: self.tom = open(dlg.GetPath(),'r+') try: PlotWindow_data = pickle.load(self.tom) self.PlotWindow_mon.SeccondsinDay = PlotWindow_data[0] self.PlotWindow_tues.SeccondsinDay = PlotWindow_data[1] self.PlotWindow_wed.SeccondsinDay = PlotWindow_data[2] self.PlotWindow_thurs.SeccondsinDay = PlotWindow_data[3] self.PlotWindow_fri.SeccondsinDay = PlotWindow_data[4] self.PlotWindow_sat.SeccondsinDay = PlotWindow_data[5] self.PlotWindow_sun.SeccondsinDay = PlotWindow_data[6] self.PlotWindow_mon.NuralGuess= PlotWindow_data[7] self.PlotWindow_tues.NuralGuess = PlotWindow_data[8] self.PlotWindow_wed.NuralGuess = PlotWindow_data[9] self.PlotWindow_thurs.NuralGuess = PlotWindow_data[10] self.PlotWindow_fri.NuralGuess = PlotWindow_data[11] self.PlotWindow_sat.NuralGuess = PlotWindow_data[12] self.PlotWindow_sun.NuralGuess = PlotWindow_data[13] self.PlotWindow_mon.OnPaint() self.PlotWindow_tues.OnPaint() self.PlotWindow_wed.OnPaint() self.PlotWindow_thurs.OnPaint() self.PlotWindow_fri.OnPaint() self.PlotWindow_sat.OnPaint() self.PlotWindow_sun.OnPaint() except: pass dlg.Destroy() self.tom.close() # << class MyFrame methods >> (6 of 10) def OnSaveData(self, event): PlotWindow_data = [] dlg = wxFileDialog(self, "Data file name", ".", "", "*.txt", wxSAVE) if dlg.ShowModal() == wxID_OK: self.tom = open(dlg.GetPath(),'r+') try: PlotWindow_data = [ self.PlotWindow_mon.SeccondsinDay, self.PlotWindow_tues.SeccondsinDay, self.PlotWindow_wed.SeccondsinDay, self.PlotWindow_thurs.SeccondsinDay, self.PlotWindow_fri.SeccondsinDay, self.PlotWindow_sat.SeccondsinDay, self.PlotWindow_sun.SeccondsinDay, self.PlotWindow_mon.NuralGuess, self.PlotWindow_tues.NuralGuess, self.PlotWindow_wed.NuralGuess, self.PlotWindow_thurs.NuralGuess, self.PlotWindow_fri.NuralGuess, self.PlotWindow_sat.NuralGuess, self.PlotWindow_sun.NuralGuess] pickle.dump(PlotWindow_data, self.tom) except: pass self.tom.close() dlg.Destroy() self.tom.close() # << class MyFrame methods >> (7 of 10) def OnCloseWindow(self, event): self.Destroy() # << class MyFrame methods >> (8 of 10) def OnGoClick(self, event): if time.localtime()[6] == 0: self.PlotWindow_mon.TimeStart() elif time.localtime()[6] == 1: self.PlotWindow_tues.TimeStart() elif time.localtime()[6] == 2: self.PlotWindow_wed.TimeStart() elif time.localtime()[6] == 3: self.PlotWindow_thurs.TimeStart() elif time.localtime()[6] == 4: self.PlotWindow_fri.TimeStart() elif time.localtime()[6] == 5: self.PlotWindow_sat.TimeStart() elif time.localtime()[6] == 6: self.PlotWindow_sun.TimeStart() self.maintimer.Start(60000) self.dayofweek = time.localtime()[6] # << class MyFrame methods >> (9 of 10) def OnStopClick(self, event): self.maintimer.Stop() self.PlotWindow_mon.TimeStop() self.PlotWindow_tues.TimeStop() self.PlotWindow_wed.TimeStop() self.PlotWindow_thurs.TimeStop() self.PlotWindow_fri.TimeStop() self.PlotWindow_sat.TimeStop() self.PlotWindow_sun.TimeStop() self.OnPaintlines() # << class MyFrame methods >> (10 of 10) def OnPaintlines(self): SeccondsDay = [] dc = wxClientDC(self.PlotWindow_all) self.PlotWindow_all.PrepareDC(dc) dc.BeginDrawing() dc.SetPen(wxPen(wxNamedColour('BLUE'), 4)) dc.DrawLine(0,50, 1000, 50) dc.SetFont(wxFont(14, wxSWISS, wxNORMAL, wxNORMAL)) dc.SetTextForeground(wxColour(0xFF, 0x20, 0xFF)) for i in range(23): dc.DrawText(str(i+1),(i +1) * 41, 20) if time.localtime()[6] == 0: SeccondsDay = self.PlotWindow_mon.NuralGuess[:] elif time.localtime()[6] == 1: SeccondsDay = self.PlotWindow_tues.NuralGuess[:] elif time.localtime()[6] == 2: SeccondsDay = self.PlotWindow_wed.NuralGuess[:] elif time.localtime()[6] == 3: SeccondsDay = self.PlotWindow_thurs.NuralGuess[:] elif time.localtime()[6] == 4: SeccondsDay = self.PlotWindow_fri.NuralGuess[:] elif time.localtime()[6] == 5: SeccondsDay = self.PlotWindow_sat.NuralGuess[:] elif time.localtime()[6] == 6: SeccondsDay = self.PlotWindow_sun.NuralGuess[:] for i in range(86399): if SeccondsDay[i] != 0.0: if (SeccondsDay[i] * 100) < 10: dc.SetPen(wxPen(wxNamedColour('RED'), 1)) elif (SeccondsDay[i] * 100) < 20: dc.SetPen(wxPen(wxNamedColour('DARKORANGE1'), 1)) elif (SeccondsDay[i] * 100) < 30: dc.SetPen(wxPen(wxNamedColour('GOLD1'), 1)) elif (SeccondsDay[i] * 100) < 40: dc.SetPen(wxPen(wxNamedColour('GREEN1'), 1)) elif (SeccondsDay[i] * 100) < 50: dc.SetPen(wxPen(wxNamedColour('BLUE1'), 1)) elif (SeccondsDay[i] * 100) < 60: dc.SetPen(wxPen(wxNamedColour('PLUM'), 1)) elif (SeccondsDay[i] * 100) < 70: dc.SetPen(wxPen(wxNamedColour('ORCHID'), 1)) elif (SeccondsDay[i] * 100) < 80: dc.SetPen(wxPen(wxNamedColour('MEDIUMORCHID'), 1)) elif (SeccondsDay[i] * 100) < 90: dc.SetPen(wxPen(wxNamedColour('DARKORCHID'), 1)) elif (SeccondsDay[i] * 100) < 100: dc.SetPen(wxPen(wxNamedColour('BLUEVIOLET'), 1)) dc.DrawLine(int(i/86.4),50, int(i/86.4), 50 + int(SeccondsDay[i] * 100)) dc.SetPen(wxPen(wxNamedColour('RED'), 1)) for i in range(time.localtime()[3] * 3600 + time.localtime()[4] * 60 + time.localtime()[5] - 60, time.localtime()[3] * 3600 + time.localtime()[4] * 60 + time.localtime()[5]): dc.DrawLine(int(i/86.4),50, int(i/86.4), 100 ) dc.EndDrawing() # -- end -- << class MyFrame methods >> # << concious3 methods >> (8 of 8) class MyApp(wxApp): # << class MyApp methods >> def OnInit(self): frame = MyFrame(None, -1, "Global concious") frame.Show(true) self.SetTopWindow(frame) return true # -- end -- << class MyApp methods >> # -- end -- << concious3 methods >> g = random.Random() h = random.Random() app = MyApp(0) app.MainLoop()