CEBL  2.1
FeaturesConfig.cpp
Go to the documentation of this file.
1 
11 #include "FeaturesConfig.hpp"
12 #include "../CEBLModel.hpp"
13 #include <algorithm>
14 using namespace std;
15 
16 
17 //----------------------------------------------------------------------
18 // CONSTRUCTORS / DESTRUCTORS
19 
20 
22 {
23  this->model = model;
24  this->plugin_loader = new PluginLoader<Feature>;
25  this->selected_feature = "";
26  std::vector<string> paths = model->preferencesGetPaths();
27  for(unsigned int i=0; i<paths.size();i++)
28  {
29  string path = paths[i]+"plugins/features/";
30  try
31  {
32  plugin_loader->loadDir(path.c_str());
33  } catch(FileException & e)
34  {
35  //cerr << e.what() << "\n";
36  //if there were exceptions, just continue
37  continue;
38  }
39  }
40  if (plugin_loader->getNames().size() > 0)
41  {
42  this->selected_feature = this->getNameList().at(0);
43  }
44 }
45 
47 {
48  delete plugin_loader;
49 }
50 
51 //----------------------------------------------------------------------
52 //GETTING OPERATIONS
53 
54 //special sort to put None and Lag at the top
55 bool featuresSort(string i, string j)
56 {
57  if(i == "None")
58  return true;
59  else if(j == "None")
60  return false;
61  else if(i == "Lag")
62  return true;
63  else if(j == "Lag")
64  return false;
65  else
66  return (i<j);
67 }
68 
69 std::vector<string> FeaturesConfig::getNameList()
70 {
71  std::vector<string> names = plugin_loader->getNames();
72  sort(names.begin(),names.end(),featuresSort);
73  return names;
74 }
75 
76 std::vector<string> FeaturesConfig::getPathList()
77 {
78  return plugin_loader->getPaths();
79 }
80 
81 bool FeaturesConfig::isTrained(string feature)
82 {
83  if(feature == "")
84  feature = this->selected_feature;
85  try
86  {
87  if(plugin_loader->getPlugin(feature) != NULL)
88  return plugin_loader->getPlugin(feature)->isTrained();
89  }
90  catch(...)
91  {
92  throw PluginException("Failed to get trained status from feature: " + feature);
93  }
94 
95  return false;
96 }
97 
99 {
100  return this->selected_feature;
101 }
102 
103 std::map<std::string, CEBL::Param> FeaturesConfig::getParams(string feature)
104 {
105  if(feature == "")
106  feature = selected_feature;
107 
108  try
109  {
110  return plugin_loader->getPlugin(feature)->getParamsList();
111  }
112  catch(...)
113  {
114  throw PluginException("Failed to get parameter list for feature: " + feature);
115  }
116 }
117 
118 //----------------------------------------------------------------------
119  //SETTING OPERATIONS
120 
121 void FeaturesConfig::setSelected(string feature)
122 {
123  this->selected_feature = feature;
124  if(plugin_loader->getPlugin(feature) == NULL)
125  {
126  cerr << "ERROR: trying to select feature " << feature
127  << " which doesn't seem to exist.\n " << flush;
128  }
129 
130 }
131 
132 
133 
134 void FeaturesConfig::train(string feature)
135 {
136  //if the parameter is blank, use the selected feature instead
137  if(feature == "")
138  feature = this->selected_feature;
139 
140  //if the selected feature is also nothing, then don't attempt to train it
141  if(feature == "")
142  return;
143  try
144  {
145  plugin_loader->getPlugin(feature)->train();
146  }
147  catch(...)
148  {
149  throw PluginException("Failed to train: " + feature);
150  }
151 
152 }
153 
154 void FeaturesConfig::reset(string feature)
155 {
156  //if the parameter is blank, use the selected feature instead
157  if(feature == "")
158  feature = this->selected_feature;
159 
160  //if the selected feature is also nothing, then don't attempt to reset it
161  if(feature == "")
162  return;
163  try
164  {
165  plugin_loader->getPlugin(feature)->reset();
166  }
167  catch(...)
168  {
169  throw PluginException("Failed to reset: " + feature);
170  }
171 }
172 
173 void FeaturesConfig::setParams(std::map<std::string, CEBL::Param> params, string feature)
174 {
175  //if the parameter is blank, use the selected feature instead
176  if(feature == "")
177  feature = this->selected_feature;
178 
179  //if the selected feature is also nothing, then don't attempt to set params
180  if(feature == "")
181  return;
182 
183  try
184  {
185  plugin_loader->getPlugin(feature)->setParamsList(params);
186  }
187  catch(...)
188  {
189  throw PluginException("Failed to set parameter list for feature: " + feature);
190  }
191 }
192 
193 
194 //----------------------------------------------------------------------
195 //EXTRACT FEATURES
196 
198 {
199  EEGTrainingData processed_data;
200  EEGData temp;
201  processed_data.reserve(data.numClasses(),data.numSequences());
202  for(int cls = 0; cls < data.numClasses(); cls++)
203  {
204  for(int seq = 0; seq < data.numSequences(cls); seq++)
205  {
206  cout << "Featurizing class " << cls << ", seq" << seq << "\n";
207  temp = data.get(cls,seq);
208  temp = extract(temp);
209  processed_data.set(cls,seq,temp);
210  }
211  }
212  return processed_data;
213 }
214 
215 
217 {
218  string feature = selected_feature;
219  EEGData ret;
220 
221  if(!isTrained())
222  {
223  throw FeatureException("Feature is not trained.");
224  }
225  else
226  {
227  ublas::matrix<double> d = data;
228  ret = EEGData(plugin_loader->getPlugin(feature)->use(d));
229  }
230 
231  return ret;
232 }
233 
234 
236 {
237  plugin_loader->getPlugin(selected_feature)->halt();
238 }