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

Gracefully handle 0s when plotting in log scale #563

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

JamesWrigley
Copy link
Member

Previously the behaviour was to add 1 before taking the log to avoid 0s causing
inf's in the output, but in some cases with very small values that gives wildly
incorrect results. Now we explicitly only take the log of non-zero values.

Before:
image

After:
image

Previously the behaviour was to add 1 before taking the log to avoid 0s causing
inf's in the output, but in some cases with very small values that gives wildly
incorrect results. Now we explicitly only take the log of non-zero values.
@JamesWrigley JamesWrigley self-assigned this Jul 20, 2022
@@ -129,7 +129,7 @@ def toLogScale(arr, policy=None):
"""Convert array result to logarithmic scale."""
ret = np.nan_to_num(arr)
ret[ret < 0] = 0
return np.log10(ret + 1)
return np.log10(ret, where=(ret > 0))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the original solution does not work in some situations, as you have shown. However, there are also corner cases for the new one. It makes 1 and 0 the same. 0 will not be affected while 1 will become 0 after the log operation. Imaging you have a step function f(x) = 1 if x > 0 else 0, with the new solution the curve will become completely flat (all 0). Therefore, a better solution might be letting users choose among different methods.

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

Successfully merging this pull request may close these issues.

2 participants