From df57f297e732bd6972ad7bda2241a2386c014cca Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Tue, 15 Oct 2024 07:51:46 +0200 Subject: [PATCH] Prevent `Mean of empty slice` warning in `fiddy.success.Consistency.method` (#55) Fixes #52. --- fiddy/success.py | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/fiddy/success.py b/fiddy/success.py index 257e72a..4cadd28 100644 --- a/fiddy/success.py +++ b/fiddy/success.py @@ -1,6 +1,6 @@ import abc from collections.abc import Callable -from typing import Any, Union +from typing import Any import numpy as np @@ -97,25 +97,26 @@ def method( equal_nan=self.equal_nan, ).all() - consistent_results = [ - np.nanmean(list(results_by_size[size].values()), axis=0) - for size, success in success_by_size.items() - if success - ] - - success = False - value = np.nanmean(np.array(consistent_results), axis=0) - if consistent_results: - success = ( - np.isclose( - consistent_results, - value, - rtol=self.rtol, - atol=self.atol, - equal_nan=self.equal_nan, - ).all() - and not np.isnan(consistent_results).all() - ) - value = np.average(np.array(consistent_results), axis=0) + consistent_results = np.array( + [ + np.nanmean(list(results_by_size[size].values()), axis=0) + for size, success in success_by_size.items() + if success + ] + ) + + if len(consistent_results) == 0: + return False, np.nan + value = np.nanmean(consistent_results, axis=0) + success = ( + np.isclose( + consistent_results, + value, + rtol=self.rtol, + atol=self.atol, + equal_nan=self.equal_nan, + ).all() + and not np.isnan(consistent_results).all() + ) return success, value