A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting...

44
A NEURON + P YTHON TUTORIAL I Learn how to use NEURON with Python

Transcript of A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting...

Page 1: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

A NEURON + PYTHON TUTORIAL

I Learn how to use NEURON with Python

I Show that neurons are capable of linearly non-separablecomputations.

Page 2: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

A NEURON + PYTHON TUTORIAL

I Learn how to use NEURON with PythonI Show that neurons are capable of linearly non-separable

computations.

Page 3: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

WHY NOT NEURON ALONE?

I Python is clearer than Hoc and easier to work with

I Generate, process and plot the data using same language

Page 4: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

WHY NOT NEURON ALONE?

I Python is clearer than Hoc and easier to work withI Generate, process and plot the data using same language

Page 5: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

USING SPYDER

I Create a new project folder called NEURON in Spyder andcreate a file called neurhon.py

I You can run this script with F5I You might need to restart the ipython kernel . . .

Page 6: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

USING SPYDER

I Create a new project folder called NEURON in Spyder andcreate a file called neurhon.py

I You can run this script with F5

I You might need to restart the ipython kernel . . .

Page 7: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

USING SPYDER

I Create a new project folder called NEURON in Spyder andcreate a file called neurhon.py

I You can run this script with F5I You might need to restart the ipython kernel . . .

Page 8: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

START BY IMPORTING NEURON AND MATPLOTLIB IN

PYTHON

#Import m a t p l o t l i b f o r p l o t t i n gimport m a t p l o t l i b . pyplot as p l t# Import NEURON f o r s imulat ingimport neuron

Page 9: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

OUR FIRST SOMA

# Access the h ( hoc ) to c r e a t e a S e c t io nsoma = neuron . h . Se c t i o n (name=”soma ”)soma . nseg = 1soma . diam = 10soma . L = 10

Page 10: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

YOUR FIRST RUN

I Run the script with F5

I You have created your first soma!I Use ipython to inspect your soma by typing dir(soma) in

the console

Page 11: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

YOUR FIRST RUN

I Run the script with F5I You have created your first soma!

I Use ipython to inspect your soma by typing dir(soma) inthe console

Page 12: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

YOUR FIRST RUN

I Run the script with F5I You have created your first soma!I Use ipython to inspect your soma by typing dir(soma) in

the console

Page 13: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

SOME ATTRIBUTS AND METHODS OF YOUR FIRST

SECTION

AttributsL The segment diameter

Ra The axial resistancenseg The number of segments

Methodconnect Connect the section to another one use ? to know

more.insert Insert a mechanism in all the segments of the

section.

Page 14: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

SOME ATTRIBUTS AND METHODS OF YOUR FIRST

SECTION

AttributsL The segment diameter

Ra The axial resistancenseg The number of segments

Methodconnect Connect the section to another one use ? to know

more.insert Insert a mechanism in all the segments of the

section.

Page 15: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

ADD HODGKIN HUXLEY MECHANISMS

For that simply add the lines:

soma . i n s e r t (” hh ”)meca = soma ( 0 . 5 ) . hh

Inspect meca of the middle section with dir(meca).

Page 16: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

SOME ATTRIBUTS OF MECHANISMS

gnabar hh Maximum sodium channel conductance S/cm2̂gkbar hh Maximum potassium channel conductance S/cm2̂

gl hh Leakage conductance S/cm2̂ena, ek, el hh Reversal potential for the sodium channel,ik

potassium and leakage channel mVm hh, n hh, h hh Sodium activation and Potassium

inactivation and activation state

Page 17: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

RECORD VARIABLES

Specify the values of the section to be recorded:

# Record Time from NEURON ( neuron . h . r e f t )r e c t = neuron . h . Vector ( )r e c t . record ( neuron . h . r e f t )# Record Voltage from the c e n t e r of the somar e c v = neuron . h . Vector ( )r e c v . record ( soma ( 0 . 5 ) . r e f v )

Page 18: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

INSERT AN ELECTRODE

# Create an e l e c t r o d e i n j e c t i n g current in the somastim = neuron . h . IClamp ( soma ( 0 . 5 ) )

# Set the e l e c t r o d e p r o p e r t i e sstim . delay = 100stim . dur = 100stim . amp = 0 . 1

Page 19: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

