Skip to content

Tutorial: How to work with your own NEURON model

Matteo Cantarelli edited this page Apr 14, 2017 · 10 revisions

All the models accessible from the Sample NEURON Model panel are normal NEURON models that can be found in the Models folder. Few UI API commands make it possible to make the graphical environment aware of a model.

Note: Many of these steps are 'temporary' and will be automated in the future. Please open bugs on this repo with your own suggestions for how to improve this process and NEURON-UI in general!

  1. Install NEURON-UI from sources https://github.com/MetaCell/NEURON-UI (see README)
  2. Copy model folder to NEURON-UI/neuron_ui/models/ (or clone there)
  3. Make soft link of x86_64 folder in NEURON-UI
e.g. 
cd NEURON-UI
ln -s  neuron_ui/models/hnn/x86_64 x86_64
  1. Create empty init.py in any other folder in path to model, so can import as package
  2. Create wrapper script in your model folder e.g. hnn_gui.py (might already be included in repo)
import logging
from neuron import h

from jupyter_geppetto.geppetto_comm import GeppettoCoreAPI as G
from neuron_ui import neuron_utils
from neuron_ui import neuron_geometries_utils

class M1net:

    def __init__(self):
        logging.debug('Loading M1net')
        neuron_utils.createProject(name='M1net')
        import init	

        self.t_vec = h.Vector()
        self.t_vec.record(h._ref_t)
        neuron_utils.createStateVariable(id='time', name='time',
                          units='ms', python_variable={"record_variable": self.t_vec,
                                                           "segment": None})

        neuron_geometries_utils.extractGeometries()

        logging.debug('M1net loaded')
  1. Launch ./NEURON-UI
  2. In Python console run (type then shift+enter as in Jupyter notebook):
import os
os.chdir('neuron_ui/models/hnn')
import hnn_gui
net=hnn_gui.HNN()
  1. To show 3D point diameters run in Javascript console:
HNN.setGeometryType("cylinders")

Video on how to use NEURON-UI (run sim, add IClamp, plot V, …)

Detailed API explanation

Let's have a look at the Multi-compartment Hodgkin-Huxley model which is shipped with NEURON-UI as an example:

class SimpleCell:

def __init__(self):
    neuron_utils.createProject(name='Simple Cell')

The first line uses the neuron_utils to make the UI aware of a new project called Simple Cell. This is a simple wrapper around the model used by the UI to group the NEURON code that will follow into a single container.

What follows in the file is traditional NEURON code:

    self.soma = h.Section(name='soma')
    self.dend = h.Section(name='dend')
    self.dend.connect(self.soma(1))

    # Surface area of cylinder is 2*pi*r*h (sealed ends are implicit).
    # Makes a soma of 500 microns squared.
    self.soma.L = self.soma.diam = 12.6157
    self.dend.L = 200  # microns
    self.dend.diam = 1  # microns

    for sec in h.allsec():
        sec.Ra = 100   # Axial resistance in Ohm * cm
        sec.cm = 1     # Membrane capacitance in micro Farads / cm^2

    # Insert active Hodgkin-Huxley current in the soma
    self.soma.insert('hh')
    self.soma.gnabar_hh = 0.12  # Sodium conductance in S/cm2
    self.soma.gkbar_hh = 0.036  # Potassium conductance in S/cm2
    self.soma.gl_hh = 0.0003    # Leak conductance in S/cm2
    self.soma.el_hh = -54.3     # Reversal potential in mV
    ...

The next API command that is used makes the UI aware of any state variable that we are recording:

    # record soma voltage and time
    self.t_vec = h.Vector()
    self.t_vec.record(h._ref_t)
    neuron_utils.createStateVariable(id='time', name='time',
                          units='ms', python_variable={"record_variable": self.t_vec,
                                                       "segment": None})

    self.v_vec_soma = h.Vector()`
    self.v_vec_soma.record(self.soma(1.0)._ref_v)  # change recoding pos`

    # TODO How do we extract the units?
    neuron_utils.createStateVariable(id='v_vec_soma', name='v_vec_soma',
                          units='mV', python_variable={"record_variable": self.v_vec_soma,"segment": self.soma(1.0)})

where createStateVariable creates a variable whose name is v_vec_soma and that maps to self.v_vec_soma defined in NEURON in the above line. The second item in the dictionary "segment" makes the UI aware of a mapping between that variable and the corresponding segment so that the UI can apply color mappings.

Note: In future versions of NEURON-UI the commands above will be integrated with the NEURON API so that they won't have to be called explicitly but nrnpython will invoke them automatically.

The last API command used is:

neuron_geometries_utils.extractGeometries()

which makes the UI aware of the morphologies defined in the model above. This command will have to be invoked at the end of the declaration of the model.

As a recap three API commands can be used to make the UI aware of a NEURON model:

    neuron_utils.createProject(name='Simple Cell')

which creates a new project,

    neuron_utils.createStateVariable(id='v_vec_soma', name='v_vec_soma',
                          units='mV', python_variable={"record_variable": self.v_vec_soma,"segment": self.soma(1.0)})

which publishes a state variable and

neuron_geometries_utils.extractGeometries()

which publishes the morphology of the model.

If you want to push it further and make your model easily available from the UI you can just add it to the Models panel:

    self.items.append(neuron_utils.add_button('My Model',
                                              self.loadModule, extraData={'module': 'simple_cell', 'model':'SimpleCell'}))

See related tutorial for more on how to create your own GUI.

Clone this wiki locally