root/library/bdm/base/loggers.h @ 727

Revision 727, 4.5 kB (checked in by smidl, 15 years ago)

Logger change. Loggers can now store settings.

Unit Tests and fixes.

  • Property svn:eol-style set to native
Line 
1/*!
2  \file
3  \brief Loggers for storing results of experiments
4  \author Vaclav Smidl.
5
6  -----------------------------------
7  BDM++ - C++ library for Bayesian Decision Making under Uncertainty
8
9  Using IT++ for numerical operations
10  -----------------------------------
11*/
12
13#ifndef LGR_H
14#define LGR_H
15
16#include "bdmbase.h"
17#include "user_info.h"
18
19namespace bdm {
20using std::string;
21
22/*!
23 * Logging into matrices in data format in memory, optionally persisted into a file.
24 */
25class memlog : public logger {
26
27protected:
28        //! Maximum length of vectors stored in memory
29        int maxlen;
30        //! Currect record to be written
31        int ind;
32        //! Storage
33        Array<mat> vectors;
34        //! name of it file to save results
35        string itfilename;
36public:
37        //! convenience constructor
38        memlog ( int maxlen0, string itf = "" ) :logger("_"), maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), itfilename ( itf ) {}
39
40        //!Default constructor
41        memlog() : logger("_"), maxlen ( 0 ), ind ( 0 ), vectors ( 0 ) {}
42
43        //! Initialize storage
44        void init();
45
46        void step() {
47                if ( ind < maxlen ) ind++;
48                else bdm_error ( "memlog::ind is too high;" );
49        }
50
51        void log_vector ( int id, const vec &v ) {
52                bdm_assert ( id < vectors.length(), "Logger was not initialized, run init()." );
53                if ( id >= 0 ) {
54                        vectors ( id ).set_row ( ind, v );
55                }
56        }
57        void logit ( int id, const double &d ) {
58                bdm_assert_debug ( id < vectors.length(), "Logger was not initialized, run init()." );
59                bdm_assert_debug ( vectors ( id ).cols() == 1, "Vector expected" );
60                if ( id >= 0 ) {
61                        vectors ( id ) ( ind ) = d;
62                }
63        }
64        //! Save values into an itfile named after \c fname.
65        void itsave ( const char* fname );
66        //!
67        void finalize() {
68                if ( itfilename.length() > 0 ) itsave ( itfilename.c_str() );
69                if (settings.length()>0){
70                        setting_conf.writeFile("memlog_setting.cfg");
71                }
72        };
73
74
75        /*! \brief UI for memlog
76
77        TODO dat tam kam patri, a to celej blok
78
79        \code
80        logger = {
81                class = "memlog";
82                filename = "file_name.it"; // resulting filename with results in it format
83                maxlen = 100;          // size of memory buffer
84        }
85        \endcode
86         */
87        void from_setting ( const Setting &set );
88
89        // TODO dodelat void to_setting( Setting &set ) const;
90};
91
92UIREGISTER ( memlog );
93SHAREDPTR ( memlog );
94
95
96/*! \brief Simple logger used for debugging
97All data records are written out to std from where they could be send to file
98*/
99class stdlog: public memlog{
100        public:
101                //! default constructor
102                stdlog():memlog(1){};
103               
104                void init() {
105                        memlog::init();
106                        for (int i=0; i<entries.length();i++){
107                                if (entries(i)._dsize()==1) {
108                                        cout << names(i) << entries(i).name(0) << "\t";
109                                }
110                                else
111                                for (int j=0; j<vectors(i).cols(); j++){
112                                        cout << names(i) << entries(i).scalarname(j) << "\t";
113                                }
114                        }
115                        cout << endl;
116                       
117                       
118                }
119                //!writes everything out
120                void step() {
121                        for (int i=0; i<vectors.length();i++){
122                                for (int j=0; j<vectors(i).cols(); j++){
123                                        cout << vectors(i)(0,j) << "\t";
124                                }
125                        }
126                        cout << endl;
127                }
128                /*! \brief UI for stdlog
129               
130                TODO dat tam kam patri, a to celej blok
131               
132                \code
133                logger = {
134                        class = "stdlog"; // no parameterization needed
135                }
136                \endcode
137                */
138                void from_setting ( const Setting &set ){
139                }
140               
141};
142UIREGISTER(stdlog);
143
144/*!
145* \brief Logging into dirfile with buffer in memory
146
147* Dirfile is a special format used by the kst program. See documentation of kst for description.
148*
149* This format is used to store scalars, hence multivariate RVs must be separated.
150*/
151class dirfilelog : public memlog {
152
153protected:
154        //!name of the directory
155        string dirname;
156        //! Automatically generated
157        Array<string> scalarnames;
158public:
159        /*!\brief Default constructor
160        @param dirname0 name of the directory in which to store the results
161        @param maxlen0 length of the memory buffers, when full the buffers will be dumped to HDD and returned to the beginning. */
162        dirfilelog ( string dirname0, int maxlen0 ) : memlog ( maxlen0 ), dirname ( dirname0 ), scalarnames ( 0 ) {}
163
164        dirfilelog() {}
165
166        //! Initialize storage
167        void init();
168        void step();
169        void finalize();
170        /*! \brief Write memory storage to disk.
171        @param Len length of buffer to be written, if 0 the file is truncated at 0.
172        */
173        void write_buffers ( int Len );
174
175        /*! \brief UI for dirfilelog (Kst file format)
176        \code
177        logger = {
178                class = "dirfilelog";
179                dirname = "directory_for_files"; // resulting files will be stored there
180                maxlen = 100;                    // size of memory buffer, when full results are written to disk
181        }
182        \endcode
183        */
184        void from_setting ( const Setting &set );
185
186        // TODO dodelat void to_setting( Setting &set ) const;
187};
188
189UIREGISTER ( dirfilelog );
190SHAREDPTR ( dirfilelog );
191
192};
193#endif // LGR_H
Note: See TracBrowser for help on using the browser.