summaryrefslogtreecommitdiff
path: root/pyspike/PieceWiseConstFunc.py
diff options
context:
space:
mode:
Diffstat (limited to 'pyspike/PieceWiseConstFunc.py')
-rw-r--r--pyspike/PieceWiseConstFunc.py40
1 files changed, 26 insertions, 14 deletions
diff --git a/pyspike/PieceWiseConstFunc.py b/pyspike/PieceWiseConstFunc.py
index 5ce5f27..e33c61d 100644
--- a/pyspike/PieceWiseConstFunc.py
+++ b/pyspike/PieceWiseConstFunc.py
@@ -5,7 +5,7 @@
from __future__ import absolute_import, print_function
import numpy as np
-import collections
+import collections.abc
import pyspike
@@ -39,7 +39,7 @@ class PieceWiseConstFunc(object):
ind = np.searchsorted(self.x, t, side='right')
- if isinstance(t, collections.Sequence):
+ if isinstance(t, collections.abc.Sequence):
# t is a sequence of values
# correct the cases t == x[0], t == x[-1]
ind[ind == 0] = 1
@@ -129,19 +129,31 @@ class PieceWiseConstFunc(object):
# no interval given, integrate over the whole spike train
a = np.sum((self.x[1:]-self.x[:-1]) * self.y)
else:
+ if interval[0]>interval[1]:
+ raise ValueError("Invalid averaging interval: interval[0]>=interval[1]")
+ if interval[0]<self.x[0]:
+ raise ValueError("Invalid averaging interval: interval[0]<self.x[0]")
+ if interval[1]>self.x[-1]:
+ raise ValueError("Invalid averaging interval: interval[0]<self.x[-1]")
# find the indices corresponding to the interval
start_ind = np.searchsorted(self.x, interval[0], side='right')
end_ind = np.searchsorted(self.x, interval[1], side='left')-1
- assert start_ind > 0 and end_ind < len(self.x), \
- "Invalid averaging interval"
- # first the contribution from between the indices
- a = np.sum((self.x[start_ind+1:end_ind+1] -
- self.x[start_ind:end_ind]) *
- self.y[start_ind:end_ind])
- # correction from start to first index
- a += (self.x[start_ind]-interval[0]) * self.y[start_ind-1]
- # correction from last index to end
- a += (interval[1]-self.x[end_ind]) * self.y[end_ind]
+ if start_ind > end_ind:
+ # contribution from between two closest edges
+ a = (self.x[start_ind]-self.x[end_ind]) * self.y[end_ind]
+ # minus the part that is not within the interval
+ a -= ((interval[0]-self.x[end_ind])+(self.x[start_ind]-interval[1])) * self.y[end_ind]
+ else:
+ assert start_ind > 0 and end_ind < len(self.x), \
+ "Invalid averaging interval"
+ # first the contribution from between the indices
+ a = np.sum((self.x[start_ind+1:end_ind+1] -
+ self.x[start_ind:end_ind]) *
+ self.y[start_ind:end_ind])
+ # correction from start to first index
+ a += (self.x[start_ind]-interval[0]) * self.y[start_ind-1]
+ # correction from last index to end
+ a += (interval[1]-self.x[end_ind]) * self.y[end_ind]
return a
def avrg(self, interval=None):
@@ -161,10 +173,10 @@ class PieceWiseConstFunc(object):
return self.integral() / (self.x[-1]-self.x[0])
# check if interval is as sequence
- assert isinstance(interval, collections.Sequence), \
+ assert isinstance(interval, collections.abc.Sequence), \
"Invalid value for `interval`. None, Sequence or Tuple expected."
# check if interval is a sequence of intervals
- if not isinstance(interval[0], collections.Sequence):
+ if not isinstance(interval[0], collections.abc.Sequence):
# just one interval
a = self.integral(interval) / (interval[1]-interval[0])
else: