diff options
Diffstat (limited to 'pyspike/function.py')
-rw-r--r-- | pyspike/function.py | 58 |
1 files changed, 26 insertions, 32 deletions
diff --git a/pyspike/function.py b/pyspike/function.py index f5a1133..f10c136 100644 --- a/pyspike/function.py +++ b/pyspike/function.py @@ -171,32 +171,32 @@ that PySpike is installed by running\n 'python setup.py build_ext --inplace'! \ ############################################################## -# IntervalSequence +# MultipleValueSequence ############################################################## -class IntervalSequence(object): +class MultipleValueSequence(object): """ A class representing a sequence of values defined in some interval. - This is very similar to a `PieceWiseConstFunc`, but with different - averaging and addition. """ - def __init__(self, x, y): - """ Constructs the interval sequence. + def __init__(self, x, y, multiplicity): + """ Constructs the value sequence. - :param x: array of length N+1 defining the edges of the intervals of - the intervals. - :param y: array of length N defining the values at the intervals. + :param x: array of length N defining the points at which the values are + defined. + :param y: array of length N degining the values at the points x. + :param multiplicity: array of length N defining the multiplicity of the + values. """ # convert parameters to arrays, also ensures copying self.x = np.array(x) self.y = np.array(y) - self.extra_zero_intervals = 0 + self.mp = np.array(multiplicity) def copy(self): """ Returns a copy of itself :rtype: :class:`IntervalSequence` """ - return IntervalSequence(self.x, self.y) + return MultipleValueSequence(self.x, self.y, self.mp) def almost_equal(self, other, decimal=14): """ Checks if the function is equal to another function up to `decimal` @@ -209,9 +209,10 @@ class IntervalSequence(object): """ eps = 10.0**(-decimal) return np.allclose(self.x, other.x, atol=eps, rtol=0.0) and \ - np.allclose(self.y, other.y, atol=eps, rtol=0.0) + np.allclose(self.y, other.y, atol=eps, rtol=0.0) and \ + np.allclose(self.mp, other.mp, atol=eps, rtol=0.0) - def get_plottable_data(self): + def get_plottable_data(self, k=0): """ Returns two arrays containing x- and y-coordinates for immeditate plotting of the interval sequence. @@ -224,17 +225,10 @@ class IntervalSequence(object): plt.plot(x, y, '-o', label="Piece-wise const function") """ - x_plot = np.empty(2*len(self.x)-2) - x_plot[0] = self.x[0] - x_plot[1::2] = self.x[1:] - x_plot[2::2] = self.x[1:-1] - y_plot = np.empty(2*len(self.y)) - y_plot[::2] = self.y - normalization = 1.0 * (len(self.y)-1) / (len(self.y) + - self.extra_zero_intervals-1) - y_plot[1::2] = self.y + if k > 0: + raise NotImplementedError() - return x_plot, y_plot * normalization + return 1.0*self.x, 1.0*self.y/self.mp def integral(self, interval=None): """ Returns the integral over the given interval. For the interval @@ -250,7 +244,7 @@ class IntervalSequence(object): if interval is None: # no interval given, integrate over the whole spike train # don't count the first value, which is zero by definition - a = np.sum(self.y) + a = 1.0*np.sum(self.y[1:-1]) else: raise NotImplementedError() return a @@ -270,15 +264,15 @@ class IntervalSequence(object): if interval is None: # no interval given, average over the whole spike train # don't count the first interval for normalization - return self.integral() / (len(self.y)-1+self.extra_zero_intervals) + return self.integral() / np.sum(self.mp[1:-1]) else: raise NotImplementedError() def add(self, f): - """ Adds another `IntervalSequence` function to this function. + """ Adds another `MultipleValueSequence` function to this function. Note: only functions defined on the same interval can be summed. - :param f: :class:`IntervalSequence` function to be added. + :param f: :class:`MultipleValueSequence` function to be added. :rtype: None """ assert self.x[0] == f.x[0], "The functions have different intervals" @@ -293,12 +287,12 @@ class IntervalSequence(object): # that PySpike is installed by running\n 'python setup.py build_ext --inplace'! \ # \n Falling back to slow python backend.") # use python backend - from python_backend import add_interval_sequence_python as \ - add_interval_sequence_impl + from python_backend import add_multiple_value_sequence_python as \ + add_multiple_value_sequence_impl - self.x, self.y, extra_intervals = \ - add_interval_sequence_impl(self.x, self.y, f.x, f.y) - self.extra_zero_intervals += extra_intervals + self.x, self.y, self.mp = \ + add_multiple_value_sequence_impl(self.x, self.y, self.mp, + f.x, f.y, f.mp) def mul_scalar(self, fac): """ Multiplies the function with a scalar value |