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