bici-sancta
bici-sancta

Reputation: 172

FutureWarning not displayed with warnings.simplefilter(action = "error", category=FutureWarning)

I am having trouble to find the line of code producing the FutureWarning message: elementwise comparison failed.

There are other questions in SO that describe the python / numpy conflict that cause this warning. I am trying to find which lines are causing this in my code.

When I include these lines in the header section of the code :

import warnings
warnings.simplefilter(action = "default", category=FutureWarning)

Then the warning message displays on console output, but without info to identify where the problem is occurring.

When I include these lines :

import warnings
warnings.simplefilter(action = "error", category=FutureWarning)

then the warning message is not displayed.

I have also used

warnings.filterwarnings()

with the same arguments as simplefilter, and have the same result.

I am trying to run the code and produce a traceback which identifies the offending lines.

What am I not doing correctly ?

Upvotes: 3

Views: 1102

Answers (2)

Pontus Hultkrantz
Pontus Hultkrantz

Reputation: 480

Try removing your warning / filter warnings and add this instead:

numpy.seterr(all='raise')

If as intended, it will raise an exception where it occurs.

More info:

Upvotes: 1

mgmussi
mgmussi

Reputation: 566

Usually, running your code from the cmd or terminal helps in the indication of warnings/errors, as is shown in the prompt which line of code yielded the message. Otherwise, building your code from Sublime Text with Ctrl + b can also help to indicate where the error is.

Now, if that still doesn't work for you, the manual thing to do this is to find every part in your code in which you used == or in. Both operators, as well explained here, can lead to that kind of error. And then you can use with warnings.catch_warnings(): before some commands and see if you find it.

And then, if you find it and you want to ignore it, do

with warnings.catch_warnings():
    warnings.simplefilter("ignore", category= FutureWarning)
    <<command>>

Upvotes: 0

Related Questions