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