pyhht icon indicating copy to clipboard operation
pyhht copied to clipboard

emd.py divide by zero & other uncaught exceptions

Open johndavidmiller opened this issue 6 years ago • 2 comments

in stop_sifting() the line: "sx = np.abs(envmoy) / amp" sometimes causes a divide by zero error.

also, I notice that right above this line (currently 376) there are some try/except checks for the return values of self.mean_and_amplitude(), but ONLY for two very specific errors. seems there should be an unconditional "except" too.

what's the right fix here?

-- jdm

johndavidmiller avatar Jul 19 '18 19:07 johndavidmiller

here's the error from anaconda python 3.6:

pyhht/emd.py:376: RuntimeWarning: divide by zero encountered in true_divide

johndavidmiller avatar Jul 19 '18 19:07 johndavidmiller

to get this to not crash, I wrapped that line with a try/except - after telling Numpy to raise Python errors (otherwise it doesn't).

as I'm not intimately familiar with the code, I just did that (below), which should be reviewed and rewritten to be cleaner, e.g., making it part of the preceding try/except, or even better, clean up the entire function.

Here's my hack of emd.py - again, I just wanted it to not crash, not make it pretty. Except for the print, where I use F-expressions, it should be fine for both Python 2.x and 3.x, but I haven't verified it on 2.x.

377 ### 378 # JDM added try/catch to keep from true_divide error when dividing by amp 379 ### 380 import sys 381 old_err = np.seterr(all='raise') # make Numpy errors raise Python exceptions, or we won't see them 382 try: 383 sx = np.abs(envmoy) / amp 384 except: 385 print(f"WARNING: stop_sifting() caught exception dividing by amp, type {sys.exc_info()[0]}, value={sys.exc_info()[1]}") 386 print(f"envmoy={envmoy}\namp={amp}") 387 return 1, np.zeros((len(m))) 388 finally: 389 np.seterr(**old_err) # restore 390 ## end jdm part

johndavidmiller avatar Jul 20 '18 17:07 johndavidmiller