-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspike_predictor.cpp
347 lines (283 loc) · 10.4 KB
/
spike_predictor.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Copyright (C) 2022 Grupo de Neurocomputacion Biologica, Departamento de
* Ingenieria Informatica, Universidad Autonoma de Madrid.
*
* Authors:
* Garrido-Peña, Alicia
* Reyes-Sanchez, Manuel
* Sanchez-Martin, Pablo
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* This module implementation is derived from DefaultGUIModel with a custom GUI.
*/
/*
* Module to predict spike activity in intracellular recordings.
* This module predicts in real-time the spike time based on the previous spikes.
* It is based in 3 different algorithms:
* Threshold by Voltage value
* Threshold by Voltage area
* Threshold by Slope value
*
*/
#include "spike_predictor.h"
#include <iostream>
#include <main_window.h>
#include <math.h>
extern "C" Plugin::Object*
createRTXIPlugin(void)
{
return new SpikePredictor();
}
static DefaultGUIModel::variable_t vars[] = {
/*Module variables*/
{"Living neuron", "Signal input to analize", DefaultGUIModel::INPUT,},
{"Integrate init input (V)", "Voltage value to reset sum", DefaultGUIModel::INPUT,},
{"Firing threshold (V)", "Threshold to declare spike beggining", DefaultGUIModel::PARAMETER,},
{"Backtime (ms)", "Time before max that define Calculated threshold", DefaultGUIModel::PARAMETER,},
{"N Points Filter", "Number of points for the filter", DefaultGUIModel::PARAMETER,},
{"N Points Slope", "Number of points for the slope", DefaultGUIModel::PARAMETER,},
{"Sum init (V)", "Voltage value to reset accumulated sum", DefaultGUIModel::PARAMETER,},
{"Accumulated sum threshold", "Value of the accumulated sum that triggers x (if >=0 calculates threshold)", DefaultGUIModel::PARAMETER,},
{"Accumulated sum threshold error", "Allowed error for v-sum_reset (recommended 0.003)", DefaultGUIModel::PARAMETER,},
{"Slope threshold", "Value of the slope that triggers the state (if -1000 calculates threshold)", DefaultGUIModel::PARAMETER,},
{"Filtered signal", "Filter", DefaultGUIModel::OUTPUT,},
{"Calculated threshold", "Calculated threshold", DefaultGUIModel::OUTPUT,},
{"Calculated slope", "Calculated slope", DefaultGUIModel::OUTPUT,},
{"Calculated sum threshold", "Calculated Accumulated sum threshold", DefaultGUIModel::OUTPUT,},
{"Slope output", "Slope value", DefaultGUIModel::OUTPUT,},
{"Sum output", "Accumulated sum value as an output", DefaultGUIModel::OUTPUT,},
{"Crossed Sum State", "Whether the sum has surpased the threshold", DefaultGUIModel::OUTPUT,},
{"Crossed Voltage State", "Whether the voltage has surpased the threshold", DefaultGUIModel::OUTPUT,},
{"Crossed Slope State", "Whether the sum has surpased the threshold", DefaultGUIModel::OUTPUT,},
{"Calculated threshold state", "Calculated threshold", DefaultGUIModel::STATE,},
{"Calculated slope state", "Calculated slope", DefaultGUIModel::STATE,},
{"Sum init input (V)", "Minimum voltage sum", DefaultGUIModel::STATE,},
{"Min sum", "Minimum voltage sum", DefaultGUIModel::STATE,},
{"Calculated sum threshold state", "Calculated threshold for sum", DefaultGUIModel::STATE,},
{"Accumulated sum", "Accumulated voltage sum", DefaultGUIModel::STATE,},
};
static size_t num_vars = sizeof(vars) / sizeof(DefaultGUIModel::variable_t);
SpikePredictor::SpikePredictor(void)
: DefaultGUIModel("Spike Predictor", ::vars, ::num_vars)
{
setWhatsThis("<p><b>Spike Predictor:</b><br>Module for spike prediction based on a threshold by voltage, area or slope.</p>");
DefaultGUIModel::createGUI(vars,
num_vars); // this is required to create the GUI
customizeGUI();
initParameters();
update(INIT); // this is optional, you may place initialization code directly
// into the constructor
refresh(); // this is required to update the GUI with parameter and state
// values
QTimer::singleShot(0, this, SLOT(resizeMe()));
}
SpikePredictor::~SpikePredictor(void)
{
}
double
SpikePredictor::filter(std::vector<double> signal, int cycle, double v, int n_points)
{
// No filter case
if (n_points == 0)
return v;
// Weighted average for each point and n previous
double fv = v*0.3;
double perc = 0.7/n_points;
int indx;
for (int i=1; i <= n_points; i++)
{
indx = (vector_size + cycle - i) % vector_size;
fv += signal[indx]*perc;
}
return fv;
}
double
SpikePredictor::calculate_slope(double x1, double x2, double dt)
{
return (x2-x1)/-dt;
}
void
SpikePredictor::execute(void)
{
double v = input(0);
int backtime_points = backtime / period;
sum_reset = input(1);
int allow_reset = 1;
/*SAVE NEW DATA*/
// v_list[cycle] = v;
// filter signal --> if n points filter > 0 v modified
double v_filtered = filter(v_list,cycle,v,n_points);
v_list[cycle] = v_filtered;
output(0) = v_filtered;
// int n_p_slope = 15;
// Calculate slope
double x1 = v_list[(vector_size + cycle) % vector_size];
double x2 = v_list[(vector_size + cycle-n_p_slope) % vector_size];
curr_slope = calculate_slope(x1, x2, n_p_slope*period);
output(4) = curr_slope;
// Spike detection
/*OVER THE THRESHOLD*/
if (v > th_spike && switch_th == true){
if (v < v_list[cycle-3]){
n_spikes++;
/*SPIKE DETECTED*/
if (backtime < 0)
{
updatable = true;
// cycle --;
}
// Save threshold values for next spike
// Get threshold for V
switch_th = false;
th_calculated = v_list[(vector_size + cycle - backtime_points) % vector_size];
output(1) = th_calculated;
// Get slope
x1 = v_list[(vector_size + cycle - backtime_points ) % vector_size];
x2 = v_list[(vector_size + cycle - backtime_points -n_p_slope) % vector_size];
sl_calculated = calculate_slope(x1, x2, n_p_slope*period);
output(2) = sl_calculated;
//Get threshold for sum
th_sum_calculated = sum_list[(vector_size + cycle - backtime_points) % vector_size];
//Get threshold by mean of 3 last spikes.
//TODO: define lim of buffer and size. (use more points ¿?)
th_sum_buff[n_spikes%10] = th_sum_calculated;
th_sum_calculated = (th_sum_calculated + th_sum_buff[(n_spikes%10)-1] + th_sum_buff[(n_spikes%10)-2])/3;
allow_reset = 1;
}
}
// Hiperpolarization --> spike detection ON again
if(switch_th==false && v < th_spike){
switch_th = true;
// if (backtime < 0)
// updatable = false;
}
// sum_reset = input(1);
if(sum_reset != 0){
sum_reset_param = sum_reset;
}else //If there is no input get default
sum_reset = sum_reset_param;
if(allow_reset and ((v - sum_reset) < sum_error)){ //Voltage at reset value
// Reset sum
sum = 0;
allow_reset = 0;
}
//increase accumulated sum
sum += v;
sum_list[cycle] = sum;
output(5) = sum;
if (th_sum_param < 0) //If param modified
th_sum_calculated = th_sum_param;
output(3) = th_sum_calculated;
if (slope_th_param < -1000) //If param modified
sl_calculated = slope_th_param;
// minimum sum value, control state variable
if (sum < sum_min)
sum_min = sum;
// Update states
if (updatable)
{
// output(6) = sum <= th_sum_param; //Area threshold crossed
output(6) = sum < th_sum_calculated; //Area threshold crossed
output(7) = v > th_calculated; //Voltage threshold crossed
output(8) = curr_slope > sl_calculated; //Current threshold crossed
}
else
{
output(6) = 0;
output(7) = 0;
output(8) = 0;
}
/*NEXT CYCLE*/
cycle++;
if (vector_size == cycle){
cycle = 0;
}
return;
}
void
SpikePredictor::initParameters(void)
{
//TODO fix for other period times
vector_size = 10*4000; // 10 reads per ms * 100 ms buffer
// vector_size = 100 / RT::System::getInstance()->getPeriod() * 1e-6; // 0.1 ms per read * 100 ms buffer
cycle = 0;
v_list.resize(vector_size, 0);
sum_list.resize(vector_size, 0);
switch_th = false;
backtime = 0;
th_spike = 0;
n_points = 0;
n_p_slope = 0;
th_calculated = 0;
th_sum_calculated = -0.05;
sl_calculated = 0;
th_sum_buff.resize(vector_size, 0);
sum = 0;
sum_reset_param = -0.05;
th_sum_param = 5;
sum_error = 0.003;
sum_min = 100;
slope_th_param = -1000;
sum_reset = 0;
updatable = true;
n_spikes = 0;
}
void
SpikePredictor::update(DefaultGUIModel::update_flags_t flag)
{
switch (flag) {
case INIT:
period = RT::System::getInstance()->getPeriod() * 1e-6; // ms
setParameter("Firing threshold (V)", th_spike);
setParameter("Backtime (ms)", backtime);
setParameter("N Points Filter", n_points);
setParameter("N Points Slope", n_p_slope);
setParameter("Accumulated sum threshold", th_sum_param);
setParameter("Accumulated sum threshold error", sum_error);
setParameter("Sum init (V)", sum_reset_param);
setParameter("Slope threshold", slope_th_param);
setState("Calculated threshold state", th_calculated);
setState("Calculated sum threshold state", th_sum_calculated);
setState("Calculated slope state", sl_calculated);
setState("Accumulated sum", sum);
setState("Min sum", sum_min);
setState("Sum init input (V)", sum_reset);
break;
case MODIFY:
th_spike = getParameter("Firing threshold (V)").toDouble();
backtime = getParameter("Backtime (ms)").toDouble();
n_points = getParameter("N Points Filter").toDouble();
n_p_slope = getParameter("N Points Slope").toDouble();
sum_reset_param = getParameter("Sum init (V)").toDouble();
th_sum_param = getParameter("Accumulated sum threshold").toDouble();
sum_error = getParameter("Accumulated sum threshold error").toDouble();
slope_th_param = getParameter("Slope threshold").toDouble();
break;
case UNPAUSE:
break;
case PAUSE:
break;
case PERIOD:
period = RT::System::getInstance()->getPeriod() * 1e-6; // ms
break;
default:
break;
}
}
void
SpikePredictor::customizeGUI(void)
{
}