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

Revision 660, 4.4 kB (checked in by smidl, 15 years ago)

doc - doxygen warnings

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