"""
example2 for snnet.

Demonstrates: noise, fast_xpp_integrator

Could alternatively use matlab_integrator. The others do not support noise.

Uses matplotlib for the graphics.

Note that fast_xpp_integrator requires OSX or Linux.

Robert A McDougal, 2011
"""

from snnet import *

# define the simulation
def simulation():
    # create 3 Hodgkin-Huxley cells, set iapp
    h_cells = neurons('hh.snnet', 3)
    
    # create 30 noise sources
    noise = exponentially_decaying_noise(30, firing_rate=5, decay_rate=2)
    
    # 1 more, now square wave and periodic
    noise2 = square_wave_noise(1, firing_rate=periodic(5))
    
    # connect the first 2 cells to the exp noise (10 noise cells per HH cell)
    # to connect with a certain probability, use p= instead of n=
    h_cells[0, 1].connect(noise, n=10, synapse=ion_synapse(gsyn='.07', vsyn='0'))

    # connect the 3rd cell to the square wave noise
    h_cells[2].connect(noise2, synapse=ion_synapse(gsyn='10', vsyn='0'))
    
    run_to(800)

# run the simulation, keep the data to work with it locally (below)
data = run(simulation, integrator=fast_xpp_integrator)

#
# loop over all three cells, plot each in a separate subplot
#

# here we test indexing access. For a cleaner way to plot individual
# variables, see test1
for i in xrange(3):
    subplot(3, 1, i + 1)
    plot(data.t, data['v', i])
    ylabel('Cell %d' % i)

subplot(3, 1, 3)
xlabel('Time (ms)')

savefig('example2.pdf')
