From 0dfb91a4e15b567a2ebbab25d68311469767f1c6 Mon Sep 17 00:00:00 2001 From: Joshua Teves Date: Wed, 25 Aug 2021 15:00:37 -0400 Subject: [PATCH 01/12] Suppresses divide by 0 warning --- tedana/utils.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tedana/utils.py b/tedana/utils.py index 7011068c1..69aa00375 100644 --- a/tedana/utils.py +++ b/tedana/utils.py @@ -3,6 +3,7 @@ """ import logging import os.path as op +import warnings import nibabel as nib import numpy as np @@ -208,7 +209,12 @@ def dice(arr1, arr2, axis=None): dsi = np.zeros(arr_sum.shape) else: intersection = np.logical_and(arr1, arr2) - dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum + if np.any(arr_sum == 0): + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + dsi = (2. * intersection.sum(axis=axis)) / arr_sum + else: + dsi = (2. * intersection.sum(axis=axis)) / arr_sum return dsi From 2457fc8fa8874883dc5e8d3fe575ee074bda7231 Mon Sep 17 00:00:00 2001 From: Joshua Teves Date: Wed, 22 Sep 2021 15:46:47 -0400 Subject: [PATCH 02/12] Adds warning on zero-counts --- tedana/utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tedana/utils.py b/tedana/utils.py index 69aa00375..0eec1ac2c 100644 --- a/tedana/utils.py +++ b/tedana/utils.py @@ -209,7 +209,16 @@ def dice(arr1, arr2, axis=None): dsi = np.zeros(arr_sum.shape) else: intersection = np.logical_and(arr1, arr2) - if np.any(arr_sum == 0): + # 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"Calculating dice coefficient with {total_zeros} " + "zero-elements in the denominator. " + "Please check your component table for dice columns with 0-" + "values" + ) + with warnings.catch_warnings(): warnings.simplefilter("ignore") dsi = (2. * intersection.sum(axis=axis)) / arr_sum From c718811f11c8374e4d45de0668179058d44684e1 Mon Sep 17 00:00:00 2001 From: Joshua Teves Date: Wed, 22 Sep 2021 16:36:30 -0400 Subject: [PATCH 03/12] Reformat --- tedana/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tedana/utils.py b/tedana/utils.py index 0eec1ac2c..d6b426135 100644 --- a/tedana/utils.py +++ b/tedana/utils.py @@ -221,9 +221,9 @@ def dice(arr1, arr2, axis=None): with warnings.catch_warnings(): warnings.simplefilter("ignore") - dsi = (2. * intersection.sum(axis=axis)) / arr_sum + dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum else: - dsi = (2. * intersection.sum(axis=axis)) / arr_sum + dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum return dsi From 1b4fd0398629e86216283cfd652d7233048876c1 Mon Sep 17 00:00:00 2001 From: Joshua Teves Date: Fri, 15 Oct 2021 09:49:12 -0400 Subject: [PATCH 04/12] Update warning message --- tedana/utils.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tedana/utils.py b/tedana/utils.py index d6b426135..c834ff749 100644 --- a/tedana/utils.py +++ b/tedana/utils.py @@ -213,10 +213,8 @@ def dice(arr1, arr2, axis=None): total_zeros = np.count_nonzero(arr_sum == 0) if total_zeros > 0: LGR.warning( - f"Calculating dice coefficient with {total_zeros} " - "zero-elements in the denominator. " - "Please check your component table for dice columns with 0-" - "values" + 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(): From 29f1bdf63361329b46fe0d4f45814ce261a9115c Mon Sep 17 00:00:00 2001 From: Joshua Teves Date: Fri, 15 Oct 2021 09:53:34 -0400 Subject: [PATCH 05/12] Address more review --- tedana/utils.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tedana/utils.py b/tedana/utils.py index c834ff749..9df89084d 100644 --- a/tedana/utils.py +++ b/tedana/utils.py @@ -207,6 +207,9 @@ def dice(arr1, arr2, axis=None): arr_sum = arr1.sum(axis=axis) + arr2.sum(axis=axis) if np.all(arr_sum == 0): dsi = np.zeros(arr_sum.shape) + LGR.warning( + "All components found to have empty maps during dice calculation" + ) else: intersection = np.logical_and(arr1, arr2) # Count number of zero-elements in the denominator and report @@ -217,11 +220,9 @@ def dice(arr1, arr2, axis=None): "Please check your component table for dice columns with 0-values." ) - 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", category=RuntimeWarning, message="invalid value encountered in true_divide") + dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum return dsi From f91c6de4658c58474e9fbf3d9d21a584b5b4c13c Mon Sep 17 00:00:00 2001 From: Joshua Teves Date: Fri, 15 Oct 2021 09:54:06 -0400 Subject: [PATCH 06/12] Fix accidental overindent --- tedana/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tedana/utils.py b/tedana/utils.py index 9df89084d..3f6daaaad 100644 --- a/tedana/utils.py +++ b/tedana/utils.py @@ -220,9 +220,9 @@ def dice(arr1, arr2, axis=None): "Please check your component table for dice columns with 0-values." ) - with warnings.catch_warnings(): - warnings.simplefilter("ignore", category=RuntimeWarning, message="invalid value encountered in true_divide") - dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum + with warnings.catch_warnings(): + warnings.simplefilter("ignore", category=RuntimeWarning, message="invalid value encountered in true_divide") + dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum return dsi From ea0f8a5b10f919e1cd98a0ba3ff2272585be94e7 Mon Sep 17 00:00:00 2001 From: Joshua Teves Date: Fri, 15 Oct 2021 09:55:55 -0400 Subject: [PATCH 07/12] Actually address the review maybe; finish coffee --- tedana/utils.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/tedana/utils.py b/tedana/utils.py index 3f6daaaad..96680cc00 100644 --- a/tedana/utils.py +++ b/tedana/utils.py @@ -205,24 +205,18 @@ 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) + 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( - "All components found to have empty maps during dice calculation" + 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." ) - else: - 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.simplefilter("ignore", category=RuntimeWarning, message="invalid value encountered in true_divide") - dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum + with warnings.catch_warnings(): + warnings.simplefilter("ignore", category=RuntimeWarning, message="invalid value encountered in true_divide") + dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum return dsi From d4031ec7045cb3d7c29a2af3185d5bf23111e1b9 Mon Sep 17 00:00:00 2001 From: Joshua Teves Date: Fri, 15 Oct 2021 15:25:54 -0400 Subject: [PATCH 08/12] Switches to correct warning filter --- tedana/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tedana/utils.py b/tedana/utils.py index 96680cc00..d4e08f32e 100644 --- a/tedana/utils.py +++ b/tedana/utils.py @@ -215,7 +215,7 @@ def dice(arr1, arr2, axis=None): ) with warnings.catch_warnings(): - warnings.simplefilter("ignore", category=RuntimeWarning, message="invalid value encountered in true_divide") + warnings.filterwarnings("ignore", category=RuntimeWarning, message="invalid value encountered in true_divide") dsi = (2.0 * intersection.sum(axis=axis)) / arr_sum return dsi From 2aef8109bb6ad93b20c823190f31f29f4cfe1458 Mon Sep 17 00:00:00 2001 From: Joshua Teves Date: Fri, 15 Oct 2021 15:32:32 -0400 Subject: [PATCH 09/12] Add nan -> 0 --- tedana/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tedana/utils.py b/tedana/utils.py index d4e08f32e..794cb23fd 100644 --- a/tedana/utils.py +++ b/tedana/utils.py @@ -217,6 +217,7 @@ def dice(arr1, arr2, axis=None): 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 From 76ba579313d2774842117351be1d72ed642e256c Mon Sep 17 00:00:00 2001 From: Joshua Teves Date: Fri, 15 Oct 2021 15:44:22 -0400 Subject: [PATCH 10/12] Style fixes --- tedana/utils.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tedana/utils.py b/tedana/utils.py index 794cb23fd..9eb57cf9c 100644 --- a/tedana/utils.py +++ b/tedana/utils.py @@ -210,12 +210,17 @@ def dice(arr1, arr2, axis=None): 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. " + 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") + 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) From e0e8c763afac42dfffd674604af2b523980eb400 Mon Sep 17 00:00:00 2001 From: Joshua Teves Date: Fri, 15 Oct 2021 15:52:47 -0400 Subject: [PATCH 11/12] Ugh style again --- tedana/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tedana/utils.py b/tedana/utils.py index 9eb57cf9c..708467e97 100644 --- a/tedana/utils.py +++ b/tedana/utils.py @@ -210,8 +210,8 @@ def dice(arr1, arr2, axis=None): 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. " + 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." ) From aa305e3cd5859b383b76a474e55cd8e58e9ad07a Mon Sep 17 00:00:00 2001 From: Joshua Teves Date: Fri, 15 Oct 2021 16:07:32 -0400 Subject: [PATCH 12/12] Reformats again --- tedana/utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tedana/utils.py b/tedana/utils.py index 708467e97..c07b4f645 100644 --- a/tedana/utils.py +++ b/tedana/utils.py @@ -217,9 +217,7 @@ def dice(arr1, arr2, axis=None): with warnings.catch_warnings(): warnings.filterwarnings( - "ignore", - category=RuntimeWarning, - message="invalid value encountered in true_divide" + "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)