-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathizhikevich.rs
155 lines (129 loc) · 3.78 KB
/
izhikevich.rs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2019 Libgirl
//
// Released under Apache 2.0 license as described in the file LICENSE.txt.
use crate::events::{Event, SpikeEvent};
use crate::models::{Neuron, NeuronActivity};
use crate::network::Network;
use crate::ode::rk4;
use crate::{Double, Parameters, Time};
#[derive(Debug)]
pub struct Model {
pub a: Double,
pub b: Double,
pub c: Double,
pub d: Double,
pub v: Double,
pub u: Double,
v_th: Double,
i_e: Double,
spikes: Double,
nid: i64,
}
impl Model {
pub fn parameters() -> Parameters {
let mut params = Parameters::new();
params.insert("v_m".to_string(), -70.);
params.insert("a".to_string(), 0.02);
params.insert("b".to_string(), 0.2);
params.insert("c".to_string(), -65.);
params.insert("d".to_string(), 6.);
params.insert("i_e".to_string(), 0.);
params.insert("v_th".to_string(), 30.);
params
}
fn get_or_default(params: &Parameters, key: &str) -> f64 {
let default_params = Model::parameters();
params
.get(key)
.cloned()
.unwrap_or(*default_params.get(key).unwrap())
}
pub fn new(params: &Parameters) -> Model {
let v_m = Model::get_or_default(params, "v_m");
let a = Model::get_or_default(params, "a");
let b = Model::get_or_default(params, "b");
let c = Model::get_or_default(params, "c");
let d = Model::get_or_default(params, "d");
let i_e = Model::get_or_default(params, "i_e");
let v_th = Model::get_or_default(params, "v_th");
Model {
a: a,
b: b,
c: c,
d: d,
v: v_m,
u: b * v_m,
v_th: v_th,
i_e: i_e,
spikes: 0.,
nid: -1,
}
}
fn get_spike(&mut self, _lag: Double) -> Double {
let spikes = self.spikes;
self.spikes = 0.;
spikes
}
fn add_spike(&mut self, e: &SpikeEvent) {
self.spikes += e.weight() * e.multiplicity();
}
}
impl Default for Model {
fn default() -> Model {
let mut params = Parameters::new();
params.insert("v_m".to_string(), -70.);
Model::new(¶ms)
}
}
impl Neuron for Model {
fn name(&self) -> String {
String::from("Izhikevich")
}
fn set_params(&mut self, _params: &Parameters) {}
fn update(&mut self, t: Double) -> NeuronActivity {
let mut v = self.v;
let mut u = self.u;
let a = self.a;
let b = self.b;
let i_e = self.i_e;
let i_syn = self.get_spike(t);
let dt = Network::resolution();
let d_v = move |y: Double| 0.04 * y * y + 5.0 * y + 140. - b * u + i_syn + i_e;
v += rk4(d_v, v, dt);
let d_u = move |y: Double| a * (v - y);
u += rk4(d_u, u, dt);
let mut activity = NeuronActivity::Silent;
if v > self.v_th {
let se = SpikeEvent::new();
activity = NeuronActivity::Fires(se);
v = self.c;
u += self.d;
}
self.v = v;
self.u = u;
activity
}
fn handle_spike(&mut self, event: SpikeEvent) {
self.add_spike(&event);
}
fn set_neuron_id(&mut self, nid: i64) {
self.nid = nid;
}
fn neuron_id(&self) -> i64 {
self.nid
}
fn new_spike_record(&mut self) {}
fn set_spike_recording(&mut self, _is_on: bool) {}
fn clear_spike_records(&mut self) {}
fn get_spike_records(&self) -> Vec<Vec<Time>> {
Vec::new()
}
fn get_property(&self, name: String) -> Double {
match name.as_ref() {
"v" => self.v,
"v_th" => self.v_th,
_ => 0.,
}
}
fn set_property(&mut self, _name: String, _value: Double) {}
}