-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtxi-lfpInferenceEngine.cpp
More file actions
293 lines (233 loc) · 10.1 KB
/
rtxi-lfpInferenceEngine.cpp
File metadata and controls
293 lines (233 loc) · 10.1 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
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
#include <rtxi-lfpInferenceEngine.h>
#include <chrono>
using namespace std;
std::vector<int> PyList_toVecInt(PyObject* py_list);
extern "C" Plugin::Object *createRTXIPlugin(void){
return new rtxilfpInferenceEngine();
}
static DefaultGUIModel::variable_t vars[] = {
// Parameters
{ "Time Window (s)", "Time Window (s)", DefaultGUIModel::PARAMETER | DefaultGUIModel::DOUBLE},
{ "Sampling Rate (Hz)", "Sampling Rate (Hz)", DefaultGUIModel::STATE | DefaultGUIModel::DOUBLE,},
{ "LF Lower Bound", "LF Lower Bound", DefaultGUIModel::PARAMETER | DefaultGUIModel::DOUBLE,},
{ "LF Upper Bound", "LF Upper Bound", DefaultGUIModel::PARAMETER | DefaultGUIModel::DOUBLE,},
{ "HF Lower Bound", "HF Lower Bound", DefaultGUIModel::PARAMETER | DefaultGUIModel::DOUBLE,},
{ "HF Upper Bound", "HF Upper Bound", DefaultGUIModel::PARAMETER | DefaultGUIModel::DOUBLE,},
{ "Time Context", "Time Context", DefaultGUIModel::PARAMETER | DefaultGUIModel::INTEGER,},
//{ "FFT step", "FFT step", DefaultGUIModel::PARAMETER | DefaultGUIModel::INTEGER,},
{ "Animal", "Animal name", DefaultGUIModel::COMMENT, },
{ "Model", "Model name", DefaultGUIModel::COMMENT, },
// Inputs
{ "input_LFP", "Input LFP", DefaultGUIModel::INPUT | DefaultGUIModel::DOUBLE,},
//Outputs
{ "ratio", "Output LFP Power Ratio", DefaultGUIModel::OUTPUT | DefaultGUIModel::DOUBLE,},
{ "LF Power", "Power in LF Band", DefaultGUIModel::OUTPUT | DefaultGUIModel::DOUBLE,},
{ "HF Power", "Power in HF Band", DefaultGUIModel::OUTPUT | DefaultGUIModel::DOUBLE,},
{ "State", "Estimated State", DefaultGUIModel::OUTPUT | DefaultGUIModel::UINTEGER,}
};
static size_t num_vars = sizeof(vars) / sizeof(DefaultGUIModel::variable_t);
std::vector<int> my_vector;
std::chrono::time_point<std::chrono::system_clock> start, stop;
std::chrono::microseconds duration;
// defining what's in the object's constructor
// sampling set by RT period
rtxilfpInferenceEngine::rtxilfpInferenceEngine(void) : DefaultGUIModel("lfpInferenceEngine with Custom GUI", ::vars, ::num_vars),
period(((double)RT::System::getInstance()->getPeriod())*1e-9), // grabbing RT period
sampling(1.0/period), // calculating RT sampling rate
lfpratiometer(N, sampling), // constructing lfpRatiometer object
lfpinferenceengine(),
DEFAULT_ANIMAL("no-animal"),
DEFAULT_MODEL("no-model")
{
setWhatsThis("<p><b>lfpInferenceEngine:</b><br>Given an lfp input, this module estimates the cortical state.</p>");
animal = DEFAULT_ANIMAL;
model = DEFAULT_MODEL;
count = 1;
fftstep = 40;
//lfpinferenceengine.init(animal.toStdString(),model.toStdString());
DefaultGUIModel::createGUI(vars, num_vars);
customizeGUI();
update(INIT);
refresh();
QTimer::singleShot(0, this, SLOT(resizeMe()));
}
// defining what's in the object's destructor
rtxilfpInferenceEngine::~rtxilfpInferenceEngine(void) { }
// real-time RTXI function
void rtxilfpInferenceEngine::execute(void) {
// push new time series reading to lfpRatiometer
lfpratiometer.pushTimeSample(input(0)/0.01);
// calculate LF/HF ratio
lfpratiometer.calcRatio();
// put the LF/HF ratio into the output
output(0) = lfpratiometer.getRatio();
output(1) = lfpratiometer.getLFpower();
output(2) = lfpratiometer.getHFpower();
// estimate cortical state from FFT data
if (count == fftstep)
{
lfpratiometer.makeFFTabs();
fft = lfpratiometer.truncateFFT(lfpratiometer.getFreqs(),lfpratiometer.getFFTabs());
lfpinferenceengine.pushFFTSample(fft);
count = 1;
}
count++;
//state_vec = lfpinferenceengine.predict();
arguments_predict = {"pyfuncs","log_likes"};
pyArgs = {lfpinferenceengine.getModel(),
lfpinferenceengine.getFeats(),
lfpinferenceengine.getScaler(),
lfpinferenceengine.getData()};
lfpinferenceengine.callPythonFunction(arguments_predict, pyArgs);
lfpinferenceengine.load_ll();
//std::cout << "pi0: " << pi0 << endl;
//std::cout << "Ps: " << Ps << endl;
//std::cout << "ll: " << ll << endl;
state_vec = lfpinferenceengine.viterbi(lfpinferenceengine.getPi0(),
lfpinferenceengine.getPs(),
lfpinferenceengine.getLl());
state_vec = lfpinferenceengine.mapStates(state_vec);
//lfpinferenceengine.callPredictFunction(pyArgs);
//state_vec = PyList_toVecInt(lfpinferenceengine.getResult());
/*
printf("Result of call: \n");
my_vector = PyList_toVecInt(lfpinferenceengine.getResult());
for (int j = 0; j < my_vector.size(); j++) {
std::cout << my_vector[j] << " ";
}
std::cout << std::endl;
*/
if (!state_vec.empty()) {
state = state_vec.back();
}else{
state = -1;
}
output(3) = state;
}
// update function (not running in real time)
void rtxilfpInferenceEngine::update(DefaultGUIModel::update_flags_t flag)
{
//std::vector<int> my_vec;
switch (flag) {
case INIT:
setParameter("Time Window (s)", sampling/N);
setState("Sampling Rate (Hz)", sampling);
// get bounds from lfpratiometer object
setParameter("LF Lower Bound", lfpratiometer.getFreqBounds()[0]);
setParameter("LF Upper Bound", lfpratiometer.getFreqBounds()[1]);
setParameter("HF Lower Bound", lfpratiometer.getFreqBounds()[2]);
setParameter("HF Upper Bound", lfpratiometer.getFreqBounds()[3]);
setParameter("Time Context", lfpinferenceengine.getTimeContext());
setParameter("Animal",animal);
setParameter("Model",model);
break;
case MODIFY:
// defining parameters needed for constructor
period = ((double)RT::System::getInstance()->getPeriod())*1e-9;
sampling = 1.0/period;
setState("Sampling Rate (Hz)", sampling); // updating GUI
N = (int) (getParameter("Time Window (s)").toDouble() * sampling);
// making new FFT plan
lfpratiometer.changeFFTPlan(N, sampling);
// setting frequency bounds based on user input
lfpratiometer.setRatioParams(getParameter("LF Lower Bound").toDouble(),
getParameter("LF Upper Bound").toDouble(),
getParameter("HF Lower Bound").toDouble(),
getParameter("HF Upper Bound").toDouble());
start = std::chrono::high_resolution_clock::now();
lfpinferenceengine.setTimeContext(getParameter("Time Context").toInt());
lfpinferenceengine.init(getComment("Animal").toStdString(),getComment("Model").toStdString());
//pModel = Py_NewRef(lfpinferenceengine.getModel());
//pFeats = Py_NewRef(lfpinferenceengine.getFeats());
//pScaler = Py_NewRef(lfpinferenceengine.getScaler());
//pData = Py_NewRef(lfpinferenceengine.getData());
//arguments_predict = {"pyfuncs","predict"};
//start = std::chrono::high_resolution_clock::now();
//state_vec = lfpinferenceengine.predict();
arguments_predict = {"pyfuncs","log_likes"};
pyArgs = {lfpinferenceengine.getModel(),
lfpinferenceengine.getFeats(),
lfpinferenceengine.getScaler(),
lfpinferenceengine.getData()};
//PyRun_SimpleString("print(lfpinferenceengine.getData().size())");
lfpinferenceengine.callPythonFunction(arguments_predict, pyArgs);
lfpinferenceengine.load_ll();
//std::cout << "pi0: " << lfpinferenceengine.getPi0() << endl;
//std::cout << "Ps[0]: " << lfpinferenceengine.getPs()[0] << endl;
//lfpinferenceengine.reportPs();
//std::cout << "ll: " << lfpinferenceengine.getLl() << endl;
state_vec = lfpinferenceengine.viterbi(lfpinferenceengine.getPi0(),
lfpinferenceengine.getPs(),
lfpinferenceengine.getLl());
state_vec = lfpinferenceengine.mapStates(state_vec);
//state_vec = PyList_toVecInt(lfpinferenceengine.getResult());
//my_vec = PyList_toVecInt(lfpinferenceengine.getResult());
stop = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
std::cout << "Predict time: " << duration.count() << std::endl;
printf("Result of call: \n");
//my_vector = PyList_toVecInt(lfpinferenceengine.getResult());
for (int j = 0; j < state_vec.size(); j++) {
std::cout << state_vec[j] << " ";
}
std::cout << std::endl;
// setting DFT windowing function choice
if (windowShape->currentIndex() == 0) {
lfpratiometer.window_rect();
}
else if (windowShape->currentIndex() == 1) {
lfpratiometer.window_hamming();
}
// clearing time series
lfpratiometer.clrTimeSeries();
break;
case UNPAUSE:
break;
case PAUSE:
//lfpinferenceengine.reportFFTdata();
lfpratiometer.clrTimeSeries();
break;
case PERIOD:
break;
default:
break;
}
}
// RTXI's customizeGUI function
void rtxilfpInferenceEngine::customizeGUI(void)
{
QGridLayout* customlayout = DefaultGUIModel::getLayout();
// adding dropdown menu for choosing FFT window shape
windowShape = new QComboBox;
windowShape->insertItem(1, "Rectangular");
windowShape->insertItem(2, "Hamming");
customlayout->addWidget(windowShape, 2, 0);
setLayout(customlayout);
}
std::vector<int> PyList_toVecInt(PyObject* py_list) {
if (PySequence_Check(py_list)) {
PyObject* seq = PySequence_Fast(py_list, "expected a sequence");
if (seq != NULL){
std::vector<int> my_vector;
my_vector.reserve(PySequence_Fast_GET_SIZE(seq));
for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); i++) {
PyObject* item = PySequence_Fast_GET_ITEM(seq,i);
if(PyNumber_Check(item)){
Py_ssize_t value = PyNumber_AsSsize_t(item, PyExc_OverflowError);
if (value == -1 && PyErr_Occurred()) {
//handle error
}
my_vector.push_back(value);
} else {
//handle error
}
}
Py_DECREF(seq);
return my_vector;
} else {
//handle error
}
}else{
//handle error
}
}