Skip to content
Snippets Groups Projects
Commit 3c9a4fc8 authored by Wolfgang Kastaun's avatar Wolfgang Kastaun
Browse files

added some unit tests for fixed shift with numpy or dask

parent 8348c5ab
No related branches found
No related tags found
No related merge requests found
import numpy as np
import pytest
from lisainstrument.dynamic_delay_numpy import DynShiftBC
from lisainstrument.dynamic_delay_dask import numpyfy_dask_multi
from lisainstrument.fixed_shift_numpy import make_fixed_shift_lagrange_numpy
from lisainstrument.fixed_shift_dask import make_fixed_shift_lagrange_dask
def test_fixed_shift_lagrange_dask() -> None:
order = 5
length = order + 1
t, dt = np.linspace(-5.345, 10.345, 1003, retstep=True)
def g(x):
return (
4.32546
+ 3.34324 * x
- 4.342 * x**2
- 0.46 * x**3
+ 1.43598 * x**4
- np.pi * x**5
)
y = g(t)
for d in (0.93456456 / dt, 3.1, 3.5, 3.9, 4.0, 4.1, 4.5, 4.9, 116.0):
op_np = make_fixed_shift_lagrange_numpy(
DynShiftBC.FLAT, DynShiftBC.EXCEPTION, length
)
op_da = make_fixed_shift_lagrange_dask(
DynShiftBC.FLAT, DynShiftBC.EXCEPTION, length
)
op_na = numpyfy_dask_multi(op_da, chunks=113)
s_np = op_np(y, d)
s_na = op_na(y, d)
s_ex = g(t - d * dt)
margin_ex = -int(np.floor(-d)) + order // 2
assert s_na == pytest.approx(s_np, rel=1e-14, abs=0)
assert s_ex[margin_ex:] == pytest.approx(s_na[margin_ex:], abs=1e-15, rel=5e-13)
with pytest.raises(RuntimeError):
op_na(y, -d)
op2_np = make_fixed_shift_lagrange_numpy(
DynShiftBC.EXCEPTION, DynShiftBC.FLAT, length
)
op2_da = make_fixed_shift_lagrange_dask(
DynShiftBC.EXCEPTION, DynShiftBC.FLAT, length
)
op2_na = numpyfy_dask_multi(op2_da, chunks=113)
s2_np = op2_np(y, -d)
s2_na = op2_na(y, -d)
s2_ex = g(t + d * dt)
margin2_ex = int(np.floor(d)) + (length - 1 - order // 2)
assert s2_na == pytest.approx(s2_np, rel=1e-14, abs=0)
assert s2_ex[:-margin2_ex] == pytest.approx(
s2_na[:-margin2_ex], abs=1e-15, rel=5e-13
)
with pytest.raises(RuntimeError):
op2_na(y, d)
op3_da = make_fixed_shift_lagrange_dask(
DynShiftBC.FLAT, DynShiftBC.FLAT, length
)
op3_na = numpyfy_dask_multi(op3_da, chunks=113)
s3_na = op3_na(y, d)
assert s3_na == pytest.approx(s_np, rel=1e-14, abs=0)
s4_na = op3_na(y, -d)
assert s4_na == pytest.approx(s2_np, rel=1e-14, abs=0)
import numpy as np
import pytest
from lisainstrument.dynamic_delay_numpy import DynShiftBC
from lisainstrument.fixed_shift_numpy import make_fixed_shift_lagrange_numpy
def test_fixed_shift_lagrange_dask() -> None:
order = 5
length = order + 1
t, dt = np.linspace(-5.345, 10.345, 1003, retstep=True)
def g(x):
return (
4.32546
+ 3.34324 * x
- 4.342 * x**2
- 0.46 * x**3
+ 1.43598 * x**4
- 3.3456 * x**5
)
y = g(t)
for d in (0.93456456/dt, 3.1, 3.5, 3.9, 4.0, 4.1, 4.5, 4.9):
# ~ d = 0.93456456 / dt
op_np = make_fixed_shift_lagrange_numpy(DynShiftBC.FLAT, DynShiftBC.EXCEPTION, length)
s_np = op_np(y, d)
s_ex = g(t - d * dt)
margin_ex = -int(np.floor(-d)) + order // 2
print(d, d*dt, margin_ex)
assert s_ex[margin_ex:] == pytest.approx(
s_np[margin_ex:], abs=1e-15, rel=5e-13
)
# ~ assert np.min(np.abs(s_ex[:margin_ex] - s_np[:margin_ex])) > np.max(np.abs(y[:margin_ex]))*1e-10
with pytest.raises(RuntimeError):
op_np(y, -d)
op2_np = make_fixed_shift_lagrange_numpy(DynShiftBC.EXCEPTION, DynShiftBC.FLAT, length)
s2_np = op2_np(y, -d)
s2_ex = g(t + d * dt)
margin2_ex = int(np.floor(d)) + (length - 1 - order // 2)
assert s2_ex[:-margin2_ex] == pytest.approx(
s2_np[:-margin2_ex], abs=1e-15, rel=5e-13
)
# ~ assert np.min(np.abs(s2_ex[-margin2_ex:] - s2_np[-margin2_ex:])) > np.max(np.abs(y[-margin2_ex:]))*1e-10
with pytest.raises(RuntimeError):
op2_np(y, d)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment