from __future__ import print_function
"""
Created on Wed Apr 22 16:02:53 2015

Basic integrate-and-fire neuron 
R Rao 2007

translated to Python by rkp 2015
"""

import numpy as np
import matplotlib.pyplot as plt


# input current
I = 1 # nA

# capacitance and leak resistance
C = 1 # nF
R = 40 # M ohms

# I & F implementation dV/dt = - V/RC + I/C
# Using h = 1 ms step size, Euler method

tstop = 200
abs_ref = 5 # absolute refractory period 
V_th = 10 # spike threshold
is_spike = False
V_count = []

for I in np.arange(0.1, 1000, 0.1):
    V = 0
    V_len = 0
    V_trace = []  # voltage trace for plotting
    ref = 0 # absolute refractory period counter
    for t in range(tstop):
       if not ref:
           V = V - (V/(R*C)) + (I/C)
       else:
           ref -= 1
           V = 0.2 * V_th # reset voltage
       if V > V_th:
           V = 50 # emit spike
           ref = abs_ref # set refractory counter
           V_len += 1
       V_trace += [V]
    V_count.append(V_len)

print (max(V_count) / (tstop/1000))

plt.plot(np.arange(0.1, 1000, 0.1), V_count)
plt.show()
plt.plot(V_trace)
plt.show()
