Skip to content

Commit

Permalink
fix LvR values from being off when units are in seconds (NeuralEnsemb…
Browse files Browse the repository at this point in the history
  • Loading branch information
morales-gregorio committed Dec 4, 2020
1 parent f3b0146 commit 1ef33db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 9 additions & 1 deletion elephant/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,8 @@ def lvr(time_intervals, R=5*pq.ms, with_nan=False):
Parameters
----------
time_intervals : pq.Quantity or np.ndarray or list
Vector of consecutive time intervals.
Vector of consecutive time intervals. Must have time units, if not unit
is passed `ms` are assumed.
R : pq.Quantity or int or float
Refractoriness constant (R >= 0). If no quantity is passed `ms` are
assumed.
Expand Down Expand Up @@ -549,6 +550,13 @@ def lvr(time_intervals, R=5*pq.ms, with_nan=False):
if R < 0:
raise ValueError('R must be >= 0')

# check units of intervals if available
if isinstance(time_intervals, pq.Quantity):
time_intervals = time_intervals.rescale('ms').magnitude
else:
warnings.warn('No units specified for time_intervals,'
' assuming milliseconds (ms)')

# convert to array, cast to float
time_intervals = np.asarray(time_intervals)
np_nan = __variation_check(time_intervals, with_nan)
Expand Down
10 changes: 8 additions & 2 deletions elephant/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,20 @@ def setUp(self):
def test_lvr_with_quantities(self):
seq = pq.Quantity(self.test_seq, units='ms')
assert_array_almost_equal(statistics.lvr(seq), self.target, decimal=9)
seq = pq.Quantity(self.test_seq, units='ms').rescale('s')
assert_array_almost_equal(statistics.lvr(seq), self.target, decimal=9)

def test_lvr_with_plain_array(self):
seq = np.array(self.test_seq)
assert_array_almost_equal(statistics.lvr(seq), self.target, decimal=9)
with self.assertWarns(UserWarning):
assert_array_almost_equal(statistics.lvr(seq),
self.target, decimal=9)

def test_lvr_with_list(self):
seq = self.test_seq
assert_array_almost_equal(statistics.lvr(seq), self.target, decimal=9)
with self.assertWarns(UserWarning):
assert_array_almost_equal(statistics.lvr(seq),
self.target, decimal=9)

def test_lvr_raise_error(self):
seq = self.test_seq
Expand Down

0 comments on commit 1ef33db

Please sign in to comment.