xmhk / FNFTpy

A python wrapper for FNFT, a C library to calculate the Nonlinear Fourier Transform
GNU General Public License v2.0
10 stars 10 forks source link

how to suppress the warnings coming from c files? #4

Closed Yarden92 closed 2 years ago

Yarden92 commented 2 years ago

if I execute INFT (nsev_inverse) with input signal that has large values (over 0.5), I get warning which are coming from this file: https://github.com/FastNFT/FNFT/blob/master/src/private/fnft__errwarn.c#L39

This is how the warning looks like:

FNFT Warning: Ill-posed spectral factorization problem.
in fnft__poly_specfact(111)-0.4.1

Now, in my thesis I explore this phenomena and try to solve it so I call these NFT functions a lot (millions) of times and that floods my terminal + slow my runtime.

I tried to suppress these warnings with some python tricks like

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    NFT(...)

But it wouldn't work since these warnings are coming from the c libraries, and not pythonics.

Is there a way to suppress them somehow? in the python side?

wahls commented 2 years ago

The C command you would have to execute to keep FNFT from printing messages is

fnft_errwarn_setprintf(NULL);

I'm not the expert for the Python interface, but from looking at other routines I guess something like this might work:

import FNFTpy
fnft_clib = FNFTpy.ctypes.CDLL(FNFTpy.get_lib_path(), winmode = FNFTpy.get_winmode_param())
clib_errwarn_setprintf = fnft_clib.fnft_errwarn_setprintf
clib_errwarn_setprintf(FNFTpy.ctypes_nullptr)
xmhk commented 2 years ago

Hi,

@wahls your proposed code works, although it has to be integrated within each single function. The reason is that the 'connection' to the c-library is actually done after the call of the python function, e.g. nsev. There may be a better solution but I don't know how to solve this in an elegant way right now.

Although I think displaying the C messages in the console can be useful, I also understand that in some situations can be a performance problem, when it floods the console.

As an optional parameter, I introduced display_c_msg for all main functions (nsev, nsev_inverse, etc.) for the master and development branches. Setting display_c_msg=False will set the error-warn-flag to FALSE, thus suppressing the C-messages. The default behavior, however, will stay display_c_msg=False. This should be a non-breaking change.

Best, Christoph

wahls commented 2 years ago

Thanks, @xmhk, that's certainly the better solution.

Just for clarification: The default behavior stays display_c_msg=True and not display_c_msg=False, right?

@Yarden92 Good luck with your thesis. I'd be interested in what you find.

xmhk commented 2 years ago

Just for clarification: The default behavior stays display_c_msg=True and not display_c_msg=False, right?

Ah I see, there was a typo in my reply. Yes, the default behavior will stay display_c_msg=True

wahls commented 2 years ago

Thought so, thanks!

Yarden92 commented 2 years ago

Wow thank you both for your super helpful comments! The solution works perfectly!