File: examples-misc_using_classes.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 (73 lines) | stat: -rw-r--r-- 2,288 bytes parent folder | download
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
.. currentmodule:: brian

.. index::
   pair: example usage; subplot
   pair: example usage; NeuronGroup
   pair: example usage; run
   pair: example usage; trace
   pair: example usage; plot
   pair: example usage; raster_plot
   pair: example usage; title
   pair: example usage; SpikeMonitor
   pair: example usage; show
   pair: example usage; StateMonitor

.. _example-misc_using_classes:

Example: using_classes (misc)
=============================

Example of using derived classes in Brian

Using a class derived from one of Brian's classes can be a useful way of
organising code in complicated simulations. A class such as a :class:`NeuronGroup`
can itself create further :class:`NeuronGroup`, :class:`Connection` and
:class:`NetworkOperation` objects. In order to have these objects included in
the simulation, the derived class has to include them in its ``contained_objects``
list (this tells Brian to add these to the :class:`Network` when the derived
class object is added to the network).

::

    from brian import *
    
    
    class PoissonDrivenGroup(NeuronGroup):
        '''
        This class is a group of leaky integrate-and-fire neurons driven by
        external Poisson inputs. The class creates the Poisson inputs and
        connects them to itself. 
        '''
        def __init__(self, N, rate, weight):
            tau = 10 * ms
            eqs = '''
            dV/dt = -V/tau : 1
            '''
            # It's essential to call the initialiser of the base class
            super(PoissonDrivenGroup, self).__init__(N, eqs, reset=0, threshold=1)
            self.poisson_group = PoissonGroup(N, rate)
            self.conn = Connection(self.poisson_group, self, 'V')
            self.conn.connect_one_to_one(weight=weight)
            self.contained_objects += [self.poisson_group,
                                       self.conn]
    
    G = PoissonDrivenGroup(100, 100 * Hz, .3)
    
    M = SpikeMonitor(G)
    M_pg = SpikeMonitor(G.poisson_group)
    trace = StateMonitor(G, 'V', record=0)
    
    run(1 * second)
    
    subplot(311)
    raster_plot(M_pg)
    title('Input spikes')
    subplot(312)
    raster_plot(M)
    title('Output spikes')
    subplot(313)
    plot(trace.times, trace[0])
    title('Sample trace')
    show()