Esempio n. 1
0
    def menuButton9(self, control):
        # head will hold sources, registrations sites and the head model
        head1 = Head()
        head2 = Head()
        head3 = Head()
        
        # headModel will be our single sphere head model
        headModel1 = HeadModelDipoleSphere(head1, 10.0)
        headModel2 = HeadModelDipoleSphere(head2, 10.0)
        headModel3 = HeadModelDipoleSphere(head3, 10.0)

        # We need only one data point per simulation
        head1.setSamplingFrequency(1)
        head2.setSamplingFrequency(1)
        head3.setSamplingFrequency(1)
        
        # Adding registration sites
        nElectrodes = 201
        angles = []
        for i in range(nElectrodes):
            angles.append(i*numpy.pi/(nElectrodes-1) - numpy.pi/2)
            head1.addRegistrationSite([angles[-1],0])
            head2.addRegistrationSite([angles[-1],0])
            head3.addRegistrationSite([angles[-1],0])
        
        # Adding a generator
        orientation1= 0;
        orientation2= numpy.pi/2;
        orientation3= numpy.pi/4;
        generator1 = GeneratorNoisy('Gen', head1, position = [ 4.0, 0, 0, orientation1, numpy.pi/2], mean=1, stddev=0.0)
        generator2 = GeneratorNoisy('Gen', head2, position = [ 4.0, numpy.pi/4, 0, orientation2, numpy.pi/2], mean=1, stddev=0.0)
        generator30 = GeneratorNoisy('Gen', head3, position = [ 4.0, 0, 0, orientation1, numpy.pi/2], mean=1, stddev=0.5)
        generator31 = GeneratorNoisy('Gen', head3, position = [ 4.0, numpy.pi/4, 0, orientation2, numpy.pi/2], mean=3, stddev=0.5)
        generator32 = GeneratorNoisy('Gen', head3, position = [ 4.0, -numpy.pi/4, 0, orientation3, numpy.pi/2], mean=2, stddev=0.5)


        # Run the simulation just once (or, equivalently for one second with the sampling rate of 1 Hz)
        simulatedData1 = numpy.array(head1.runSimulation(1))
        simulatedData2 = numpy.array(head2.runSimulation(1))
        simulatedData3 = numpy.array(head3.runSimulation(1))
        
        pylab.subplot(311)
        pylab.plot(angles,simulatedData1)
        pylab.title("Potential from superficial dipoles of different orientations")
        pylab.subplot(312)
        pylab.plot(angles,simulatedData2)
        pylab.subplot(313)
        pylab.plot(angles,simulatedData3)
        pylab.xlabel("Location of measurement on scalp [radians]")
        pylab.ylabel("Scalp potential [relative]")
        pylab.show()
Esempio n. 2
0
    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()
Esempio n. 3
0
def gen_simulation(gen_conf, num_sim=1, gen_magnitude_stddev=0):
    """Runs a single simulation of generators of ERP components.
    
    Currently only one epoch for one subject is calculated, maybe this should
    change. The generator configuration is specified as input.
    
    """
    # TODO: Remove all the scaling variables!
    # Initialisation of the PyBrainSim modelling framework with a homogeneous
    # spherical head model with a head radius set to 11.5 cm. Since we are
    # modelling only the spatical characteristics of the ERP component, we set
    # the temporal sampling frequency to 1, to obtain only one simulated value
    # per epoch.
    head = Head()
    head.setSamplingFrequency(1)
    headModel = HeadModelDipoleSphere(head, 11.5)
    
    # Initialisation of the simulated electrode array from predefined electrode
    # locations simulating the 10-20 electrode placement system.
    # TODO: This isn't elegant and should be changed!
    # TODO: This excludes simulations with a line of electrodes, which are
    # important for some applications as well, see doOneSimulation.py for
    # example implementation.
    [electrodes, topoX, topoY, thetaAngles, phiAngles] =\
        read_electrode_locations()
    for i in range(len(electrodes)):
        head.addRegistrationSite([thetaAngles[i], phiAngles[i]])
    
    # TODO: Here we are assuming that we know the generator locations!
    # (from the gen_conf variable)

    # Adding dipole generators to the head with the configuration parameters
    # given in gen_conf.
    # TODO: This is not working code
    # TODO: It would be really good if this code could fit in less lines!
    # TODO: Actually adding the generators to a list is not necessary any more,
    # maybe I could just run the constructor?
    generators = []
    for i in range(len(gen_conf)):
        # Combining the generator with the head model
        generators.append(
            GeneratorNoisy('Gen', head, 
                           position=[gen_conf[i]['depth'], 
                                     gen_conf[i]['theta'], 
                                     gen_conf[i]['phi'], 
                                     gen_conf[i]['orientation'], 
                                     gen_conf[i]['orientation_phi']], 
                           mean=gen_conf[i]['magnitude'], 
                           stddev=gen_magnitude_stddev))
    
    # Running the simulation.
    # TODO: runSimulation() should already return a numpy.array, now it returns
    # a list (checked that)!
    simulated_data = array(head.runSimulation(num_sim))

    # TODO: I should be returning something different here, perhaps?
    # TODO: I need to transpose simulated data to enable it to be used with
    # e.g. the topographic maps without transposing, like with the real data...
    # but whether this is a good idea in the long run - no idea...
    return [simulated_data.transpose(), gen_conf, electrodes]
Esempio n. 4
0
    def menuButton1(self, control):
        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 ))
        output = str(exampleExperiment.getRecording())
        self.log.SetValue(output)
        self.logWindow.Show()
Esempio n. 5
0
    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)
        exampleStimulus.setStimulusTimes(exampleExperiment.getStimulusTimes()[0])
        exampleGenerator = GeneratorSine('Gen', exampleHead)
        exampleConnection = Connection('Con', exampleHead, exampleStimulus, exampleGenerator)

        exampleExperiment.setRecording(exampleHead.runSimulation(exampleExperiment.getDuration()))
        exampleExperiment.plotRecording()
Esempio n. 6
0
    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()))
        output = str(exampleExperiment.getRecording())
        self.log.SetValue(output)
        self.logWindow.Show()