File: examples-twister_anonymous.txt

package info (click to toggle)
brian 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, stretch
  • size: 23,436 kB
  • sloc: python: 68,707; cpp: 29,040; ansic: 5,182; sh: 111; makefile: 61
file content (142 lines) | stat: -rw-r--r-- 4,386 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
.. currentmodule:: brian

.. index::
   pair: example usage; SpikeMonitor

.. _example-twister_anonymous:

Example: anonymous (twister)
============================

Anonymous entry for the 2012 Brian twister.

::

    '''
    My contribution to the brian twister!
    
    I meant to give it more thought, but I forgot about the deadline!
    '''
    from brian import *
    from brian.hears import *
    import pygame
    
    _mixer_status = [-1,-1]
    class SoundMonitor(SpikeMonitor):
        """
        Listen to you networks!
        
        Plays pure tones whenever a neuron spikes, frequency is set according to the neuron number.
        """
        def __init__(self, source, record=False, delay=0, 
                     frange = (100.*Hz, 5000.*Hz),
                     duration = 50*ms,
                     samplerate = 44100*Hz):
            super(SoundMonitor, self).__init__(source, record = record, delay = delay)
    
            self.samplerate = samplerate
            self.nsamples = np.rint(duration * samplerate)
    
            p = linspace(0, 1, len(source)).reshape((1, len(source)))
            p = np.tile(p, (self.nsamples, 1))
            freqs = frange[0] * p + (1-p) * frange[1]
            del p
    
            times = linspace(0*ms, duration, self.nsamples).reshape((self.nsamples, 1))
            times = np.tile(times, (1, len(source)))
            
            self.sounds = np.sin(2 * np.pi * freqs * times) 
            self._init_mixer()
    
    
        def propagate(self, spikes):
            if len(spikes):
                data = np.sum(self.sounds[:,spikes], axis = 1)
                x = array((2 ** 15 - 1) * clip(data/amax(data), -1, 1), dtype=int16)
                x.shape = x.size
                # Make sure pygame receives an array in C-order
                x = pygame.sndarray.make_sound(np.ascontiguousarray(x))
                x.play()
    
        def _init_mixer(self):
            global _mixer_status
            if _mixer_status==[-1,-1] or _mixer_status[0]!=1 or _mixer_status != self.samplerate:
                pygame.mixer.quit()
                pygame.mixer.init(int(self.samplerate), -16, 1)
                _mixer_status=[1,self.samplerate]
    
    
    def test_cuba():
        # The CUBA example with sound!
        taum = 20 * ms
        taue = 5 * ms
        taui = 10 * ms
        Vt = -50 * mV
        Vr = -60 * mV
        El = -49 * mV
    
        eqs = Equations('''
        dv/dt  = (ge+gi-(v-El))/taum : volt
        dge/dt = -ge/taue : volt
        dgi/dt = -gi/taui : volt
        ''')
    
        P = NeuronGroup(4000, model=eqs, threshold=Vt, reset=Vr, refractory=5 * ms)
        P.v = Vr
        P.ge = 0 * mV
        P.gi = 0 * mV
    
        Pe = P.subgroup(3200)
        Pi = P.subgroup(800)
        we = (60 * 0.27 / 10) * mV # excitatory synaptic weight (voltage)
        wi = (-20 * 4.5 / 10) * mV # inhibitory synaptic weight
        Ce = Connection(Pe, P, 'ge', weight=we, sparseness=0.5)
        Ci = Connection(Pi, P, 'gi', weight=wi, sparseness=0.5)
        P.v = Vr + rand(len(P)) * (Vt - Vr)
    
        # Record the number of spikes
        M = SoundMonitor(P)
        run(10 * second)
    
    def test_synfire():
        from brian import *
        # Neuron model parameters
        Vr = -70 * mV
        Vt = -55 * mV
        taum = 10 * ms
        taupsp = 0.325 * ms
        weight = 4.86 * mV
        # Neuron model
        eqs = Equations('''
        dV/dt=(-(V-Vr)+x)*(1./taum) : volt
        dx/dt=(-x+y)*(1./taupsp) : volt
        dy/dt=-y*(1./taupsp)+25.27*mV/ms+\
            (39.24*mV/ms**0.5)*xi : volt
        ''')
        # Neuron groups
        P = NeuronGroup(N=1000, model=eqs,
            threshold=Vt, reset=Vr, refractory=1 * ms)
        Pinput = PulsePacket(t=50 * ms, n=85, sigma=1 * ms)
        # The network structure
        Pgp = [ P.subgroup(100) for i in range(10)]
        C = Connection(P, P, 'y')
        for i in range(9):
            C.connect_full(Pgp[i], Pgp[i + 1], weight)
        Cinput = Connection(Pinput, Pgp[0], 'y')
        Cinput.connect_full(weight=weight)
    
        monitor = SoundMonitor(P)
    
        # Setup the network, and run it
        P.V = Vr + rand(len(P)) * (Vt - Vr)
        run(1*second)
        # Plot result
    
        show()
    
    
    if __name__ == '__main__':
        test_synfire()