00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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
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