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
20 changes: 16 additions & 4 deletions tedana/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import logging
import os.path as op
import warnings

import nibabel as nib
import numpy as np
Expand Down Expand Up @@ -204,11 +205,22 @@ def dice(arr1, arr2, axis=None):
raise ValueError("Axis provided {} not supported by the input arrays.".format(axis))

arr_sum = arr1.sum(axis=axis) + arr2.sum(axis=axis)
if np.all(arr_sum == 0):
dsi = np.zeros(arr_sum.shape)
else:
intersection = np.logical_and(arr1, arr2)
intersection = np.logical_and(arr1, arr2)
# Count number of zero-elements in the denominator and report
total_zeros = np.count_nonzero(arr_sum == 0)
if total_zeros > 0:
LGR.warning(
f"{total_zeros} of {arr_sum.size} components have empty maps, resulting in Dice "
"values of 0. "
"Please check your component table for dice columns with 0-values."
)

with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", category=RuntimeWarning, message="invalid value encountered in true_divide"
)
dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum
dsi = np.nan_to_num(dsi)

return dsi

Expand Down