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

Revision 728, 4.6 kB (checked in by smidl, 15 years ago)

logger now has ability to store settings - used in estimator. New mexfunction epdf_mean

  • 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  file to save results (IT file for vectors and cfg for settings)
35        string filename;
36public:
37        //! convenience constructor
38        memlog ( int maxlen0, string fn = "" ) :logger("_"), maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), filename ( fn ) {}
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 ( filename.length() > 0 ) {
69                        itsave ( (filename+".it").c_str());
70                        if (settings.length()>0){
71                                setting_conf.writeFile( (filename+".cfg").c_str());
72                        }
73                }
74               
75        };
76
77
78        /*! \brief UI for memlog
79
80        TODO dat tam kam patri, a to celej blok
81
82        \code
83        logger = {
84                class = "memlog";
85                filename = "file_name"; // resulting filename with vectors in it format and setting in cfg
86                maxlen = 100;           // size of memory buffer
87        }
88        \endcode
89         */
90        void from_setting ( const Setting &set );
91
92        // TODO dodelat void to_setting( Setting &set ) const;
93};
94
95UIREGISTER ( memlog );
96SHAREDPTR ( memlog );
97
98
99/*! \brief Simple logger used for debugging
100All data records are written out to std from where they could be send to file
101*/
102class stdlog: public memlog{
103        public:
104                //! default constructor
105                stdlog():memlog(1){};
106               
107                void init() {
108                        memlog::init();
109                        for (int i=0; i<entries.length();i++){
110                                if (entries(i)._dsize()==1) {
111                                        cout << names(i) << entries(i).name(0) << "\t";
112                                }
113                                else
114                                for (int j=0; j<vectors(i).cols(); j++){
115                                        cout << names(i) << entries(i).scalarname(j) << "\t";
116                                }
117                        }
118                        cout << endl;
119                       
120                       
121                }
122                //!writes everything out
123                void step() {
124                        for (int i=0; i<vectors.length();i++){
125                                for (int j=0; j<vectors(i).cols(); j++){
126                                        cout << vectors(i)(0,j) << "\t";
127                                }
128                        }
129                        cout << endl;
130                }
131                /*! \brief UI for stdlog
132               
133                TODO dat tam kam patri, a to celej blok
134               
135                \code
136                logger = {
137                        class = "stdlog"; // no parameterization needed
138                }
139                \endcode
140                */
141                void from_setting ( const Setting &set ){
142                }
143               
144};
145UIREGISTER(stdlog);
146
147/*!
148* \brief Logging into dirfile with buffer in memory
149
150* Dirfile is a special format used by the kst program. See documentation of kst for description.
151*
152* This format is used to store scalars, hence multivariate RVs must be separated.
153*/
154class dirfilelog : public memlog {
155
156protected:
157        //!name of the directory
158        string dirname;
159        //! Automatically generated
160        Array<string> scalarnames;
161public:
162        /*!\brief Default constructor
163        @param dirname0 name of the directory in which to store the results
164        @param maxlen0 length of the memory buffers, when full the buffers will be dumped to HDD and returned to the beginning. */
165        dirfilelog ( string dirname0, int maxlen0 ) : memlog ( maxlen0 ), dirname ( dirname0 ), scalarnames ( 0 ) {}
166
167        dirfilelog() {}
168
169        //! Initialize storage
170        void init();
171        void step();
172        void finalize();
173        /*! \brief Write memory storage to disk.
174        @param Len length of buffer to be written, if 0 the file is truncated at 0.
175        */
176        void write_buffers ( int Len );
177
178        /*! \brief UI for dirfilelog (Kst file format)
179        \code
180        logger = {
181                class = "dirfilelog";
182                dirname = "directory_for_files"; // resulting files will be stored there
183                maxlen = 100;                    // size of memory buffer, when full results are written to disk
184        }
185        \endcode
186        */
187        void from_setting ( const Setting &set );
188
189        // TODO dodelat void to_setting( Setting &set ) const;
190};
191
192UIREGISTER ( dirfilelog );
193SHAREDPTR ( dirfilelog );
194
195};
196#endif // LGR_H
Note: See TracBrowser for help on using the browser.