From 2a99e3bf2c575efc9abbc1cf262810d223f2cad0 Mon Sep 17 00:00:00 2001 From: Mario Mulansky Date: Wed, 15 Oct 2014 12:32:09 +0200 Subject: +average_profile function --- pyspike/function.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'pyspike/function.py') diff --git a/pyspike/function.py b/pyspike/function.py index bd3e2d5..46fdea2 100644 --- a/pyspike/function.py +++ b/pyspike/function.py @@ -26,9 +26,17 @@ class PieceWiseConstFunc: function. - y: array of length N defining the function values at the intervals. """ + # convert parameters to arrays, also ensures copying self.x = np.array(x) self.y = np.array(y) + def copy(self): + """ Returns a copy of itself + Returns: + - PieceWiseConstFunc copy + """ + return PieceWiseConstFunc(self.x, self.y) + def almost_equal(self, other, decimal=14): """ Checks if the function is equal to another function up to `decimal` precision. @@ -108,10 +116,18 @@ class PieceWiseLinFunc: - y2: array of length N defining the function values at the right of the intervals. """ + # convert to array, which also ensures copying self.x = np.array(x) self.y1 = np.array(y1) self.y2 = np.array(y2) + def copy(self): + """ Returns a copy of itself + Returns: + - PieceWiseLinFunc copy + """ + return PieceWiseLinFunc(self.x, self.y1, self.y2) + def almost_equal(self, other, decimal=14): """ Checks if the function is equal to another function up to `decimal` precision. @@ -183,3 +199,22 @@ class PieceWiseLinFunc: """ self.y1 *= fac self.y2 *= fac + + +def average_profile(profiles): + """ Computes the average profile from the given ISI- or SPIKE-profiles. + Args: + - profiles: list of PieceWiseConstFunc or PieceWiseLinFunc representing + ISI- or SPIKE-profiles to be averaged + Returns: + - avrg_profile: PieceWiseConstFunc or PieceWiseLinFunc containing the + average profile. + """ + assert len(profiles) > 1 + + avrg_profile = profiles[0].copy() + for i in xrange(1, len(profiles)): + avrg_profile.add(profiles[i]) + avrg_profile.mul_scalar(1.0/len(profiles)) # normalize + + return avrg_profile -- cgit v1.2.3