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