Skip to content
Snippets Groups Projects
Select Git revision
  • fce2c76b471a26f9355540c0aac1b374d0ad41ad
  • master default protected
  • tut_ring_allen
  • docs_furo
  • docs_reorder_cable_cell
  • docs_graphviz
  • docs_rtd_dev
  • ebrains_mirror
  • doc_recat
  • docs_spike_source
  • docs_sim_sample_clar
  • docs_pip_warn
  • github_template_updates
  • docs_fix_link
  • cv_default_and_doc_clarification
  • docs_add_numpy_req
  • readme_zenodo_05
  • install_python_fix
  • install_require_numpy
  • typofix_propetries
  • docs_recipe_lookup
  • v0.10.0
  • v0.10.1
  • v0.10.0-rc5
  • v0.10.0-rc4
  • v0.10.0-rc3
  • v0.10.0-rc2
  • v0.10.0-rc
  • v0.9.0
  • v0.9.0-rc
  • v0.8.1
  • v0.8
  • v0.8-rc
  • v0.7
  • v0.6
  • v0.5.2
  • v0.5.1
  • v0.5
  • v0.4
  • v0.3
  • v0.2.2
41 results

simple_synapse.py

Blame
  • user avatar
    Sam Yates authored
    * Add a tweaked version of exp2syn.mod that is modparser compatible.
    * Add option to simple_synapse.py to choose which syn type to use.
    * Compatibility tweak for runinng nrn scripts with nrniv -python
    fce2c76b
    History
    simple_synapse.py 4.55 KiB
    from timeit import default_timer as timer
    import os.path
    from matplotlib import pyplot
    import numpy as np
    import json
    import argparse
    from neuron import gui, h
    
    parser = argparse.ArgumentParser(description='generate spike train ball and stick model with hh channels at soma and pas channels in dendrite')
    parser.add_argument('--plot', action='store_true', dest='plot')
    parser.add_argument('--synapse', metavar='SYN', dest='synapse',
                        choices=['exp', 'exp2'],
                        default='exp',
                        help='use synapse type SYN (exp or exp2)')
    
    # hack to make things work with nrniv ... -python:
    # throw away args before -python foo.py if -python present.
    
    if '-python' in sys.argv:
        argv = sys.argv[sys.argv.index('-python')+2:]
    else:
        argv = sys.argv
    
    args = parser.parse_args(argv)
    
    soma = h.Section(name='soma')
    dend = h.Section(name='dend')
    
    dend.connect(soma(1))
    
    # Surface area of cylinder is 2*pi*r*h (sealed ends are implicit).
    # Here we make a square cylinder in that the diameter
    # is equal to the height, so diam = h. ==> Area = 4*pi*r^2
    # We want a soma of 500 microns squared:
    # r^2 = 500/(4*pi) ==> r = 6.2078, diam = 12.6157
    soma.L = soma.diam = 12.6157 # Makes a soma of 500 microns squared.
    dend.L = 200 # microns
    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
    soma.insert('hh')
    soma.gnabar_hh = 0.12  # Sodium conductance in S/cm2
    soma.gkbar_hh = 0.036  # Potassium conductance in S/cm2
    soma.gl_hh = 0.0003    # Leak conductance in S/cm2
    soma.el_hh = -54.3     # Reversal potential in mV
    
    # Insert passive current in the dendrite
    dend.insert('pas')
    dend.g_pas = 0.001  # Passive conductance in S/cm2
    dend.e_pas = -65    # Leak reversal potential mV
    
    # create a stimulator that stimulates once at 9 ms
    stim = h.NetStim()
    stim.number = 1
    stim.start  = 9
    
    if args.plot :
        pyplot.figure(figsize=(8,4)) # Default figsize is (8,6)
        pyplot.grid()
    
    simdur = 50.0
    h.tstop = simdur
    h.dt = 0.01
    
    start = timer()
    results = []
    for nseg in [5, 11, 51, 101] :
    
        print 'simulation with ', nseg, ' compartments in dendrite...'
    
        dend.nseg=nseg
    
        # add a synapse
        if args.synapse == 'exp':
            syn_ = h.ExpSyn(dend(0.5))
            syn_.tau = 2
        elif args.synapse == 'exp2':
            syn_ = h.Exp2Syn(dend(0.5))
            syn_.tau1 = 0.5 # artificially slow onset
            syn_.tau2 = 2
        else:
            raise RuntimeError('unrecognized synapse type')
    
        ncstim = h.NetCon(stim, syn_)
        ncstim.delay = 1 # 1 ms delay (arrives at 10ms)
        ncstim.weight[0] = 0.04 # NetCon weight is a vector
    
        ncstim1 = h.NetCon(stim, syn_)
        ncstim1.delay = 11 # 1 ms delay (arrives at 10ms)
        ncstim1.weight[0] = 0.04 # NetCon weight is a vector
        ncstim2 = h.NetCon(stim, syn_)
        ncstim2.delay = 31 # 1 ms delay (arrives at 10ms)
        ncstim2.weight[0] = 0.04 # NetCon weight is a vector
    
        # record voltages
        v_soma = h.Vector() # soma
        v_dend = h.Vector() # middle of dendrite
    
        v_soma.record( soma(0.5)._ref_v)
        v_dend.record( dend(0.5)._ref_v)
    
        # record spikes
        spike_counter_soma = h.APCount(soma(0.5))
        spike_counter_soma.thresh = 20
        spike_counter_dend = h.APCount(dend(0.5))
        spike_counter_dend.thresh = -10
    
        spikes_soma = h.Vector() # soma
        spikes_dend = h.Vector() # middle of dendrite
    
        spike_counter_soma.record(spikes_soma)
        spike_counter_dend.record(spikes_dend)
    
        # record time stamps
        t_vec = h.Vector()
        t_vec.record(h._ref_t)
    
        # finally it's time to run the simulation
        h.run()
    
        results.append(
            {
                "nseg": nseg,
                "dt" : h.dt,
                "measurements": {
                   "soma" : {
                       "thresh" :  spike_counter_soma.thresh,
                       "spikes" :  spikes_soma.to_python()
                   },
                   "dend" : {
                       "thresh" :  spike_counter_dend.thresh,
                       "spikes" :  spikes_dend.to_python()
                   },
               }
            }
        )
    
        if args.plot :
            pyplot.plot(t_vec, v_soma,  'k', linewidth=1, label='soma ' + str(nseg))
            pyplot.plot(t_vec, v_dend,  'b', linewidth=1, label='dend ' + str(nseg))
    
    # time the simulations
    end = timer()
    print "took ", end-start, " seconds"
    
    # save the spike info as in json format
    fp = open('simple_'+args.synapse+'_synapse.json', 'w')
    json.dump(results, fp, indent=2)
    
    if args.plot :
        pyplot.xlabel('time (ms)')
        pyplot.ylabel('mV')
        pyplot.xlim([0, simdur])
        pyplot.legend()
    
        pyplot.show()