Marsyas
0.6.0-alpha
|
00001 /* 00002 ** Copyright (C) 1998-2006 George Tzanetakis <gtzan@cs.princeton.edu> 00003 ** 00004 ** This program is free software; you can redistribute it and/or modify 00005 ** it under the terms of the GNU General Public License as published by 00006 ** the Free Software Foundation; either version 2 of the License, or 00007 ** (at your option) any later version. 00008 ** 00009 ** This program is distributed in the hope that it will be useful, 00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 ** GNU General Public License for more details. 00013 ** 00014 ** You should have received a copy of the GNU General Public License 00015 ** along with this program; if not, write to the Free Software 00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 */ 00018 00019 #include "ZeroRClassifier.h" 00020 #include "../common_source.h" 00021 00022 using namespace std; 00023 using namespace Marsyas; 00024 00025 ZeroRClassifier::ZeroRClassifier(mrs_string name):MarSystem("ZeroRClassifier",name) 00026 { 00027 addControls(); 00028 } 00029 00030 ZeroRClassifier::~ZeroRClassifier() 00031 { 00032 } 00033 00034 MarSystem* 00035 ZeroRClassifier::clone() const 00036 { 00037 return new ZeroRClassifier(*this); 00038 } 00039 00040 void 00041 ZeroRClassifier::addControls() 00042 { 00043 addctrl("mrs_string/mode", "train"); 00044 addctrl("mrs_natural/nClasses", 1); 00045 setctrlState("mrs_natural/nClasses", true); 00046 addctrl("mrs_bool/done", false); 00047 addctrl("mrs_natural/prediction", 0); 00048 } 00049 00050 void 00051 ZeroRClassifier::myUpdate(MarControlPtr sender) 00052 { 00053 (void) sender; //suppress warning of unused parameter(s) 00054 MRSDIAG("ZeroRClassifier.cpp - ZeroRClassifier:myUpdate"); 00055 00056 setctrl("mrs_natural/onSamples", getctrl("mrs_natural/inSamples")); 00057 setctrl("mrs_natural/onObservations", (mrs_natural)2); 00058 setctrl("mrs_real/osrate", getctrl("mrs_real/israte")); 00059 00060 mrs_natural nlabels = getctrl("mrs_natural/nClasses")->to<mrs_natural>(); 00061 00062 if ((mrs_natural)labelSizes_.getSize() != nlabels) 00063 labelSizes_.create(nlabels); 00064 mrs_string mode = getctrl("mrs_string/mode")->to<mrs_string>(); 00065 if (mode == "predict") 00066 { 00067 00068 } 00069 } 00070 00071 void 00072 ZeroRClassifier::myProcess(realvec& in, realvec& out) 00073 { 00074 mrs_string mode = getctrl("mrs_string/mode")->to<mrs_string>(); 00075 mrs_natural nlabels = getctrl("mrs_natural/nClasses")->to<mrs_natural>(); 00076 mrs_natural l, t; 00077 mrs_natural prediction = 0; 00078 00079 mrs_real label; 00080 00081 if ((prev_mode_ == "predict") && (mode == "train")) 00082 { 00083 labelSizes_.setval(0.0); 00084 } 00085 00086 if (mode == "train") 00087 { 00088 for (t=0; t < inSamples_; t++) 00089 { 00090 label = in(inObservations_-1, t); 00091 if(label >= 0) 00092 { 00093 labelSizes_((int)label) = labelSizes_((int)label) + 1; 00094 } 00095 out(0,t) = label; 00096 out(1,t) = label; 00097 } 00098 } 00099 00100 if ((prev_mode_ == "train") && (mode == "predict")) 00101 { 00102 int max = -1; 00103 for (l=0; l < nlabels; l++) 00104 { 00105 if (labelSizes_(l) > max) 00106 { 00107 prediction = l; 00108 max = (int)labelSizes_(l); 00109 } 00110 } 00111 updControl("mrs_natural/prediction", prediction); 00112 } 00113 00114 if (mode == "predict") 00115 { 00116 for (t=0; t < inSamples_; t++) 00117 { 00118 label = in(inObservations_-1, t); 00119 prediction = getctrl("mrs_natural/prediction")->to<mrs_natural>(); 00120 out(0,t) = (mrs_real)prediction; 00121 out(1,t) = label; 00122 } 00123 } 00124 prev_mode_ = mode; 00125 }