<filename>tests/test_sgolay2.py
# -*- coding: utf-8 -*-

import pytest
import sgolay2

from numpy.testing import assert_almost_equal


@pytest.mark.parametrize('window_size', [(5, 1), (2, 3), (3, -3)])
def test_window_odd(window_size):
    with pytest.raises(ValueError):
        _ = sgolay2.SGolayFilter2(window_size, 2)


@pytest.mark.parametrize('poly_order', [0, (2, 0)])
def test_poly_order_great_zero(poly_order):
    with pytest.raises(ValueError):
        _ = sgolay2.SGolayFilter2((3, 3), poly_order)


def test_kernel_w33_p22():
    k = sgolay2.SGolayKernel2((3, 3), (2, 2))

    assert_almost_equal(k.kernel, [
        (-0.111111111111111, 0.222222222222222, -0.111111111111111),
        (0.222222222222222, 0.555555555555555, 0.222222222222222),
        (-0.111111111111111, 0.222222222222222, -0.111111111111111),
    ])


def test_kernel_w55_p33():
    k = sgolay2.SGolayKernel2((5, 5), (3, 3))

    assert_almost_equal(k.kernel, [
        (-0.0742857142857143, 0.0114285714285714, 0.04, 0.0114285714285714, -0.0742857142857144),
        (0.0114285714285714, 0.0971428571428572, 0.125714285714286, 0.0971428571428571, 0.0114285714285715),
        (0.04, 0.125714285714286, 0.154285714285714, 0.125714285714286, 0.04),
        (0.0114285714285714, 0.0971428571428572, 0.125714285714286, 0.0971428571428571, 0.0114285714285714),
        (-0.0742857142857143, 0.0114285714285714, 0.04, 0.0114285714285714, -0.0742857142857143),
    ])


def test_kernel_w77_p44():
    k = sgolay2.SGolayKernel2((7, 7), (2, 2))

    assert_almost_equal(k.kernel, [
        (0.0424654710368997, -0.0358688930117502, -0.00494743351886203, 0.0183467326324469, -0.00494743351886203, -0.0358688930117501, 0.0424654710368996),
        (-0.0358688930117501, -0.0575139146567719, 0.00742115027829309, 0.0420531849103278, 0.00742115027829313, -0.0575139146567719, -0.0358688930117503),
        (-0.00494743351886212, 0.00742115027829312, 0.0927643784786642, 0.134199134199134, 0.0927643784786642, 0.00742115027829292, -0.00494743351886182),
        (0.0183467326324469, 0.0420531849103278, 0.134199134199134, 0.177901463615749, 0.134199134199134, 0.0420531849103277, 0.0183467326324468),
        (-0.00494743351886206, 0.00742115027829312, 0.0927643784786642, 0.134199134199134, 0.0927643784786642, 0.0074211502782931, -0.00494743351886207),
        (-0.0358688930117503, -0.0575139146567718, 0.00742115027829308, 0.0420531849103277, 0.00742115027829304, -0.0575139146567718, -0.0358688930117501),
        (0.0424654710368996, -0.0358688930117501, -0.00494743351886201, 0.0183467326324469, -0.00494743351886209, -0.0358688930117501, 0.0424654710368996),
    ])


def test_kernel_w77_p24():
    k = sgolay2.SGolayKernel2((7, 7), (2, 4))

    assert_almost_equal(k.kernel, [
        (0.0257678829107401, 0.00309214594928882, -0.0105132962275819, -0.0150484436198722, -0.0105132962275819, 0.00309214594928886, 0.0257678829107401),
        (-0.0525664811379097, -0.0185528756957329, 0.00185528756957326, 0.00865800865800862, 0.00185528756957323, -0.018552875695733, -0.05256648113791),
        (-0.0216450216450216, 0.0463821892393321, 0.0871985157699444, 0.100803957946815, 0.0871985157699443, 0.046382189239332, -0.0216450216450213),
        (0.0016491445062874, 0.0810142238713668, 0.128633271490414, 0.14450628736343, 0.128633271490414, 0.0810142238713667, 0.00164914450628729),
        (-0.0216450216450217, 0.0463821892393321, 0.0871985157699443, 0.100803957946815, 0.0871985157699443, 0.0463821892393321, -0.0216450216450217),
        (-0.0525664811379098, -0.0185528756957329, 0.00185528756957326, 0.00865800865800864, 0.00185528756957327, -0.0185528756957328, -0.0525664811379097),
        (0.0257678829107401, 0.00309214594928884, -0.0105132962275819, -0.0150484436198722, -0.0105132962275819, 0.00309214594928882, 0.02576788291074),
    ])


