11 #include "../CEBLModel.hpp"
12 #include "../cppR/cppR.hpp"
24 this->selected_components_string =
"(not set)";
25 this->selected_components_valid =
false;
26 this->num_components = 0;
27 this->num_expected_channels = 0;
28 this->num_expected_lags = 0;
29 this->num_channels = 0;
30 this->trained =
false;
33 for(
unsigned int i=0; i<paths.size();i++)
35 string path = paths[i] +
"plugins/filters/";
38 plugin_loader->
loadDir(path.c_str());
77 if(selected_filter ==
"")
95 return selected_components;
102 this->num_channels = data.
size1();
105 string filter = this->selected_filter;
108 throw PluginException(
"Extracting filtering components: No filter selected.");
110 if(data.
nrow() == 0 || data.
ncol() == 0)
112 throw DataException(
"Extracting filtering components: No data recorded in EEG data buffer.");
118 this->num_components = ublas::matrix<double>(data).size1();
126 throw PluginException(
"Failed to extract components using filter: " + filter);
136 return selected_components_string;
143 return selected_components_valid;
156 if(data.
size1() != this->num_expected_channels)
158 throw PluginException(
"Filter cannot be applied because the number of channels does not match the filter.");
161 if(num_lags != this->num_expected_lags)
163 throw PluginException(
"Filter cannot be applied because the number of lags has changed.");
166 filtered = plugin_loader->
getPlugin(this->selected_filter)->
apply(filtered);
179 return num_expected_channels;
208 this->selected_filter =
filter;
215 this->trained =
false;
216 std::vector<int> selected_components = this->selected_components;
217 for(
unsigned int i=0; i<selected_components.size();i++)
219 selected_components[i]--;
222 if(selected_components[selected_components.size()-1] > num_components
223 || selected_components[0] < 0)
225 throw PluginException(
"Training filter failed because selected components are out of range.");
229 this->num_expected_channels = training_data.
size1();
230 this->num_expected_lags = num_lags;
234 filter = this->selected_filter;
236 plugin_loader->
getPlugin(filter)->
make(training_data, selected_components);
237 this->trained =
true;
244 const char * components = comp_string.c_str();
245 std::vector<int> comp;
246 int MAX_VALUE = num_channels * (this->num_lags+1);
247 num_components = MAX_VALUE;
254 char buffer[25] =
"";
266 current = components[location++];
272 if(current >=
'0' && current <=
'9')
274 buffer[buffer_loc++] = current;
289 current = components[location++];
295 if(current >=
'0' && current <=
'9')
297 buffer[buffer_loc++] = current;
301 else if(current ==
',')
303 buffer[buffer_loc] =
'\0';
304 int val = atoi(buffer);
305 if(val > MAX_VALUE || val < MIN_VALUE)
314 else if(current ==
':')
316 colon_start = atoi(buffer);
317 if(colon_start > MAX_VALUE || colon_start < MIN_VALUE)
325 else if(current ==
'\0')
327 buffer[buffer_loc] =
'\0';
328 int val = atoi(buffer);
329 if(val > MAX_VALUE || val < MIN_VALUE)
347 current = components[location++];
353 if(current >=
'0' && current <=
'9')
355 buffer[buffer_loc++] = current;
367 current = components[location++];
374 if(current >=
'0' && current <=
'9')
376 buffer[buffer_loc++] = current;
380 else if(current ==
',')
382 buffer[buffer_loc] =
'\0';
383 int val = atoi(buffer);
384 if(val > MAX_VALUE || val < MIN_VALUE )
389 if(val > colon_start)
390 for(
int i=colon_start; i<= val; i++)
393 for(
int i=val; i<= colon_start; i++)
398 else if(current ==
'\0')
400 buffer[buffer_loc] =
'\0';
401 int val = atoi(buffer);
402 if(val > MAX_VALUE || val < MIN_VALUE)
407 if(val > colon_start)
408 for(
int i=colon_start; i<= val; i++)
411 for(
int i=val; i<= colon_start; i++)
433 std::sort(comp.begin(), comp.end());
434 std::vector<int>::iterator new_end =
436 comp.erase(new_end, comp.end());
440 this->selected_components.clear();
441 for(
unsigned int i=0; i < comp.size()-1; i++)
443 this->selected_components.push_back(comp[i]);
444 s << comp[i] <<
", ";
446 this->selected_components.push_back(comp[comp.size()-1]);
447 s << comp[comp.size()-1];
450 bool comp_num_okay =
false;
451 if(this->selected_components.size() <= unsigned(MAX_VALUE)
452 && this->selected_components.size() > 0)
454 if(this->selected_components[0] > 0
455 && this->selected_components[0] <= MAX_VALUE
456 && this->selected_components[this->selected_components.size()-1] > 0
457 && this->selected_components[this->selected_components.size()-1] <= MAX_VALUE)
459 comp_num_okay =
true;
462 this->selected_components_valid = comp_num_okay;
465 this->selected_components_string = s.str();
469 this->selected_components_string =
"(value out of range)";
474 this->selected_components_valid =
false;
475 this->selected_components_string =
"(error parsing input)";