From 9fc5d4a446b281092416d76d1cfcdd4ea0b817cd Mon Sep 17 00:00:00 2001 From: Moritz-Alexander-Kern Date: Fri, 2 Jun 2023 11:02:46 +0200 Subject: [PATCH 1/4] add logging to elephant.utils.round_binning_errors --- elephant/test/test_utils.py | 10 ++++------ elephant/utils.py | 26 ++++++++++++++++++-------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/elephant/test/test_utils.py b/elephant/test/test_utils.py index 5db79bd23..fcfb92263 100644 --- a/elephant/test/test_utils.py +++ b/elephant/test/test_utils.py @@ -44,15 +44,13 @@ def test_check_neo_consistency(self): object_type=neo.SpikeTrain) def test_round_binning_errors(self): - with self.assertWarns(UserWarning): - n_bins = utils.round_binning_errors(0.999999, tolerance=1e-6) - self.assertEqual(n_bins, 1) + n_bins = utils.round_binning_errors(0.999999, tolerance=1e-6) + self.assertEqual(n_bins, 1) self.assertEqual(utils.round_binning_errors(0.999999, tolerance=None), 0) array = np.array([0, 0.7, 1 - 1e-8, 1 - 1e-9]) - with self.assertWarns(UserWarning): - corrected = utils.round_binning_errors(array.copy()) - assert_array_equal(corrected, [0, 0, 1, 1]) + corrected = utils.round_binning_errors(array.copy()) + assert_array_equal(corrected, [0, 0, 1, 1]) assert_array_equal( utils.round_binning_errors(array.copy(), tolerance=None), [0, 0, 0, 0]) diff --git a/elephant/utils.py b/elephant/utils.py index f634a570a..00f30f62e 100644 --- a/elephant/utils.py +++ b/elephant/utils.py @@ -12,6 +12,7 @@ from __future__ import division, print_function, unicode_literals import ctypes +import logging import warnings from functools import wraps @@ -32,6 +33,15 @@ ] +# Create logger and set configuration +logger = logging.getLogger(__file__) +log_handler = logging.StreamHandler() +log_handler.setFormatter( + logging.Formatter(f"[%(asctime)s] {__name__[__name__.rfind('.')+1::]} -" + " %(levelname)s: %(message)s")) +logger.addHandler(log_handler) +logger.propagate = False + def is_binary(array): """ Parameters @@ -288,18 +298,18 @@ def round_binning_errors(values, tolerance=1e-8): if isinstance(values, np.ndarray): num_corrections = correction_mask.sum() if num_corrections > 0: - warnings.warn(f'Correcting {num_corrections} rounding errors by ' - f'shifting the affected spikes into the following ' - f'bin. You can set tolerance=None to disable this ' - 'behaviour.') + logger.info(f'Correcting {num_corrections} rounding errors by ' + 'shifting the affected spikes into the following ' + 'bin. You can set tolerance=None to disable this ' + 'behaviour.') values[correction_mask] += 0.5 return values.astype(np.int32) if correction_mask: - warnings.warn('Correcting a rounding error in the calculation ' - 'of the number of bins by incrementing the value by 1. ' - 'You can set tolerance=None to disable this ' - 'behaviour.') + logger.info('Correcting a rounding error in the calculation ' + 'of the number of bins by incrementing the value by 1. ' + 'You can set tolerance=None to disable this ' + 'behaviour.') values += 0.5 return int(values) From 5a698de5d5563b8a4125a03e16d3b08707600ed1 Mon Sep 17 00:00:00 2001 From: Moritz-Alexander-Kern Date: Fri, 2 Jun 2023 11:53:07 +0200 Subject: [PATCH 2/4] remove assert warning from conversion unit test, which checks a warning raised in elephant.utils --- elephant/test/test_conversion.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/elephant/test/test_conversion.py b/elephant/test/test_conversion.py index 05e02edd7..a0f714a17 100644 --- a/elephant/test/test_conversion.py +++ b/elephant/test/test_conversion.py @@ -717,10 +717,9 @@ def test_binned_sparsity(self): def test_binned_spiketrain_rounding(self): train = neo.SpikeTrain(times=np.arange(120000) / 30000. * pq.s, t_start=0 * pq.s, t_stop=4 * pq.s) - with self.assertWarns(UserWarning): - bst = cv.BinnedSpikeTrain(train, - t_start=0 * pq.s, t_stop=4 * pq.s, - bin_size=1. / 30000. * pq.s) + bst = cv.BinnedSpikeTrain(train, + t_start=0 * pq.s, t_stop=4 * pq.s, + bin_size=1. / 30000. * pq.s) assert_array_equal(bst.to_array().nonzero()[1], np.arange(120000)) From 23f732c7ccafb1607a77894725c69d1ffa00fd84 Mon Sep 17 00:00:00 2001 From: Moritz Kern <92092328+Moritz-Alexander-Kern@users.noreply.github.com> Date: Wed, 25 Oct 2023 10:29:10 +0200 Subject: [PATCH 3/4] Update elephant/utils.py Co-authored-by: Michael Denker --- elephant/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elephant/utils.py b/elephant/utils.py index 00f30f62e..9f3e72599 100644 --- a/elephant/utils.py +++ b/elephant/utils.py @@ -298,7 +298,7 @@ def round_binning_errors(values, tolerance=1e-8): if isinstance(values, np.ndarray): num_corrections = correction_mask.sum() if num_corrections > 0: - logger.info(f'Correcting {num_corrections} rounding errors by ' + logger.warning(f'Correcting {num_corrections} rounding errors by ' 'shifting the affected spikes into the following ' 'bin. You can set tolerance=None to disable this ' 'behaviour.') From 7c2390a44cea45fcbfc9cbfafb49ad4c3995ebee Mon Sep 17 00:00:00 2001 From: Moritz Kern <92092328+Moritz-Alexander-Kern@users.noreply.github.com> Date: Wed, 25 Oct 2023 10:29:18 +0200 Subject: [PATCH 4/4] Update elephant/utils.py Co-authored-by: Michael Denker --- elephant/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elephant/utils.py b/elephant/utils.py index 9f3e72599..361a1c103 100644 --- a/elephant/utils.py +++ b/elephant/utils.py @@ -306,7 +306,7 @@ def round_binning_errors(values, tolerance=1e-8): return values.astype(np.int32) if correction_mask: - logger.info('Correcting a rounding error in the calculation ' + logger.warning('Correcting a rounding error in the calculation ' 'of the number of bins by incrementing the value by 1. ' 'You can set tolerance=None to disable this ' 'behaviour.')