1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
""" test_function.py
Tests the PieceWiseConst and PieceWiseLinear functions
Copyright 2014, Mario Mulansky <mario.mulansky@gmx.net>
Distributed under the BSD License
"""
from __future__ import print_function
import numpy as np
from copy import copy
import pytest
from numpy.testing import assert_allclose, assert_almost_equal, \
assert_array_equal, assert_array_almost_equal
import pyspike as spk
def test_pwc():
# some random data
x = [0.0, 1.0, 2.0, 2.5, 4.0]
y = [1.0, -0.5, 1.5, 0.75]
f = spk.PieceWiseConstFunc(x, y)
# function values
assert_allclose(f(0.0), 1.0)
assert_allclose(f(0.5), 1.0)
assert_allclose(f(1.0), 0.25)
assert_allclose(f(2.0), 0.5)
assert_allclose(f(2.25), 1.5)
assert_allclose(f(2.5), 2.25/2)
assert_allclose(f(3.5), 0.75)
assert_allclose(f(4.0), 0.75)
assert_array_equal(f([0.0, 0.5, 1.0, 2.0, 2.25, 2.5, 3.5, 4.0]),
[1.0, 1.0, 0.25, 0.5, 1.5, 2.25/2, 0.75, 0.75])
xp, yp = f.get_plottable_data()
xp_expected = [0.0, 1.0, 1.0, 2.0, 2.0, 2.5, 2.5, 4.0]
yp_expected = [1.0, 1.0, -0.5, -0.5, 1.5, 1.5, 0.75, 0.75]
assert_array_almost_equal(xp, xp_expected, decimal=16)
assert_array_almost_equal(yp, yp_expected, decimal=16)
assert_almost_equal(f.avrg(), (1.0-0.5+0.5*1.5+1.5*0.75)/4.0, decimal=16)
# interval averaging
a = f.avrg([0.5, 3.5])
assert_almost_equal(a, (0.5-0.5+0.5*1.5+1.0*0.75)/3.0, decimal=16)
a = f.avrg([1.5, 3.5])
assert_almost_equal(a, (-0.5*0.5+0.5*1.5+1.0*0.75)/2.0, decimal=16)
a = f.avrg([1.0, 2.0])
assert_almost_equal(a, (1.0*-0.5)/1.0, decimal=16)
a = f.avrg([1.0, 3.5])
assert_almost_equal(a, (-0.5*1.0+0.5*1.5+1.0*0.75)/2.5, decimal=16)
a = f.avrg([1.0, 4.0])
assert_almost_equal(a, (-0.5*1.0+0.5*1.5+1.5*0.75)/3.0, decimal=16)
a = f.avrg([0.0, 2.2])
assert_almost_equal(a, (1.0*1.0-0.5*1.0+0.2*1.5)/2.2, decimal=15)
# averaging over multiple intervals
a = f.avrg([(0.5, 1.5), (1.5, 3.5)])
assert_almost_equal(a, (0.5-0.5+0.5*1.5+1.0*0.75)/3.0, decimal=16)
# averaging over multiple intervals
a = f.avrg([(0.5, 1.5), (2.2, 3.5)])
assert_almost_equal(a, (0.5*1.0-0.5*0.5+0.3*1.5+1.0*0.75)/2.3, decimal=15)
def test_pwc_add():
# some random data
x = [0.0, 1.0, 2.0, 2.5, 4.0]
y = [1.0, -0.5, 1.5, 0.75]
f = spk.PieceWiseConstFunc(x, y)
f1 = copy(f)
x = [0.0, 0.75, 2.0, 2.5, 2.7, 4.0]
y = [0.5, 1.0, -0.25, 0.0, 1.5]
f2 = spk.PieceWiseConstFunc(x, y)
f1.add(f2)
x_expected = [0.0, 0.75, 1.0, 2.0, 2.5, 2.7, 4.0]
y_expected = [1.5, 2.0, 0.5, 1.25, 0.75, 2.25]
assert_array_almost_equal(f1.x, x_expected, decimal=16)
assert_array_almost_equal(f1.y, y_expected, decimal=16)
f2.add(f)
assert_array_almost_equal(f2.x, x_expected, decimal=16)
assert_array_almost_equal(f2.y, y_expected, decimal=16)
f1.add(f2)
# same x, but y doubled
assert_array_almost_equal(f1.x, f2.x, decimal=16)
assert_array_almost_equal(f1.y, 2*f2.y, decimal=16)
def test_pwc_mul():
x = [0.0, 1.0, 2.0, 2.5, 4.0]
y = [1.0, -0.5, 1.5, 0.75]
f = spk.PieceWiseConstFunc(x, y)
f.mul_scalar(1.5)
assert_array_almost_equal(f.x, x, decimal=16)
assert_array_almost_equal(f.y, 1.5*np.array(y), decimal=16)
f.mul_scalar(1.0/5.0)
assert_array_almost_equal(f.y, 1.5/5.0*np.array(y), decimal=16)
def test_pwc_avrg():
# some random data
x = [0.0, 1.0, 2.0, 2.5, 4.0]
y = [1.0, -0.5, 1.5, 0.75]
f1 = spk.PieceWiseConstFunc(x, y)
x = [0.0, 0.75, 2.0, 2.5, 2.7, 4.0]
y = [0.5, 1.0, -0.25, 0.0, 1.5]
f2 = spk.PieceWiseConstFunc(x, y)
f1.add(f2)
f1.mul_scalar(0.5)
x_expected = [0.0, 0.75, 1.0, 2.0, 2.5, 2.7, 4.0]
y_expected = [0.75, 1.0, 0.25, 0.625, 0.375, 1.125]
assert_array_almost_equal(f1.x, x_expected, decimal=16)
assert_array_almost_equal(f1.y, y_expected, decimal=16)
def test_pwc_integral():
# some random data
x = [0.0, 1.0, 2.0, 2.5, 4.0]
y = [1.0, -0.5, 1.5, 0.75]
f1 = spk.PieceWiseConstFunc(x, y)
# test full interval
full = 1.0*1.0 + 1.0*-0.5 + 0.5*1.5 + 1.5*0.75;
assert_allclose(f1.integral(), full)
assert_allclose(f1.integral((np.min(x),np.max(x))), full)
# test part interval, spanning an edge
assert_allclose(f1.integral((0.5,1.5)), 0.5*1.0 + 0.5*-0.5)
# test part interval, just over two edges
assert_almost_equal(f1.integral((1.0-1e-16,2+1e-16)), 1.0*-0.5, decimal=14)
# test part interval, between two edges
assert_allclose(f1.integral((1.0,2.0)), 1.0*-0.5)
assert_allclose(f1.integral((1.2,1.7)), (1.7-1.2)*-0.5)
# test part interval, start to before and after edge
assert_allclose(f1.integral((0.0,0.7)), 0.7*1.0)
assert_allclose(f1.integral((0.0,1.1)), 1.0*1.0+0.1*-0.5)
# test part interval, before and after edge till end
assert_allclose(f1.integral((2.6,4.0)), (4.0-2.6)*0.75)
assert_allclose(f1.integral((2.4,4.0)), (2.5-2.4)*1.5+(4-2.5)*0.75)
def test_pwc_integral_bad_bounds_inv():
with pytest.raises(ValueError):
# some random data
x = [0.0, 1.0, 2.0, 2.5, 4.0]
y = [1.0, -0.5, 1.5, 0.75]
f1 = spk.PieceWiseConstFunc(x, y)
f1.integral((3,2))
def test_pwc_integral_bad_bounds_oob_1():
with pytest.raises(ValueError):
# some random data
x = [0.0, 1.0, 2.0, 2.5, 4.0]
y = [1.0, -0.5, 1.5, 0.75]
f1 = spk.PieceWiseConstFunc(x, y)
f1.integral((1,6))
def test_pwc_integral_bad_bounds_oob_2():
with pytest.raises(ValueError):
# some random data
x = [0.0, 1.0, 2.0, 2.5, 4.0]
y = [1.0, -0.5, 1.5, 0.75]
f1 = spk.PieceWiseConstFunc(x, y)
f1.integral((-1,3))
def test_pwl():
x = [0.0, 1.0, 2.0, 2.5, 4.0]
y1 = [1.0, -0.5, 1.5, 0.75]
y2 = [1.5, -0.4, 1.5, 0.25]
f = spk.PieceWiseLinFunc(x, y1, y2)
# function values
assert_allclose(f(0.0), 1.0)
assert_allclose(f(0.5), 1.25)
assert_allclose(f(1.0), 0.5)
assert_allclose(f(2.0), 1.1/2)
assert_allclose(f(2.25), 1.5)
assert_allclose(f(2.5), 2.25/2)
assert_allclose(f(3.5), 0.75-0.5*1.0/1.5)
assert_allclose(f(4.0), 0.25)
assert_array_equal(f([0.0, 0.5, 1.0, 2.0, 2.25, 2.5, 3.5, 4.0]),
[1.0, 1.25, 0.5, 0.55, 1.5, 2.25/2, 0.75-0.5/1.5, 0.25])
xp, yp = f.get_plottable_data()
xp_expected = [0.0, 1.0, 1.0, 2.0, 2.0, 2.5, 2.5, 4.0]
yp_expected = [1.0, 1.5, -0.5, -0.4, 1.5, 1.5, 0.75, 0.25]
assert_array_almost_equal(xp, xp_expected, decimal=16)
assert_array_almost_equal(yp, yp_expected, decimal=16)
avrg_expected = (1.25 - 0.45 + 0.75 + 1.5*0.5) / 4.0
assert_almost_equal(f.avrg(), avrg_expected, decimal=16)
# interval averaging
a = f.avrg([0.5, 2.5])
assert_almost_equal(a, (1.375*0.5 - 0.45 + 0.75)/2.0, decimal=16)
a = f.avrg([1.5, 3.5])
assert_almost_equal(a, (-0.425*0.5 + 0.75 + (0.75+0.75-0.5/1.5)/2) / 2.0,
decimal=16)
a = f.avrg((1.0, 3.5))
assert_almost_equal(a, (-0.45 + 0.75 + (0.75+0.75-0.5/1.5)/2) / 2.5,
decimal=16)
a = f.avrg([1.0, 4.0])
assert_almost_equal(a, (-0.45 + 0.75 + 1.5*0.5) / 3.0, decimal=16)
# interval between support points
a = f.avrg([1.1, 1.5])
assert_almost_equal(a, (-0.5+0.1*0.1 - 0.45) * 0.5, decimal=14)
# starting at a support point
a = f.avrg([1.0, 1.5])
assert_almost_equal(a, (-0.5 - 0.45) * 0.5, decimal=14)
# start and end at support point
a = f.avrg([1.0, 2.0])
assert_almost_equal(a, (-0.5 - 0.4) * 0.5, decimal=14)
# averaging over multiple intervals
a = f.avrg([(0.5, 1.5), (1.5, 2.5)])
assert_almost_equal(a, (1.375*0.5 - 0.45 + 0.75)/2.0, decimal=16)
def test_pwl_add():
x = [0.0, 1.0, 2.0, 2.5, 4.0]
y1 = [1.0, -0.5, 1.5, 0.75]
y2 = [1.5, -0.4, 1.5, 0.25]
f = spk.PieceWiseLinFunc(x, y1, y2)
f1 = copy(f)
x = [0.0, 0.75, 2.0, 2.5, 2.7, 4.0]
y1 = [0.5, 1.0, -0.25, 0.0, 1.5]
y2 = [0.8, 0.2, -1.0, 0.0, 2.0]
f2 = spk.PieceWiseLinFunc(x, y1, y2)
f1.add(f2)
x_expected = [0.0, 0.75, 1.0, 2.0, 2.5, 2.7, 4.0]
y1_expected = [1.5, 1.0+1.0+0.5*0.75, -0.5+1.0-0.8*0.25/1.25, 1.5-0.25,
0.75, 1.5+0.75-0.5*0.2/1.5]
y2_expected = [0.8+1.0+0.5*0.75, 1.5+1.0-0.8*0.25/1.25, -0.4+0.2, 1.5-1.0,
0.75-0.5*0.2/1.5, 2.25]
assert_array_almost_equal(f1.x, x_expected, decimal=16)
assert_array_almost_equal(f1.y1, y1_expected, decimal=16)
assert_array_almost_equal(f1.y2, y2_expected, decimal=16)
f2.add(f)
assert_array_almost_equal(f2.x, x_expected, decimal=16)
assert_array_almost_equal(f2.y1, y1_expected, decimal=16)
assert_array_almost_equal(f2.y2, y2_expected, decimal=16)
f1.add(f2)
# same x, but y doubled
assert_array_almost_equal(f1.x, f2.x, decimal=16)
assert_array_almost_equal(f1.y1, 2*f2.y1, decimal=16)
assert_array_almost_equal(f1.y2, 2*f2.y2, decimal=16)
def test_pwl_mul():
x = [0.0, 1.0, 2.0, 2.5, 4.0]
y1 = [1.0, -0.5, 1.5, 0.75]
y2 = [1.5, -0.4, 1.5, 0.25]
f = spk.PieceWiseLinFunc(x, y1, y2)
f.mul_scalar(1.5)
assert_array_almost_equal(f.x, x, decimal=16)
assert_array_almost_equal(f.y1, 1.5*np.array(y1), decimal=16)
assert_array_almost_equal(f.y2, 1.5*np.array(y2), decimal=16)
f.mul_scalar(1.0/5.0)
assert_array_almost_equal(f.y1, 1.5/5.0*np.array(y1), decimal=16)
assert_array_almost_equal(f.y2, 1.5/5.0*np.array(y2), decimal=16)
def test_pwl_avrg():
x = [0.0, 1.0, 2.0, 2.5, 4.0]
y1 = [1.0, -0.5, 1.5, 0.75]
y2 = [1.5, -0.4, 1.5, 0.25]
f1 = spk.PieceWiseLinFunc(x, y1, y2)
x = [0.0, 0.75, 2.0, 2.5, 2.7, 4.0]
y1 = [0.5, 1.0, -0.25, 0.0, 1.5]
y2 = [0.8, 0.2, -1.0, 0.0, 2.0]
f2 = spk.PieceWiseLinFunc(x, y1, y2)
x_expected = [0.0, 0.75, 1.0, 2.0, 2.5, 2.7, 4.0]
y1_expected = np.array([1.5, 1.0+1.0+0.5*0.75, -0.5+1.0-0.8*0.25/1.25,
1.5-0.25, 0.75, 1.5+0.75-0.5*0.2/1.5]) / 2
y2_expected = np.array([0.8+1.0+0.5*0.75, 1.5+1.0-0.8*0.25/1.25, -0.4+0.2,
1.5-1.0, 0.75-0.5*0.2/1.5, 2.25]) / 2
f1.add(f2)
f1.mul_scalar(0.5)
assert_array_almost_equal(f1.x, x_expected, decimal=16)
assert_array_almost_equal(f1.y1, y1_expected, decimal=16)
assert_array_almost_equal(f1.y2, y2_expected, decimal=16)
def test_df():
# testing discrete function
x = [0.0, 1.0, 2.0, 2.5, 4.0]
y = [0.0, 1.0, 1.0, 0.0, 1.0]
mp = [1.0, 2.0, 1.0, 2.0, 1.0]
f = spk.DiscreteFunc(x, y, mp)
xp, yp = f.get_plottable_data()
xp_expected = [0.0, 1.0, 2.0, 2.5, 4.0]
yp_expected = [0.0, 0.5, 1.0, 0.0, 1.0]
assert_array_almost_equal(xp, xp_expected, decimal=16)
assert_array_almost_equal(yp, yp_expected, decimal=16)
assert_almost_equal(f.avrg(), 2.0/5.0, decimal=16)
# interval averaging
a = f.avrg([0.5, 2.4])
assert_almost_equal(a, 2.0/3.0, decimal=16)
a = f.avrg([1.5, 3.5])
assert_almost_equal(a, 1.0/3.0, decimal=16)
a = f.avrg((0.9, 3.5))
assert_almost_equal(a, 2.0/5.0, decimal=16)
a = f.avrg([1.1, 4.0])
assert_almost_equal(a, 1.0/3.0, decimal=16)
# averaging over multiple intervals
a = f.avrg([(0.5, 1.5), (1.5, 2.6)])
assert_almost_equal(a, 2.0/5.0, decimal=16)
if __name__ == "__main__":
test_pwc()
test_pwc_add()
test_pwc_mul()
test_pwc_avrg()
test_pwl()
test_pwl_add()
test_pwl_mul()
test_pwl_avrg()
test_df()
|