From 50b85d976f2f5ec7e40faec1ede047cf45b10bc1 Mon Sep 17 00:00:00 2001 From: Mario Mulansky Date: Wed, 19 Sep 2018 16:53:50 -0700 Subject: Fix incorrect integrals in PieceWiseLinFunc (#38) Integrals of piece-wise linear functions were incorrect if the requested interval lies completely between two support points. This has been fixed, and a unit test exercising this behavior was added. Fixes #38 --- pyspike/PieceWiseLinFunc.py | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) (limited to 'pyspike/PieceWiseLinFunc.py') diff --git a/pyspike/PieceWiseLinFunc.py b/pyspike/PieceWiseLinFunc.py index 8145e63..8faaec4 100644 --- a/pyspike/PieceWiseLinFunc.py +++ b/pyspike/PieceWiseLinFunc.py @@ -146,31 +146,47 @@ class PieceWiseLinFunc: if interval is None: # no interval given, integrate over the whole spike train - integral = np.sum((self.x[1:]-self.x[:-1]) * 0.5*(self.y1+self.y2)) + return np.sum((self.x[1:]-self.x[:-1]) * 0.5*(self.y1+self.y2)) + + # 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" + if start_ind > end_ind: + print(start_ind, end_ind, self.x[start_ind]) + # contribution from between two closest edges + y_x0 = intermediate_value(self.x[start_ind-1], + self.x[start_ind], + self.y1[start_ind-1], + self.y2[start_ind-1], + interval[0]) + y_x1 = intermediate_value(self.x[start_ind-1], + self.x[start_ind], + self.y1[start_ind-1], + self.y2[start_ind-1], + interval[1]) + print(y_x0, y_x1, interval[1] - interval[0]) + integral = (y_x0 + y_x1) * 0.5 * (interval[1] - interval[0]) + print(integral) else: - # 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 integral = np.sum((self.x[start_ind+1:end_ind+1] - - self.x[start_ind:end_ind]) * - 0.5*(self.y1[start_ind:end_ind] + - self.y2[start_ind:end_ind])) + self.x[start_ind:end_ind]) * + 0.5*(self.y1[start_ind:end_ind] + + self.y2[start_ind:end_ind])) # correction from start to first index integral += (self.x[start_ind]-interval[0]) * 0.5 * \ (self.y2[start_ind-1] + - intermediate_value(self.x[start_ind-1], + intermediate_value(self.x[start_ind-1], self.x[start_ind], self.y1[start_ind-1], self.y2[start_ind-1], - interval[0] - )) + interval[0])) # correction from last index to end integral += (interval[1]-self.x[end_ind]) * 0.5 * \ (self.y1[end_ind] + - intermediate_value(self.x[end_ind], self.x[end_ind+1], + intermediate_value(self.x[end_ind], self.x[end_ind+1], self.y1[end_ind], self.y2[end_ind], interval[1] )) -- cgit v1.2.3 From ac99d20549a103e8242021d4d388b6ec8557efd0 Mon Sep 17 00:00:00 2001 From: gspr Date: Wed, 29 Dec 2021 22:55:27 +0100 Subject: Python 3.10 compatibility fix (#53) Importing Sequence et al. from collections has been deprecated since Python 3.7. --- pyspike/DiscreteFunc.py | 6 +++--- pyspike/PieceWiseConstFunc.py | 8 ++++---- pyspike/PieceWiseLinFunc.py | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) (limited to 'pyspike/PieceWiseLinFunc.py') diff --git a/pyspike/DiscreteFunc.py b/pyspike/DiscreteFunc.py index caad290..48bc787 100644 --- a/pyspike/DiscreteFunc.py +++ b/pyspike/DiscreteFunc.py @@ -5,7 +5,7 @@ from __future__ import absolute_import, print_function import numpy as np -import collections +import collections.abc import pyspike @@ -155,11 +155,11 @@ class DiscreteFunc(object): multiplicity = np.sum(self.mp[1:-1]) else: # 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): # find the indices corresponding to the interval start_ind, end_ind = get_indices(interval) value = np.sum(self.y[start_ind:end_ind]) diff --git a/pyspike/PieceWiseConstFunc.py b/pyspike/PieceWiseConstFunc.py index 17fdd3f..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 @@ -173,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: diff --git a/pyspike/PieceWiseLinFunc.py b/pyspike/PieceWiseLinFunc.py index 8faaec4..b3b503b 100644 --- a/pyspike/PieceWiseLinFunc.py +++ b/pyspike/PieceWiseLinFunc.py @@ -5,7 +5,7 @@ from __future__ import absolute_import, print_function import numpy as np -import collections +import collections.abc import pyspike @@ -46,7 +46,7 @@ class PieceWiseLinFunc: 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 @@ -211,10 +211,10 @@ class PieceWiseLinFunc: 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: -- cgit v1.2.3