Skip to content

Commit

Permalink
[REF] Suppresses divide by 0 warning (#786)
Browse files Browse the repository at this point in the history
* Suppresses divide by 0 warning

* Adds warning on zero-counts

* Reformat

* Update warning message

* Address more review

* Fix accidental overindent

* Actually address the review maybe; finish coffee

* Switches to correct warning filter

* Add nan -> 0

* Style fixes

* Ugh style again

* Reformats again
  • Loading branch information
Joshua Teves authored Sep 15, 2022
1 parent 802849e commit c7924b5
Showing 1 changed file with 16 additions and 4 deletions.
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 @@ -178,11 +179,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

0 comments on commit c7924b5

Please sign in to comment.