This file is indexed.

/usr/share/doc/python-brian-doc/docs/efficient.html is in python-brian-doc 1.3.1-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>How to write efficient Brian code &mdash; Brian v1.3.1 documentation</title>
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '1.3.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Brian v1.3.1 documentation" href="index.html" />
    <link rel="up" title="Advanced concepts" href="advanced.html" />
    <link rel="next" title="Compiled code" href="compiledcode.html" />
    <link rel="prev" title="Advanced concepts" href="advanced.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="compiledcode.html" title="Compiled code"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="advanced.html" title="Advanced concepts"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Brian v1.3.1 documentation</a> &raquo;</li>
          <li><a href="advanced.html" accesskey="U">Advanced concepts</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="how-to-write-efficient-brian-code">
<span id="index-0"></span><h1>How to write efficient Brian code<a class="headerlink" href="#how-to-write-efficient-brian-code" title="Permalink to this headline"></a></h1>
<p>There are a few keys to writing fast and efficient Brian code. The
first is to use Brian itself efficiently. The second is to write
good vectorised code, which is using Python and NumPy efficiently.
For more performance tips, see also <a class="reference internal" href="compiledcode.html#compiled-code"><em>Compiled code</em></a>.</p>
<div class="section" id="brian-specifics">
<h2>Brian specifics<a class="headerlink" href="#brian-specifics" title="Permalink to this headline"></a></h2>
<p>You can switch off Brian&#8217;s entire unit checking module
by including the line:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">brian_no_units</span>
</pre></div>
</div>
<p>before importing Brian itself. Good practice is to leave unit checking
on most of the time when developing and debugging a model, but
switching it off for long runs once the basic model is stable.</p>
<p>Another way to speed up code is to store references to arrays rather
than extracting them from Brian objects each time you need them. For
example, if you know the custom reset object in the code above is
only ever applied to a group <tt class="docutils literal"><span class="pre">custom_group</span></tt> say, then you could
do something like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">myreset</span><span class="p">(</span><span class="n">P</span><span class="p">,</span> <span class="n">spikes</span><span class="p">):</span>
        <span class="n">custom_group_V_</span><span class="p">[</span><span class="n">spikes</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="o">*</span><span class="n">mV</span>
        <span class="n">custom_group_Vt_</span><span class="p">[</span><span class="n">spikes</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="n">mV</span>

<span class="n">custom_group</span> <span class="o">=</span> <span class="o">...</span>
<span class="n">custom_group_V_</span> <span class="o">=</span> <span class="n">custom_group</span><span class="o">.</span><span class="n">V_</span>
<span class="n">custom_group_Vt_</span> <span class="o">=</span> <span class="n">custom_group</span><span class="o">.</span><span class="n">Vt_</span>
</pre></div>
</div>
<p>In this case, the speed increase will be quite small, and probably
not worth doing because it makes it less readable, but in more
complicated examples where you repeatedly refer to <tt class="docutils literal"><span class="pre">custom_group.V_</span></tt>
it could add up.</p>
</div>
<div class="section" id="vectorisation">
<span id="efficiency-vectorisation"></span><span id="index-1"></span><h2>Vectorisation<a class="headerlink" href="#vectorisation" title="Permalink to this headline"></a></h2>
<p>Python is a fast language, but each line of Python code has an
associated overhead attached to it. Sometimes you can get considerable
increases in speed by writing a vectorised version of it. A good guide
to this in general is the <a class="reference external" href="http://www.scipy.org/PerformancePython">Performance Python</a>
page. Here we will do a single worked example in Brian.</p>
<p>Suppose you wanted to multiplicatively depress the connection
strengths every time step by some amount, you might do something like
this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">C</span> <span class="o">=</span> <span class="n">Connection</span><span class="p">(</span><span class="n">G1</span><span class="p">,</span> <span class="n">G2</span><span class="p">,</span> <span class="s">&#39;V&#39;</span><span class="p">,</span> <span class="n">structure</span><span class="o">=</span><span class="s">&#39;dense&#39;</span><span class="p">)</span>
<span class="o">...</span>
<span class="nd">@network_operation</span><span class="p">(</span><span class="n">when</span><span class="o">=</span><span class="s">&#39;end&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">depress_C</span><span class="p">():</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">G1</span><span class="p">)):</span>
                <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">G2</span><span class="p">)):</span>
                        <span class="n">C</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">C</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span><span class="o">*</span><span class="n">depression_factor</span>
</pre></div>
</div>
<p>This will work, but it will be very, very slow.</p>
<p>The first thing to note is that the Python expression <tt class="docutils literal"><span class="pre">range(N)</span></tt>
actually constructs a list <tt class="docutils literal"><span class="pre">[0,1,2,...,N-1]</span></tt> each time it is called,
which is not really necessary if you are only iterating over the list.
Instead, use the <tt class="docutils literal"><span class="pre">xrange</span></tt> iterator which doesn&#8217;t construct the list
explicitly:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">G1</span><span class="p">)):</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">G2</span><span class="p">)):</span>
                <span class="n">C</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">C</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span><span class="o">*</span><span class="n">depression_factor</span>
</pre></div>
</div>
<p>The next thing to note is that when you call C[i,j] you are doing an
operation on the <a class="reference internal" href="reference-connections.html#brian.Connection" title="brian.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a> object, not directly on the underlying
matrix. Instead, do something like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">C</span> <span class="o">=</span> <span class="n">Connection</span><span class="p">(</span><span class="n">G1</span><span class="p">,</span> <span class="n">G2</span><span class="p">,</span> <span class="s">&#39;V&#39;</span><span class="p">,</span> <span class="n">structure</span><span class="o">=</span><span class="s">&#39;dense&#39;</span><span class="p">)</span>
<span class="n">C_matrix</span> <span class="o">=</span> <span class="n">asarray</span><span class="p">(</span><span class="n">C</span><span class="o">.</span><span class="n">W</span><span class="p">)</span>
<span class="o">...</span>
<span class="nd">@network_operation</span><span class="p">(</span><span class="n">when</span><span class="o">=</span><span class="s">&#39;end&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">depress_C</span><span class="p">():</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">G1</span><span class="p">)):</span>
                <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">G2</span><span class="p">)):</span>
                        <span class="n">C_matrix</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span> <span class="o">*=</span> <span class="n">depression_factor</span>
</pre></div>
</div>
<p>What&#8217;s going on here? First of all, <tt class="docutils literal"><span class="pre">C.W</span></tt> refers to the <a class="reference internal" href="reference-connections.html#brian.ConnectionMatrix" title="brian.ConnectionMatrix"><tt class="xref py py-class docutils literal"><span class="pre">ConnectionMatrix</span></tt></a>
object, which is a 2D NumPy array with some extra stuff - we don&#8217;t need the extra
stuff so we convert it to a straight NumPy array <tt class="docutils literal"><span class="pre">asarray(C.W)</span></tt>. We also store
a copy of this as the variable <tt class="docutils literal"><span class="pre">C_matrix</span></tt> so we don&#8217;t need to do this every
time step. The other thing we do is to use the <tt class="docutils literal"><span class="pre">*=</span></tt> operator instead of the <tt class="docutils literal"><span class="pre">*</span></tt>
operator.</p>
<p>The most important step of all though is to vectorise the entire operation. You
don&#8217;t need to loop over <tt class="docutils literal"><span class="pre">i</span></tt> and <tt class="docutils literal"><span class="pre">j</span></tt> at all, you can manipulate the array
object with a single NumPy expression:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">C</span> <span class="o">=</span> <span class="n">Connection</span><span class="p">(</span><span class="n">G1</span><span class="p">,</span> <span class="n">G2</span><span class="p">,</span> <span class="s">&#39;V&#39;</span><span class="p">,</span> <span class="n">structure</span><span class="o">=</span><span class="s">&#39;dense&#39;</span><span class="p">)</span>
<span class="n">C_matrix</span> <span class="o">=</span> <span class="n">asarray</span><span class="p">(</span><span class="n">C</span><span class="o">.</span><span class="n">W</span><span class="p">)</span>
<span class="o">...</span>
<span class="nd">@network_operation</span><span class="p">(</span><span class="n">when</span><span class="o">=</span><span class="s">&#39;end&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">depress_C</span><span class="p">():</span>
        <span class="n">C_matrix</span> <span class="o">*=</span> <span class="n">depression_factor</span>
</pre></div>
</div>
<p>This final version will probably be hundreds if not thousands of times faster
than the original. It&#8217;s usually possible to work out a way using NumPy
expressions only to do whatever you want in a vectorised way, but in some
very rare instances it might be necessary to have a loop. In this case, if
this loop is slowing your code down, you might want to try writing that
loop in inline C++ using the <a class="reference external" href="http://www.scipy.org/Weave">SciPy Weave</a>
package. See the documentation at that link for more details, but as an
example we could rewrite the code above using inline C++ as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">scipy</span> <span class="kn">import</span> <span class="n">weave</span>
<span class="o">...</span>
<span class="n">C</span> <span class="o">=</span> <span class="n">Connection</span><span class="p">(</span><span class="n">G1</span><span class="p">,</span> <span class="n">G2</span><span class="p">,</span> <span class="s">&#39;V&#39;</span><span class="p">,</span> <span class="n">structure</span><span class="o">=</span><span class="s">&#39;dense&#39;</span><span class="p">)</span>
<span class="n">C_matrix</span> <span class="o">=</span> <span class="n">asarray</span><span class="p">(</span><span class="n">C</span><span class="o">.</span><span class="n">W</span><span class="p">)</span>
<span class="o">...</span>
<span class="nd">@network_operation</span><span class="p">(</span><span class="n">when</span><span class="o">=</span><span class="s">&#39;end&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">depress_C</span><span class="p">():</span>
        <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">G1</span><span class="p">)</span>
        <span class="n">m</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">G2</span><span class="p">)</span>
        <span class="n">code</span> <span class="o">=</span> <span class="s">&#39;&#39;&#39;</span>
<span class="s">                for(int i=0;i&lt;n;i++)</span>
<span class="s">                        for(int j=0;j&lt;m;j++)</span>
<span class="s">                                C_matrix(i,j) *= depression_factor</span>
<span class="s">                &#39;&#39;&#39;</span>
        <span class="n">weave</span><span class="o">.</span><span class="n">inline</span><span class="p">(</span><span class="n">code</span><span class="p">,</span>
                <span class="p">[</span><span class="s">&#39;C_matrix&#39;</span><span class="p">,</span> <span class="s">&#39;n&#39;</span><span class="p">,</span> <span class="s">&#39;m&#39;</span><span class="p">,</span> <span class="s">&#39;depression_factor&#39;</span><span class="p">],</span>
                <span class="n">type_converters</span><span class="o">=</span><span class="n">weave</span><span class="o">.</span><span class="n">converters</span><span class="o">.</span><span class="n">blitz</span><span class="p">,</span>
                <span class="n">compiler</span><span class="o">=</span><span class="s">&#39;gcc&#39;</span><span class="p">,</span>
                <span class="n">extra_compile_args</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;-O3&#39;</span><span class="p">])</span>
</pre></div>
</div>
<p>The first time you run this it will be slower because it compiles the
C++ code and stores a copy, but the second time will be much faster as
it just loads the saved copy. The way it works is that Weave converts
the listed Python and NumPy variables (<tt class="docutils literal"><span class="pre">C_matrix</span></tt>, <tt class="docutils literal"><span class="pre">n</span></tt>, <tt class="docutils literal"><span class="pre">m</span></tt>
and <tt class="docutils literal"><span class="pre">depression_factor</span></tt>) into C++ compatible data types. <tt class="docutils literal"><span class="pre">n</span></tt> and
<tt class="docutils literal"><span class="pre">m</span></tt> are turned into <tt class="docutils literal"><span class="pre">int``s,</span> <span class="pre">``depression_factor</span></tt> is turned into
a <tt class="docutils literal"><span class="pre">double</span></tt>, and <tt class="docutils literal"><span class="pre">C_matrix</span></tt> is turned into a Weave
<tt class="docutils literal"><span class="pre">Array</span></tt> class. The only thing you need to know about this is that
elements of a Weave array are referenced with parentheses rather than
brackets, i.e. <tt class="docutils literal"><span class="pre">C_matrix(i,j)</span></tt> rather than <tt class="docutils literal"><span class="pre">C_matrix[i,j]</span></tt>. In
this example, I have used the <tt class="docutils literal"><span class="pre">gcc</span></tt> compiler and added the optimisation
flag <tt class="docutils literal"><span class="pre">-O3</span></tt> for maximum optimisations. Again, in this case it&#8217;s much
simpler to just use the <tt class="docutils literal"><span class="pre">C_matrix</span> <span class="pre">*=</span> <span class="pre">depression_factor</span></tt> NumPy expression,
but in some cases using inline C++ might be necessary, and as you can see
above, it&#8217;s very easy to do this with Weave, and the C++ code for a
snippet like this is often almost as simple as the Python code would be.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/brian-logo.png" alt="Logo"/>
            </a></p>
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">How to write efficient Brian code</a><ul>
<li><a class="reference internal" href="#brian-specifics">Brian specifics</a></li>
<li><a class="reference internal" href="#vectorisation">Vectorisation</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="advanced.html"
                        title="previous chapter">Advanced concepts</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="compiledcode.html"
                        title="next chapter">Compiled code</a></p>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="compiledcode.html" title="Compiled code"
             >next</a> |</li>
        <li class="right" >
          <a href="advanced.html" title="Advanced concepts"
             >previous</a> |</li>
        <li><a href="index.html">Brian v1.3.1 documentation</a> &raquo;</li>
          <li><a href="advanced.html" >Advanced concepts</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2008, Romain Brette, Dan Goodman.
      Last updated on Feb 19, 2012.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.8.
    </div>
  </body>
</html>