2

In my simulation, when reaching such an expression of a function inside a for loop:

d1ToUse = d2Weights[:,i] > 1e-9 

a warning is sometimes thrown:

RuntimeWarning: invalid value encountered in greater...

I would like to print out the value of the 'greater' whenever this warning is thrown in order to inspect the variable d2Weights and eliminate the problem.

For some instantiations of this warning, the d2Weights variable was an empty list. I have tried to eliminate those instantiations from the analysis by filtering them out by any of the following filters:

            if d2Weights[:,i] == []:
                continue
            if np.any(d2Weights[:, i] == None):
                continue
            if np.any(d2Weights[:, i] == np.nan):
                continue
            if sum(d2Weights[d1ToUse, i]) == 0.0:
                continue
            if d2Weights[d1ToUse, i] is None:
                continue
            if np.any(d2Weights[d1ToUse, i] == None):
                continue
            if np.any(d2Weights[d1ToUse, i] == np.nan):
                continue

As the problem lingers, I would like to see how the 'greater' value looks to inspect it and identify the root cause.

I do NOT want to suppress this warning because whenever it is thrown, my simulation behaves weirdly.

I will be grateful for any suggestion.

EDIT:

d2Weights is numpy array with floats. It stores weights for circular weighted averaging with RBF kernel.

mats_snaps
  • 57
  • 6
  • Can you not use a debugger to inspect the value of greater and the frame calls? – AzyCrw4282 Apr 29 '20 at 23:57
  • 1
    Don't use `==` with `None` or `np.nan`. Use `is None` and `np.isnan`. Also pay attention to `dtype`. `None` will only exist in an `object` dtype array. `np.nan` is a float. – hpaulj Apr 30 '20 at 00:37
  • 1
    You shouldn't be testing a numpy array against an empty list `[]`. You can check the `shape`. – hpaulj Apr 30 '20 at 00:54
  • 1
    Give us a sense of what `d2Weights` is. shape, dtype, possible values. – hpaulj Apr 30 '20 at 00:55
  • To convert all warnings to errors, see https://stackoverflow.com/questions/5644836/in-python-how-does-one-catch-warnings-as-if-they-were-exceptions – Hackless Dec 07 '22 at 17:55

1 Answers1

1

You could use the warnings library: https://docs.python.org/3/library/warnings.html https://www.geeksforgeeks.org/warnings-in-python/

A warnings filter controls whether warnings are ignored, displayed, or turned into errors (raising an exception). So you could turn the warning into an error and put the critical line in your code inside a try block. Then you can use the except block to find out what causes your issue and print out the 'greater' value for example. https://www.w3schools.com/python/python_try_except.asp

Hope this helps. I am actually a beginner in python but I had a similar problem this week.

Felix Lipo
  • 31
  • 6