Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

emd.py divide by zero & other uncaught exceptions #48

Open
johndavidmiller opened this issue Jul 19, 2018 · 2 comments
Open

emd.py divide by zero & other uncaught exceptions #48

johndavidmiller opened this issue Jul 19, 2018 · 2 comments
Milestone

Comments

@johndavidmiller
Copy link

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
Copy link
Author

here's the error from anaconda python 3.6:

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

@johndavidmiller
Copy link
Author

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

@jaidevd jaidevd added this to the 0.2 Release milestone Jan 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants