"""
example4 -- synaptic delays

Demonstrates synaptic delays.

The matplotlib code here is more complex than in most of the examples.
"""

from snnet import *
from matplotlib.font_manager import FontProperties

# set max delay property on the integrator
# could also set dt, etc...
integrator = fast_xpp_integrator
integrator.delay = 70

def simulation():
    n = neurons('hh_syn.snnet')
    others = neurons('hh_syn.snnet', 3)
    n.gsyn = 0
    others.gsyn = 0
    others.iapp = 0
    others.v = -66
    noise = exponentially_decaying_noise(firing_rate=5, decay_rate=2, delay=1)
    
    n.connect(noise, synapse=ion_synapse(.1, 0))
    for d in [0, 1, 2]:
        others[d].connect(n, synapse=ion_synapse(.1, 0, 's', delay=(d * 20)))
    advance(300)
    
data = run(simulation, integrator=integrator)

a = axes(frameon=False)
ylabel('Membrane Potential (mV)')
title('Test of Synaptic Delays')
xticks([])
yticks([])

top_axes = gcf().add_subplot(2, 1, 1)
plot(data.t, data.v0, color=[0, 0, 0])
title('Signal', fontsize=10)
bottom_axes = gcf().add_subplot(2, 1, 2)
plot(data.t, data.v1, label='delay: 00 ms')
plot(data.t, data.v2, label='delay: 10 ms')
plot(data.t, data.v3, label='delay: 20 ms')
legend(prop=FontProperties(size=8))
xlabel('t (ms)')
title('Response', fontsize=10)

# shift plots for better spacing, adjust tick label size
for a in [bottom_axes, top_axes]:
    p = a.get_position()
    p.x0 += .07
    p.y1 -= .05
    a.set_position(p)
    
    for tick in a.get_xticklabels() + a.get_yticklabels():
        tick.set_size('8')



savefig('example4.pdf')

