CEBL  2.1
ClassifiersConfig.cpp
Go to the documentation of this file.
1 #include "ClassifiersConfig.hpp"
2 #include "../CEBLModel.hpp"
3 
4 //----------------------------------------------------------------------
5 // CONSTRUCTORS / DESTRUCTORS
6 
8 {
9  this->model = model;
10  this->plugin_loader = new PluginLoader<Classifier>;
11  this->selected_classifier = "";
12  std::vector<string> paths = model->preferencesGetPaths();
13  for(unsigned int i=0; i<paths.size();i++)
14  {
15  string path = paths[i]+"plugins/classifiers/";
16  try
17  {
18  plugin_loader->loadDir(path.c_str());
19  }catch(FileException & e)
20  {
21  //cerr << e.what() << "\n";
22  //if there were exceptions, just continue
23  continue;
24  }
25  }
26 }
27 
29 {
30  delete plugin_loader;
31 }
32 
33 //----------------------------------------------------------------------
34 //GETTING OPERATIONS
35 
36 //special sort to put QDA on top
37 bool classifierSort(string i, string j)
38 {
39  if(i == "QDA")
40  return true;
41  else
42  return (i<j);
43 }
44 
45 std::vector<string> ClassifiersConfig::getNameList()
46 {
47  std::vector<string> names = plugin_loader->getNames();
48  sort(names.begin(),names.end(),classifierSort);
49  return names;
50 }
51 
52 std::vector<string> ClassifiersConfig::getPathList()
53 {
54  return plugin_loader->getPaths();
55 }
56 
57 bool ClassifiersConfig::isTrained(string classifier)
58 {
59  if(classifier == "")
60  classifier = this->selected_classifier;
61  try
62  {
63  if(plugin_loader->getPlugin(classifier) != NULL)
64  return plugin_loader->getPlugin(classifier)->isTrained();
65  }
66  catch(...)
67  {
68  throw PluginException("Failed to get trained status from classifier: "
69  + classifier);
70  }
71  return false;
72 }
73 
75 {
76  return this->selected_classifier;
77 }
78 
79 std::map<std::string, CEBL::Param> ClassifiersConfig::getParams(string classifier)
80 {
81  try
82  {
83  return plugin_loader->getPlugin(classifier)->getParamsList();
84  }
85  catch(...)
86  {
87  throw PluginException("Failed to get parameter list for classifier: "
88  + classifier);
89  }
90 }
91 
93 {
94  string classifier = selected_classifier;
95  return plugin_loader->getPlugin(classifier)->getProbabilitiesFlag();
96 }
97 
98 std::vector<std::vector<double> > ClassifiersConfig::getLastProbs()
99 {
100  string classifier = selected_classifier;
101  return plugin_loader->getPlugin(classifier)->getProbabilities();
102 }
103 
105 {
106  string classifier = selected_classifier;
107  return plugin_loader->getPlugin(classifier)->getTrainedClasses();
108 }
109 
111 {
112  string classifier = selected_classifier;
113  return plugin_loader->getPlugin(classifier)->getTrainedLags();
114 }
115 
116 //----------------------------------------------------------------------
117 //SETTING OPERATIONS
118 
119 void ClassifiersConfig::reset(CEBL::Param param, string classifier)
120 {
121  if(classifier == "")
122  classifier = this->selected_classifier;
123  try
124  {
125  if(plugin_loader->getPlugin(classifier)!=NULL)
126  plugin_loader->getPlugin(classifier)->reset(param);
127  }
128  catch(...)
129  {
130  throw PluginException("Failed to reset classifier: " + classifier);
131  }
132 }
133 
134 void ClassifiersConfig::setSelected(string classifier)
135 {
136  this->selected_classifier = classifier;
137  if(plugin_loader->getPlugin(classifier) == NULL)
138  {
139  cerr << "ERROR: trying to select classifier " << classifier
140  << " which doesn't seem to exist.\n";
141  }
142 }
143 
144 void ClassifiersConfig::train(EEGTrainingData &training_data, string classifier)
145 {
146  if(classifier == "")
147  classifier = this->selected_classifier;
148  try
149  {
150  this->is_training = true;
151  this->currently_training_classifier = classifier;
152  plugin_loader->getPlugin(classifier)->train(training_data);
153  }
154  catch(...)
155  {
156  this->is_training = false;
157  throw PluginException("Failed to train classifier: " + classifier);
158  }
159 }
160 
162 {
163  if(is_training)
164  {
165  try
166  {
167  plugin_loader->getPlugin(currently_training_classifier)->halt();
168  }
169  catch(...)
170  {
171  cerr << "error halting training.\n";
172  }
173  }
174  this->is_training = false;
175 }
176 
177 void ClassifiersConfig::setParams(std::map<std::string, CEBL::Param> params,
178  string classifier)
179 {
180  //if the parameter is blank, use the selected classifier instead
181  if(classifier == "")
182  classifier = this->selected_classifier;
183 
184  //if the selected classifier is also nothing, then don't attempt to set params
185  if(classifier == "")
186  return;
187 
188  try
189  {
190  plugin_loader->getPlugin(classifier)->setParamsList(params);
191  }
192  catch(...)
193  {
194  throw PluginException("Failed to set parameter list for classifier: "
195  + classifier);
196  }
197 }
198 
200 {
201  string classifier = selected_classifier;
202  plugin_loader->getPlugin(classifier)->setProbabilitiesFlag(flag);
203 }
204 
205 //----------------------------------------------------------------------
206 // USE CLASSIFIER
207 
208 ublas::vector<int> ClassifiersConfig::use(EEGData &data)
209 {
210  string classifier = selected_classifier;
211  if(!isTrained())
212  {
213  throw ClassificationException("Classifier is not trained.");
214  }
215  else
216  {
217  ublas::matrix<double> d = data;
218  return plugin_loader->getPlugin(classifier)->use(d);
219  }
220 }
221 
222