def test_kernel_w57_p42():
    k = sgolay2.SGolayKernel2((5, 7), (4, 2))

    assert_almost_equal(k.kernel, [
        (-0.0122448979591836, 0.0489795918367346, 0.0272108843537415, 0.142857142857143, 0.0653061224489796, 9.8879238130678e-17, 0.0204081632653063),
        (-0.016326530612245, -6.93889390390723e-18, -0.0816326530612245, 0.212244897959184, 0.0489795918367346, 0.0204081632653061, -2.8796409701215e-16),
        (-0.0122448979591835, -0.0816326530612245, -9.71445146547012e-17, 0.235374149659864, -2.77555756156289e-17, -0.0816326530612245, -0.0122448979591838),
        (-4.85722573273506e-17, 0.0204081632653061, 0.0489795918367347, 0.212244897959184, -0.0816326530612246, -2.08166817117217e-17, -0.0163265306122449),
        (0.0204081632653061, 2.08166817117217e-17, 0.0653061224489796, 0.142857142857143, 0.0272108843537415, 0.0489795918367347, -0.0122448979591835),
    ])


def test_kernel_w75_p24():
    k = sgolay2.SGolayKernel2((7, 5), (2, 4))

    assert_almost_equal(k.kernel, [
        (2.52624557451942e-17, 0.0489795918367346, -0.0122448979591836, 0.142857142857143, 0.0204081632653063),
        (1.54497931277737e-18, 0.212244897959184, -0.0163265306122449, -2.40606043346556e-18, -0.0816326530612245),
        (0.0204081632653061, 0.0489795918367346, 0.0653061224489796, -6.12020448137612e-17, 0.0272108843537414),
        (-0.0816326530612245, -0.0122448979591836, 0.235374149659864, -0.0122448979591838, -0.0816326530612244),
        (0.0272108843537416, 2.57778825444279e-17, 0.0653061224489795, 0.0489795918367347, 0.0204081632653061),
        (-0.0816326530612244, -6.15921080004729e-17, -0.0163265306122449, 0.212244897959184, -2.27438224057307e-16),
        (0.0204081632653061, 0.142857142857143, -0.0122448979591836, 0.0489795918367347, -6.52442371524539e-18),
    ])


def test_kernel_w75_p42():
    k = sgolay2.SGolayKernel2((7, 5), (4, 2))

    assert_almost_equal(k.kernel, [
        (-0.00556586270871992, 0.105751391465677, -0.0166975881261595, 0.0148423005565863, 0.0315398886827458),
        (-0.0667903525046383, 0.146567717996289, 0.0181818181818183, -0.00556586270871982, -0.00927643784786633),
        (0.0315398886827458, 0.105751391465677, 0.161038961038961, -0.0667903525046383, -0.0228818800247371),
        (-0.0092764378478664, -0.0166975881261595, 0.208658008658009, -0.0166975881261595, -0.00927643784786644),
        (-0.0228818800247371, -0.0667903525046384, 0.161038961038961, 0.105751391465677, 0.0315398886827458),
        (-0.0092764378478663, -0.00556586270871997, 0.0181818181818183, 0.146567717996289, -0.0667903525046383),
        (0.0315398886827458, 0.0148423005565862, -0.0166975881261596, 0.105751391465677, -0.00556586270872),
    ])
