00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "WekaSink.h"
00020
00021 using namespace std;
00022 using namespace Marsyas;
00023
00024 WekaSink::WekaSink(mrs_string name) : MarSystem("WekaSink",name)
00025 {
00026 mos_ = NULL;
00027 stabilizingTicks_ = 0;
00028 addControls();
00029 }
00030
00031 WekaSink::~WekaSink()
00032 {
00033
00034 if (mos_ != NULL)
00035 {
00036 mos_->close();
00037 delete mos_;
00038 }
00039 }
00040
00041 WekaSink::WekaSink(const WekaSink& a) : MarSystem(a)
00042 {
00043 mos_ = NULL;
00044 stabilizingTicks_ = 0;
00045
00046 ctrl_regression_ = getControl("mrs_bool/regression");
00047 ctrl_putHeader_ = getControl("mrs_bool/putHeader");
00048 ctrl_labelNames_ = getControl("mrs_string/labelNames");
00049 ctrl_nLabels_ = getControl("mrs_natural/nLabels");
00050 ctrl_precision_ = getControl("mrs_natural/precision");
00051 ctrl_downsample_ = getControl("mrs_natural/downsample");
00052 ctrl_filename_ = getControl("mrs_string/filename");
00053 ctrl_currentlyPlaying_ = getControl("mrs_string/currentlyPlaying");
00054 ctrl_inject_ = getControl("mrs_bool/inject");
00055 ctrl_injectComment_ = getControl("mrs_string/injectComment");
00056 ctrl_injectVector_ = getControl("mrs_realvec/injectVector");
00057 ctrl_onlyStable_ = getControl("mrs_bool/onlyStable");
00058 ctrl_resetStable_ = getControl("mrs_bool/resetStable");
00059 }
00060
00061 MarSystem*
00062 WekaSink::clone() const
00063 {
00064 return new WekaSink(*this);
00065 }
00066
00067 void
00068 WekaSink::addControls()
00069 {
00070 addctrl("mrs_natural/precision", 6, ctrl_precision_);
00071 setctrlState("mrs_natural/precision", true);
00072 addctrl("mrs_string/filename", "weka.arff", ctrl_filename_);
00073 setctrlState("mrs_string/filename", true);
00074 addctrl("mrs_natural/nLabels", 2, ctrl_nLabels_);
00075 addctrl("mrs_natural/downsample", 1, ctrl_downsample_);
00076 setctrlState("mrs_natural/downsample", true);
00077 addctrl("mrs_string/labelNames", "Music,Speech", ctrl_labelNames_);
00078 setctrlState("mrs_string/labelNames", true);
00079
00080
00081 addctrl("mrs_bool/regression", false, ctrl_regression_);
00082 addctrl("mrs_string/currentlyPlaying", "", ctrl_currentlyPlaying_);
00083 addctrl("mrs_bool/putHeader", false, ctrl_putHeader_);
00084 setctrlState(ctrl_putHeader_, true);
00085 addctrl("mrs_bool/inject", false, ctrl_inject_);
00086 setctrlState(ctrl_inject_, true);
00087 addctrl("mrs_string/injectComment", "", ctrl_injectComment_);
00088 setctrlState(ctrl_injectComment_, true);
00089 addctrl("mrs_realvec/injectVector", realvec(), ctrl_injectVector_);
00090 setctrlState(ctrl_injectVector_, true);
00091
00092 addctrl("mrs_bool/onlyStable", false, ctrl_onlyStable_);
00093 setctrlState(ctrl_onlyStable_, true);
00094 addctrl("mrs_bool/resetStable", false, ctrl_resetStable_);
00095 }
00096
00097 void
00098 WekaSink::putHeader(mrs_string inObsNames)
00099 {
00100
00101 ctrl_putHeader_->setValue(true);
00102
00103
00104
00105
00106 if ((filename_ != ctrl_filename_->to<mrs_string>()))
00107 {
00108
00109 if (mos_ != NULL)
00110 {
00111 mos_->close();
00112 delete mos_;
00113
00114 if (filename_ == "weka.arff")
00115 {
00116 remove(filename_.c_str());
00117 }
00118 }
00119
00120
00121 filename_ = ctrl_filename_->to<mrs_string>();
00122
00123
00124 mos_ = new ofstream;
00125 mos_->open(filename_.c_str());
00126
00127
00128 (*mos_) << "% Created by Marsyas" << endl;
00129 (*mos_) << "@relation " << filename_ << endl;
00130
00131
00132
00133
00134
00135
00136 mrs_natural nAttributes = ctrl_inObservations_->to<mrs_natural>() - 1;
00137 mrs_natural nLabels = ctrl_nLabels_->to<mrs_natural>();
00138
00139
00140
00141
00142 mrs_natural i;
00143 for (i =0; i < nAttributes; ++i)
00144 {
00145 mrs_string inObsName;
00146 mrs_string temp;
00147 inObsName = inObsNames.substr(0, inObsNames.find(","));
00148 temp = inObsNames.substr(inObsNames.find(",") + 1, inObsNames.length());
00149 inObsNames = temp;
00150
00151 ostringstream oss;
00152
00153 (*mos_) << "@attribute " << inObsName << " real" << endl;
00154 }
00155
00156
00157 if (!ctrl_regression_->isTrue())
00158 {
00159 (*mos_) << "@attribute output {";
00160
00161
00162 for (i=0; i < nLabels; ++i)
00163 {
00164
00165 ostringstream oss;
00166
00167 oss << labelNames_[i];
00168 (*mos_) << oss.str();
00169 if (i < nLabels - 1)
00170 {
00171 (*mos_) << ",";
00172 }
00173
00174 }
00175 (*mos_) << "}" << endl;
00176 }
00177 else
00178 {
00179 (*mos_) << "@attribute output real" << endl;
00180 }
00181
00182
00183 (*mos_) << "\n\n@data" << endl;
00184 }
00185 }
00186
00187 void
00188 WekaSink::myUpdate(MarControlPtr sender)
00189 {
00190 MRSDIAG("WekaSink.cpp - WekaSink:myUpdate");
00191
00192 MarSystem::myUpdate(sender);
00193
00194
00195 mrs_string labelNames = ctrl_labelNames_->to<mrs_string>();
00196
00197 labelNames_.clear();
00198
00199
00200 for (int i = 0; i < ctrl_nLabels_->to<mrs_natural>(); ++i)
00201 {
00202 mrs_string labelName;
00203 mrs_string temp;
00204 labelName = labelNames.substr(0, labelNames.find(","));
00205 temp = labelNames.substr(labelNames.find(",") + 1, labelNames.length());
00206 labelNames = temp;
00207 labelNames_.push_back(labelName);
00208 }
00209
00210 downsample_ = ctrl_downsample_->to<mrs_natural>();
00211 ctrl_israte_->setValue(israte_ / downsample_, NOUPDATE);
00212
00213
00214
00215
00216 if (!ctrl_mute_->isTrue())
00217 {
00218 mrs_string onObsNames = ctrl_onObsNames_->to<mrs_string>();
00219 putHeader(onObsNames);
00220 }
00221
00222
00223
00224
00225
00226
00227
00228 if (!ctrl_mute_->isTrue())
00229 {
00230 if (ctrl_inject_->isTrue())
00231 {
00232 (* mos_) << ctrl_injectComment_->to<mrs_string>() << endl;
00233 (* mos_) << "% srate " << israte_ << endl;
00234 ctrl_inject_->setValue(false, NOUPDATE);
00235 MarControlAccessor acc_injectVector(ctrl_injectVector_);
00236 realvec& injectVector = acc_injectVector.to<mrs_realvec>();
00237
00238 for (mrs_natural j=0; j < injectVector.getSize() - 1; j++)
00239 {
00240 (*mos_) << fixed << setprecision(precision_) << injectVector(j) << ",";
00241 }
00242
00243
00244 int label = (int)injectVector(injectVector.getSize() - 1);
00245
00246 ostringstream oss;
00247 oss << labelNames_[label];
00248 (*mos_) << oss.str();
00249 (*mos_) << endl;
00250 }
00251 }
00252 precision_ = ctrl_precision_->to<mrs_natural>();
00253
00254
00255
00256
00257 count_ = 0;
00258 }
00259
00260 void
00261 WekaSink::myProcess(realvec& in, realvec& out)
00262 {
00263 mrs_natural o,t;
00264
00265 if (ctrl_mute_->isTrue())
00266 {
00267 for (o=0; o < inObservations_; o++)
00268 {
00269 for (t = 0; t < inSamples_; t++)
00270 {
00271 out(o,t) = in(o,t);
00272 }
00273 }
00274 return;
00275 }
00276
00277 mrs_bool print_line;
00278 if (ctrl_onlyStable_->isTrue())
00279 {
00280 stabilizingTicks_++;
00281
00282
00283 if (stabilizingTicks_ <= ctrl_inStabilizingDelay_->to<mrs_natural>()) {
00284 print_line = false;
00285 } else {
00286 print_line = true;
00287 }
00288
00289 if (ctrl_resetStable_->isTrue()) {
00290 stabilizingTicks_ = 0;
00291
00292 if (ctrl_currentlyPlaying_->to<mrs_string>() == prev_playing_) {
00293 print_line = false;
00294 }
00295 }
00296 } else {
00297 print_line = true;
00298 }
00299
00300
00301
00302 mrs_natural label_class = 0;
00303
00304
00305 for (t = 0; t < inSamples_; t++)
00306 {
00307
00308 if (ctrl_currentlyPlaying_->to<mrs_string>() != prev_playing_)
00309 {
00310 (*mos_) << "% filename " << ctrl_currentlyPlaying_->to<mrs_string>() << endl;
00311 (*mos_) << "% srate " << israte_ << endl;
00312 prev_playing_ = ctrl_currentlyPlaying_->to<mrs_string>();
00313 }
00314
00315
00316 label_class = (mrs_natural) (in(inObservations_ - 1, t) + 0.5);
00317
00318
00319
00320
00321 for (o=0; o < inObservations_; o++)
00322 {
00323 out(o,t) = in(o,t);
00324 if ((label_class >= 0) || (ctrl_regression_->isTrue()))
00325 {
00326 if (o < inObservations_ - 1)
00327 {
00328 if ((count_ % downsample_) == 0)
00329 {
00330 if (print_line)
00331 {
00332 if ( out(o,t) != out(o,t) )
00333 {
00334
00335
00336
00337
00338 (*mos_) << "?" << ",";
00339 }
00340 else
00341 {
00342 (*mos_) << fixed << setprecision(precision_) << out(o,t) << ",";
00343
00344 }
00345 }
00346 }
00347 }
00348 }
00349 }
00350
00351
00352 ostringstream oss;
00353 if ((count_ % downsample_) == 0)
00354 {
00355 if (print_line)
00356 {
00357 if (!ctrl_regression_->isTrue())
00358 {
00359 if (label_class >= 0)
00360 {
00361
00362
00363 if (label_class >= (mrs_natural)labelNames_.size())
00364 {
00365 MRSWARN("WekaSink: label number is too big");
00366 oss << "non-label";
00367 }
00368 else
00369 {
00370 oss << labelNames_[label_class];
00371 }
00372 (*mos_) << oss.str();
00373 (*mos_) << endl;
00374 }
00375
00376
00377
00378
00379
00380 }
00381 else
00382 {
00383 (*mos_) << in(inObservations_ - 1, t);
00384 (*mos_) << endl;
00385 }
00386 }
00387 }
00388 }
00389 count_++;
00390 }