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)) 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..361a1c103 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.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.') 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.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.') values += 0.5 return int(values)