[Pybrainsim-activity] SF.net SVN: pybrainsim:[131] trunk/src/PyBrainSim.py
Status: Planning
Brought to you by:
rgoj
From: <rg...@us...> - 2009-09-13 23:39:38
|
Revision: 131 http://pybrainsim.svn.sourceforge.net/pybrainsim/?rev=131&view=rev Author: rgoj Date: 2009-09-13 23:39:27 +0000 (Sun, 13 Sep 2009) Log Message: ----------- * Removing the old text based PyBrainSim.py script to make room for PyBrainSimGUI. Removed Paths: ------------- trunk/src/PyBrainSim.py Deleted: trunk/src/PyBrainSim.py =================================================================== --- trunk/src/PyBrainSim.py 2009-09-13 23:37:32 UTC (rev 130) +++ trunk/src/PyBrainSim.py 2009-09-13 23:39:27 UTC (rev 131) @@ -1,265 +0,0 @@ -# PyBrainSim -# Copyright 2009 Roman Goj -# -# This file is part of PyBrainSim. -# -# PyBrainSim is free software: you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation, either version 3 of the License, or (at your option) any later -# version. -# -# PyBrainSim is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along with -# PyBrainSim. If not, see <http://www.gnu.org/licenses/>. - -""" -PyBrainSim is an interactive tool for the simulation and visualization of the -electromagnetic activity of the brain, currently under development. - -This script can be executed to run an example simulation, using the Head and -GeneratorDummy classes. Currently it only generates a list of integer -numbers... but work is under way to make it simulate the brain... Have fun! - -pybrainsim.sourceforge.net -""" - -import numpy -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 - -welcomeMessage = "\n\ -Welcome to this early version of PyBrainSim\n\n\ -Choose the type of simulation you would like to perform:\n\ - 1. Example dummy simulation\n\ - 2. Incrementing numbers, one generator and a stimulus\n\ - 3. Incrementing numbers, two generators and a stimulus for one of them\n\ - 4. A single sinusoidal generator\n\ - 5. A hundred sinusoidal generators with random frequencies, some connected\ - to a stimulus\n\ - 6. A single sinusoidal generator but with two registration sites\n\ - 7. A presentation of the 3d head model\n\ - 8. A model of a cortical area, based on Jansen and Rit 1995." - -print(welcomeMessage) -userChoice = input("Your choice: ") -print("\n") - -if userChoice == 1: - 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()) -elif userChoice == 2: - 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() -elif userChoice == 3: - 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() -elif userChoice == 4: - 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() -elif userChoice == 5: - 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() -elif userChoice == 6: - 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() -elif userChoice == 7: - exampleHead = Head() - exampleHead.displayHead() -elif userChoice == 8: - # 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 - from pylab import * - for i in range(len(parameter)): - subplot( len(parameter), 1, i+1) - if i==0: - title("Simulations based on Fig. 3 from Jansen and Rit 1995") - ylabel( "C = " + str(parameter[i]) ) - plot(recording[i][firstPointToDisplay:]) - show() -else: - print("No such option unfortunately...") This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |