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