summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMario Mulansky <mario.mulansky@gmx.net>2015-05-13 18:19:02 +0200
committerMario Mulansky <mario.mulansky@gmx.net>2015-05-13 18:19:02 +0200
commit8841138b74242ed9eb77c972c76e9a617778a79a (patch)
tree17dfc3f732af4af7b3f0f119197e05a36baa5d70
parentf3e21dcc82d48f0980b0107f1e5cf320a8b213f3 (diff)
pwc function now returns intermediate value at exact spike times
-rw-r--r--pyspike/PieceWiseConstFunc.py35
-rw-r--r--test/test_function.py6
2 files changed, 31 insertions, 10 deletions
diff --git a/pyspike/PieceWiseConstFunc.py b/pyspike/PieceWiseConstFunc.py
index cf64e58..dea1a56 100644
--- a/pyspike/PieceWiseConstFunc.py
+++ b/pyspike/PieceWiseConstFunc.py
@@ -37,16 +37,35 @@ class PieceWiseConstFunc(object):
"Invalid time: " + str(t)
ind = np.searchsorted(self.x, t, side='right')
- # correct the cases t == x[0], t == x[-1]
- try:
+ if isinstance(t, collections.Sequence):
+ # t is a sequence of values
+ # correct the cases t == x[0], t == x[-1]
ind[ind == 0] = 1
ind[ind == len(self.x)] = len(self.x)-1
- except TypeError:
- if ind == 0:
- ind = 1
- if ind == len(self.x):
- ind = len(self.x)-1
- return self.y[ind-1]
+ value = self.y[ind-1]
+ # correct the values at exact spike times: there the value should
+ # be the at half of the step
+ # obtain the 'left' side indices for t
+ ind_l = np.searchsorted(self.x, t, side='left')
+ # if left and right side indices differ, the time t has to appear
+ # in self.x
+ ind_at_spike = ind[np.logical_and(np.logical_and(ind != ind_l,
+ ind > 1),
+ ind < len(self.x))]
+ value[ind_at_spike] = 0.5 * (self.y[ind_at_spike-1] +
+ self.y[ind_at_spike-2])
+ return value
+ else:
+ # specific check for interval edges
+ if t == self.x[0]:
+ return self.y[0]
+ if t == self.x[-1]:
+ return self.y[-1]
+ # check if we are on any other exact spike time
+ if sum(self.x == t) > 0:
+ # use the middle of the left and right ISI value
+ return 0.5 * (self.y[ind-1] + self.y[ind-2])
+ return self.y[ind-1]
def copy(self):
""" Returns a copy of itself
diff --git a/test/test_function.py b/test/test_function.py
index c56a295..8ad4b17 100644
--- a/test/test_function.py
+++ b/test/test_function.py
@@ -25,12 +25,14 @@ def test_pwc():
# function values
assert_equal(f(0.0), 1.0)
assert_equal(f(0.5), 1.0)
+ assert_equal(f(1.0), 0.25)
assert_equal(f(2.25), 1.5)
+ assert_equal(f(2.5), 2.25/2)
assert_equal(f(3.5), 0.75)
assert_equal(f(4.0), 0.75)
- assert_array_equal(f([0.0, 0.5, 2.25, 3.5, 4.0]),
- [1.0, 1.0, 1.5, 0.75, 0.75])
+ assert_array_equal(f([0.0, 0.5, 1.0, 2.25, 2.5, 3.5, 4.0]),
+ [1.0, 1.0, 0.25, 1.5, 2.25/2, 0.75, 0.75])
xp, yp = f.get_plottable_data()