-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenoitModel.jl
More file actions
115 lines (92 loc) · 4.03 KB
/
Copy pathBenoitModel.jl
File metadata and controls
115 lines (92 loc) · 4.03 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
module BenoitModel
using DiffEqNoiseProcess, JSON, Plots, StatsBase
include("./Oscilltrack.jl")
using .Oscilltrack: Oscilltracker, update!, get_phase, decide_stim
export BenoitModel, create_benoit_model, create_benoit_node, create_benoit_network, simulate_benoit_model
struct Node
tau_E::Float32
tau_I::Float32
w_EE::Float32
w_EI::Float32
w_IE::Float32
beta::Float32
WE
WI
end
struct Network
nodes::Array{Node, 1}
W::Matrix{Float32}
etta::Float32
noise_dev::Float32
stim_mag::Float32
end
struct NodeActivity
rE::Array{Float32, 1}
rI::Array{Float32, 1}
end
function create_benoit_model(N::Int64, W::Matrix{Float32}, etta::Float32, tau_E::Float32, tau_I::Float32, w_EE::Float32, w_EI::Float32, w_IE::Float32, beta::Float32, noise_dev::Float32, stim_mag::Float32)
nodes = [Node(tau_E, tau_I, w_EE, w_EI, w_IE, beta, WienerProcess(0.0,noise_dev, 1.0), WienerProcess(0.0,noise_dev, 1.0)) for _ in 1:N]
return Network(nodes, W, etta, noise_dev, stim_mag)
end
function create_benoit_node(tau_E::Float32, tau_I::Float32, w_EE::Float32, w_EI::Float32, w_IE::Float32, beta::Float32, noise_dev::Float32)
return Node(tau_E, tau_I, w_EE, w_EI, w_IE, beta, WienerProcess(0.0,noise_dev, 1.0), WienerProcess(0.0,noise_dev, 1.0))
end
function create_benoit_network(nodes, W::Matrix{Float32}, etta::Float32, noise_dev::Float32, stim_mag::Float32)
return Network([nodes[i] for i in 1:length(nodes)], W, etta, noise_dev, stim_mag)
end
function sigmoid(x, beta)
return 1.0 / (1.0 + exp(-beta * (x - 1)))
end
function simulate_benoit_model(N::Network, range_t, dt, theta_E, theta_I, oscilltracker, stimBlock)
# Initial conditions
Lt = length(range_t)
R = [NodeActivity(zeros(Lt), zeros(Lt)) for _ in N.nodes]
stim_delivered = zeros(Lt)
rWE = [zeros(Lt) for _ in N.nodes]
rWI = [zeros(Lt) for _ in N.nodes]
uWE = [nothing for _ in N.nodes];
pWE = [nothing for _ in N.nodes]; # for state-dependent distributions
uWI = [nothing for _ in N.nodes];
pWI = [nothing for _ in N.nodes];
for (j, n) in enumerate(N.nodes)
n.WE.dt = dt
n.WI.dt = dt
calculate_step!(n.WE, dt, uWE[j], pWE[j])
calculate_step!(n.WI, dt, uWI[j], pWI[j])
end
stimIndex = 0
# Simulate the model
for i in 2:Lt
for (j, n) in enumerate(N.nodes)
C = 0.0
for (k, _) in enumerate(N.nodes)
C += N.W[k,j] * R[k].rE[i-1]
end
C = N.etta * C / length(N.nodes)
drE = (dt / n.tau_E) * (-R[j].rE[i-1] + sigmoid(theta_E[j][i-1] + n.w_EE * R[j].rE[i-1] - n.w_IE * R[j].rI[i-1] + C, n.beta))
drI = (dt / n.tau_I) * (-R[j].rI[i-1] + sigmoid(theta_I[j][i-1] + n.w_EI * R[j].rE[i-1], n.beta))
accept_step!(n.WE, dt, uWE[j], pWE[j])
accept_step!(n.WI, dt, uWI[j], pWI[j])
if j == 1 && N.stim_mag > 0
update!(oscilltracker, R[j].rE[i-1])
if decide_stim(oscilltracker) && stimIndex == 0
stimIndex = 1
end
if stimIndex > 0
drE = drE + stimBlock[stimIndex] * N.stim_mag
stim_delivered[i] = stimBlock[stimIndex] * N.stim_mag
stimIndex +=1
end
if stimIndex > length(stimBlock)
stimIndex = 0
end
end
rWE[j][i] = n.WE[i]
rWI[j][i] = n.WI[i]
R[j].rE[i] = R[j].rE[i-1] + drE + N.noise_dev * (n.WE[i] - n.WE[i - 1])
R[j].rI[i] = R[j].rI[i-1] + drI + N.noise_dev * (n.WI[i] - n.WI[i - 1])
end
end
return R, stim_delivered
end
end