NEST 2.0 can be used with the Python programming language via the PyNEST extension module. This means that Python is used instead of NEST's native simulation language interpreter (SLI). Python has a more convenient syntax and gives you access to many supplementary packages, like e.g. the Matplotlib/Pylab library to visualize the data directly from within the simulation script. The PyNEST commands are mostly the same as in SLI, so that the SLI documentation can be used for both.
The implementation of PyNEST is published as Jochen Martin Eppler, Moritz Helias, Eilif Muller, Markus Diesmann, and Marc-Oliver Gewaltig. PyNEST: A convenient interface to the NEST simulator. Frontiers in Neuroinformatics, 2:12, 2009. doi:10.3389/neuro.11.012.2008 and avaliable from here.
Note on the usage of SciPy
We recently observed that if PyNEST is used with some versions of SciPy, a segmentation fault is caused. The exact reason for this is not identified yet. However, to circumvent the problem, you can import SciPy before PyNEST.
where you must replace 2.5 by your Python version. Again, you can consult extras/nest.config to learn which Python version NEST was compiled for.
OSX 10.6 and IPython 2.6
IPython 2.6 under OSX 10.6 has some issues with NEST, at least the IPython version from MacPorts. You can use the following work-around to start NEST in IPython 2.6 on Snow Leopard: First start plain python and then load nest first before loading IPython by hand:
>>> import nest >>> import IPython.Shell >>> IPython.Shell.start().mainloop() In : import nest In : nest.Simulate(1)
Note that you need to import NEST a second time once you have entered the IPython shell. Output has been removed in the code snippet above. We are working on this issue under ticket #397.
A small example
Here is a small example that creates the same network as in Neural simulations, but now using PyNEST instead of SLI. After staring the Python interpreter (or Ipython, an enhanced, interactive Python shell), you first have to import the PyNEST module by typing
This will initialize the NEST library that runs in the background. Now we can start to set up our simulation.
neuron = nest.Create("iaf_neuron")
The first command will create a neuron of type iaf_neuron. The Create command has an optional argument for the number of neurons to be created. The return value of Create is always a list that contains the IDs of the created elements. In our case it has only a single element, the ID of our neuron.
vm = nest.Create("voltmeter")
The next command connects the voltmeter with the neuron:
Following is a list of commands that are available in PyNEST. They correspond directly to the SLI commands with similar names. As Python already has extensive standard libraries for things like math, linear algebra, handling of arrays, statistics, etc. we do not provide them in the PyNEST API. However, PyNEST provides direct access to SLI via the low-level functions
Please download the Poster describing the Python API to NEST (pdf), presented at the Neuroinformatics 2008 conference in Stockholm.
Information on NEST
- Show the authors of NEST.
- Print information on the platform on which NEST was compiled.
- Print the NEST version.
- Show the help page for the given object
- Open the NEST helpdesk in the given browser.
Low-level access to SLI
- Pop an object off the SLI stack and return it.
- Push an object onto the SLI stack.
- Send a command string to the NEST kernel to be executed and raise errors as Python errors.
- Simulate the network for t milliseconds.
- Resume an interrupted simulation.
- Reset the simulation kernel. This will destroy the network as well as all custom models created with CopyModel(). Calling this function is equivalent to restarting NEST.
- Reset all nodes and connections to their original state.
- Return a dictionary with the parameters of the simulation kernel.
- Set parameters for the simulation kernel.
- Models(mtype='all', sel=None)
- Return a list of all available NEST models (neurons, devices, synapses). Use mtype='nodes' to only see neuron and device models, mtype='synapses' to only see synapse models. sel can be a string, used to filter the result list and only return models containing it.
- CopyModel(existing, new, params=None)
- Create a new model by copying an existing one. Default parameters can be given as params, or else are taken from existing.
- Return a dictionary with the default parameters for the given model, specified by a string.
- SetDefaults(model, params)
- Set the default parameters of the given model to the values specified in the params dictionary.
- Create(model, n=1, params=None)
- Create n instances of type model. Parameters for the new nodes can are given as params (a single dictionary or a list of dictionaries with size n). If omitted, the model's defaults are used.
- GetStatus(nodes, keys=None)
- Return the parameter dictionaries of the given list of nodes. If keys is given, a list of values is returned instead. keys may also be a list, in which case the returned list contains lists of values.
- SetStatus(nodes, params, val=None)
- Set the parameters of nodes to params, which may be a single dictionary or a list of dictionaries. If val is given, params has to be the name of an attribute, which is set to val on the nodes. val can be a single value or a list of the same size as nodes.
- Return the addresses of one or more nodes.
- Return the global ids of one or more nodes.
- Return the local id of a node with gid.
- Connect(pre, post, params=None, delay=None, model='static_synapse')
- Make one-to-one connections of type model between the nodes in pre and the nodes in post. pre and post have to be lists of the same length. If params is given (as dictionary or list of dictionaries), they are used as parameters for the connections. If params is given as a single float or as list of floats, it is used as weight(s), in which case delay also has to be given as float or as list of floats.
- ConvergentConnect(pre, post, weight=None, delay=None, model='static_synapse')
- Connect all neurons in pre to each neuron in post. pre and post have to be lists. If weight is given (as a single float or as list of floats), delay also has to be given as float or as list of floats.
- RandomConvergentConnect(pre, post, n, weight=None, delay=None, model='static_synapse', options=None)
- Connect n randomly selected neurons from pre to each neuron in post. pre and post have to be lists. If weight is given (as a single float or as list of floats), delay also has to be given as float or as list of floats. options is a dictionary specifying options to the RandomConvergentConnect function: allow_autapses, allow_multapses.
- DivergentConnect(pre, post, weight=None, delay=None, model='static_synapse')
- Connect each neuron in pre to all neurons in post. pre and post have to be lists. If weight is given (as a single float or as list of floats), delay also has to be given as float or as list of floats.
- RandomDivergentConnect(pre, post, n, weight=None, delay=None, model='static_synapse', options=None)
- Connect each neuron in pre to n randomly selected neurons from post. pre and post have to be lists. If weight is given (as a single float or as list of floats), delay also has to be given as float or as list of floats. options is a dictionary specifying options to the RandomDivergentConnect function: allow_autapses, allow_multapses.
- FindConnections(source, target=None, synapse_type=None)
- Return an array of identifiers for connections that match the given parameters. Only source is mandatory and must be a list of one or more nodes. If target and/or synapse_type is/are given, they must be single values, lists of length one or the same length as source. Use GetStatus()/SetStatus() to inspect/modify the found connections.
- GetStatus(conns, keys=None)
- Return the parameter dictionaries of the given list of connections (identified by handles as returned by FindConnections()). If keys is given, a list of values is returned instead. keys may also be a list, in which case the returned list contains lists of values.
- SetStatus(conns, params, val=None)
- Set the parameters of connections (identified by handles as returned by FindConnections()) to params, which may be a single dictionary or a list of dictionaries. If val is given, params has to be the name of an attribute, which is set to val on the connections. val can be a single value or a list of the same size as conns.
- Return the overall number of MPI processes.
- Return the MPI rank of the local process.
- PrintNetwork(depth=1, subnet=None)
- Print the network tree up to depth, starting at subnet. if subnet is omitted, the current subnet is used instead.
- Returns the global id of the current subnet.
- Make subnet the current subnet.
- Create a new subnet and change into it. A string argument can be used to name the new subnet A dictionary argument can be used to set the subnet's custom dict.
- Change to the parent subnet and return the gid of the current.
- Return the global ids of all leaf nodes of the given subnet.
- Return the complete list of nodes (including sub-networks) under subnet.
- GetNodeAt(gidlist, adr)
- For each subnet in gidlist, get the gid of the node at position adr.
- Return the immediate children of subnet id.
- GetNetwork(gid, depth)
- Return a nested list with the children of subnet id at level depth. If depth==0, the immediate children of the subnet are returned. The returned list is depth+1 dimensional.
- LayoutNetwork(model, dim, label=None, params=None)
- Create a subnetwork of dimension dim with nodes of type model and return a list of ids.