iRoCS Toolbox  1.1.0
ProgressReporter.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2004-2015 Olaf Ronneberger, Florian Pigorsch, Jörg Mechnich,
4  * Thorsten Falk
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25 ** Title: interface for your own progress reporter
26 ** $RCSfile$
27 ** $Revision: 4820 $$Name$
28 ** $Date: 2011-11-08 10:57:01 +0100 (Tue, 08 Nov 2011) $
29 ** Copyright: GPL $Author: tschmidt $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log$
37 ** Revision 1.3 2004/09/08 14:34:53 ronneber
38 ** - adapted to new ParamInfo class
39 ** - added clearScreen()
40 ** - cosmetic changes
41 **
42 ** Revision 1.2 2004/08/26 15:22:01 ronneber
43 ** - fixed help text
44 **
45 ** Revision 1.1 2004/08/26 08:36:59 ronneber
46 ** initital import
47 **
48 ** Revision 1.2 2002/10/28 16:00:52 mechnich
49 ** fixed compatibility problems with gcc3
50 **
51 ** Revision 1.1 2002/05/22 16:39:06 ronneber
52 ** - added progress reporting capabilities
53 **
54 **
55 **
56 **************************************************************************/
57 
58 #ifndef PROGRESSREPORTER_HH
59 #define PROGRESSREPORTER_HH
60 
61 #ifdef HAVE_CONFIG_H
62 #include <config.hh>
63 #endif
64 
65 #include <iostream>
66 #include <map>
67 #include <set>
68 #include <string>
69 #include <vector>
70 
71 #include "ParamInfo.hh"
72 
73 namespace svt
74 {
75  const int TASK_LEVEL_GRID_SEARCH = 1;
76  const int TASK_LEVEL_CROSS_VAL = 2;
77  const int TASK_LEVEL_MULTICLASS = 3;
78  const int TASK_LEVEL_TWOCLASS = 4;
79  const int TASK_LEVEL_TRAINING_INFO = 5;
80 
81 
82 
84  {
85  public:
86  ProgressReporter( std::ostream& os = std::cerr)
87  :_os(os),
88  _verboseLevel(TASK_LEVEL_TWOCLASS),
89  _drawProgressBar( true),
90  _cursorPosSaved( false),
91  _progressBarLength( 20),
92  _rotDashIndex(0),
93  _maxTaskLevel(1),
94  _dotCounter(0)
95  {
96  _rotatingDash.push_back("|");
97  _rotatingDash.push_back("/");
98  _rotatingDash.push_back("-");
99  _rotatingDash.push_back("\\");
100  }
101 
102 
104  {}
105 
106 
107  virtual void setMaxTaskLevel( int n)
108  {
109  _maxTaskLevel = n;
110  }
111 
112  /*======================================================================*/
122  /*======================================================================*/
123  virtual void clearScreen()
124  {
125  _os << "\033[2J\033[H";
126  }
127 
128 
129 
130  /*======================================================================*/
155  /*======================================================================*/
156  virtual void reportProgress( int taskLevel,
157  const std::string& taskName,
158  float completenessPercent,
159  const std::string& completenessPlainText)
160  {
161  if( taskLevel <= _verboseLevel)
162  {
163  /*------------------------------------------------------------
164  * if progress bar is requested, set cursor to proper line
165  *------------------------------------------------------------*/
166  if( _drawProgressBar)
167  {
168  /*-----------------------------------------------------------
169  * go to upper line
170  *-----------------------------------------------------------*/
171  if( _cursorPosSaved == false)
172  {
173  // go 15 lines down and up to scroll terminal up
174  for( int i = 0; i < 15; ++i)
175  {
176  _os << "\n";
177  }
178 
179  _os << "\033[" << 15 << "A";
180 
181  // save cursor position
182  _os << "\033[s";
183  _cursorPosSaved = true;
184  }
185 
186 
187  /*-----------------------------------------------------------
188  * go 'task level'-1 *2 lines down and write
189  * plain text message
190  *-----------------------------------------------------------*/
191  // go to saved position
192  _os << "\033[u\033[s";
193  _os << "\n";
194  if( taskLevel-_maxTaskLevel > 0)
195  {
196  _os << "\033[" << (taskLevel-_maxTaskLevel)*3 << "B";
197  }
198  _os << taskName << " ";
199 
200 
201  /*---------------------------------------------------------
202  * draw progress bar
203  *--------------------------------------------------------*/
204  // go to saved position
205  _os << "\033[u\033[s";
206  _os << "\n";
207  if( taskLevel-_maxTaskLevel+1 > 0)
208  {
209  _os << "\033[" << (taskLevel-_maxTaskLevel)*3+1 << "B";
210  }
211 
212 
213  if( completenessPercent >= 0)
214  {
215  _os << " |";
216 
217  int nHashs = static_cast<int>(
218  static_cast<float>(_progressBarLength) *
219  completenessPercent + 0.5f);
220 
221  for( int i = 0; i < nHashs; ++i)
222  {
223  _os << "#";
224  }
225  for( int i = nHashs; i < _progressBarLength; ++i)
226  {
227  _os << "-";
228  }
229  _os << "| (" << completenessPlainText << ") "
230  << int(completenessPercent*100) << "% "<< std::flush;
231 
232 
233  }
234  else
235  {
236  _os << _rotatingDash[_rotDashIndex];
237  _rotDashIndex = (_rotDashIndex + 1) %
238  static_cast<int>(_rotatingDash.size());
239 
240  if( completenessPercent == -2)
241  {
242  _dotCounter = 0;
243  }
244  ++_dotCounter;
245  for( int i = 0; i < _dotCounter; ++i)
246  {
247  _os << ".";
248  }
249  _os << " " << std::flush;
250 
251 
252  }
253 
254 
255 
256  /*-----------------------------------------------------------
257  * go some lines down, that other messages don't
258  * overwrite progress bars
259  *-----------------------------------------------------------*/
260  // go to saved position
261  _os << "\033[u\033[s";
262  // go down _maxTaskLevel*2
263  _os << "\033[" << (5-_maxTaskLevel)*3 << "B\n";
264  }
265  else
266  {
267  /*-----------------------------------------------------------
268  * no progress bar requested, write plaintext messages
269  *-----------------------------------------------------------*/
270  if( completenessPercent < 0)
271  {
272  // unknown completeness is displayed with dots, or
273  // something else
274  _os << completenessPlainText << std::flush;
275  }
276  else
277  {
278  // indentation according to task level
279  for( int i = 1; i < taskLevel; ++i)
280  {
281  _os << "\t";
282  }
283  _os << taskName << ": " << int(completenessPercent*100)
284  << "% (" << completenessPlainText << ")\n";
285  }
286 
287  }
288 
289  }
290  }
291 
292 
293  /*======================================================================*/
303  /*======================================================================*/
304  virtual void additionalInfo( int taskLevel,
305  const std::string& text)
306  {
307  if( taskLevel <= _verboseLevel)
308  {
309  if( _drawProgressBar)
310  {
311  /*-----------------------------------------------------------
312  * go some lines down, that other messages don't
313  * overwrite progress bars
314  *-----------------------------------------------------------*/
315  // go to saved position
316  _os << "\033[u\033[s";
317  // go down _maxTaskLevel*2
318  _os << "\033[" << (5-_maxTaskLevel)*3 << "B\n";
319  }
320 
321  _os << text << std::flush;
322  }
323  }
324 
325 
326 
327  /*======================================================================*/
336  /*======================================================================*/
337  void setVerboseLevel( int verboseLevel)
338  {
339  _verboseLevel = verboseLevel;
340  }
341 
342  template<typename STDATA>
343  void loadParameters( STDATA& stData)
344  {
345  stData.getValue( "verbose_level", _verboseLevel);
346  stData.getValue( "draw_progress_bar", _drawProgressBar);
347  }
348 
349  template<typename STDATA>
350  void saveParameters( STDATA& stData) const
351  {
352  stData.setValue( "verbose_level", _verboseLevel);
353  stData.setValue( "draw_progress_bar", _drawProgressBar);
354  }
355 
356  /*======================================================================*/
365  /*======================================================================*/
366  static void getParamInfos( std::vector<ParamInfo>& p)
367  {
368  p.push_back( ParamInfo( "verbose_level", "vb"));
369  p.back().addAlternative( "0", "report nothing");
370  p.back().addAlternative( "1", "report parameter tuning");
371  p.back().addAlternative( "2", "report cross validation");
372  p.back().addAlternative( "3", "report multi class svm");
373  p.back().addAlternative( "4", "report two class svm");
374  p.back().addAlternative( "5", "report addtitional training infos");
375 
376  p.push_back( ParamInfo( "draw_progress_bar", "p"));
377  p.back().addAlternative( "0", "no acsii progress bars");
378  p.back().addAlternative( "1",
379  "draw acsii progress bars (default)");
380 
381  }
382 
383  static const char* name()
384  {
385  return "ascii_progress_reporter";
386  }
387 
388  static const char* description()
389  {
390  return "reports progress on ansi ascii terminal";
391  }
392 
393  private:
394  std::ostream& _os;
395  int _verboseLevel;
396  bool _drawProgressBar;
397  bool _cursorPosSaved;
398  int _progressBarLength;
399  int _rotDashIndex;
400  std::vector< std::string> _rotatingDash;
401  int _maxTaskLevel;
402  int _dotCounter;
403 
404 
405 
406 
407 
408  };
409 }
410 
411 #endif
virtual void reportProgress(int taskLevel, const std::string &taskName, float completenessPercent, const std::string &completenessPlainText)
This method is called if some progress was made.
virtual void clearScreen()
(description)
static const char * name()
const int TASK_LEVEL_TRAINING_INFO
void setVerboseLevel(int verboseLevel)
set the reporting level for reportProgress() method.
ProgressReporter(std::ostream &os=std::cerr)
virtual void setMaxTaskLevel(int n)
static const char * description()
const int TASK_LEVEL_GRID_SEARCH
void loadParameters(STDATA &stData)
virtual void additionalInfo(int taskLevel, const std::string &text)
(description)
const int TASK_LEVEL_CROSS_VAL
const int TASK_LEVEL_MULTICLASS
The ParamInfo class contains informations about one parameter like key, help text, guiHints etc.
Definition: ParamInfo.hh:82
void saveParameters(STDATA &stData) const
const int TASK_LEVEL_TWOCLASS
static void getParamInfos(std::vector< ParamInfo > &p)
get information about the parameters, that are used in loadParameters() and saveParameters().