libeblearntools
/home/rex/ebltrunk/tools/libeblearntools/include/metaparser.h
00001 /***************************************************************************
00002  *   Copyright (C) 2009 by Pierre Sermanet   *
00003  *   pierre.sermanet@gmail.com   *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source 
00014  *       Initiative (http://www.opensource.org) must display the 
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING 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 
00033 #ifndef METAPLOT_H_
00034 #define METAPLOT_H_
00035 
00036 #include <sstream>
00037 #include <stdlib.h>
00038 
00039 #ifndef __WINDOWS__
00040 #include <sys/wait.h>
00041 #include <unistd.h>
00042 #endif
00043 
00044 #include <stdio.h>
00045 #include <map>
00046 #include <list>
00047 
00048 #include "configuration.h"
00049 #include "sort.h"
00050 
00051 #define VALUE_SEPARATOR '='
00052 
00053 using namespace std;
00054 
00055 namespace ebl {
00056 
00058   // iteration
00059 
00061   typedef map<uint,map<uint,uint> > natural_varmap;
00062   typedef list<map<uint,uint> > varmaplist;
00063 
00069   class EXPORT pairtree {
00070   public:
00074     pairtree(string &var, string &val);
00076     pairtree(uint var, uint val);
00078     pairtree();
00080     virtual ~pairtree();
00081 
00082     // data addition methods ///////////////////////////////////////////////////
00083 
00086     static uint get_var_id(const string &var);
00089     static uint get_var_id(const char *var);
00092     static uint get_val_id(const string &val);
00095     static list<uint> to_varid_list(list<string> &l);
00097     void add(const string &var, string &val);
00100     map<uint,uint> add(list<uint> &subvar, map<uint,uint> &ivars);
00101 
00102     // flattening methods //////////////////////////////////////////////////////
00103 
00110     natural_varmap flatten(const string &key, natural_varmap *flat = NULL,
00111                            map<uint,uint> *path = NULL);
00114     varmaplist flatten(varmaplist *flat = NULL,
00115                        map<uint,uint> *path = NULL);
00118     natural_varmap best(const string &key, uint n = 0, bool display = false);
00123     varmaplist best(list<string> &keys, uint n = 0, bool display = false,
00124                     int maxiter = -1);
00128     varmaplist best(list<string> &keys, const string &key,
00129                     bool display = false);
00134     varmaplist best(list<string> &keys, list<string> &keycomb,
00135                     bool display = false);
00136 
00138     uint& get_variable();
00139 
00141     uint& get_value();
00142 
00144     void pretty(string offset = "");
00145 
00149     static string flat_to_string(const string key, natural_varmap *flat = NULL);
00154     static string flat_to_string(varmaplist *flat = NULL,
00155                                  list<string> *keys = NULL);
00159     void pretty_flat(const string key, natural_varmap *flat = NULL);
00164     void pretty_flat(varmaplist *flat = NULL, list<string> *keys = NULL);
00166     uint get_max_uint(const string &var);
00167 
00169     bool exists(const string &var);
00170  
00173     bool delete_pair(const char *var, const char *value);
00174 
00176     map<uint,pairtree>& get_subtree();
00177 
00179     // members
00180   private:
00181     uint                        variable;       
00182     uint                        value;  
00183     uint                        subvariable;    
00184 
00185 
00186     map<uint,pairtree>                  subtree;
00187     map<uint,uint>              vars;
00188     static map<string,uint> vars_map; 
00189     static map<string,uint> vals_map; 
00190   public:
00191     static vector<string> vars_vector; 
00192     static vector<string> vals_vector; 
00193   };
00194   
00196   // metaparser
00197   
00198   typedef map<uint,pairtree> t_subtree;
00199 
00202   class EXPORT metaparser {
00203   public:
00208     metaparser();
00209 
00211     virtual ~metaparser();
00212 
00216     void parse_logs(const string &root, list<string> *sticky = NULL,
00217                     list<string> *watch = NULL);
00218 
00227     void organize_plot(list<string> &names, varmaplist &flat,
00228                        pairtree &p);
00229 
00233     void write_plots(configuration &conf, const char *dir = NULL,
00234                      pairtree *p = NULL, string *prefix = NULL);
00235 
00237     natural_varmap best(const string &key, uint n, bool display = false);
00239     varmaplist best(list<string> &keys, uint n, bool display = false);
00240 
00243     int get_max_iter();
00244     
00247     int get_max_common_iter();
00248     
00253     int get_max_common_iter(configuration &conf, const string &dir);
00254 
00256     void process(const string &dir);
00257 
00261     varmaplist analyze(configuration &conf, const string &dir, 
00262                        int &maxiter, varmaplist &besteach, 
00263                        bool displayall = false,
00264                        int *maxiter_common = NULL, 
00265                        varmaplist *best_common = NULL); 
00266 
00269     void send_report(configuration &conf, const string dir,
00270                      varmaplist &best, int max_iter, 
00271                      string conf_fullfname,
00272                      string jobs_info, uint nrunning = 0,
00273                      double maxminutes = 0.0, double minminutes = 0.0,
00274                      varmaplist *besteach = NULL,
00275                      varmaplist *best_common = NULL, 
00276                      int *maxiter_common = NULL);
00277 
00279     // internal methods
00280   private:
00283     bool parse_log(const string &fname, list<string> *sticky = NULL,
00284                    list<string> *watch = NULL);
00285     
00287     // members
00288   private:
00289     pairtree            tree;           
00290     char                separator;      
00291     map<string,string>  curpath;        
00292     list<uint>  hierarchy;      
00293   };
00294 
00295 } // end namespace ebl
00296 
00297 #endif /* METAPLOT_H_ */