ITK  5.3.0
Insight Toolkit
itkSimpleFilterWatcher.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkSimpleFilterWatcher_h
19 #define itkSimpleFilterWatcher_h
20 
21 #include "itkCommand.h"
22 #include "itkProcessObject.h"
23 #include "itkTimeProbe.h"
24 #include <mutex>
25 
26 namespace itk
27 {
68 class ITKCommon_EXPORT SimpleFilterWatcher
69 {
70 public:
73  SimpleFilterWatcher(itk::ProcessObject * o, const char * comment = "");
74 
77 
81 
84  operator=(const SimpleFilterWatcher &);
85 
87  virtual ~SimpleFilterWatcher();
88 
91  const char *
93  {
94  return (m_Process ? m_Process->GetNameOfClass() : "None");
95  }
96 
99  void
101  {
102  m_Quiet = true;
103  }
104  void
106  {
107  m_Quiet = false;
108  }
114  void
116  {
117  m_TestAbort = true;
118  }
119  void
121  {
122  m_TestAbort = false;
123  }
129  ProcessObject *
131  {
132  return m_Process.GetPointer();
133  }
134 
136  void
137  SetSteps(int val)
138  {
139  m_Steps = val;
140  }
141  int
142  GetSteps() const
143  {
144  return m_Steps;
145  }
149  void
150  SetIterations(int val)
151  {
152  m_Iterations = val;
153  }
154  int
156  {
157  return m_Iterations;
158  }
163  void
164  SetQuiet(bool val)
165  {
166  m_Quiet = val;
167  }
168  bool
169  GetQuiet() const
170  {
171  return m_Quiet;
172  }
176  std::string
178  {
179  return m_Comment;
180  }
181 
183  TimeProbe &
185  {
186  return m_TimeProbe;
187  }
188 
189 protected:
191  virtual void
193  {
194  if (m_Process)
195  {
196  std::lock_guard<std::mutex> outputSerializer(m_ProgressOutput);
197  ++m_Steps;
198  if (!m_Quiet)
199  {
200  std::cout << " | " << m_Process->GetProgress() << std::flush;
201  if ((m_Steps % 10) == 0)
202  {
203  std::cout << std::endl;
204  }
205  }
206  if (m_TestAbort)
207  {
208  if (m_Process->GetProgress() > .03)
209  {
210  m_Process->AbortGenerateDataOn();
211  }
212  }
213  }
214  }
218  void
219  CreateCommands();
220 
222  void
223  RemoveObservers();
224 
226  void
227  DeepCopy(const SimpleFilterWatcher & watch);
228 
230  virtual void
232  {
233  std::cout << std::endl << "-------Aborted" << std::endl << std::flush;
234  }
235 
237  virtual void
239  {
240  std::cout << " #" << std::flush;
241  ++m_Iterations;
242  }
243 
245  virtual void
247  {
248  m_Steps = 0;
249  m_Iterations = 0;
250  m_TimeProbe.Start();
251  std::cout << "-------- Start " << (m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None") << " \""
252  << m_Comment << "\" ";
253  if (!m_Quiet)
254  {
255  if (m_Process)
256  {
257  std::cout << m_Process;
258  }
259  else
260  {
261  std::cout << "Null";
262  }
263  }
264  std::cout << (m_Quiet ? "Progress Quiet " : "Progress ") << std::flush;
265  }
269  virtual void
271  {
272  m_TimeProbe.Stop();
273  std::cout << std::endl << "Filter took " << m_TimeProbe.GetMean() << " seconds.";
274  std::cout << std::endl
275  << "-------- End " << (m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None") << " \""
276  << m_Comment << "\" " << std::endl;
277  if (!m_Quiet)
278  {
279  if (m_Process)
280  {
281  std::cout << m_Process;
282  }
283  else
284  {
285  std::cout << "None";
286  }
287  std::cout << std::flush;
288  }
289  if (m_Steps < 1)
290  {
291  itkExceptionMacro("Filter does not have progress.");
292  }
293  }
296 private:
298  int m_Steps{ 0 };
299  int m_Iterations{ 0 };
300  bool m_Quiet{ false };
301  bool m_TestAbort{ false };
302  std::string m_Comment;
304  std::mutex m_ProgressOutput;
305 
312 
313  unsigned long m_StartTag{ 0 };
314  unsigned long m_EndTag{ 0 };
315  unsigned long m_ProgressTag{ 0 };
316  unsigned long m_IterationTag{ 0 };
317  unsigned long m_AbortTag{ 0 };
318 };
319 } // end namespace itk
320 
321 #endif
itk::SimpleFilterWatcher::m_AbortFilterCommand
CommandType::Pointer m_AbortFilterCommand
Definition: itkSimpleFilterWatcher.h:311
itk::SimpleFilterWatcher::m_TimeProbe
TimeProbe m_TimeProbe
Definition: itkSimpleFilterWatcher.h:297
itkTimeProbe.h
itk::SimpleFilterWatcher::GetQuiet
bool GetQuiet() const
Definition: itkSimpleFilterWatcher.h:169
itk::SimpleFilterWatcher::ShowIteration
virtual void ShowIteration()
Definition: itkSimpleFilterWatcher.h:238
itk::SimpleFilterWatcher::m_Process
itk::ProcessObject::Pointer m_Process
Definition: itkSimpleFilterWatcher.h:303
itk::SimpleFilterWatcher::GetProcess
ProcessObject * GetProcess()
Definition: itkSimpleFilterWatcher.h:130
itk::SimpleMemberCommand
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:239
itk::SimpleFilterWatcher::m_StartFilterCommand
CommandType::Pointer m_StartFilterCommand
Definition: itkSimpleFilterWatcher.h:307
itk::SmartPointer< Self >
itk::SimpleFilterWatcher::QuietOn
void QuietOn()
Definition: itkSimpleFilterWatcher.h:100
itk::SimpleFilterWatcher
Simple mechanism for monitoring the pipeline events of a filter and reporting these events to std::co...
Definition: itkSimpleFilterWatcher.h:68
itk::SimpleFilterWatcher::ShowAbort
virtual void ShowAbort()
Definition: itkSimpleFilterWatcher.h:231
itkProcessObject.h
itk::SimpleFilterWatcher::SetSteps
void SetSteps(int val)
Definition: itkSimpleFilterWatcher.h:137
itk::SimpleFilterWatcher::TestAbortOn
void TestAbortOn()
Definition: itkSimpleFilterWatcher.h:115
itk::SimpleFilterWatcher::GetSteps
int GetSteps() const
Definition: itkSimpleFilterWatcher.h:142
itk::SimpleFilterWatcher::m_Comment
std::string m_Comment
Definition: itkSimpleFilterWatcher.h:302
itk::SimpleFilterWatcher::GetNameOfClass
const char * GetNameOfClass()
Definition: itkSimpleFilterWatcher.h:92
itk::TimeProbe
Computes the time passed between two points in code.
Definition: itkTimeProbe.h:44
itk::SimpleFilterWatcher::GetIterations
int GetIterations() const
Definition: itkSimpleFilterWatcher.h:155
itk::SimpleFilterWatcher::GetComment
std::string GetComment()
Definition: itkSimpleFilterWatcher.h:177
itk::SimpleFilterWatcher::TestAbortOff
void TestAbortOff()
Definition: itkSimpleFilterWatcher.h:120
itk::SimpleFilterWatcher::StartFilter
virtual void StartFilter()
Definition: itkSimpleFilterWatcher.h:246
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::SimpleFilterWatcher::m_ProgressOutput
std::mutex m_ProgressOutput
Definition: itkSimpleFilterWatcher.h:304
itk::SimpleFilterWatcher::m_EndFilterCommand
CommandType::Pointer m_EndFilterCommand
Definition: itkSimpleFilterWatcher.h:308
itk::SimpleFilterWatcher::GetTimeProbe
TimeProbe & GetTimeProbe()
Definition: itkSimpleFilterWatcher.h:184
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:138
itk::SimpleFilterWatcher::ShowProgress
virtual void ShowProgress()
Definition: itkSimpleFilterWatcher.h:192
itk::SimpleFilterWatcher::SetIterations
void SetIterations(int val)
Definition: itkSimpleFilterWatcher.h:150
itk::SimpleFilterWatcher::m_ProgressFilterCommand
CommandType::Pointer m_ProgressFilterCommand
Definition: itkSimpleFilterWatcher.h:309
itk::SimpleFilterWatcher::QuietOff
void QuietOff()
Definition: itkSimpleFilterWatcher.h:105
itk::SimpleFilterWatcher::SetQuiet
void SetQuiet(bool val)
Definition: itkSimpleFilterWatcher.h:164
itk::SimpleFilterWatcher::m_IterationFilterCommand
CommandType::Pointer m_IterationFilterCommand
Definition: itkSimpleFilterWatcher.h:310
itkCommand.h
itk::SimpleFilterWatcher::EndFilter
virtual void EndFilter()
Definition: itkSimpleFilterWatcher.h:270