-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuron.py
94 lines (78 loc) · 2.77 KB
/
neuron.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import numpy as np
import matplotlib.pyplot as plt
from configs import LIFneuronConfig, IzhikevichConfig
import math
class LIF():
def __init__(self):
LIFneuronConfig.__init__(self,)
self.num=0
self.isNoise = 0
self.vprev = self.v_base
def noiseTerm(self,dt):
sigma = self.noise_amp
y = np.random.normal(0,1,1)
return sigma*math.sqrt(dt)*y
def generateSpiking(self, I, t, dt):
v = self.v_base
if t >= self.initRefrac:
noise = 0
if self.isNoise:
noise = self.noiseTerm(dt)
v = self.vprev + (-self.vprev + I*self.R) / self.tau_m * dt +noise
if v >= self.v_thresh:
self.num+=1
v += self.v_spike
self.initRefrac = t + self.refracTime
self.vprev = v
return v
class Izhikevich():
def __init__(self,):
IzhikevichConfig.__init__(self,)
# #parameters that define the neuron model
self.uv = []
#Initial conditions
self.vprev = self.c #resting potential
# self.u = self.u0 #recovery variable
self.uprev = self.u0
#integrate the model over one step
def generateSpiking(self,input_current,t,dt):
#find the next value of the membrane voltage
# print(self.vprev)
v = self.vprev + dt*((self.A*self.vprev**2 + self.B*self.vprev - self.uprev + self.C + (input_current/self.Cm)))
#find the next value of the recovery variable
u = self.uprev + dt*self.a*((self.b * self.vprev) - self.uprev)
#spike event
#if a spike is generated
self.uv.append(u)
if v >= 30:
self.vprev = self.c #reset membrane voltage
v = 31
u = u + self.d #reset recovery variable
self.uv.append(u)
else:
self.vprev = v
self.uprev = u
# print(self.vprev)
# print(v)
return v
def runNeuron(model, t_span, dt, I):
v = np.zeros_like(t_span)
for i, t in enumerate(t_span):
v[i] = model.generateSpiking(I[i], t, dt)
if model.isPlot:
plt.plot(t_span,v, label = 'V')
plt.plot(t_span,I, label = 'I')
plt.title('Neuron Model')
plt.ylabel('Membrane Potential (V) and input current(I)')
plt.xlabel('Time (msec)')
plt.grid()
plt.legend(loc="upper right")
plt.show()
return v
if __name__=='__main__':
t_tot = 1000
dt = 0.01
t_span = np.arange(0, t_tot+dt, dt)
I = [1 if 200/dt <= i <= 600/dt else 10 for i in range(len(t_span))]
neuron = Izhikevich()
v = runNeuron(neuron, t_span, dt, I)