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

logger refactoring

Files:
1 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        //!@}