CEBL  2.1
CEBLModel.cpp
Go to the documentation of this file.
1 #include "CEBLModel.hpp"
2 #include "model/FileUtils.hpp"
4 #include "model/DeviceConfig.hpp"
6 #include "model/FilterConfig.hpp"
10 #include "model/Training.hpp"
12 #include "model/DataSource.hpp"
13 #include "model/DataProcess.hpp"
14 #include "model/SessionManager.hpp"
15 #include "model/Preferences.hpp"
16 #include "model/StringTable.hpp"
17 #include "model/DataIO.hpp"
18 #include "CompiledStrings.hpp"
19 
20 //----------------------------------------------------------------------
21 // Constructors / Destructors
22 
24 {
25  //set this to false to begin with
26  this->initialized_successfully = false;
27 
28  //initialize preferences
29  preferences = new Preferences();
30 }
31 
32 void CEBLModel::InitModel(int ac, char ** av)
33  {
34  //set this to false to begin with
35  this->initialized_successfully = false;
36 
37  //parse command line options
38  if(!preferences->processCL(ac,av))
39  {
40  return;
41  }
42  //init cebl
43  preferences->initCEBL();
44 
45  //create all internal objects
46  try
47  {
48  //initialize string table
49  string_table = new StringTable();
50  string_table->loadFromString(string_table_en);
51  //string_table->loadFromFile(preferences->getStringTableFilename());
52 
53 
54  channels = new ChannelsConfig(this);
55  device = new DeviceConfig(this);
56  file_data_stream_config = new FileDataStreamConfig(this);
57  filters = new FilterConfig(this);
58  features = new FeaturesConfig(this);
59  decisions = new DecisionConfig(this);
60  classifiers = new ClassifiersConfig(this);
61  training = new Training(this);
62  realtime = new RealTimeClassification(this);
63  data_source = new DataSource(this);
64  data_process = new DataProcess(this);
65  session_manager = new SessionManager(this);
66  }
67  catch(exception &e)
68  {
69  cerr << "Exception occured when trying to initialize the CEBL model: " << e.what() << "\n";
70  return;
71  }
72 
73  //now load session file, if specified
74  if(preferences->getSessionFilename() != "")
75  {
76  try
77  {
78  sessionLoad(preferences->getSessionFilename());
79  }
80  catch(...)
81  {
82  cerr << "Failed to load session file " << preferences->getSessionFilename() << "\n";
83  }
84  }
85  //if we have gotten this far, allow cebl to continue
86  this->initialized_successfully = true;
87 }
88 
89 
91 {
92  delete channels;
93  delete device;
94  delete file_data_stream_config;
95  delete filters;
96  delete features;
97  delete decisions;
98  delete classifiers;
99  delete training;
100  delete realtime;
101  delete data_source;
102  delete data_process;
103  delete session_manager;
104  delete preferences;
105  delete string_table;
106 }
107 
108 
109 
110 //======================================================================
111 
112 
113 void CEBLModel::preferencesProcessCL(int ac, char ** av)
114 {
115  preferences->processCL(ac,av);
116 }
117 
119 {
120  preferences->initCEBL();
121 }
122 
123 std::vector<std::string> CEBLModel::preferencesGetPaths()
124 {
125  return preferences->getPaths();
126 }
127 
129  string description,
130  char short_name)
131 {
132  preferences->addOption(name,description,short_name);
133 }
134 
136 {
137  return preferences->getOption(name);
138 }
139 
140 //======================================================================
141 // STRING TABLE
142 
143 const char * CEBLModel::getString(string name)
144 {
145  try
146  {
147  return string_table->getString(name);
148  }
149  catch(exception &e)
150  {
151  cerr << e.what();
152  return "";
153  }
154 }
155 
156 //======================================================================
157 // DATA SOURCE
158 
159 
160 
161 
162 //----------------------------------------
163 //GETTING OPERATIONS
164 
165 
167 {
168  EEGData ret = data_source->readAll();
169  return ret;
170 }
171 
173 {
174  EEGData ret = data_source->readAll();
175  ret = data_process->process(ret);
176  return ret;
177 }
178 
180 {
181  EEGData ret = data_source->readAll();
182  return ret;
183 }
184 
186 {
187  EEGData ret = data_source->readAll();
188  ret = data_process->process(ret);
189  return ret;
190 }
191 
193 {
194  return data_source->samplesAvailable();
195 }
196 
197 std::vector<string> CEBLModel::dataGetSources()
198 {
199  return data_source->getSources();
200 }
201 
203 {
204  return data_source->getSource();
205 }
206 
208 {
209  return data_source->sourceReady();
210 }
211 
213 {
214  return data_source->isStarted();
215 }
216 
218 {
219  return data_source->getStoreFlag();
220 }
221 
223 {
224  return data_source->getStoreNumSamples();
225 }
226 
228 {
229  return data_source->getStoredData();
230 }
231 
232 
233 //----------------------------------------
234 //SETTING OPERATIONS
235 
236 
238 {
239  data_source->clearStoredData();
240 }
241 
243 {
244  data_source->setStoreFlag(flag);
245 }
246 
248 {
249  data_source->setSource(n);
250 }
251 
252 void CEBLModel::dataSetSource(string source)
253 {
254  data_source->setSource(source);
255 }
256 
258 {
259  data_source->clearSamples();
260 }
261 
263 {
264  data_source->start();
265 }
266 
268 {
269  data_source->stop();
270 }
271 
272 
273 
274 
275 //======================================================================
276 // DATA PROCESS
277 
278 
279 
280 
281 //----------------------------------------
282 //GETTING OPERATIONS
283 
284 
286 {
287  return data_process->getReferenceEnabled();
288 }
289 
291 {
292  return data_process->getRemoveEnabled();
293 }
294 
296 {
297  return data_process->getFilterEnabled();
298 }
299 
300 
301 //----------------------------------------
302 //SETTING OPERATIONS
303 
304 
306 {
307  data_process->setReferenceEnabled(enabled);
308 }
309 
311 {
312  data_process->setRemoveEnabled(enabled);
313 }
314 
316 {
317  data_process->setFilterEnabled(enabled);
318 }
319 
320 
321 //----------------------------------------
322 //PROCESS DATA OPERATIONS
323 
325 {
326  return data_process->process(data);
327 }
328 
330 {
331  return data_process->process(data);
332 }
333 
334 EEGData& CEBLModel::processData(EEGData &data, bool remove_disabled, bool reference, bool filter)
335 {
336  return data_process->process(data, remove_disabled, reference, filter);
337 }
338 
339 
340 
341 
342 //======================================================================
343 // CHANNELS
344 
345 
346 
347 
348 //----------------------------------------
349 //GETTING OPERATIONS
350 
351 
353 {
354  return channels->getCurrentFilename();
355 }
356 
358 {
359  return FileUtils::fileExists(filename);
360 }
361 
363 {
364  return channels->getElectrodeName(electrode);
365 }
366 
368 {
369  return channels->getElectrodeReference(electrode);
370 }
371 
373 {
374  return channels->getElectrodeEnabled(electrode);
375 }
376 
378 {
379  return channels->getMaxNumChannels();
380 }
381 
383 {
384  return channels->getNumEnabled();
385 }
386 
388 {
389  return channels->getEnabledNames();
390 }
391 
393 {
394  return channels->getConfigurationString();
395 }
396 
397 
398 //----------------------------------------
399 //SETTING OPERATIONS
400 
401 
402 void CEBLModel::channelsLoadFile(string filename)
403 {
404  channels->loadFile(filename);
405 }
406 
407 void CEBLModel::channelsSaveFile(string filename)
408 {
409  channels->saveFile(filename);
410 }
411 
412 void CEBLModel::channelsSetElectrodeName(int electrode, string name)
413 {
414  channels->setElectrodeName(electrode, name);
415 }
416 
417 void CEBLModel::channelsSetElectrodeReference(int electrode, bool reference)
418 {
419  channels->setElectrodeReference(electrode, reference);
420 }
421 
422 void CEBLModel::channelsSetElectrodeEnabled(int electrode, bool enabled)
423 {
424  channels->setElectrodeEnabled(electrode, enabled);
425 }
426 
428 {
429  channels->setConfigurationFromString(config);
430 }
431 
432 
433 
434 
435 //======================================================================
436 // DEVICE
437 
438 
439 
440 
441 //----------------------------------------
442 //GETTING OPERATIONS
443 
444 
446 {
447  return device->getLocation();
448 }
449 
451 {
452  return device->isReady();
453 }
454 
456 {
457  return device->getError();
458 }
459 
461 {
462  return device->getInquiry();
463 }
464 
466 {
467  return device->getSampleRate();
468 }
469 
471 {
472  return device->getBlockSize();
473 }
474 
476 {
477  return device->exists();
478 }
479 
480 
481 //----------------------------------------
482 //SETTING OPERATIONS
483 
484 
486 {
487  device->setDeviceLocation(filename);
488 }
489 
490 void CEBLModel::deviceSetSampleRate(int sample_rate)
491 {
492  device->setSampleRate(sample_rate);
493 }
494 
495 void CEBLModel::deviceSetBlockSize(int block_size)
496 {
497  device->setBlockSize(block_size);
498 }
499 
501 {
502  device->scanForDevices();
503 }
504 
505 
506 
507 
508 
509 //======================================================================
510 //File Data Stream
511 
512 
513 
514 
515 //----------------------------------------
516 //GETTING OPERATIONS
517 
518 
520 {
521  return this->file_data_stream_config->getFilename();
522 }
523 
525 {
526  return this->file_data_stream_config->isReady();
527 }
528 
530 {
531  return this->file_data_stream_config->getSampleRate();
532 }
533 
535 {
536  return this->file_data_stream_config->getNumSamples();
537 }
538 
540 {
541  return this->file_data_stream_config->getNumChannels();
542 }
543 
545 {
546  return this->file_data_stream_config->getNumClasses();
547 }
548 
550 {
551  return this->file_data_stream_config->getNumSequences();
552 }
553 
555 {
556  return this->file_data_stream_config->getTrainingData();
557 }
558 
560 {
561  return this->file_data_stream_config->getData();
562 }
563 
564 
565 //----------------------------------------
566 //SETTING OPERATIONS
567 
568 
569 void CEBLModel::fileStreamOpenFile(string filename)
570 {
571  this->file_data_stream_config->openFile(filename);
572 }
573 
575 {
576  this->file_data_stream_config->setSampleRate(sample_rate);
577 }
578 
579 
580 
581 
582 //======================================================================
583 // SESSION SAVING and LOADING
584 
585 
586 
587 
588 //----------------------------------------
589 //GETTING OPERATIONS
590 
591 
593 {
594  return session_manager->shouldSaveAs();
595 }
596 
597 //----------------------------------------
598 //SETTING OPERATIONS
599 
600 
602 {
603  session_manager->save();
604 }
605 
606 void CEBLModel::sessionSaveAs(string filename)
607 {
608  session_manager->saveAs(filename);
609 }
610 
611 void CEBLModel::sessionLoad(string filename)
612 {
613  session_manager->load(filename);
614 }
615 
616 
617 
618 //======================================================================
619 //FILTER
620 
621 
622 //----------------------------------------
623 //GETTING OPERATIONS
624 
625 
626 std::vector<string> CEBLModel::filterGetNameList()
627 {
628  return filters->getNameList();
629 }
630 
631 std::vector<string> CEBLModel::filterGetPathList()
632 {
633  return filters->getPathList();
634 }
635 
637 {
638  return filters->isTrained(filter);
639 }
640 
642 {
643  return filters->getNumLags();
644 }
645 
647 {
648  return filters->getSelected();
649 }
650 
652 {
653  return filters->getSelectedComponents();
654 }
655 
657 {
658  return filters->getComponents(training_data);
659 }
660 
662 {
663  return filters->getSelectedComponentsString();
664 }
665 
667 {
668  return filters->getSelectedComponentsValid();
669 }
670 
672 {
673  return filters->apply(data);
674 }
675 
677 {
678  return filters->getNumExpectedChannels();
679 }
680 
681 ublas::matrix<double> CEBLModel::filterGetFilterMatrix()
682 {
683  return filters->getFilterMatrix();
684 }
685 
686 
687 //----------------------------------------
688 //SETTING OPERATIONS
689 
690 
692 {
693  filters->setSelectedComponentsString(components);
694 }
695 
697 {
698  filters->setSelected(filter);
699 }
700 
701 void CEBLModel::filterTrain(EEGData training_data, string filter)
702 {
703  filters->train(training_data, filter);
704 }
705 
707 {
708  filters->setNumLags(n);
709 }
710 
711 
712 //======================================================================
713 //FEATURES
714 
715 
716 //----------------------------------------
717 //GETTING OPERATIONS
718 
719 
720 std::vector<string> CEBLModel::featuresGetNameList()
721 {
722  return features->getNameList();
723 }
724 
725 std::vector<string> CEBLModel::featuresGetPathList()
726 {
727  return features->getPathList();
728 }
729 
730 bool CEBLModel::featureIsTrained(string feature)
731 {
732  return features->isTrained(feature);
733 }
734 
736 {
737  return features->getSelected();
738 }
739 
740 std::map<std::string, CEBL::Param> CEBLModel::featureGetParams(string feature)
741 {
742  return features->getParams(feature);
743 }
744 
745 
746 //----------------------------------------
747 //SETTING OPERATIONS
748 
749 
750 void CEBLModel::featuresSetSelected(string feature)
751 {
752  features->setSelected(feature);
753 }
754 
755 void CEBLModel::featureTrain(string feature)
756 {
757  features->train(feature);
758 }
759 
760 void CEBLModel::featureReset(string feature)
761 {
762  features->reset(feature);
763 }
764 
765 void CEBLModel::featureSetParams(std::map<std::string, CEBL::Param> params, string feature)
766 {
767  features->setParams(params,feature);
768 }
769 
770 //----------------------------------------
771 //USE FEATURE
772 
774 {
775  return features->extract(data);
776 }
778 {
779  return features->extract(data);
780 }
781 
783 {
784  features->halt();
785 }
786 
787 
788 
789 //======================================================================
790 //FEATURES
791 
792 
793 //----------------------------------------
794 //GETTING OPERATIONS
795 std::vector<string> CEBLModel::decisionGetNameList()
796 {
797  return decisions->getNameList();
798 }
799 
800 std::vector<string> CEBLModel::decisionGetPathList()
801 {
802  return decisions->getPathList();
803 }
804 
806 {
807  return decisions->getSelected();
808 }
809 
810 std::map<std::string, CEBL::Param> CEBLModel::decisionGetParams(string decision)
811 {
812  return decisions->getParams(decision);
813 }
814 
815 
816 //----------------------------------------
817 //SETTING OPERATIONS
818 
819 
820 void CEBLModel::decisionSetSelected(string feature)
821 {
822  decisions->setSelected(feature);
823 }
824 
825 void CEBLModel::decisionSetParams(std::map<std::string, CEBL::Param> params, string decision)
826 {
827  decisions->setParams(params,decision);
828 }
829 //DECISION OPERATIONS
830 
832 (std::vector<std::vector<double> >probs)
833 {
834  decisions->updateWithProbabilities(probs);
835 }
836 
837 void CEBLModel::decisionUpdateWithProbabilities(std::vector<double> probs)
838 {
839  decisions->updateWithProbabilities(probs);
840 }
841 
843 (ublas::vector<int> classes)
844 {
845  decisions->updateWithClassification(classes);
846 }
847 
849 {
850  decisions->updateWithClassification(cls);
851 }
852 
853 void CEBLModel::decisionInit(int num_classes)
854 {
855  decisions->init(num_classes);
856 }
857 
858 std::vector<double> CEBLModel::decisionDecideClasses()
859 {
860  return decisions->decideClasses();
861 }
862 
863 //======================================================================
864 //CLASSIFIERS
865 
866 
867 
868 //----------------------------------------
869 //GETTING OPERATIONS
870 
872 {
873  return classifiers->getNameList();
874 }
875 
877 {
878  return classifiers->getPathList();
879 }
880 
881 bool CEBLModel::classifierIsTrained(string classifier)
882 {
883  return classifiers->isTrained(classifier);
884 }
885 
886 std::map<std::string, CEBL::Param> CEBLModel::classifierGetParams(string classifier)
887 {
888  return classifiers->getParams(classifier);
889 }
890 
892 {
893  return classifiers->getSelected();
894 }
895 
897 {
898  return classifiers->getUseProbs();
899 }
900 
901 std::vector<std::vector<double> > CEBLModel::classifierGetLastProbs()
902 {
903  return classifiers->getLastProbs();
904 }
905 
907 {
908  return classifiers->getTrainedClasses();
909 }
910 
912 {
913  return classifiers->getTrainedLags();
914 }
915 
916 //----------------------------------------
917 //SETTING OPERATIONS
918 
919 
920 void CEBLModel::classifierReset(CEBL::Param params, string classifier)
921 {
922  classifiers->reset(params);
923 }
924 
925 void CEBLModel::classifiersSetSelected(string classifier)
926 {
927  classifiers->setSelected(classifier);
928 }
929 
930 
931 void CEBLModel::classifierTrain(EEGTrainingData &training_data, string classifier)
932 {
933  classifiers->train(training_data,classifier);
934 }
935 
937 {
938  classifiers->haltTrain();
939 }
940 
941 void CEBLModel::classifierSetParams(std::map<std::string, CEBL::Param> params, string classifier)
942 {
943  classifiers->setParams(params,classifier);
944 }
945 
947 {
948  classifiers->setUseProbs(flag);
949 }
950 
951 //----------------------------------------
952 //USE CLASSIFIER
953 ublas::vector<int> CEBLModel::classifierUse(EEGData &data)
954 {
955  return classifiers->use(data);
956 }
957 
958 //======================================================================
959 // Training
960 
961 //----------------------------------------
962 //GETTING OPERATIONS
963 
965 {
966  return training->getClassLabels();
967 }
968 
969 string CEBLModel::trainingGetClassLabel(int class_num)
970 {
971  return training->getClassLabel(class_num);
972 }
973 
975 {
976  return training->getNumClasses();
977 }
978 
980 {
981  return training->getNumSequences();
982 }
983 
985 {
986  return training->getSequenceLength();
987 }
988 
990 {
991  return training->getPauseLength();
992 }
993 
995 {
996  return training->getData();
997 }
998 
1000 {
1001  return training->dataIsLoaded();
1002 }
1003 
1005 {
1006  return training->isDataFileLoaded();
1007 }
1008 
1010 {
1011  return training->getDataFilename();
1012 }
1013 
1015 {
1016  return training->isActive();
1017 }
1018 
1020 {
1021  return training->failed();
1022 }
1023 
1025 {
1026  return training->getFailureMessage();
1027 }
1028 
1030 {
1031  return training->isPaused();
1032 }
1033 
1035 {
1036  return training->getTrainingClass();
1037 }
1038 
1040 {
1041  return training->getTrainingSequence();
1042 }
1043 
1045 {
1046  return training->feedbackEnabled();
1047 }
1049 {
1050  return training->isTrainingClassifier();
1051 }
1053 {
1054  return training->getClassProportions();
1055 }
1056 
1057 
1058 //----------------------------------------
1059 //SETTING OPERATIONS
1060 
1062 {
1063  training->start();
1064 }
1065 
1067 {
1068  training->stop();
1069 }
1070 
1072 {
1073  training->setNumClasses(n);
1074 }
1075 
1077 {
1078  training->setNumSequences(n);
1079 }
1080 
1082 {
1083  training->setSequenceLength(n);
1084 }
1085 
1087 {
1088  training->setPauseLength(n);
1089 }
1090 
1091 void CEBLModel::trainingSetClassLabels(std::vector<string> labels)
1092 {
1093  training->setClassLabels(labels);
1094 }
1095 
1096 void CEBLModel::trainingSetClassLabel(int class_number, string label)
1097 {
1098  training->setClassLabel(class_number,label);
1099 }
1100 
1101 void CEBLModel::trainingLoadData(string filename)
1102 {
1103  training->loadData(filename);
1104 }
1105 
1107 {
1108  training->clearData();
1109 }
1110 
1111 void CEBLModel::trainingSaveData(string filename)
1112 {
1113  training->saveData(filename);
1114 }
1115 
1117 {
1118  training->setFeedbackEnabled(flag);
1119 }
1120 
1121 //======================================================================
1122 // RealTime Classification
1123 
1124 
1125 //----------------------------------------
1126 //GETTING OPERATIONS
1127 
1129 {
1130  return realtime->isReady();
1131 }
1132 
1134 {
1135  return realtime->lastTrainFailed();
1136 }
1137 
1139 {
1140  return realtime->isClassifying();
1141 }
1142 
1144 {
1145  return realtime->readClassificationQueue();
1146 }
1147 
1149 {
1150  return realtime->getClassProportions();
1151 }
1152 
1154 {
1155  return realtime->peekClassificationQueue();
1156 }
1157 
1159 {
1160  return realtime->getSelectedClass();
1161 }
1162 
1163 
1164 
1165 //----------------------------------------
1166 //SETTING OPERATIONS
1167 
1168 
1170 {
1171  realtime->clearClassificationQueue();
1172 }
1173 
1174 
1175 //CONTROL CLASSIFICATION
1177 {
1178  realtime->trainClassifier();
1179 }
1180 
1182 {
1183  realtime->startClassifying();
1184 }
1185 
1187 {
1188  realtime->stopClassifying();
1189 }
1190 
1192 {
1193  realtime->trainClassifierThreaded();
1194 }
1195 
1197 {
1198  realtime->trainClassifierHalt();
1199 }
1200 
1202 {
1203  return realtime->isTrainingClassifier();
1204 }
1206 {
1207  realtime->clearSelectedClass();
1208 }
1209 
1210 //======================================================================
1211 // DATA FUNCTIONS
1212 
1214 {
1215  return DataIO::loadTrainingDataFromFile(filename);
1216 }