SOME ATTRIBUTS OF THE ELECTRODE

amp Amplitude of the injected currentdelay Time of activation in ms

dur Duration of the stimulation

Page 20: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

RUN A SIMULATION

# I n i t i a l i s e the value of the vol tageneuron . h . f i n i t i a l i z e (−65)# Set the time of the s imulat iont s t o p = 300#Run the s imulat ionneuron . run ( t s t o p )

Page 21: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

PLOT THE RESULT

#Record time and vol tage as numpy arraystime = r e c t . as numpy ( )vol tage = r e c v . as numpy ( )# P l o tp l t . p l o t ( time , voltage , c o l o r = ’b ’ )p l t . x l a b e l (” Time [ms] ” )p l t . y l a b e l (” Voltage [mV] ” )p l t . a x i s ( xmin=0 , xmax=max( time ) , \

ymin=min ( vol tage )−5 , ymax=max( vol tage )+ 5 )p l t . show ( )

Page 22: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

YOUR JOB I

I After writing the part creating the soma / insertingmecanism / recording variables / inserting an electrode /plotting the result. You can run your script.

I Change the amplitude of the stimulation and observe theoutcome

Page 23: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

YOUR JOB I

I After writing the part creating the soma / insertingmecanism / recording variables / inserting an electrode /plotting the result. You can run your script.

I Change the amplitude of the stimulation and observe theoutcome

Page 24: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

ADDING DENDRITES

# Create ndendri tesndend = 2dends = range ( ndend )f o r i in dends :

dend = neuron . h . Se c t i o n ( )dend . nseg = 5dend . L = 300dend . diam = 0 . 5dend . Ra = 125dend . i n s e r t (” pas ”)dend . connect ( soma , 0 )dends [ i ] = dend

Page 25: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

REFACTOR THE CODE

I Never rewrite something two times

I Enable to identify bottleneck (see %timeit in ipython). InNEURON this is often the run() obviously

Page 26: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

REFACTOR THE CODE

I Never rewrite something two timesI Enable to identify bottleneck (see %timeit in ipython). In

NEURON this is often the run() obviously

Page 27: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

AN EXAMPLE OF FUNCTION: SET RECORDINGS

def rec ( seg ) :””” Record vol tage of a segment seg .f o r example soma ( 0 . 5 ) ” ” ”r e c v = neuron . h . Vector ( )r e c v . record ( seg . r e f v )re turn r e c v

Page 28: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

YOUR JOB II

I Refactor in two functions: run to run a simulation, and plotto plot a voltage trace.

I Plot the voltage from soma(0.5) and from one of thedendrites at 0.9 with two different colors

I Hint: Use the functions rec two times and the function plottwo times

I Increase the number of dendrites and explain yourobservation.

Page 29: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

YOUR JOB II

I Refactor in two functions: run to run a simulation, and plotto plot a voltage trace.

I Plot the voltage from soma(0.5) and from one of thedendrites at 0.9 with two different colors

I Hint: Use the functions rec two times and the function plottwo times

I Increase the number of dendrites and explain yourobservation.

Page 30: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

YOUR JOB II

I Refactor in two functions: run to run a simulation, and plotto plot a voltage trace.

I Plot the voltage from soma(0.5) and from one of thedendrites at 0.9 with two different colors

I Hint: Use the functions rec two times and the function plottwo times

I Increase the number of dendrites and explain yourobservation.

Page 31: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

YOUR JOB II

I Refactor in two functions: run to run a simulation, and plotto plot a voltage trace.

I Plot the voltage from soma(0.5) and from one of thedendrites at 0.9 with two different colors

I Hint: Use the functions rec two times and the function plottwo times

I Increase the number of dendrites and explain yourobservation.

Page 32: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

THE FEATURE BINDING PROBLEM

I Caze et al 2013 “Passive dendrites enable single neurons tocompute linearly non-separable functions”

I Use NEURON to show that neurons are capable of linearlynon-separable computations.

Page 33: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

THE FEATURE BINDING PROBLEM

[x1, x2, x3, x4] y[1, 1.0, 0] 0[1, 0, 1, 0] 1[0, 1, 0, 1] 1[0, 0, 1, 1] 0

Page 34: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

THE FEATURE BINDING PROBLEM IS LINEARLY

NON-SEPARABLE

