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 | //!Default constructor |
---|
38 | memlog ( int maxlen0, string itf="" ) : maxlen ( maxlen0 ),ind ( 0 ),vectors ( 0 ),itfilename(itf) {} |
---|
39 | |
---|
40 | memlog(): ind ( 0 ),vectors ( 0 ) {} |
---|
41 | |
---|
42 | //! Initialize storage |
---|
43 | void init() { |
---|
44 | int i; int n =entries.length(); |
---|
45 | vectors.set_size ( n ); |
---|
46 | for ( i=0;i<n;i++ ) {vectors(i).set_size (maxlen,entries(i)._dsize() );} |
---|
47 | } |
---|
48 | void step() {if ( ind<maxlen ) ind++; else it_error ( "memlog::ind is too high;" );} |
---|
49 | void logit ( int id, const vec &v ) { |
---|
50 | it_assert_debug(id<vectors.length(),"Logger was not initialized, run init()."); |
---|
51 | if(id>=0){ vectors ( id ).set_row ( ind,v );} |
---|
52 | } |
---|
53 | void logit ( int id, const double &d ) { |
---|
54 | it_assert_debug(id<vectors.length(),"Logger was not initialized, run init()."); |
---|
55 | it_assert_debug(vectors(id).cols()==1,"Vector expected"); |
---|
56 | if(id>=0){ vectors ( id ) ( ind )=d;} |
---|
57 | } |
---|
58 | //! Save values into an itfile named after \c fname. |
---|
59 | void itsave(const char* fname); |
---|
60 | //! |
---|
61 | void finalize() {if (itfilename.length()>0) itsave(itfilename.c_str());}; |
---|
62 | |
---|
63 | |
---|
64 | /*! \brief UI for memlog |
---|
65 | |
---|
66 | TODO dat tam kam patri, a to celej blok |
---|
67 | |
---|
68 | \code |
---|
69 | logger = { |
---|
70 | class = "memlog"; |
---|
71 | filename = "file_name.it"; // resulting filename with results in it format |
---|
72 | maxlen = 100; // size of memory buffer |
---|
73 | } |
---|
74 | \endcode |
---|
75 | */ |
---|
76 | void from_setting( const Setting &set ); |
---|
77 | |
---|
78 | // TODO dodelat void to_setting( Setting &set ) const; |
---|
79 | }; |
---|
80 | |
---|
81 | UIREGISTER(memlog); |
---|
82 | |
---|
83 | /*! |
---|
84 | * \brief Logging into dirfile with buffer in memory |
---|
85 | |
---|
86 | * Dirfile is a special format used by the kst program. See documentation of kst for description. |
---|
87 | * |
---|
88 | * This format is used to store scalars, hence multivariate RVs must be separated. |
---|
89 | */ |
---|
90 | class dirfilelog : public memlog { |
---|
91 | |
---|
92 | protected: |
---|
93 | //!name of the directory |
---|
94 | string dirname; |
---|
95 | //! Automatically generated |
---|
96 | Array<string> scalarnames; |
---|
97 | public: |
---|
98 | /*!\brief Default constructor |
---|
99 | @param dirname0 name of the directory in which to store the results |
---|
100 | @param maxlen0 length of the memory buffers, when full the buffers will be dumped to HDD and returned to the beginning. */ |
---|
101 | dirfilelog ( string dirname0, int maxlen0 ) : memlog ( maxlen0 ), dirname ( dirname0 ), scalarnames ( 0 ) {} |
---|
102 | |
---|
103 | dirfilelog() {} |
---|
104 | |
---|
105 | //! Initialize storage |
---|
106 | void init(); |
---|
107 | void step(); |
---|
108 | void finalize(); |
---|
109 | /*! \brief Write memory storage to disk. |
---|
110 | @param Len length of buffer to be written, if 0 the file is truncated at 0. |
---|
111 | */ |
---|
112 | void write_buffers ( int Len ); |
---|
113 | |
---|
114 | /*! \brief UI for dirfilelog (Kst file format) |
---|
115 | \code |
---|
116 | logger = { |
---|
117 | class = "dirfilelog"; |
---|
118 | dirname = "directory_for_files"; // resulting files will be stored there |
---|
119 | maxlen = 100; // size of memory buffer, when full results are written to disk |
---|
120 | } |
---|
121 | \endcode |
---|
122 | */ |
---|
123 | void from_setting( const Setting &set ); |
---|
124 | |
---|
125 | // TODO dodelat void to_setting( Setting &set ) const; |
---|
126 | }; |
---|
127 | |
---|
128 | UIREGISTER(dirfilelog); |
---|
129 | |
---|
130 | }; |
---|
131 | #endif // LGR_H |
---|