Marsyas  0.5.0-beta1
/Users/jleben/code/marsyas/src/marsyas/marsystems/FMeasure.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2006 George Tzanetakis <gtzan@cs.uvic.ca>
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 "FMeasure.h"
00020 
00021 using std::ostringstream;
00022 using namespace Marsyas;
00023 
00024 FMeasure::FMeasure(mrs_string name):MarSystem("FMeasure", name)
00025 {
00026   addControls();
00027   numObsInRef_          = 0;
00028   numObsInTest_         = 0;
00029   numTruePos_               = 0;
00030 }
00031 
00032 FMeasure::FMeasure(const FMeasure& a) : MarSystem(a)
00033 {
00034   ctrl_numObsInRef_     = getControl("mrs_natural/numObservationsInReference");
00035   ctrl_numObsInTest_        = getControl("mrs_natural/numObservationsInTest");
00036   ctrl_numTruePos_      = getControl("mrs_natural/numTruePositives");
00037   ctrl_reset_               = getControl("mrs_bool/reset");
00038 
00039   numObsInRef_          = 0;
00040   numObsInTest_         = 0;
00041   numTruePos_               = 0;
00042 }
00043 
00044 FMeasure::~FMeasure()
00045 {
00046 }
00047 
00048 MarSystem*
00049 FMeasure::clone() const
00050 {
00051   return new FMeasure(*this);
00052 }
00053 
00054 void
00055 FMeasure::addControls()
00056 {
00057   //Add specific controls needed by this MarSystem.
00058   addControl("mrs_natural/numObservationsInReference", -1, ctrl_numObsInRef_);
00059   addControl("mrs_natural/numObservationsInTest", -1, ctrl_numObsInTest_);
00060   addControl("mrs_natural/numTruePositives", -1, ctrl_numTruePos_);
00061   addControl("mrs_bool/reset", true, ctrl_reset_);
00062 }
00063 
00064 void
00065 FMeasure::myUpdate(MarControlPtr sender)
00066 {
00067   // no need to do anything FMeasure-specific in myUpdate
00068   MarSystem::myUpdate(sender);
00069 
00070   updControl ("mrs_natural/onSamples", 1);
00071   updControl ("mrs_natural/onObservations", 3);
00072 }
00073 
00074 
00075 void
00076 FMeasure::myProcess(realvec& in, realvec& out)
00077 {
00078   (void) in;
00079 
00080   if (ctrl_reset_->to<mrs_bool>())
00081   {
00082     numObsInRef_    = 0;
00083     numObsInTest_   = 0;
00084     numTruePos_     = 0;
00085     updControl ("mrs_bool/reset", false, false);
00086   }
00087   numObsInRef_  += ctrl_numObsInRef_->to<mrs_natural> ();
00088   numObsInTest_ += ctrl_numObsInTest_->to<mrs_natural> ();
00089   numTruePos_       += ctrl_numTruePos_->to<mrs_natural> ();
00090 
00091   out.setval(0.);
00092 
00093   MRSASSERT(ctrl_numObsInRef_->to<mrs_natural> () > 0 || ctrl_numObsInTest_->to<mrs_natural> () >= 0 || ctrl_numTruePos_->to<mrs_natural> () >= 0);
00094   MRSASSERT(ctrl_numTruePos_->to<mrs_natural> () <= ctrl_numObsInRef_->to<mrs_natural> () && ctrl_numTruePos_->to<mrs_natural> () <= ctrl_numObsInTest_->to<mrs_natural> ());
00095 
00096   if (numObsInTest_ == 0)
00097     return;
00098 
00099   out(kPrecision,0) = numTruePos_ * (1.0/ numObsInTest_),
00100   out(kRecall,0)        = numTruePos_ * (1.0/ numObsInRef_);
00101 
00102   if (out(kPrecision,0) <= 0 && out(kRecall,0) <= 0)
00103     return;
00104 
00105   out(kFMeasure,0)  = 2 * out(kPrecision,0) * out(kRecall,0) / (out(kPrecision,0) + out(kRecall,0));
00106 }
00107 
00108 
00109 
00110 
00111 
00112 
00113 
00114