First neuron simulation with NEURON + Python

Overview

[Hodgkin-Huxley model](https: //) using NEURON, a general-purpose neuron / neural circuit simulator developed by Michael Hines et al. Of Yale University. En.wikipedia.org/wiki/Hodgkin%E2%80%93Huxley_model) is used for nerve cell simulation.

Although it is a little difficult to understand, it is used as a standard simulator for multi-compartment models of nerve cells, and it can also be used for large-scale simulations using MPI, so if you can master it, it will be a very powerful tool.

(As an aside, the cover image of the book AGE OF SUPER SENSING The Future of Sensing Design is by NEURON. It is visualized based on a neural circuit simulation.)

testing environment

If NEURON + Jupyter notebook is working, I think it will work in other environments such as CentOS, Mac, and Windows.

setup

Building a NEURON environment for Python is a bit cumbersome, so I created a Docker Image.

Docker installation

Please refer to http://docs.docker.jp/engine/installation/ etc. If it is an older version, you can also install it with the following command.

$ sudo apt-get install docker.io
$ sudo yum install docker-io

Run Docker image

When you execute the following command, Jupyter notebook with NEURON setup will start, so access the displayed URL (http: // localhost: 8888 /? Token = 91d2 in the example below).

$ docker run -p 8888:8888 dmiyamoto/neuron:jupyter
/usr/local/lib/python2.7/dist-packages/IPython/paths.py:69: UserWarning: IPython parent '/home/neuron' is not a writable location, using a temp directory.
  " using a temp directory.".format(parent))
[I 15:21:30.297 NotebookApp] Writing notebook server cookie secret to /home/neuron/.local/share/jupyter/runtime/notebook_cookie_secret
[W 15:21:30.307 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 15:21:30.311 NotebookApp] Serving notebooks from local directory: /work
[I 15:21:30.311 NotebookApp] 0 active kernels 
[I 15:21:30.311 NotebookApp] The Jupyter Notebook is running at: http://[all ip addresses on your system]:8888/?token=91d2
[I 15:21:30.311 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 15:21:30.311 NotebookApp] 
    
    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:
        http://localhost:8888/?token=91d2

If you do not use Jupyter notebook, you can use it as a python interpreter with the following command.

$ docker run -it neuron:jupyter python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Code to use

The sample code used this time is as follows.

In addition, since the same code is included in ʻexamples` on the file screen of Jupyter Notebook, use this.

calc_hh()

Except for the calc_hh () function, you can understand it if you have general knowledge of Python and Matplotlib, so I will explain only the contents of calc_hh ().

Definition of cells

soma = neuron.h.Section(name="soma")

soma.nseg = 1
soma.diam = 10   # [um]
soma.L = 10      # [um]
soma.insert("hh")

In NEURON Python, a function with the same name as HOC is defined under the h class. (Explanation for those who understand) Here, first, the Section function is used to define the newly calculated cell soma. After that, the parameters for the soma object are set. The explanation of each parameter is as follows.

--nseg: Number of divisions when calculating the cell potential. When targeting elongated cells, it is necessary to increase the number of divisions because the potentials at the center point and the end point are different. --diam: Cell diameter --L: Cell length --ʻInsert ('hh') : Hodgkin-Set a Huxley type (hh) model. Others include pas`, which assumes only Leak current. You can also create your own model.

Also, what kind of parameters exist and what the values are can be displayed with the psection function. (It should be, but it seems that the print function inside NEURON is not displayed well on Jupyter, so I will show an example of execution in the interpreter.) It can be newly set by ʻinsert the'hh'` model. You can also observe where the parameters are increasing.

>>> soma = neuron.h.Section(name='soma')
>>> neuron.h.psection()
soma { nseg=1  L=100  Ra=35.4
	/*location 0 attached to cell 0*/
	/* First segment only */
	insert morphology { diam=500}
	insert capacitance { cm=1}
}
1.0
>>> soma.insert('hh')
<nrn.Section object at 0x7fe655562648>
>>> neuron.h.psection()
soma { nseg=1  L=100  Ra=35.4
	/*location 0 attached to cell 0*/
	/* First segment only */
	insert morphology { diam=500}
	insert capacitance { cm=1}
	insert hh { gnabar_hh=0.12 gkbar_hh=0.036 gl_hh=0.0003 el_hh=-54.3}
	insert na_ion { ena=50}
	insert k_ion { ek=-77}
}
1.0

Stimulation settings

stim = neuron.h.IClamp(soma(0.5))
stim.delay = 50  # [ms]
stim.dur = 200   # [ms]
stim.amp = 0.1   # [nA]

Even if cells are defined and a Hodgkin-Huxley type model is introduced, if there is no stimulation, the potential remains constant, so constant current stimulation is set. In NEURON, a constant current stimulus can be added by the ʻIClamp` function. The explanation of the parameters is as follows.

--deley: Time to start stimulation -- dur: Stimulation duration --ʻAmp`: Stimulation intensity

Measurement method setting

rec_t = neuron.h.Vector()
rec_t.record(neuron.h._ref_t)
rec_v = neuron.h.Vector()
rec_v.record(soma(0.5)._ref_v)

Allocate an array in NEUORON with the Vector function and set the value to be recorded with the record function. As a result, the value set for each calculation step is added to the array.

Setting and executing simulation conditions

neuron.h.finitialize(-65)
tstop = 300
neuron.run(tstop)

The finitialize function initializes the cell potential to -65 [mV]. Also, use the run function to actually start the simulation. The argument is the end time of the simulation (300 msec in this case).

Post-processing

t = rec_t.as_numpy()
v = rec_v.as_numpy()
return np.array(t), np.array(v)

The recorded value is converted from the internal format of NEURON to ndarray by the ʻas_numpyfunction. Also, (probably) due to a bug on the NEURON side, the object may be released without permission, so usenp.array` to copy the value and use it as the return value.

If you graph this value with matplotlib, you can get the following Hodgkin-Huxley spikes.

download.png

in conclusion

Artificial intelligence is booming in the world, but for those who are interested in simulating the actual brain and neural circuits, rather than the extremely highly abstracted model like Deep Learning, this miscellaneous text will help. I hope it will be.

reference

change history

--The docker image settings have changed, so some corrections

Recommended Posts

First neuron simulation with NEURON + Python
Web scraping with Python First step
[GUI with Python] PyQt5-The first step-
Try frequency control simulation with Python
FizzBuzz with Python3
First time python
Scraping with Python
First Python 3 ~ First comparison ~
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
First time python
ambisonics simulation python
[Python / PyRoom Acoustics] Room acoustic simulation with Python
with syntax (Python)
First Python ~ Coding 2 ~
Bingo with python
Zundokokiyoshi with python
First python [O'REILLY]
"First Elasticsearch" starting with a python client
Excel with Python
Microcomputer with Python
I made a neuron simulator with Python
Cast with python
First python ① Environment construction with pythonbrew & Hello World !!
[Introduction to Udemy Python3 + Application] 9. First, print with print
[Cloud102] # 1 Get Started with Python (Part 1 Python First Steps)
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
First Python 3rd Edition
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
PyQ ~ Python First Steps ~
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Scraping with Python + PhantomJS
First Python image processing
Drive WebDriver with python
[Python] Redirect with CGIHTTPServer