How to catch warnings during registration with simpleITK (python)

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 stackoverflow question

1 Like

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.

However, the general problem for catch warnings/errors from ITK can be discussed here. I presume the code use the itkWarningMacro. Where the task of displaying warning, errors etc is delegated to an instance of OutputWindow.
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.

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

1 Like

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);

@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.

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.

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.