# How to catch warnings during registration with simpleITK (python)

**URL:** https://discourse.itk.org/t/how-to-catch-warnings-during-registration-with-simpleitk-python/501
**Category:** Beginner Questions
**Tags:** python, simpleitk
**Created:** [December 11, 2017, 4:26pm UTC](https://discourse.itk.org/t/how-to-catch-warnings-during-registration-with-simpleitk-python/501 "2017-12-11T16:26:45Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![madanh](https://discourse.itk.org/letter_avatar_proxy/v4/letter/m/858c86/32.png) [@madanh](https://discourse.itk.org/u/madanh)
#### Post date: [December 11, 2017, 4:26pm UTC](https://discourse.itk.org/t/how-to-catch-warnings-during-registration-with-simpleitk-python/501/1 "2017-12-11T16:26:45Z")

</div>

I’m performing image registration with simpleITK (python) using `ImageRegistrationMethod.Execute()` approach.  
The purpose is to collect some statistics of the image registration process (e.g. find out when it succeeds and when it fails), so I initialize the `ImageRegistrationMethod` instance with different initial transformations, including ones that will not lead to registration success.  
Upon execution simpleITK would sometimes throw an exception, which is fine, since I can catch that. But sometimes I get warnings in stderr like this

```
WARNING: In /mnt/emptyplaceholder/projects/elastix/build/ITK-prefix/include/ITK-4.12/itkObjectToObjectMetric.hxx, line 529
Self (0x3a0af70): No valid points were found during metric evaluation. For image metrics, verify that the images overlap appropriately. For instance, you can align the image centers by translation. For point-set metrics, verify that the fixed points, once transformed into the virtual domain space, actually lie within the virtual domain.

```

or this

```
WARNING: In /mnt/emptyplaceholder/projects/elastix/build/ITK-prefix/include/ITK-4.12/itkCorrelationImageToImageMetricv4HelperThreader.hxx, line 85
CorrelationImageToImageMetricv4HelperThreader (0x3a01370): collected only zero points

```

but registration proceeds.

The question is how can I distinguish (from python) instances when registration went completely smoothly from such cases with warnings. A simple and stupid option is to monitor stderr, but maybe there is some ITK functionality to report these occurrences?

PS This is copy of [this](https://stackoverflow.com/q/47752670/3791466) stackoverflow question

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [December 12, 2017, 3:16pm UTC](https://discourse.itk.org/t/how-to-catch-warnings-during-registration-with-simpleitk-python/501/2 "2017-12-12T15:16:29Z")

</div>

You are appear to be using SimpleElastix and not just vanilla SimpleITK.

The method your are having a problem with is from their additions, and issues with that algorithm should be reported the the [SimpleElastix project](https://github.com/SuperElastix/SimpleElastix).

However, the general problem for catch warnings/errors from ITK can be discussed here. I presume the code use the [itkWarningMacro](https://github.com/InsightSoftwareConsortium/ITK/blob/db6a7f2f6e0ac2ca2b503e9766a428beef808dcb/Modules/Core/Common/include/itkMacro.h#L422-L432). Where the task of displaying warning, errors etc is delegated to an instance of [OutputWindow](https://itk.org/Doxygen/html/classitk_1_1OutputWindow.html#a84c554e25a3bd1394b865773835a9db9).  
There are a couple different implementations that could be chosen from in ITK proper.

Currently, in SimpleITK these logger options are not exposed. I have no experience with them and it’s not clear to me what the interface exposed in SimpleITK should/would be. Such an options would likely go into the static public interface of [itk::simple::ProcessObject](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1ProcessObject.html).

If you have any specific suggestions or usages, I’d like to hear them.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [December 12, 2017, 5:55pm UTC](https://discourse.itk.org/t/how-to-catch-warnings-during-registration-with-simpleitk-python/501/3 "2017-12-12T17:55:08Z")

</div>

Here is a sample code for ITK and VTK I used in some project:

```
itk::FileOutputWindow::Pointer win=itk::FileOutputWindow::New();
win->SetFileName(someDir+"itkOutput.txt");
win->SetInstance(win);
vtkFileOutputWindow *winVtk=vtkFileOutputWindow::New();
winVtk->SetFileName((someDir+"vtkOutput.txt").c_str());
winVtk->SetInstance(winVtk);
```

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [December 12, 2017, 6:26pm UTC](https://discourse.itk.org/t/how-to-catch-warnings-during-registration-with-simpleitk-python/501/4 "2017-12-12T18:26:44Z")

</div>

@dzenanz Thanks for sharing the use case. That would certainly log/write all the errors and warnings to a file, but I don’t believe that the same file can be opened for reading to monitor so that the process will be notified when a warning occurs.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [December 12, 2017, 6:29pm UTC](https://discourse.itk.org/t/how-to-catch-warnings-during-registration-with-simpleitk-python/501/5 "2017-12-12T18:29:55Z")

</div>

Examining the size of the file should give an answer whether there have been additional warnings or not. That is easier than redirecting output streams and examining them.

Alternatively, that class could be inherited and customized, and thus completely bypass outputting to file or stream.

---

<div class="post-metadata">

### Author: ![madanh](https://discourse.itk.org/letter_avatar_proxy/v4/letter/m/858c86/32.png) [@madanh](https://discourse.itk.org/u/madanh)
#### Post date: [December 13, 2017, 10:03am UTC](https://discourse.itk.org/t/how-to-catch-warnings-during-registration-with-simpleitk-python/501/6 "2017-12-13T10:03:22Z")

</div>

Thank you for the useful information. Indeed I use SimpleITK from SimpleElastix. I’m on relatively tight schedule, so most likely I will just monitor stderr.