Line Weights ConditionL1 w1 + w2 + 0 + 0 < ΘL2 w1 + 0 + w3 + 0 ≥ ΘL3 0 + w2 + 0 + w4 ≥ ΘL4 0 + 0 + w3 + w4 < Θ

Contradiction apparent when you add L1 + L4 and L2 + L3→no set of weights can compute this function

Page 35: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

A FUNCTION TO ADD SYNAPSES TO A SEGMENT

def add syn ( seg , time ) :”””Add a synapse a t a given l o c a t i o n ”””stim = neuron . h . AlphaSynapse ( seg )stim . onset = timestim . gmax = 0 . 2re turn stim

Page 36: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

YOUR JOB III

I Show that a neuron with two passive dendrites (bipolar)can implement the feature binding problem

I Hint 1: synapses should be located in the middle ofdendritic sections to make the neuron spike

I Hint 2: add only synapses that should be activeI Hint 3: use the symmetry of the problem to study only two

cases instead of four.

Page 37: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

YOUR JOB III

I Show that a neuron with two passive dendrites (bipolar)can implement the feature binding problem

I Hint 1: synapses should be located in the middle ofdendritic sections to make the neuron spike

I Hint 2: add only synapses that should be activeI Hint 3: use the symmetry of the problem to study only two

cases instead of four.

Page 38: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

YOUR JOB III

I Show that a neuron with two passive dendrites (bipolar)can implement the feature binding problem

I Hint 1: synapses should be located in the middle ofdendritic sections to make the neuron spike

I Hint 2: add only synapses that should be active

I Hint 3: use the symmetry of the problem to study only twocases instead of four.

Page 39: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

YOUR JOB III

I Show that a neuron with two passive dendrites (bipolar)can implement the feature binding problem

I Hint 1: synapses should be located in the middle ofdendritic sections to make the neuron spike

I Hint 2: add only synapses that should be activeI Hint 3: use the symmetry of the problem to study only two

cases instead of four.

Page 40: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

TO GO FURTHER

I NEURON a programming tutorial:http://www.anc.ed.ac.uk/school/neuron/

I You should be able to reproduce the Part A and B usingNEURON + python without help

I The part C introduce with 3D models, a bit more involvedbut here also NEURON + python helps

I Part D is standalone as a .mod file can be used both inNEURON + Python and NEURON

I Part E is obsolete because Python enables more complexdata processing/recording

Page 41: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

TO GO FURTHER

I NEURON a programming tutorial:http://www.anc.ed.ac.uk/school/neuron/

I You should be able to reproduce the Part A and B usingNEURON + python without help

I The part C introduce with 3D models, a bit more involvedbut here also NEURON + python helps

I Part D is standalone as a .mod file can be used both inNEURON + Python and NEURON

I Part E is obsolete because Python enables more complexdata processing/recording

Page 42: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

TO GO FURTHER

I NEURON a programming tutorial:http://www.anc.ed.ac.uk/school/neuron/

I You should be able to reproduce the Part A and B usingNEURON + python without help

I The part C introduce with 3D models, a bit more involvedbut here also NEURON + python helps

I Part D is standalone as a .mod file can be used both inNEURON + Python and NEURON

I Part E is obsolete because Python enables more complexdata processing/recording

Page 43: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

TO GO FURTHER

I NEURON a programming tutorial:http://www.anc.ed.ac.uk/school/neuron/

I You should be able to reproduce the Part A and B usingNEURON + python without help

I The part C introduce with 3D models, a bit more involvedbut here also NEURON + python helps

I Part D is standalone as a .mod file can be used both inNEURON + Python and NEURON

I Part E is obsolete because Python enables more complexdata processing/recording

Page 44: A NEURON + PYTHON TUTORIAL Learn how to use NEURON with Python · #Import matplotlib for plotting import matplotlib . pyplot as plt #Import NEURON for simulating ... insertInsert

TO GO FURTHER

I NEURON a programming tutorial:http://www.anc.ed.ac.uk/school/neuron/

I You should be able to reproduce the Part A and B usingNEURON + python without help

I The part C introduce with 3D models, a bit more involvedbut here also NEURON + python helps

I Part D is standalone as a .mod file can be used both inNEURON + Python and NEURON

I Part E is obsolete because Python enables more complexdata processing/recording