| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, print_function |
| 6 |
|
| 7 |
import numpy as np |
| 8 |
import collections.abc |
| 9 |
import pyspike |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class PieceWiseConstFunc(object): |
| 16 |
""" A class representing a piece-wise constant function. """ |
| 17 |
|
| 18 |
def __init__(self, x, y): |
| 19 |
""" Constructs the piece-wise const function. |
| 20 |
|
| 21 |
:param x: array of length N+1 defining the edges of the intervals of |
| 22 |
the pwc function. |
| 23 |
:param y: array of length N defining the function values at the |
| 24 |
intervals. |
| 25 |
""" |
| 26 |
|
| 27 |
self.x = np.array(x) |
| 28 |
self.y = np.array(y) |
| 29 |
|
| 30 |
def __call__(self, t): |
| 31 |
""" Returns the function value for the given time t. If t is a list of |
| 32 |
times, the corresponding list of values is returned. |
| 33 |
|
| 34 |
:param: time t, or list of times |
| 35 |
:returns: function value(s) at that time(s). |
| 36 |
""" |
| 37 |
assert np.all(t >= self.x[0]) and np.all(t <= self.x[-1]), \ |
| 38 |
"Invalid time: " + str(t) |
| 39 |
|
| 40 |
ind = np.searchsorted(self.x, t, side='right') |
| 41 |
|
| 42 |
if isinstance(t, collections.abc.Sequence): |
| 43 |
|
| 44 |
|
| 45 |
ind[ind == 0] = 1 |
| 46 |
ind[ind == len(self.x)] = len(self.x)-1 |
| 47 |
value = self.y[ind-1] |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
ind_l = np.searchsorted(self.x, t, side='left') |
| 52 |
|
| 53 |
|
| 54 |
ind_at_spike = np.logical_and(np.logical_and(ind != ind_l, |
| 55 |
ind > 1), |
| 56 |
ind < len(self.x)) |
| 57 |
|
| 58 |
val_ind = np.arange(len(ind))[ind_at_spike] |
| 59 |
|
| 60 |
xy_ind = ind[ind_at_spike] |
| 61 |
|
| 62 |
value[val_ind] = 0.5 * (self.y[xy_ind-1] + self.y[xy_ind-2]) |
| 63 |
return value |
| 64 |
else: |
| 65 |
|
| 66 |
if t == self.x[0]: |
| 67 |
return self.y[0] |
| 68 |
if t == self.x[-1]: |
| 69 |
return self.y[-1] |
| 70 |
|
| 71 |
if sum(self.x == t) > 0: |
| 72 |
|
| 73 |
return 0.5 * (self.y[ind-1] + self.y[ind-2]) |
| 74 |
return self.y[ind-1] |
| 75 |
|
| 76 |
def copy(self): |
| 77 |
""" Returns a copy of itself |
| 78 |
|
| 79 |
:rtype: :class:`PieceWiseConstFunc` |
| 80 |
""" |
| 81 |
return PieceWiseConstFunc(self.x, self.y) |
| 82 |
|
| 83 |
def almost_equal(self, other, decimal=14): |
| 84 |
""" Checks if the function is equal to another function up to `decimal` |
| 85 |
precision. |
| 86 |
|
| 87 |
:param other: another :class:`PieceWiseConstFunc` |
| 88 |
:returns: True if the two functions are equal up to `decimal` decimals, |
| 89 |
False otherwise |
| 90 |
:rtype: bool |
| 91 |
""" |
| 92 |
eps = 10.0**(-decimal) |
| 93 |
return np.allclose(self.x, other.x, atol=eps, rtol=0.0) and \ |
| 94 |
np.allclose(self.y, other.y, atol=eps, rtol=0.0) |
| 95 |
|
| 96 |
def get_plottable_data(self): |
| 97 |
""" Returns two arrays containing x- and y-coordinates for immeditate |
| 98 |
plotting of the piece-wise function. |
| 99 |
|
| 100 |
:returns: (x_plot, y_plot) containing plottable data |
| 101 |
:rtype: pair of np.array |
| 102 |
|
| 103 |
Example:: |
| 104 |
|
| 105 |
x, y = f.get_plottable_data() |
| 106 |
plt.plot(x, y, '-o', label="Piece-wise const function") |
| 107 |
""" |
| 108 |
|
| 109 |
x_plot = np.empty(2*len(self.x)-2) |
| 110 |
x_plot[0] = self.x[0] |
| 111 |
x_plot[1::2] = self.x[1:] |
| 112 |
x_plot[2::2] = self.x[1:-1] |
| 113 |
y_plot = np.empty(2*len(self.y)) |
| 114 |
y_plot[::2] = self.y |
| 115 |
y_plot[1::2] = self.y |
| 116 |
|
| 117 |
return x_plot, y_plot |
| 118 |
|
| 119 |
def integral(self, interval=None): |
| 120 |
""" Returns the integral over the given interval. |
| 121 |
|
| 122 |
:param interval: integration interval given as a pair of floats, if |
| 123 |
None the integral over the whole function is computed. |
| 124 |
:type interval: Pair of floats or None. |
| 125 |
:returns: the integral |
| 126 |
:rtype: float |
| 127 |
""" |
| 128 |
if interval is None: |
| 129 |
|
| 130 |
a = np.sum((self.x[1:]-self.x[:-1]) * self.y) |
| 131 |
else: |
| 132 |
if interval[0]>interval[1]: |
| 133 |
raise ValueError("Invalid averaging interval: interval[0]>=interval[1]") |
| 134 |
if interval[0]<self.x[0]: |
| 135 |
raise ValueError("Invalid averaging interval: interval[0]<self.x[0]") |
| 136 |
if interval[1]>self.x[-1]: |
| 137 |
raise ValueError("Invalid averaging interval: interval[0]<self.x[-1]") |
| 138 |
|
| 139 |
start_ind = np.searchsorted(self.x, interval[0], side='right') |
| 140 |
end_ind = np.searchsorted(self.x, interval[1], side='left')-1 |
| 141 |
if start_ind > end_ind: |
| 142 |
|
| 143 |
a = (self.x[start_ind]-self.x[end_ind]) * self.y[end_ind] |
| 144 |
|
| 145 |
a -= ((interval[0]-self.x[end_ind])+(self.x[start_ind]-interval[1])) * self.y[end_ind] |
| 146 |
else: |
| 147 |
assert start_ind > 0 and end_ind < len(self.x), \ |
| 148 |
"Invalid averaging interval" |
| 149 |
|
| 150 |
a = np.sum((self.x[start_ind+1:end_ind+1] - |
| 151 |
self.x[start_ind:end_ind]) * |
| 152 |
self.y[start_ind:end_ind]) |
| 153 |
|
| 154 |
a += (self.x[start_ind]-interval[0]) * self.y[start_ind-1] |
| 155 |
|
| 156 |
a += (interval[1]-self.x[end_ind]) * self.y[end_ind] |
| 157 |
return a |
| 158 |
|
| 159 |
def avrg(self, interval=None): |
| •160 |
r""" Computes the average of the piece-wise const function: |
| 161 |
:math:`a = 1/T \int_0^T f(x) dx` where T is the length of the interval. |
| 162 |
|
| 163 |
:param interval: averaging interval given as a pair of floats, a |
| 164 |
sequence of pairs for averaging multiple intervals, or |
| 165 |
None, if None the average over the whole function is |
| 166 |
computed. |
| 167 |
:type interval: Pair, sequence of pairs, or None. |
| 168 |
:returns: the average a. |
| 169 |
:rtype: float |
| 170 |
""" |
| 171 |
if interval is None: |
| 172 |
|
| 173 |
return self.integral() / (self.x[-1]-self.x[0]) |
| 174 |
|
| 175 |
|
| 176 |
assert isinstance(interval, collections.abc.Sequence), \ |
| 177 |
"Invalid value for `interval`. None, Sequence or Tuple expected." |
| 178 |
|
| 179 |
if not isinstance(interval[0], collections.abc.Sequence): |
| 180 |
|
| 181 |
a = self.integral(interval) / (interval[1]-interval[0]) |
| 182 |
else: |
| 183 |
|
| 184 |
a = 0.0 |
| 185 |
int_length = 0.0 |
| 186 |
for ival in interval: |
| 187 |
a += self.integral(ival) |
| 188 |
int_length += ival[1] - ival[0] |
| 189 |
a /= int_length |
| 190 |
return a |
| 191 |
|
| 192 |
def add(self, f): |
| 193 |
""" Adds another PieceWiseConst function to this function. |
| 194 |
Note: only functions defined on the same interval can be summed. |
| 195 |
|
| 196 |
:param f: :class:`PieceWiseConstFunc` function to be added. |
| 197 |
:rtype: None |
| 198 |
""" |
| 199 |
assert self.x[0] == f.x[0], "The functions have different intervals" |
| 200 |
assert self.x[-1] == f.x[-1], "The functions have different intervals" |
| 201 |
|
| 202 |
|
| 203 |
try: |
| 204 |
from .cython.cython_add import add_piece_wise_const_cython as \ |
| 205 |
add_piece_wise_const_impl |
| 206 |
except ImportError: |
| 207 |
pyspike.NoCythonWarn() |
| 208 |
|
| 209 |
|
| 210 |
from .cython.python_backend import add_piece_wise_const_python as \ |
| 211 |
add_piece_wise_const_impl |
| 212 |
|
| 213 |
self.x, self.y = add_piece_wise_const_impl(self.x, self.y, f.x, f.y) |
| 214 |
|
| 215 |
def mul_scalar(self, fac): |
| 216 |
""" Multiplies the function with a scalar value |
| 217 |
|
| 218 |
:param fac: Value to multiply |
| 219 |
:type fac: double |
| 220 |
:rtype: None |
| 221 |
""" |
| 222 |
self.y *= fac |