[Pybrainsim-activity] SF.net SVN: pybrainsim:[130] trunk/src/PyBrainSimGUI.pyw
Status: Planning
Brought to you by:
rgoj
From: <rg...@us...> - 2009-09-13 23:37:51
|
Revision: 130 http://pybrainsim.svn.sourceforge.net/pybrainsim/?rev=130&view=rev Author: rgoj Date: 2009-09-13 23:37:32 +0000 (Sun, 13 Sep 2009) Log Message: ----------- * Added all the content from the PyBrainSim script, preparing PyBrainSimGUI one to be the main executable. * Removed all the left over code from wxPython tutorials. Modified Paths: -------------- trunk/src/PyBrainSimGUI.pyw Modified: trunk/src/PyBrainSimGUI.pyw =================================================================== --- trunk/src/PyBrainSimGUI.pyw 2009-09-13 23:11:44 UTC (rev 129) +++ trunk/src/PyBrainSimGUI.pyw 2009-09-13 23:37:32 UTC (rev 130) @@ -30,92 +30,252 @@ import wx from wx import xrc +import numpy +import pylab +from random import random +from scipy.integrate import ode + +from Head import Head +from HeadModel import HeadModel +from HeadModelHalfSphere import HeadModelHalfSphere +from Experiment import Experiment +from Stimulus import Stimulus +from StimulusDummy import StimulusDummy +from GeneratorDummy import GeneratorDummy +from GeneratorNumberIncrementing import GeneratorNumberIncrementing +from GeneratorSine import GeneratorSine +from Connection import Connection +from ConnectionDummy import ConnectionDummy + class PyBrainSimGUI(wx.App): def OnInit(self): self.res = xrc.XmlResource("PyBrainSimGUI.xrc") - #self.InitFrame() - #self.InitMenu() - #self.InitEverythingElse() self.frame = self.res.LoadFrame(None, "menuFrame") self.frame.Bind(wx.EVT_BUTTON, self.menuButton1, id=xrc.XRCID("menuButton1")) + self.frame.Bind(wx.EVT_BUTTON, self.menuButton2, id=xrc.XRCID("menuButton2")) + self.frame.Bind(wx.EVT_BUTTON, self.menuButton3, id=xrc.XRCID("menuButton3")) + self.frame.Bind(wx.EVT_BUTTON, self.menuButton4, id=xrc.XRCID("menuButton4")) + self.frame.Bind(wx.EVT_BUTTON, self.menuButton5, id=xrc.XRCID("menuButton5")) + self.frame.Bind(wx.EVT_BUTTON, self.menuButton6, id=xrc.XRCID("menuButton6")) + self.frame.Bind(wx.EVT_BUTTON, self.menuButton7, id=xrc.XRCID("menuButton7")) + self.frame.Bind(wx.EVT_BUTTON, self.menuButton8, id=xrc.XRCID("menuButton8")) self.frame.Bind(wx.EVT_BUTTON, self.menuButtonQuit, id=xrc.XRCID("menuButtonQuit")) - #self.panel = xrc.XRCCTRL(self.frame, "MainPanel") - #self.first_arg = xrc.XRCCTRL(self.panel, "FirstArg") - #self.second_arg = xrc.XRCCTRL(self.panel, "SecondArg") - #self.result = xrc.XRCCTRL(self.panel, "Result") - #self.first_arg.SetValue("Hi") - #self.second_arg.SetValue("You") - #self.result.SetValue("man") - #self.menuBar = self.res.LoadMenuBar("MenuBar") - #self.frame.SetMenuBar(self.menuBar) - #sizer = self.panel.GetSizer() - #sizer.Fit(self.frame) - #sizer.SetSizeHints(self.frame) self.frame.Show() return True def menuButton1(self, control): - print "Hullo World!" + exampleHead = Head() + exampleHeadModel = HeadModel(exampleHead) + exampleHead.setSamplingFrequency(10) + exampleHead.addRegistrationSite([0, 0, 0]) + + exampleStimulus = StimulusDummy('Stim', exampleHead) + exampleGenerator = GeneratorDummy('Gen', exampleHead) + exampleConnection = ConnectionDummy('Con', exampleHead, exampleStimulus, exampleGenerator) + + exampleExperiment = Experiment(exampleHead.getSamplingFrequency(), 1.0, exampleHead.runSimulation( 1.0 )) + print("\nSimulations resulted in the following recording:") + print(exampleExperiment.getRecording()) + def menuButton2(self, control): + exampleHead = Head() + exampleHeadModel = HeadModel(exampleHead) + exampleHead.setSamplingFrequency(10) + exampleHead.addRegistrationSite([0, 0, 0]) + + exampleStimulus = Stimulus('Stim', exampleHead) + exampleStimulus.setStimulusTimes([0.3, 0.6]) + exampleGenerator = GeneratorNumberIncrementing('Gen', exampleHead) + exampleConnection = Connection('Con', exampleHead, exampleStimulus, exampleGenerator) + + exampleExperiment = Experiment(exampleHead.getSamplingFrequency(), 1.0, exampleHead.runSimulation( 1.0 )) + print("\nSimulations resulted in the following recording:") + print exampleExperiment.getRecording() + + def menuButton3(self, control): + exampleHead = Head() + exampleHeadModel = HeadModel(exampleHead) + exampleHead.setSamplingFrequency(10) + exampleHead.addRegistrationSite([0, 0, 0]) + + exampleExperiment = Experiment(exampleHead.getSamplingFrequency(), 1.0) + exampleExperiment.setStimulusTimes([[0.3, 0.6], [0.5]]) + + exampleStimulus1 = Stimulus('Stim1', exampleHead) + exampleStimulus2 = Stimulus('Stim2', exampleHead) + exampleStimulus1.setStimulusTimes(exampleExperiment.getStimulusTimes()[0]) + exampleStimulus2.setStimulusTimes(exampleExperiment.getStimulusTimes()[1]) + + exampleGenerator1 = GeneratorNumberIncrementing('Gen1', exampleHead) + exampleGenerator2 = GeneratorNumberIncrementing('Gen2', exampleHead) + exampleConnection1 = Connection('Con1', exampleHead, exampleStimulus1, exampleGenerator1) + exampleConnection2 = Connection('Con2', exampleHead, exampleStimulus2, exampleGenerator2) + + exampleExperiment.setRecording(exampleHead.runSimulation(exampleExperiment.getDuration())) + print("\nSimulations resulted in the following recording:") + print exampleExperiment.getRecording() + + def menuButton4(self, control): + exampleHead = Head() + exampleHeadModel = HeadModel(exampleHead) + exampleHead.setSamplingFrequency(128) + exampleHead.addRegistrationSite([0, 0, 0]) + + exampleExperiment = Experiment(exampleHead.getSamplingFrequency(), 10.0) + exampleExperiment.setStimulusTimes([[0.3, 1.75, 2.16, 3.87, 4.31, 5.183, 6.34, 7.13]]) + + exampleStimulus = Stimulus('Stim', exampleHead) + print exampleExperiment.getStimulusTimes()[0] + exampleStimulus.setStimulusTimes(exampleExperiment.getStimulusTimes()[0]) + exampleGenerator = GeneratorSine('Gen', exampleHead) + exampleConnection = Connection('Con', exampleHead, exampleStimulus, exampleGenerator) + + exampleExperiment.setRecording(exampleHead.runSimulation(exampleExperiment.getDuration())) + print("\nSimulations resulted in the following recording:") + print exampleExperiment.getRecording() + exampleExperiment.plotRecording() + + def menuButton5(self, control): + exampleHead = Head() + exampleHeadModel = HeadModel(exampleHead) + exampleHead.setSamplingFrequency(256) + exampleHead.addRegistrationSite([0, 0, 0]) + exampleExperiment = Experiment(exampleHead.getSamplingFrequency(), 100.0) + + # Randomizing stimuli times + stimuli = [] + for i in range(100): + stimuli.append( i + 0.2 +random()/2 ) + exampleExperiment.setStimulusTimes([stimuli]) + exampleStimulus = Stimulus('Stim', exampleHead) + exampleStimulus.setStimulusTimes(exampleExperiment.getStimulusTimes()[0]) + + # Creating many generators with random frequencies in the range 2-20 Hz and + # random phases. Connecting some of them to the stimulus generator + exampleGenerators = [] + exampleConnections = [] + for i in range(100): + randomFrequency = 2.0 + random() * 18 + randomPhaseShift = random() + exampleGenerators.append(GeneratorSine('Gen', exampleHead, frequency=randomFrequency, phaseShift=randomPhaseShift)) + if(random() > 0.75): + exampleConnections.append(Connection('Con', exampleHead, exampleStimulus, exampleGenerators[i])) + + exampleExperiment.setRecording(exampleHead.runSimulation(exampleExperiment.getDuration())) + exampleExperiment.plotRecording() + + def menuButton6(self, control): + exampleHead = Head() + exampleHeadModel = HeadModelHalfSphere(exampleHead) + exampleHead.setSamplingFrequency(128) + exampleHead.addRegistrationSite([ 0.5, 0.0, 0.866]) + exampleHead.addRegistrationSite([ 0.0, 0.0, 0.866]) + exampleHead.addRegistrationSite([-0.5, 0.0, 0.866]) + + exampleExperiment = Experiment(exampleHead.getSamplingFrequency(), 10.0) + exampleExperiment.setStimulusTimes([[0.3, 1.75, 2.16, 3.87, 4.31, 5.183, 6.34, 7.13]]) + + exampleStimulus = Stimulus('Stim', exampleHead) + exampleStimulus.setStimulusTimes(exampleExperiment.getStimulusTimes()[0]) + exampleGenerator = GeneratorSine('Gen', exampleHead, position = [0.5, 0, 0.366], frequency = 7) + exampleGenerator = GeneratorSine('Gen', exampleHead, position = [-0.5, 0, 0.366], frequency = 4) + exampleConnection = Connection('Con', exampleHead, exampleStimulus, exampleGenerator) + + exampleExperiment.setRecording(exampleHead.runSimulation(exampleExperiment.getDuration())) + exampleExperiment.plotRecording() + + def menuButton7(self, control): + exampleHead = Head() + exampleHead.displayHead() + + def menuButton8(self, control): + # This class will produce noise that will be fed into the modelled + # cortical area as input. + class NeuralNoise: + def __init__(self, timeSpan, dtime): + self.noise = [] + self.dtime = dtime + self.timePoints = range( int(timeSpan//dtime+1) ) + # Random noise distributed evenly between 120 and 320 + for i in self.timePoints: + self.noise.append( 120+200*random() ) + def getNoise(self, time): + # Searching for the appropriate time point + for i in self.timePoints: + if time < self.timePoints[i]*self.dtime: + # Interpolating between the randomly generated values + return self.noise[i] + \ + (self.noise[i+1]-self.noise[i]) \ + * ((self.timePoints[i]*self.dtime-time)/self.dtime) + + # The sigmoid function + def sigmoidFunction(v): + """Constants taken from Jansen and Rit 1995, p. 360""" + cEzero = 2.5 + cR = 0.56 + cVzero = 6 + + """Sigmoid function taken from Jansen and Rit 1995, p. 360""" + return 2*cEzero / ( 1 + numpy.exp( cR*( cVzero - v ) ) ) + + # Defining the equations + def f(t, y, noiseObject, cC): + cA=3.25 + ca=100.0 + cB=22.0 + cb=50.0 + cC1=cC + cC2=0.8*cC1 + cC3=0.25*cC1 + cC4=0.25*cC1 + noise = noiseObject.getNoise(t) + #print("Time: " + str(t) + " Noise: " + str(noise)) + return [y[1], cA*ca*( sigmoidFunction(y[2] - y[4]) ) - 2*ca*y[1] - + ca**2*y[0], y[3], cA*ca*( noise + cC2*sigmoidFunction(cC1*y[0]) ) - + 2*ca*y[3] - ca**2*y[2], y[5], cB*cb*( cC4*sigmoidFunction(cC3*y[0]) + ) - 2*cb*y[5] - cb**2*y[4]] + + # How many seconds should be modelled and how accurately + timeSpan = 3 + dtime = 0.001 + firstPointToDisplay = 1000 + + # A noise object that will be used as input into the equations + someNoise = NeuralNoise(timeSpan+1, dtime*10) + + # Initial conditions + y0, t0 = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 0 + + # Preparing for simulations + r = [] + recording = [ [], [], [], [], [], [] ] + parameter = [ 68, 128, 135, 270, 675, 1350 ] + + # Differential equation integration + for i in range(len(parameter)): + # Preparing the integration + r.append(ode(f).set_integrator('vode', method='bdf')) + r[i].set_initial_value(y0, t0) + r[i].set_f_params( someNoise, parameter[i] ) + # Integrating + print("Performing simulation " + str(i+1) + "/" + str(len(parameter))) + while r[i].successful() and r[i].t < timeSpan: + r[i].integrate(r[i].t+dtime) + recording[i].append(r[i].y[2] - r[i].y[4]) + + # Show results of simulations + for i in range(len(parameter)): + pylab.subplot( len(parameter), 1, i+1) + if i==0: + pylab.title("Simulations based on Fig. 3 from Jansen and Rit 1995") + pylab.ylabel( "C = " + str(parameter[i]) ) + pylab.plot(recording[i][firstPointToDisplay:]) + pylab.show() + def menuButtonQuit(self, control): self.Exit() - #def InitFrame(self): - # self.frame = self.res.LoadFrame(None, "MainFrame") - # self.panel = xrc.XRCCTRL(self.frame, "MainPanel") - # self.first_arg = xrc.XRCCTRL(self.panel, "FirstArg") - # self.second_arg = xrc.XRCCTRL(self.panel, "SecondArg") - # self.result = xrc.XRCCTRL(self.panel, "Result") - # self.first_arg.SetValue("Hi") - # self.second_arg.SetValue("You") - # self.result.SetValue("man") - #def InitMenu(self): - # self.menuBar = self.res.LoadMenuBar("MenuBar") - # self.frame.Bind(wx.EVT_MENU, self.Add, id=xrc.XRCID("AddMenuItem")) - # self.frame.Bind(wx.EVT_MENU, self.Subtract, id=xrc.XRCID("SubtractMenuItem")) - # self.frame.Bind(wx.EVT_MENU, self.Multiply, id=xrc.XRCID("MultiplyMenuItem")) - # self.frame.Bind(wx.EVT_MENU, self.Divide, id=xrc.XRCID("DivideMenuItem")) - # self.frame.SetMenuBar(self.menuBar) - #def InitEverythingElse(self): - # sizer = self.panel.GetSizer() - # sizer.Fit(self.frame) - # sizer.SetSizeHints(self.frame) - # self.frame.Show() - #def InitArgs(self): - # try: - # self.first = float(self.first_arg.GetValue()) - # except ValueError: - # return self.BadFloatValue(self.first_arg) - # try: - # self.second = float(self.second_arg.GetValue()) - # except ValueError: - # return self.BadFloatValue(self.second_arg) - # return True - #def BadFloatValue(self, control): - # dlg = wx.MessageDialog(self.frame, "I can't convert this to float.", - # 'Conversion error', wx.OK | wx.ICON_ERROR) - # dlg.ShowModal() - # dlg.Destroy() - # control.SetFocus() - # control.SetSelection(-1, -1) - # return False - #def Add(self, evt): - # if self.InitArgs(): - # self.result.SetValue(str(self.first + self.second)) - #def Subtract(self, evt): - # if self.InitArgs(): - # self.result.SetValue(str(self.first - self.second)) - #def Multiply(self, evt): - # if self.InitArgs(): - # self.result.SetValue(str(self.first * self.second)) - #def Divide(self, evt): - # if self.InitArgs(): - # if self.second != 0: - # self.result.SetValue(str(self.first / self.second)) - # else: - # self.result.SetValue("#ERROR") - if __name__ == '__main__': app = PyBrainSimGUI(0) app.MainLoop() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |