Changeset 676 for library

Show
Ignore:
Timestamp:
10/22/09 01:13:47 (15 years ago)
Author:
smidl
Message:

logger refactoring

Location:
library/bdm
Files:
9 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/bdmbase.h

    r675 r676  
    298298RV concat ( const RV &rv1, const RV &rv2 ); 
    299299 
     300/*! 
     301@brief Class for storing results (and semi-results) of an experiment 
     302 
     303This class abstracts logging of results from implementation. This class replaces direct logging of results (e.g. to files or to global variables) by calling methods of a logger. Specializations of this abstract class for specific storage method are designed. 
     304*/ 
     305class logger : public root { 
     306        protected: 
     307                //! RVs of all logged variables. 
     308                Array<RV> entries; 
     309                //! Names of logged quantities, e.g. names of algorithm variants 
     310                Array<string> names; 
     311                //!separator of prefixes of entries 
     312                const string separator; 
     313        public: 
     314                //!Default constructor 
     315                logger(const string separator0) : entries ( 0 ), names ( 0 ), separator(separator0) {} 
     316                 
     317                //! returns an identifier which will be later needed for calling the \c logit() function 
     318                //! For empty RV it returns -1, this entry will be ignored by \c logit(). 
     319                virtual int add ( const RV &rv, string prefix = "" ) { 
     320                        int id; 
     321                        if ( rv._dsize() > 0 ) { 
     322                                id = entries.length(); 
     323                                names = concat ( names, prefix ); // diff 
     324                                entries.set_length ( id + 1, true ); 
     325                                entries ( id ) = rv; 
     326                        } else { 
     327                                id = -1; 
     328                        } 
     329                        return id; // identifier of the last entry 
     330                } 
     331                 
     332                //! log this vector 
     333                virtual void logit ( int id, const vec &v ) { 
     334                        bdm_error ( "Not implemented" ); 
     335                }; 
     336                //! log this double 
     337                virtual void logit ( int id, const double &d ) { 
     338                        bdm_error ( "Not implemented" ); 
     339                }; 
     340                 
     341                //! Shifts storage position for another time step. 
     342                virtual void step() { 
     343                        bdm_error ( "Not implemneted" ); 
     344                }; 
     345                 
     346                //! Finalize storing information 
     347                virtual void finalize() {}; 
     348                 
     349                //! Initialize the storage 
     350                virtual void init() {}; 
     351                 
     352                //!separator of prefixes for this logger 
     353                const string& prefix_sep() {return separator;} 
     354}; 
     355 
     356 
    300357//! Class representing function \f$f(x)\f$ of variable \f$x\f$ represented by \c rv 
    301358class fnc : public root { 
     
    532589                //bdm_assert_debug ( isnamed(), "" ); 
    533590                return rv; 
     591        } 
     592        //! store values of the epdf on the following levels: 
     593        //!  #1 mean 
     594        //!  #2 mean + lower & upper bound 
     595        void log_register(logger &L, const string &prefix){ 
     596                RV r; 
     597                if ( isnamed() ) { 
     598                        r = _rv(); 
     599                } else { 
     600                        r = RV ( "", dimension() ); 
     601                }; 
     602                root::log_register(L,prefix); 
     603                logrec->ids.set_size(3); 
     604                if (log_level >0){ 
     605                        logrec->ids(0) = logrec->L.add ( r, prefix + logrec->L.prefix_sep()+ "mean" ); 
     606                } 
     607                if (log_level >1){ 
     608                        logrec->ids(1) = logrec->L.add ( r, prefix + logrec->L.prefix_sep()+ "lb" ); 
     609                        logrec->ids(2) = logrec->L.add ( r, prefix + logrec->L.prefix_sep()+ "ub" ); 
     610                } 
    534611        } 
    535612        //!@} 
     
    861938}; 
    862939 
    863 /*! 
    864 @brief Class for storing results (and semi-results) of an experiment 
    865  
    866 This class abstracts logging of results from implementation. This class replaces direct logging of results (e.g. to files or to global variables) by calling methods of a logger. Specializations of this abstract class for specific storage method are designed. 
    867  */ 
    868 class logger : public root { 
    869 protected: 
    870         //! RVs of all logged variables. 
    871         Array<RV> entries; 
    872         //! Names of logged quantities, e.g. names of algorithm variants 
    873         Array<string> names; 
    874 public: 
    875         //!Default constructor 
    876         logger() : entries ( 0 ), names ( 0 ) {} 
    877  
    878         //! returns an identifier which will be later needed for calling the \c logit() function 
    879         //! For empty RV it returns -1, this entry will be ignored by \c logit(). 
    880         virtual int add ( const RV &rv, string prefix = "" ) { 
    881                 int id; 
    882                 if ( rv._dsize() > 0 ) { 
    883                         id = entries.length(); 
    884                         names = concat ( names, prefix ); // diff 
    885                         entries.set_length ( id + 1, true ); 
    886                         entries ( id ) = rv; 
    887                 } else { 
    888                         id = -1; 
    889                 } 
    890                 return id; // identifier of the last entry 
    891         } 
    892  
    893         //! log this vector 
    894         virtual void logit ( int id, const vec &v ) { 
    895                 bdm_error ( "Not implemented" ); 
    896         }; 
    897         //! log this double 
    898         virtual void logit ( int id, const double &d ) { 
    899                 bdm_error ( "Not implemented" ); 
    900         }; 
    901  
    902         //! Shifts storage position for another time step. 
    903         virtual void step() { 
    904                 bdm_error ( "Not implemneted" ); 
    905         }; 
    906  
    907         //! Finalize storing information 
    908         virtual void finalize() {}; 
    909  
    910         //! Initialize the storage 
    911         virtual void init() {}; 
    912  
    913 }; 
    914  
    915940 
    916941//! \brief Combines RVs from a list of mpdfs to a single one. 
     
    946971                //!Description of output data 
    947972                RV Yrv; // 
    948         //! Remember its own index in Logger L, [0=dt, 1=ut] 
    949         ivec LIDs; 
    950973public: 
    951974        //! default constructors 
    952                 DS() : Drv(), Urv(),Yrv(), LIDs(2) {}; 
     975                DS() : Drv(), Urv(),Yrv() {log_level=1;}; 
    953976 
    954977        //! Returns maximum number of provided data, by default it is set to maximum allowed length, shorter DS should overload this method! See, MemDS.max_length(). 
    955978        virtual int max_length() {return std::numeric_limits< int >::max();} 
    956979        //! Returns full vector of observed data=[output, input] 
    957         virtual void getdata ( vec &dt ) { 
     980        virtual void getdata ( vec &dt ) const { 
    958981                bdm_error ( "abstract class" ); 
    959982        } 
     
    9801003 
    9811004        //! Register DS for logging into logger L 
    982         virtual void log_add ( logger &L ) { 
    983                 bdm_assert ( dtsize == Drv._dsize(), "invalid DS: dtsize (" + num2str ( dtsize ) + ") different from Drv " + num2str ( Drv._dsize() ) ); 
     1005        virtual void log_register (logger &L,  const string &prefix ) { 
     1006                bdm_assert ( ytsize == Yrv._dsize(), "invalid DS: ytsize (" + num2str ( ytsize ) + ") different from Drv " + num2str ( Yrv._dsize() ) ); 
    9841007                bdm_assert ( utsize == Urv._dsize(), "invalid DS: utsize (" + num2str ( utsize ) + ") different from Urv " + num2str ( Urv._dsize() ) ); 
    9851008 
    986                 LIDs(0) = L.add ( Drv, "" ); 
    987                 LIDs(1) = L.add ( Urv, "" ); 
     1009                root::log_register(L,prefix); 
     1010                //we know that  
     1011                if (log_level >0){ 
     1012                        logrec->ids.set_size(2); 
     1013                        logrec->ids(0) = logrec->L.add ( Yrv, "" ); 
     1014                        logrec->ids(1) = logrec->L.add ( Urv, "" ); 
     1015                } 
    9881016        } 
    9891017        //! Register DS for logging into logger L 
    990         virtual void logit ( logger &L ) { 
    991                 vec tmp ( Drv._dsize() + Urv._dsize() ); 
    992                 getdata ( tmp ); 
    993                 // d is first in getdata 
    994                 L.logit ( LIDs(0), tmp.left ( Drv._dsize() ) ); 
    995                 // u follows after d in getdata 
    996                 L.logit ( LIDs(1), tmp.mid ( Drv._dsize(), Urv._dsize() ) ); 
     1018        virtual void log_write ( ) const { 
     1019                if (log_level >0) { 
     1020                        vec tmp ( Yrv._dsize() + Urv._dsize()); 
     1021                        getdata ( tmp ); 
     1022                        // d is first in getdata 
     1023                        logrec->L.logit ( logrec->ids(0), tmp.left ( Yrv._dsize() ) ); 
     1024                        // u follows after d in getdata 
     1025                        logrec->L.logit ( logrec->ids(1), tmp.mid ( Yrv._dsize(), Urv._dsize() ) ); 
     1026                } 
    9971027        } 
    9981028        //!access function 
    9991029        virtual const RV& _drv() const { 
    1000                 //              return concat (Drv, Urv);// why!!! 
    1001                 return Drv;// why!!! 
     1030                return Drv; 
    10021031        } 
    10031032        //!access function 
     
    10051034                return Urv; 
    10061035        } 
    1007                 //!access function 
    1008                 const RV& _yrv() const { 
    1009                         return Yrv; 
    1010                 } 
     1036        //!access function 
     1037        const RV& _yrv() const { 
     1038                return Yrv; 
     1039        } 
    10111040        //! set random variables 
    1012                 virtual void set_drv (const  RV &yrv, const RV &urv) { 
    1013                     Yrv = yrv; 
    1014                         Drv = concat(yrv,urv); 
     1041        virtual void set_drv (const  RV &yrv, const RV &urv) { 
     1042                Yrv = yrv; 
     1043                Drv = concat(yrv,urv); 
    10151044                Urv = urv; 
    10161045        } 
     
    10461075        //!  If true, the filter will compute likelihood of the data record and store it in \c ll . Set to false if you want to save computational time. 
    10471076        bool evalll; 
     1077         
    10481078public: 
    10491079        //! \name Constructors 
    10501080        //! @{ 
    10511081 
    1052         BM() : ll ( 0 ), evalll ( true ), LIDs ( 4 ), LFlags ( 4 ) { 
    1053                 LIDs = -1;/*empty IDs*/ 
    1054                 LFlags = 0; 
    1055                 LFlags ( 0 ) = 1;  /*log only mean*/ 
    1056         }; 
     1082        BM() : ll ( 0 ), evalll ( true ) { }; 
    10571083        BM ( const BM &B ) :  drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {} 
    10581084        //! \brief Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! 
     
    11511177        //! Set boolean options from a string, recognized are: "logbounds,logll" 
    11521178        virtual void set_options ( const string &opt ) { 
    1153                 LFlags ( 0 ) = 1; 
    11541179                if ( opt.find ( "logbounds" ) != string::npos ) { 
    1155                         LFlags ( 1 ) = 1; 
    1156                         LFlags ( 2 ) = 1; 
     1180                        const_cast<epdf&>(posterior()).set_log_level(2) ; 
     1181                } else { 
     1182                        const_cast<epdf&>(posterior()).set_log_level(1) ; 
    11571183                } 
    11581184                if ( opt.find ( "logll" ) != string::npos ) { 
    1159                         LFlags ( 3 ) = 1; 
    1160                 } 
    1161         } 
    1162         //! IDs of storages in loggers 4:[1=mean,2=lb,3=ub,4=ll] 
    1163         ivec LIDs; 
    1164  
    1165         //! Flags for logging - same size as LIDs, each entry correspond to the same in LIDs 
    1166         ivec LFlags; 
     1185                        log_level = 1; 
     1186                } 
     1187        } 
     1188 
    11671189        //! Add all logged variables to a logger 
    1168         virtual void log_add ( logger &L, const string &name = "" ) { 
    1169                 // internal 
    1170                 RV r; 
    1171                 if ( posterior().isnamed() ) { 
    1172                         r = posterior()._rv(); 
    1173                 } else { 
    1174                         r = RV ( "est", posterior().dimension() ); 
    1175                 }; 
    1176  
    1177                 // Add mean value 
    1178                 if ( LFlags ( 0 ) ) LIDs ( 0 ) = L.add ( r, name + "mean_" ); 
    1179                 if ( LFlags ( 1 ) ) LIDs ( 1 ) = L.add ( r, name + "lb_" ); 
    1180                 if ( LFlags ( 2 ) ) LIDs ( 2 ) = L.add ( r, name + "ub_" ); 
    1181                 if ( LFlags ( 3 ) ) LIDs ( 3 ) = L.add ( RV ( "ll", 1 ), name );    //TODO: "local" RV 
    1182         } 
    1183         virtual void logit ( logger &L ) { 
    1184                 L.logit ( LIDs ( 0 ), posterior().mean() ); 
    1185                 if ( LFlags ( 1 ) || LFlags ( 2 ) ) {  //if one of them is off, its LID==-1 and will not be stored 
    1186                         vec ub, lb; 
    1187                         posterior().qbounds ( lb, ub ); 
    1188                         L.logit ( LIDs ( 1 ), lb ); 
    1189                         L.logit ( LIDs ( 2 ), ub ); 
    1190                 } 
    1191                 if ( LFlags ( 3 ) ) L.logit ( LIDs ( 3 ), ll ); 
     1190        //! Log levels two digits: xy where 
     1191        //!  * y = 0/1 log-likelihood is to be logged 
     1192        //!  * x = level of the posterior (typically 0/1/2 for nothing/mean/bounds) 
     1193        virtual void log_register ( logger &L, const string &prefix = "" ) { 
     1194                root::log_register(L,prefix);            
     1195 
     1196                const_cast<epdf&>(posterior()).log_register(L, prefix+L.prefix_sep()+"apost");  
     1197                 
     1198                if ((log_level) > 0){ 
     1199                        logrec->ids.set_size(1); 
     1200                        logrec->ids(0) = L.add(RV("ll",1), prefix+L.prefix_sep()+"ll"); 
     1201                } 
     1202        } 
     1203        //! Save results to the given logger, details of what is stored is configured by \c LIDs and \c options 
     1204        virtual void log_write ( ) const { 
     1205                posterior().log_write(); 
     1206                if (log_level >0) { logrec->L.logit ( logrec->ids ( 0 ), ll );} 
    11921207        } 
    11931208        //!@} 
  • library/bdm/base/datasources.cpp

    r665 r676  
    44using namespace bdm; 
    55 
    6 void MemDS::getdata ( vec &dt ) { 
     6void MemDS::getdata ( vec &dt ) const { 
    77        int i; 
    88 
  • library/bdm/base/datasources.h

    r660 r676  
    3838        public: 
    3939                int max_length() {return Data.cols();} 
    40                 void getdata ( vec &dt ); 
     40                void getdata ( vec &dt ) const; 
    4141                void getdata ( vec &dt, const ivec &indeces ); 
    4242                void set_drv (const RV &drv,const  RV &urv ); 
     
    8989                        set_drv(*r,RV()); //empty urv 
    9090                        dtsize=r->_dsize(); 
     91                        ytsize = dtsize; 
    9192                        utsize=0; 
    9293                } 
     
    109110                        dt=iepdf->sample(); 
    110111                } 
    111                 void getdata ( vec &dt_out ) { 
     112                void getdata ( vec &dt_out ) const { 
    112113                        dt_out = dt; 
    113114                } 
     
    167168                        ut2rgr.step(ut); //u is now history 
    168169                } 
    169                 void getdata ( vec &dt_out ) { 
     170                void getdata ( vec &dt_out ) const { 
    170171                        bdm_assert_debug(dt_out.length()>=utsize+ytsize,"Short output vector"); 
    171172                        dt_out.set_subvector(0, yt); 
     
    249250                } 
    250251                //! no sense to log this type 
    251                 void log_add ( logger &L ) {}; 
     252                void log_register(logger &L, const string &prefix){}; 
    252253                //! no sense to log this type 
    253                 void logit ( logger &L ) {}; 
     254                void log_write ( ) const {}; 
    254255}; 
    255256 
     
    341342                } 
    342343 
    343                 virtual void log_add ( logger &L ) { 
    344                         DS::log_add ( L ); 
    345                         L_xt = L.add ( IM->_rv(), "true" ); 
    346                 } 
    347                 virtual void logit ( logger &L ) { 
    348                         DS::logit ( L ); 
    349                         L.logit ( L_xt, xt ); 
     344                virtual void log_register(logger &L, const string &prefix){ 
     345                        DS::log_register ( L, prefix ); //ids 0 and 1 
     346                         
     347                        logrec->ids.set_size(3,true);//copy 
     348                        logrec->ids(2)=logrec->L.add ( IM->_rv(), "true" ); 
     349                } 
     350                virtual void log_write () { 
     351                        DS::log_write ( ); 
     352                        logrec->L.logit ( logrec->ids(2), xt ); 
    350353                } 
    351354 
  • library/bdm/base/loggers.cpp

    r565 r676  
    7878                        // for non-empty names 
    7979                        if ( names ( i ).length() > 0 ) { 
    80                                 scalarnames ( ii ) = names ( i ) + "_"; 
     80                                scalarnames ( ii ) = names ( i ) + separator; 
    8181                        } 
    8282                        // add name 
     
    9191                                for ( k = 0; k < rvsize; k++ ) { //for all scalars in given RV 
    9292                                        sprintf ( num, "%d", k ); 
    93                                         scalarnames ( ii ) += ( std::string ) "_" + num; 
     93                                        scalarnames ( ii ) += separator + num; 
    9494                                        ii++; 
    9595                                } 
  • library/bdm/base/loggers.h

    r660 r676  
    3636public: 
    3737        //! convenience constructor 
    38         memlog ( int maxlen0, string itf = "" ) : maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), itfilename ( itf ) {} 
     38        memlog ( int maxlen0, string itf = "" ) :logger("."), maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), itfilename ( itf ) {} 
    3939 
    4040        //!Default constructor 
    41         memlog() : maxlen ( 0 ), ind ( 0 ), vectors ( 0 ) {} 
     41        memlog() : logger("."), maxlen ( 0 ), ind ( 0 ), vectors ( 0 ) {} 
    4242 
    4343        //! Initialize storage 
  • library/bdm/bdmroot.h

    r673 r676  
     1 
    12/*! 
    23  \file 
     
    2425 
    2526namespace bdm { 
    26  
     27         
     28//forward declaration 
     29class logger; 
     30         
     31//! information about connection to a logger 
     32class log_record { 
     33public: 
     34        //!remember which logger is registered  
     35        logger &L; 
     36        //! vector of log IDs - one element for each entry 
     37        ivec ids; 
     38         
     39        //!default constructor 
     40        log_record(logger &L0): L(L0),ids(0){} 
     41}; 
     42         
    2743//! Root class of BDM objects 
    2844class root { 
    2945        protected: 
    30         //! level of details that will be logged to logger 
     46        //! record of connections to the logger 
     47        log_record* logrec; 
     48        //! level of details that will be logged to a logger 
    3149        int log_level; 
    32         //! vector of log IDs - one element for each entry 
    33         ivec log_ids; 
     50         
    3451public: 
     52        //!default constructor 
     53        root() : logrec(NULL),log_level(0) {}; 
     54         
    3555        //! make sure this is a virtual object 
    3656        virtual ~root() { 
     57                if (logrec) delete logrec; 
    3758        } 
    3859 
    39         //! This method returns a basic info about the current instance 
     60        //! Returns a basic textual info about the current instance 
    4061        virtual string to_string() const { 
    4162                return ""; 
    4263        } 
     64        //! Register itself in a logger, i.e. allocate space for data from this class 
     65        //! The level of details (parameter \c level ) is individual for each class. 
     66        virtual void log_register(logger &L, const string &prefix){ 
     67                logrec = new log_record(L); 
     68        } 
     69         
     70        //! Write current information into the given logger  
     71        virtual void log_write() const { 
     72        } 
     73        //! set level of details to be logged - needs to be called before log_register! 
     74        virtual void set_log_level(int level) {log_level = level;} 
    4375 
    44         //! This method arrange instance properties according the data stored in the Setting structure 
     76        //! Read instance properties according the data stored in the Setting structure 
    4577        virtual void from_setting ( const Setting &set ) { 
    4678        } 
    4779 
    48         //! This method save all the instance properties into the Setting structure 
     80        //! Save all the instance properties into the Setting structure 
    4981        virtual void to_setting ( Setting &set ) const { 
    5082        } 
    5183 
    52         //! This method checks that all internal structures has been correctly set-up, always call at the ned of from_setting 
     84        //! Check that all internal structures has been correctly set-up. Called at the end of from_setting. 
    5385        virtual void validate() { 
    5486        } 
     87         
    5588}; 
    5689 
  • library/bdm/design/ctrlbase.h

    r660 r676  
    146146                        const RV& _drv() {return drv;} 
    147147                        //! register this controller with given datasource under name "name" 
    148                         virtual void log_add ( logger &L, const string &name = "" ) { } 
     148                        virtual void log_register (logger &L,  int level, const string &prefix ) { } 
    149149                        //! write requested values into the logger 
    150                         virtual void logit ( logger &L ) { } 
     150                        virtual void log_write ( ) const { } 
    151151                         
    152152        }; 
  • library/bdm/estim/particles.h

    r675 r676  
    5050        //! \name Options 
    5151        //!@{ 
    52  
    53         //! Log all samples 
    54         bool opt_L_smp; 
    55         //! Log all samples 
    56         bool opt_L_wei; 
    5752        //!@} 
    5853 
     
    6055        //! \name Constructors 
    6156        //!@{ 
    62         PF ( ) : est(), _w ( est._w() ), _samples ( est._samples() ), opt_L_smp ( false ), opt_L_wei ( false ) { 
    63                 LIDs.set_size ( 5 ); 
     57        PF ( ) : est(), _w ( est._w() ), _samples ( est._samples() ) { 
    6458        }; 
    6559         
     
    8983        void set_options ( const string &opt ) { 
    9084                BM::set_options ( opt ); 
    91                 opt_L_wei = ( opt.find ( "logweights" ) != string::npos ); 
    92                 opt_L_smp = ( opt.find ( "logsamples" ) != string::npos ); 
    9385        } 
    9486        //! bayes I - generate samples and add their weights to lls 
  • library/bdm/mex/mex_datasource.h

    r660 r676  
    4545                rowid = linspace(0,Data.rows()-1); 
    4646                dtsize=rowid.length(); 
     47                ytsize=rowid.length(); 
    4748                utsize=0; 
    4849                 
     
    126127        } 
    127128        void write(const vec &ut0){ ut=ut0;} 
    128         void getdata(vec &dt_out){dt_out = dt;  } 
     129        void getdata(vec &dt_out) const {dt_out = dt;   } 
    129130 
    130131