Skip to content

Commit

Permalink
Fix/onedimensional sigma for kernels (#499)
Browse files Browse the repository at this point in the history
* add error message when passing high dimensional sigma to kernel, add handling of 1-dimensional kernel with size one

* add test raiseError for multidimensional sigma, add test for handling one-dimensional sigma of size 1

* add check for size
  • Loading branch information
Moritz-Alexander-Kern authored Oct 13, 2022
1 parent 5127aba commit 5885fff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 11 additions & 0 deletions elephant/kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ class Kernel(object):
TypeError
If `sigma` is not `pq.Quantity`.
If `sigma` is a multidimensional array
ValueError
If `sigma` is negative.
If `invert` is not `bool`.
Expand All @@ -142,6 +146,13 @@ def __init__(self, sigma, invert=False):
if not isinstance(sigma, pq.Quantity):
raise TypeError("'sigma' must be a quantity")

if sigma.ndim > 0:
# handle the one-dimensional case of size 1
if sigma.ndim == 1 & sigma.size == 1:
sigma = sigma[0]
else:
raise TypeError("'sigma' cannot be multidimensional")

if sigma.magnitude < 0:
raise ValueError("'sigma' cannot be negative")

Expand Down
9 changes: 7 additions & 2 deletions elephant/test/test_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ def test_error_kernels(self):
"""
Test of various error cases in the kernels module.
"""
self.assertRaises(
TypeError, kernels.RectangularKernel, sigma=2.0)
# pass multidimensional sigma
self.assertRaises(TypeError, kernels.RectangularKernel,
sigma=[2.0, 2.3] * pq.s)
# pass one-dimensional sigma, this should be handled
self.assertEqual(
kernels.RectangularKernel(sigma=[2.0] * pq.s).sigma.ndim, 0)
self.assertRaises(TypeError, kernels.RectangularKernel, sigma=2.0)
self.assertRaises(
ValueError, kernels.RectangularKernel, sigma=-0.03 * pq.s)
self.assertRaises(
Expand Down

0 comments on commit 5885fff

Please sign in to comment.