# Path: msibi/utils/error_calculation.py
# def calc_similarity(arr1, arr2):
#     f_fit = np.sum(np.absolute(arr1 - arr2))
#     f_fit /= np.sum((np.absolute(arr1) + np.absolute(arr2)))
#     return 1.0 - f_fit
# 
# Path: msibi/utils/general.py
# def find_nearest(array, target):
#     """Find array component whose numeric value is closest to 'target'. """
#     idx = np.abs(array - target).argmin()
#     return idx, array[idx]
# 
# Path: msibi/utils/smoothing.py
# def savitzky_golay(y, window_size, order, deriv=0, rate=1):
#     """
# 
#     Parameters
#     ----------
#     y:
#     window_size:
#     order:
#     deriv:
#     rate:
# 
#     Returns
#     -------
# 
#     """
#     if not (isinstance(window_size, int) and isinstance(order, int)):
#         raise ValueError("window_size and order must be of type int")
#     if window_size % 2 != 1 or window_size < 1:
#         raise TypeError('window_size must be a positive odd number')
#     if window_size < order + 2:
#         raise TypeError('window_size is too small for the polynomials order')
# 
#     order_range = range(order+1)
#     half_window = (window_size - 1) // 2
#     b = np.mat([[k**i for i in order_range] for k in range(-half_window,
#                                                            half_window +1)])
#     m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
#     firstvals = y[0] - np.abs(y[1:half_window+1][::-1] - y[0])
#     lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
#     y = np.concatenate((firstvals, y, lastvals))
#     return np.convolve(m[::-1], y, mode='valid')


import pytest
import numpy as np
from msibi.utils.error_calculation import calc_similarity
from msibi.utils.general import find_nearest
from msibi.utils.smoothing import savitzky_golay
    assert calc_similarity(a, b) == 0.0


def test_find_nearest():
    a = np.arange(10)
    idx, nearest = find_nearest(a, 2.1)
    assert idx == 2
    assert nearest == 2


def test_savitzky_golay():
    x = np.arange(0, 1, 0.01)
    y = 2 * x + 1
    y2 = savitzky_golay(y, 3, 1)
    assert y.shape == y2.shape
    assert np.allclose(y, y2)

    y = x**3.0
    y2 = savitzky_golay(y, 3, 1)
    assert calc_similarity(y, y2) > 0.99

    with pytest.raises(ValueError):
        y2 = savitzky_golay(y, 3.1, 1)
    with pytest.raises(TypeError):
        y2 = savitzky_golay(y, 2, 1)
    with pytest.raises(TypeError):
        y2 = savitzky_golay(y, 4, 1)
    with pytest.raises(TypeError):
        y2 = savitzky_golay(y, 3, 3)
    with pytest.raises(TypeError):

# Path: msibi/utils/error_calculation.py
# def calc_similarity(arr1, arr2):
#     f_fit = np.sum(np.absolute(arr1 - arr2))
#     f_fit /= np.sum((np.absolute(arr1) + np.absolute(arr2)))
#     return 1.0 - f_fit
# 
# Path: msibi/utils/general.py
# def find_nearest(array, target):
#     """Find array component whose numeric value is closest to 'target'. """
#     idx = np.abs(array - target).argmin()
#     return idx, array[idx]
# 
# Path: msibi/utils/smoothing.py
# def savitzky_golay(y, window_size, order, deriv=0, rate=1):
#     """
# 
#     Parameters
#     ----------
#     y:
#     window_size:
#     order:
#     deriv:
#     rate:
# 
#     Returns
#     -------
# 
#     """
#     if not (isinstance(window_size, int) and isinstance(order, int)):
#         raise ValueError("window_size and order must be of type int")
#     if window_size % 2 != 1 or window_size < 1:
#         raise TypeError('window_size must be a positive odd number')
#     if window_size < order + 2:
#         raise TypeError('window_size is too small for the polynomials order')
# 
#     order_range = range(order+1)
#     half_window = (window_size - 1) // 2
#     b = np.mat([[k**i for i in order_range] for k in range(-half_window,
#                                                            half_window +1)])
#     m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
#     firstvals = y[0] - np.abs(y[1:half_window+1][::-1] - y[0])
#     lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
#     y = np.concatenate((firstvals, y, lastvals))
#     return np.convolve(m[::-1], y, mode='valid')


# Path: msibi/tests/test_utils.py
import pytest
import numpy as np
from msibi.utils.error_calculation import calc_similarity
from msibi.utils.general import find_nearest
from msibi.utils.smoothing import savitzky_golay

    assert calc_similarity(a, b) == 0.0


def test_find_nearest():
    a = np.arange(10)
    idx, nearest = find_nearest(a, 2.1)
    assert idx == 2
    assert nearest == 2


def test_savitzky_golay():
    x = np.arange(0, 1, 0.01)
    y = 2 * x + 1
    y2 = savitzky_golay(y, 3, 1)
    assert y.shape == y2.shape
    assert np.allclose(y, y2)

    y = x**3.0
    y2 = savitzky_golay(y, 3, 1)
    assert calc_similarity(y, y2) > 0.99

    with pytest.raises(ValueError):
        y2 = savitzky_golay(y, 3.1, 1)
    with pytest.raises(TypeError):
        y2 = savitzky_golay(y, 2, 1)
    with pytest.raises(TypeError):
        y2 = savitzky_golay(y, 4, 1)
    with pytest.raises(TypeError):
        y2 = savitzky_golay(y, 3, 3)
    with pytest.raises(TypeError):

        y2 = savitzky_golay(y, 3, 2)