Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] use warnings with logging in elephant.utils.round_binning_errors #571

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions elephant/test/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
10 changes: 4 additions & 6 deletions elephant/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
26 changes: 18 additions & 8 deletions elephant/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from __future__ import division, print_function, unicode_literals

import ctypes
import logging
import warnings
from functools import wraps

Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down