PLearn 0.1
StatsCommand.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // StatsCommand.cc
00004 //
00005 // Copyright (C) 2008 Frederic Bastien
00006 //
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 //
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 //
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 //
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 //
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 // Authors: Frederic Bastien
00036 
00040 #include "StatsCommand.h"
00041 #include <plearn/math/StatsCollector.h>
00042 #include <plearn/math/VecStatsCollector.h>
00043 #include <plearn/math/TMat_sort.h>
00044 #include <plearn/io/load_and_save.h>
00045 #include <boost/regex.hpp>
00046 #include <plearn/base/plerror.h>
00047 #include <plearn/base/stringutils.h>
00048 namespace PLearn {
00049 using namespace std;
00050 
00052 PLearnCommandRegistry StatsCommand::reg_(new StatsCommand);
00053 
00054 StatsCommand::StatsCommand()
00055     : PLearnCommand(
00056         "stats",
00057         "allow to extract some stats from stats file",
00058         "plearn stats sum_sort <statsfile> --filter=boost_regex --replace=boost_regex=new_value\n"
00059         "FULL DETAILED HELP HERE \n"
00060         )
00061 {}
00062 
00064 void StatsCommand::run(const vector<string>& args)
00065 {
00066     if(args[0]=="sum_sort"){
00067         //plearn Stats sum_sort <statsfile> --filter=boost_regex --replace=boost_regex=new_value
00068         if(args.size()<2)
00069             PLERROR("plearn stats sum_sort <statsfile> --filter=boost_regex --replace=boost_regex=new_value");
00070 
00071         //default regex that match all non new line caracter
00072         boost::regex filter(".*");
00073         boost::regex replace_re;
00074         string replace_new = "";
00075 
00076             
00077         string statsfile = args[1];
00078         string test_re;
00079         try{
00080             for(uint i=2;i<args.size();i++){
00081                 if(args[i].substr(0,9)=="--filter="){
00082                     test_re=args[i].substr(9);
00083                     filter.assign(test_re);
00084                 }else if(args[i].substr(0,10)=="--replace="){
00085                     split_on_first(args[i].substr(10), "=",
00086                                    test_re, replace_new);
00087                     replace_re.assign(test_re);
00088                 }else
00089                     PLERROR("In StatsCommand::run() - unknow parameter '%s'", 
00090                             args[i].c_str());
00091             }
00092         }catch (boost::regex_error& e){
00093             PLERROR("invalid regular expression: \"%s\"\n %s",
00094                     test_re.c_str(),
00095                     e.what());
00096         }
00097         TVec<StatsCollector> stats1;
00098         VecStatsCollector stats;
00099 
00100         PLearn::load(statsfile, stats);
00101         Mat m(stats.size(),2);
00102         for(int i = 0;i<stats.size();i++){
00103             m(i,0)=i;
00104             m(i,1)=stats.getStats(i).sum();
00105         }
00106 
00107         pout<<"NAME "<<"SUM"<<endl;
00108         sortRows(m,1,false);
00109         for(int i=0;i<m.length();i++){
00110             string f = stats.getFieldNames()[int(m(i,0))];
00111             if(boost::regex_match(f, filter)){
00112                 if(replace_re.size()>0)
00113                     f = boost::regex_replace(f,replace_re,"");
00114                 pout<<f<<" "<<m(i,1)<<endl;
00115             }
00116         }
00117     }else
00118         PLERROR("Currently only the sub commands sum_sort is supported!");
00119 }
00120 
00121 } // end of namespace PLearn
00122 
00123 
00124 /*
00125   Local Variables:
00126   mode:c++
00127   c-basic-offset:4
00128   c-file-style:"stroustrup"
00129   c-file-offsets:((innamespace . 0)(inline-open . 0))
00130   indent-tabs-mode:nil
00131   fill-column:79
00132   End:
00133 */
00134 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines