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

[REF] Suppresses divide by 0 warning #786

Merged
merged 13 commits into from
Sep 15, 2022
Merged

[REF] Suppresses divide by 0 warning #786

merged 13 commits into from
Sep 15, 2022

Conversation

jbteves
Copy link
Collaborator

@jbteves jbteves commented Aug 25, 2021

Closes #762 . Note that there is some root issue causing the divide by 0 in the first place, but I haven't figured out why it's happening. Since we at least want to suppress the warning for now, this is a small patch to only suppress the warning.

Changes proposed in this pull request:

  • Simply suppresses warning

@codecov
Copy link

codecov bot commented Aug 25, 2021

Codecov Report

Merging #786 (129abd6) into main (64fa9d0) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #786      +/-   ##
==========================================
+ Coverage   93.27%   93.29%   +0.01%     
==========================================
  Files          27       27              
  Lines        2217     2222       +5     
==========================================
+ Hits         2068     2073       +5     
  Misses        149      149              
Impacted Files Coverage Δ
tedana/utils.py 97.46% <100.00%> (+0.08%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 64fa9d0...129abd6. Read the comment docs.

tedana/utils.py Outdated
dsi = (2. * intersection.sum(axis=axis)) / arr_sum
if np.any(arr_sum == 0):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
warnings.simplefilter("ignore")
warnings.simplefilter("ignore", RuntimeWarning)

If we're specific enough about the warning we catch, we could just get rid of the entire if statement starting at line 198. Right now we check if all thresholded maps are empty, then here we check if some are empty, and in the new else we run when none are empty. We should be able to just filter the warning and calculate across all maps without checking if any are empty, right?

Copy link
Member

Choose a reason for hiding this comment

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

Or I guess we could mask things and unmask them? Maybe with a debug message reporting the number of empty maps?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree with your suggestion, I actually meant to add that filter, whoops.
Yes, we can just not check for emptiness, but I'm inclined to think we actually should warn users if all of the maps are empty. The issue is it's difficult to make a meaningful warning in that context.

Copy link
Member

Choose a reason for hiding this comment

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

What about a warning of how many are empty? Then we don't have to check if all are empty, then any are empty. We check if any are empty, raise a warning with how many, and then outside that if statement we apply the warning filter?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That would be pretty sensible and straightforward. We also can add a prompt for the users to examine the component table for dice values of 0.

Copy link
Member

Choose a reason for hiding this comment

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

I like it!

tedana/utils.py Outdated
Comment on lines 222 to 226
with warnings.catch_warnings():
warnings.simplefilter("ignore")
dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum
else:
dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum
Copy link
Member

@tsalo tsalo Sep 22, 2021

Choose a reason for hiding this comment

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

It would be simpler to drop the else completely, I think.

Suggested change
with warnings.catch_warnings():
warnings.simplefilter("ignore")
dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum
else:
dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum
with warnings.catch_warnings():
warnings.simplefilter("ignore")
dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum

Can we have the filter target the divide-by-zero warning specifically?

tedana/utils.py Outdated
Comment on lines 216 to 219
f"Calculating dice coefficient with {total_zeros} "
"zero-elements in the denominator. "
"Please check your component table for dice columns with 0-"
"values"
Copy link
Member

Choose a reason for hiding this comment

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

How about something like f"{total_zeros}/{arr_sum.size} components have empty maps, resulting in Dice values of 0. Please check your..."

tedana/utils.py Outdated
Comment on lines 209 to 210
dsi = np.zeros(arr_sum.shape)
else:
Copy link
Member

Choose a reason for hiding this comment

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

I can't tag the whole if statement above, but it can be removed now that you have the warning and filter below.

@jbteves
Copy link
Collaborator Author

jbteves commented Oct 15, 2021

@tsalo I'm actually running into a mild style issue. If I run black --diff tedana from the top level, it says that it would make 11 changes, but I'm able to pass the local make lint. Is there some black configuration I don't have set correctly?

@tsalo
Copy link
Member

tsalo commented Oct 18, 2021

The black configuration info should all be in pyproject.toml.

tedana/pyproject.toml

Lines 1 to 5 in 4d5c645

[tool.black]
line-length = 99
target-version = ['py37']
include = '\.pyi?$'
exclude = '''

However, we also have our flake8 settings set up to be compatible with black, so that might also come into play here:

tedana/setup.cfg

Lines 9 to 15 in 4d5c645

[flake8]
max-line-length = 99
exclude=*build/
ignore = E203,E402,W503
per-file-ignores =
*/__init__.py:F401
docstring-convention = numpy

Could it be a black version issue? Maybe they made changes to the actual rules black implements?

Copy link
Member

@tsalo tsalo left a comment

Choose a reason for hiding this comment

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

The changes look good to me. I don't know if you're still running into a styling issue, but the linter job in the CI is passing, so I'm happy to see it merged.

@tsalo tsalo requested a review from eurunuela August 12, 2022 14:29
Copy link
Collaborator

@eurunuela eurunuela left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you!

@jbteves
Copy link
Collaborator Author

jbteves commented Sep 13, 2022

I forgot, did we pause this merge for some reason? I feel like I recall some hesitation due to not being sure about precisely what caused the underlying issue.

@eurunuela
Copy link
Collaborator

I forgot, did we pause this merge for some reason? I feel like I recall some hesitation due to not being sure about precisely what caused the underlying issue.

I honestly do not remember we paused it for any reason.

@jbteves jbteves merged commit c7924b5 into ME-ICA:main Sep 15, 2022
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.

Warning in true_divide from dice calculation
3 participants