[92] | 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 | |
---|
[387] | 16 | #include "bdmbase.h" |
---|
| 17 | #include "user_info.h" |
---|
[92] | 18 | |
---|
[477] | 19 | namespace bdm { |
---|
[92] | 20 | using std::string; |
---|
| 21 | |
---|
| 22 | /*! |
---|
[425] | 23 | * Logging into matrices in data format in memory, optionally persisted into a file. |
---|
| 24 | */ |
---|
[92] | 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; |
---|
[347] | 34 | //! name of it file to save results |
---|
| 35 | string itfilename; |
---|
[92] | 36 | public: |
---|
[477] | 37 | memlog ( int maxlen0, string itf = "" ) : maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), itfilename ( itf ) {} |
---|
[357] | 38 | |
---|
[548] | 39 | //!Default constructor |
---|
| 40 | memlog() : maxlen ( 0 ), ind ( 0 ), vectors ( 0 ) {} |
---|
[357] | 41 | |
---|
[92] | 42 | //! Initialize storage |
---|
[551] | 43 | void init(); |
---|
| 44 | |
---|
[477] | 45 | void step() { |
---|
| 46 | if ( ind < maxlen ) ind++; |
---|
[565] | 47 | else bdm_error ( "memlog::ind is too high;" ); |
---|
[477] | 48 | } |
---|
[565] | 49 | |
---|
[198] | 50 | void logit ( int id, const vec &v ) { |
---|
[565] | 51 | bdm_assert_debug ( id < vectors.length(), "Logger was not initialized, run init()." ); |
---|
[477] | 52 | if ( id >= 0 ) { |
---|
| 53 | vectors ( id ).set_row ( ind, v ); |
---|
| 54 | } |
---|
[271] | 55 | } |
---|
[338] | 56 | void logit ( int id, const double &d ) { |
---|
[565] | 57 | bdm_assert_debug ( id < vectors.length(), "Logger was not initialized, run init()." ); |
---|
| 58 | bdm_assert_debug ( vectors ( id ).cols() == 1, "Vector expected" ); |
---|
[477] | 59 | if ( id >= 0 ) { |
---|
| 60 | vectors ( id ) ( ind ) = d; |
---|
| 61 | } |
---|
[338] | 62 | } |
---|
[145] | 63 | //! Save values into an itfile named after \c fname. |
---|
[477] | 64 | void itsave ( const char* fname ); |
---|
| 65 | //! |
---|
| 66 | void finalize() { |
---|
| 67 | if ( itfilename.length() > 0 ) itsave ( itfilename.c_str() ); |
---|
| 68 | }; |
---|
[347] | 69 | |
---|
[357] | 70 | |
---|
[477] | 71 | /*! \brief UI for memlog |
---|
[357] | 72 | |
---|
| 73 | TODO dat tam kam patri, a to celej blok |
---|
| 74 | |
---|
| 75 | \code |
---|
| 76 | logger = { |
---|
[425] | 77 | class = "memlog"; |
---|
[357] | 78 | filename = "file_name.it"; // resulting filename with results in it format |
---|
| 79 | maxlen = 100; // size of memory buffer |
---|
| 80 | } |
---|
| 81 | \endcode |
---|
| 82 | */ |
---|
[477] | 83 | void from_setting ( const Setting &set ); |
---|
[357] | 84 | |
---|
[377] | 85 | // TODO dodelat void to_setting( Setting &set ) const; |
---|
[92] | 86 | }; |
---|
| 87 | |
---|
[477] | 88 | UIREGISTER ( memlog ); |
---|
[529] | 89 | SHAREDPTR ( memlog ); |
---|
[357] | 90 | |
---|
[611] | 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 | |
---|
[92] | 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; |
---|
[477] | 152 | //! Automatically generated |
---|
[92] | 153 | Array<string> scalarnames; |
---|
| 154 | public: |
---|
[477] | 155 | /*!\brief Default constructor |
---|
[92] | 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. */ |
---|
[357] | 158 | dirfilelog ( string dirname0, int maxlen0 ) : memlog ( maxlen0 ), dirname ( dirname0 ), scalarnames ( 0 ) {} |
---|
| 159 | |
---|
| 160 | dirfilelog() {} |
---|
| 161 | |
---|
[92] | 162 | //! Initialize storage |
---|
| 163 | void init(); |
---|
[162] | 164 | void step(); |
---|
| 165 | void finalize(); |
---|
[477] | 166 | /*! \brief Write memory storage to disk. |
---|
[92] | 167 | @param Len length of buffer to be written, if 0 the file is truncated at 0. |
---|
| 168 | */ |
---|
| 169 | void write_buffers ( int Len ); |
---|
[357] | 170 | |
---|
[477] | 171 | /*! \brief UI for dirfilelog (Kst file format) |
---|
[357] | 172 | \code |
---|
| 173 | logger = { |
---|
[425] | 174 | class = "dirfilelog"; |
---|
| 175 | dirname = "directory_for_files"; // resulting files will be stored there |
---|
[357] | 176 | maxlen = 100; // size of memory buffer, when full results are written to disk |
---|
| 177 | } |
---|
| 178 | \endcode |
---|
| 179 | */ |
---|
[477] | 180 | void from_setting ( const Setting &set ); |
---|
[357] | 181 | |
---|
[377] | 182 | // TODO dodelat void to_setting( Setting &set ) const; |
---|
[92] | 183 | }; |
---|
| 184 | |
---|
[477] | 185 | UIREGISTER ( dirfilelog ); |
---|
[529] | 186 | SHAREDPTR ( dirfilelog ); |
---|
[357] | 187 | |
---|
[263] | 188 | }; |
---|
[92] | 189 | #endif // LGR_H |
---|