IntroductionExamplesDocumentationDownloadLinksContact
 

Support for Python in neuroConstruct

Scripting interface to neuroConstruct

While a graphical user interface (GUI) is useful for constructing cell and network models visually, tuning a model will normally require running of large numbers of simulations, differing only in small details, and comparing the results. This can be achieved through the GUI of neuroConstruct, but it is a time consuming process to load projects, adjust a value in a text field, run a simulation, open the simulation results and repeat for several iterations.

The scripting interface for neuroConstruct allows projects to be accessed (either through the command line interface or more usually by a script file), the model elements of the project edited, simulations in any of the supported simulators to be generated and executed, and simulation results analysed.

The scripting interface is based on Jython. Jython is an implementation of Python purely in Java; the more widely used version, and the reference implementation, is "CPython". Jython allows access using a Python based syntax to Java classes in external packages. This has been used to make it possible to call methods in the Java classes of neuroConstruct from Python scripts.

The standard release of neuroConstruct comes bundled with Jython, and an instance of the Jython script interpreter (with access to the classes in the neuroConstruct jar file) can be run using nC.bat -python (Windows) or ./nC.sh -python (Linux/Mac). The scripts below give some examples of the usage of the neuroConstruct scripting interface (these are present in the pythonnC directory under the main installation folder of neuroConstruct).

 

 Ex1_Simple.py

This simple example shows a neuroConstruct project being loaded and its status checked. Note the line:

from ucl.physiol.neuroconstruct.project import ProjectManager

which imports the Java class ProjectManager. The methods available on this can be seen in the Java API for ProjectManager

 

 Ex2_SaveProject.py

This example demonstrates changing a property of the neuroConstruct project (in this case the amplitude of one of the current clamp inputs) and saving the project. If the lines

myProject.markProjectAsEdited()

myProject.saveProject()

are called, the project will be saved, otherwise the edits to the cells, connections, etc. will be lost once the script exits.

 

 Ex3_ManualCreate.py

Locations of individual cells in Cell Groups and network connections are usually generated by specifying the packing patterns and connection conditions through the GUI. There are a limited number of options allowed here. For complete flexibility the Python interface can be used to specify the exact (x, y, z) postion of all cells in a Cell Group, and the indiviual connections between cells in a network connection. The example above demonstrates this. Cell positions are added directly to the GeneratedCellPositions object. In a similar manner network connections and electrical input can be added. This is opposed to using the doGenerate() function of ProjectManager which would use the templates for Cell Groups, etc. as set through the GUI (see Ex5_MultiSimGenerate.py), Any code (e.g. NEURON simulation scripts) which is generated after "manually" adding the positions/conections will use this custom network structure.

 

 Ex4_SaveNetworkML.py

This script is similar to the previous one, in that it manually creates cell positions and connections. This script saves the network structure to a NetworkML file though. This can be useful for generating a custom network structure via a script, and afterwards loading the NetworkML in through the GUI for visualisation and possible use in simulations. Note that an example below Ex1_CreateNetworkML.py carries out a similar task, but does not use neuroConstruct specific methods (and so can be used on CPython).

 

 Ex5_MultiSimGenerate.py

This script generates a number of simulations in NEURON, each with a small parameter change, and runs a maximum of 4 simulations simultaneously (this number can be modified to reflect the max number of local processors to use). The script can be customised to make any change in the cells/network structure between simulations. The simulations can be replayed through the GUI, or alternatively loaded and analysed in the same script.

 

 Ex6_LoadCells.py

This script uses the MorphMLConverter class to load a cell structure from a NeuroML morphology file and include it in a neuroConstruct project. The project can then be saved and the neuroConstruct GUI opened (by uncommenting the lines at the end of the file). This illustrates how the scripting interface could be used to handle/import large data set outside of the GUI. Other converters for various morphology types are available in the package ucl.physiol.neuroconstruct.cell.converters

 

 Ex7_ExtractNeuroML.py

This example opens a neuroConstruct project, and saves cells in NeuroML format. It accesses the utility method saveAllCellsInNeuroML(...) in class MorphMLConverter.

 

 Example of Python scripts in use

There is an example of a Python script for generating F-I (firing frequency versus injected current) plots for 2 realistic cell models in the neuroConstruct project GranCellLayer. The script GranGolgiF_I.py generates the plots and can be found in the pythonScripts directory of the project, and the GenerateF_ICurve.py script can be reused for generating F_I plots in other projects.

 

Python scripts for NeuroML support

A number of Python scripts for dealing with NeuroML files can be found in the pythonNeuroML/Examples directory under the main installation folder of neuroConstruct. These utlilse the utility files under the pythonNeuroML/NeuroMLUtils directory.

 

 Ex1_CreateNetworkML.py

This file can be used to create a NetworkML file in valid XML or HDF5 format. Note it does not use the neuroConstruct Java classes, and so can be used with CPython, Jython, or the nC.xxx -python scripts.

 

 Ex2_ReadNetworkML_XML.py

 Ex3_ReadNetworkML_HDF5.py

These files can be used to read in the contents of NetworkML files. One version is provided for XML based files, another for HDF5 based files (note, PyTables needs to be installed before this example can be run).

 

 Ex4_SimplePyNN.py

 Ex5_NetworkMLPyNN.py

These examples illustrate some basic interaction with PyNN, which is a Python package for network creation on multiple simulators. Ex4_SimplePyNN.py is a simple example which cane be used to test the correct local installation of PyNN and one of the supporting simulators. Ex5_NetworkMLPyNN.py uses many of the same classes as Ex2_ReadNetworkML_XML.py to parse a NetworkML file, and call the appropriate functions in the PyNN API to create the network contained in the NetworkML file. Both of these can be run using the command python Ex***.py sim, with sim replaced by the name of the PyNN compliant simulator.

These examples use the utility classes under the pythonNeuroML/PyNNUtils directory. These files are also used by neuroConstuct in the PyNN example project: Ex8_PyNNDemo

 

Python usage in native NEURON simulations

There is an option at the Export -> NEURON -> Generate code tab to create Python/XML/hoc or Python/HDF5 simulations, as opposed to "pure" hoc (and mod) NEURON scripts. These use a similar method to Ex2_ReadNetworkML_XML.py and Ex5_NetworkMLPyNN.py above, using utility files in pythonNeuroML/NeuroMLUtils and pythonNeuroML/NEURONUtils to parse XML/HDF5 based NetworkML and call the appropriate NEURON methods to create cells/connections.

This functionality is still in development, and requires NEURON to be compiled with Python, and for HDF5, for PyTables to be correctly installed.