InsightSoftwareConsortium / ITK

Insight Toolkit (ITK) -- Official Repository. ITK builds on a proven, spatially-oriented architecture for processing, segmentation, and registration of scientific images in two, three, or more dimensions.
https://itk.org
Apache License 2.0
1.37k stars 660 forks source link

itkWarningMacro output streams to Jupyter server console (not the notebook) #4690

Open auneri opened 1 month ago

auneri commented 1 month ago

Description

When using itk inside a jupyter notebook, warnings triggered by itkWarningMacro (and possibly others) are streamed to the server console instead of the notebook.

Steps to Reproduce

directory = pathlib.Path('/path/to/dicom/')
filepaths = sorted(directory.glob('*.dcm'))
del filepaths[2]   # remove an image slice from the list to trigger non-uniform sampling warning
reader = itk.ImageSeriesReader.IF3.New()
reader.SetFileNames(filepaths)
reader.Update()

Expected behavior

The following warning should appear in the notebook:

WARNING: In C:\P\IPP\ITK-source\ITK\Modules\IO\ImageBase\include\itkImageSeriesReader.hxx, line 478
ImageSeriesReader (XXX): Non uniform sampling or missing slices detected,  maximum nonuniformity:XXX

Actual behavior

The warning only appears in the jupyter server console.

Reproducibility

Happens every time.

Versions

Tested on v5.3 and v5.4.

Environment

Tested on Windows 10 and 11.

Additional Information

blowekamp commented 1 month ago

You may find this SimpleITK Example relevant. A similar approach should be possible with ITK Python: https://simpleitk.readthedocs.io/en/master/link_Logging_docs.html

auneri commented 1 month ago

Thanks, that is indeed very relevant. If I understand it correctly, this solution would require a LoggerBase class implemented on the ITK (C++) side.

I also noticed ITK has TextOutput, LoggerOutput, and FileOutputWindow, all derived from OutputWindow. Only OutputWindow is exposed in Python. I wonder if TextOutput would do the job in this case. Something like:

text = itk.TextOutput.New()  # after TextOutput gets wrapped
itk.OutputWindow.SetInstance(text)

I even tried defining my own TextOutput class in Python, but was not able to overload methods like DisplayText.

thewtex commented 1 month ago

@N-Dekker are your experiences with logging in elastix relevant here?

N-Dekker commented 1 month ago

Sorry, I don't really know. Looks like itkWarningMacro outputs to standard error (std::cerr) by default, as that's what OutputWindow::DisplayText does. Is it not possible to show standard error output directly in a notebook? And why isn't itk::TextOutput available in Python? Again, I don't know 🤷