/usr/share/pyshared/brian/plotting.py is in python-brian 1.4.1-2.
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | # ----------------------------------------------------------------------------------
# Copyright ENS, INRIA, CNRS
# Contributors: Romain Brette (brette@di.ens.fr) and Dan Goodman (goodman@di.ens.fr)
#
# Brian is a computer program whose purpose is to simulate models
# of biological neural networks.
#
# This software is governed by the CeCILL license under French law and
# abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.
# ----------------------------------------------------------------------------------
#
"""
Plotting routines for Brian
Functions:
* ``raster_plot(monitors...,options...)``
* ``hist_plot(monitor,options...)``
"""
__docformat__ = "restructuredtext en"
__all__ = ['plot', 'show', 'figure', 'xlabel', 'ylabel', 'title', 'axis',
'raster_plot', 'raster_plot_spiketimes', 'hist_plot']
try:
from pylab import plot, show, figure, xlabel, ylabel, title, axis, xlim
import pylab, matplotlib
except:
plot, show, figure, xlabel, ylabel, title, axis, xlim = (None,)*8
from stdunits import *
import magic
from connections import *
from monitor import *
from monitor import HistogramMonitorBase
from network import network_operation
from clock import EventClock
import warnings
from log import *
from numpy import amax, amin, array, hstack
import bisect
def _take_options(myopts, givenopts):
"""Takes options from one dict into another
Any key defined in myopts and givenopts will be removed
from givenopts and placed in myopts.
"""
for k in myopts.keys():
if k in givenopts:
myopts[k] = givenopts.pop(k)
def raster_plot(*monitors, **additionalplotoptions):
"""Raster plot of a :class:`SpikeMonitor`
**Usage**
``raster_plot(monitor,options...)``
Plots the spike times of the monitor
on the x-axis, and the neuron number on the y-axis
``raster_plot(monitor0,monitor1,...,options...)``
Plots the spike times
for all the monitors given, with y-axis defined by placing a spike
from neuron n of m in monitor i at position i+n/m
``raster_plot(options...)``
Guesses the monitors to plot automagically
**Options**
Any of PyLab options for the ``plot`` command can be given, as well as:
``showplot=False``
set to ``True`` to run pylab's ``show()`` function
``newfigure=False``
set to ``True`` to create a new figure with pylab's ``figure()`` function
``xlabel``
label for the x-axis
``ylabel``
label for the y-axis
``title``
title for the plot
``showgrouplines=False``
set to ``True`` to show a line between each monitor
``grouplinecol``
colour for group lines
``spacebetweengroups``
value between 0 and 1 to insert a space between
each group on the y-axis
``refresh``
Specify how often (in simulation time) you would like the plot to
refresh. Note that this will only work if pylab is in interactive mode,
to ensure this call the pylab ``ion()`` command.
``showlast``
If you are using the ``refresh`` option above, plots are much quicker
if you specify a fixed time window to display (e.g. the last 100ms).
``redraw``
If you are using more than one realtime monitor, only one of them needs
to issue a redraw command, therefore set this to ``False`` for all but
one of them.
Note that with some IDEs, interactive plotting will not work with the
default matplotlib backend, try doing something like this at the
beginning of your script (before importing brian)::
import matplotlib
matplotlib.use('WXAgg')
You may need to experiment, try WXAgg, GTKAgg, QTAgg, TkAgg.
"""
if len(monitors) == 0:
(monitors, monitornames) = magic.find_instances(SpikeMonitor)
if len(monitors):
# OPTIONS
# Defaults
myopts = {"title":"", "xlabel":"Time (ms)", "showplot":False,
"showgrouplines":False, "spacebetweengroups":0.0,
"grouplinecol":"k", 'newfigure':False, 'refresh':None,
'showlast':None, 'redraw':True}
plotoptions = {'mew':0}
if len(monitors) == 1:
myopts["ylabel"] = 'Neuron number'
else:
myopts["ylabel"] = 'Group number'
# User options
_take_options(myopts, additionalplotoptions)
plotoptions.update(additionalplotoptions)
# PLOTTING ROUTINE
spacebetween = myopts['spacebetweengroups']
class SecondTupleArray(object):
def __init__(self, obj):
self.obj = obj
def __getitem__(self, i):
return float(self.obj[i][1])
def __len__(self):
return len(self.obj)
def get_plot_coords(tmin=None, tmax=None):
allsn = []
allst = []
for i, m in enumerate(monitors):
mspikes = m.spikes
if tmin is not None and tmax is not None:
x = SecondTupleArray(mspikes)
imin = bisect.bisect_left(x, tmin)
imax = bisect.bisect_right(x, tmax)
mspikes = mspikes[imin:imax]
if len(mspikes):
sn, st = array(mspikes).T
else:
sn, st = array([]), array([])
st /= ms
if len(monitors) == 1:
allsn = [sn]
else:
allsn.append(i + ((1. - spacebetween) / float(len(m.source))) * sn)
allst.append(st)
sn = hstack(allsn)
st = hstack(allst)
if len(monitors) == 1:
nmax = len(monitors[0].source)
else:
nmax = len(monitors)
return st, sn, nmax
st, sn, nmax = get_plot_coords()
if myopts['newfigure']:
pylab.figure()
if myopts['refresh'] is None:
line, = pylab.plot(st, sn, '.', **plotoptions)
else:
line, = pylab.plot([], [], '.', **plotoptions)
if myopts['refresh'] is not None:
pylab.axis(ymin=0, ymax=nmax)
if myopts['showlast'] is not None:
pylab.axis(xmin= -myopts['showlast'] / ms, xmax=0)
ax = pylab.gca()
if myopts['showgrouplines']:
for i in range(len(monitors)):
pylab.axhline(i, color=myopts['grouplinecol'])
pylab.axhline(i + (1 - spacebetween), color=myopts['grouplinecol'])
pylab.ylabel(myopts['ylabel'])
pylab.xlabel(myopts['xlabel'])
pylab.title(myopts["title"])
if myopts["showplot"]:
pylab.show()
if myopts['refresh'] is not None:
@network_operation(clock=EventClock(dt=myopts['refresh']))
def refresh_raster_plot(clk):
if matplotlib.is_interactive():
if myopts['showlast'] is None:
st, sn, nmax = get_plot_coords()
line.set_xdata(st)
line.set_ydata(sn)
ax.set_xlim(0, amax(st))
else:
st, sn, nmax = get_plot_coords(clk._t - float(myopts['showlast']), clk._t)
ax.set_xlim((clk.t - myopts['showlast']) / ms, clk.t / ms)
line.set_xdata(array(st))
line.set_ydata(sn)
if myopts['redraw']:
pylab.draw()
pylab.get_current_fig_manager().canvas.flush_events()
monitors[0].contained_objects.append(refresh_raster_plot)
def raster_plot_spiketimes(spiketimes):
"""
Raster plot of a list of spike times
"""
m = Monitor()
m.source = []
m.spikes = spiketimes
raster_plot(m)
t = array(spiketimes)[:,1]
def hist_plot(histmon=None, **plotoptions):
"""Plot a histogram
**Usage**
``hist_plot(histmon,options...)``
Plot the given histogram monitor
``hist_plot(options...)``
Guesses which histogram monitor to use
with argument:
``histmon``
is a monitor of histogram type
**Notes**
Plots only the first n-1 of n bars in the histogram, because
the nth bar is for the interval (-,infinity).
**Options**
Any of PyLab options for bar can be given, as well as:
``showplot=False``
set to ``True`` to run pylab's ``show()`` function
``newfigure=True``
set to ``False`` not to create a new figure with pylab's ``figure()`` function
``xlabel``
label for the x-axis
``ylabel``
label for the y-axis
``title``
title for the plot
"""
if histmon is None:
(histmons, histmonnames) = magic.find_instances(HistogramMonitorBase)
if len(histmons) == 0:
raise TypeError, "No histogram monitors found."
elif len(histmons) > 1:
log_info('brian.hist_plot', "Found more than one histogram monitor, using first one.")
histmon = histmons[0]
# OPTIONS
# Defaults
myopts = {"title":"", "xlabel":"Time (ms)", "ylabel":"Count", "showplot":False, 'newfigure':True }
# User options
_take_options(myopts, plotoptions)
# PLOTTING ROUTINE
if myopts['newfigure']:
pylab.figure()
pylab.bar(histmon.bins[:-1] / ms, histmon.count[:-1], (histmon.bins[1:] - histmon.bins[:-1]) / ms, **plotoptions)
pylab.ylabel(myopts['ylabel'])
pylab.xlabel(myopts['xlabel'])
pylab.title(myopts["title"])
if myopts["showplot"]:
pylab.show()
|