# Path: glimix_core/_util/check.py
# def check_covariates(X):
#     if not X.ndim == 2:
#         raise ValueError("Covariates must be a bidimensional array.")
# 
#     if not npall(isfinite(X)):
#         raise ValueError("Covariates must have finite values only.")
# 
#     return X
# 
# def check_economic_qs(QS):
#     if not isinstance(QS, tuple):
#         raise ValueError("QS must be a tuple.")
# 
#     if not isinstance(QS[0], tuple):
#         raise ValueError("QS[0] must be a tuple.")
# 
#     fmsg = "QS has non-finite values."
# 
#     if not all(npall(isfinite(Q)) for Q in QS[0]):
#         raise ValueError(fmsg)
# 
#     if not npall(isfinite(QS[1])):
#         raise ValueError(fmsg)
# 
#     return QS
# 
# def check_outcome(y, lik):
#     if not isinstance(lik, (list, tuple)):
#         lik = (lik,)
# 
#     str_err = "The first item of ``lik`` has to be a string."
#     if not isinstance(lik[0], str):
#         raise ValueError(str_err)
# 
#     lik_name = lik[0].lower()
# 
#     y = ascontiguousarray(y, float)
#     lik = lik[:1] + tuple(ascontiguousarray(i, float) for i in lik[1:])
# 
#     if not npall(isfinite(y)):
#         raise ValueError("Outcome must be finite.")
# 
#     if lik_name == "poisson":
#         return _check_poisson_outcome(y)
# 
#     if lik_name in ("binomial", "normal"):
#         if len(lik) != 2:
#             msg = "``lik`` must be a tuple of two elements for"
#             msg += " {} likelihood.".format(lik_name[0].upper() + lik_name[1:])
#             raise ValueError(msg)
# 
#     return y
# 
# Path: glimix_core/_util/solve.py
# def hinv(a, b, d):
#     rcond = 1e-7
# 
#     b = atleast_1d(b)
#     d = atleast_1d(d)
#     a = float(a)
#     cond = heigvals(a, b, d)
#     norm = a * d - b * b
#     with errstate(invalid="ignore", divide="ignore"):
#         ai = d / norm
#         bi = -b / norm
#         di = a / norm
# 
#     nok = cond > 1 / rcond
#     ai[nok], bi[nok], di[nok] = _hinv_svd(a, b[nok], d[nok])
#     return ai, bi, di
# 
# Path: glimix_core/_util/solve.py
# def heigvals(a, b, d):
#     a = float(a)
#     b = atleast_1d(b)
#     d = atleast_1d(d)
#     T = a + d
#     D = a * d - b * b
# 
#     t0 = T / 2
#     t1 = sqrt(maximum(T * T / 4 - D, 0))
#     eig0 = t0 + t1
#     eig1 = t0 - t1
# 
#     eig0 = absolute(eig0)
#     eig1 = absolute(eig1)
# 
#     with errstate(invalid="ignore", divide="ignore"):
#         return nan_to_num(maximum(eig0, eig1) / minimum(eig0, eig1))
# 
# def hsvd(a, b, d):
#     a = atleast_1d(a)
#     b = atleast_1d(b)
#     d = atleast_1d(d)
# 
#     aa = a * a
#     bb = b * b
#     dd = d * d
#     ab = a * b
#     bd = b * d
# 
#     e = aa - dd
#     s1 = aa + 2 * bb + dd
#     s2 = sqrt(e ** 2 + 4 * (ab + bd) ** 2)
# 
#     t = 2 * ab + 2 * bd
#     theta = arctan2(t, e) / 2
#     psi = arctan2(t, aa - dd) / 2
# 
#     Ct = cos(theta)
#     St = sin(theta)
#     Cp = cos(psi)
#     Sp = sin(psi)
# 
#     s11 = (a * Ct + b * St) * Cp + (b * Ct + d * St) * Sp
#     s22 = (a * St - b * Ct) * Sp + (-b * St + d * Ct) * Cp
# 
#     U = [[Ct, -St], [St, Ct]]
#     S = [sqrt((s1 + s2) / 2), sqrt(maximum((s1 - s2) / 2, 0.0))]
# 
#     VT = [[sign(s11) * Cp, sign(s11) * Sp], [-sign(s22) * Sp, sign(s22) * Cp]]
# 
#     # U S V.T
#     return U, S, VT


import pytest
from numpy import array, asarray, block, inf, nan, ones
from numpy.linalg import inv, pinv, svd
from numpy.testing import assert_allclose
from numpy_sugar.linalg import ddot
from glimix_core._util import check_covariates, check_economic_qs, check_outcome, hinv
from glimix_core._util.solve import heigvals, hsvd



def test_util_check_economic_qs():

    A = ones((3, 2))
    B = ones((3, 1))
    C = ones(2)

    with pytest.raises(ValueError):
        check_economic_qs(A)

    with pytest.raises(ValueError):
        check_economic_qs((A, C))

    A[0, 0] = inf
    QS = ((A, B), C)

    with pytest.raises(ValueError):

# Path: glimix_core/_util/check.py
# def check_covariates(X):
#     if not X.ndim == 2:
#         raise ValueError("Covariates must be a bidimensional array.")
# 
#     if not npall(isfinite(X)):
#         raise ValueError("Covariates must have finite values only.")
# 
#     return X
# 
# def check_economic_qs(QS):
#     if not isinstance(QS, tuple):
#         raise ValueError("QS must be a tuple.")
# 
#     if not isinstance(QS[0], tuple):
#         raise ValueError("QS[0] must be a tuple.")
# 
#     fmsg = "QS has non-finite values."
# 
#     if not all(npall(isfinite(Q)) for Q in QS[0]):
#         raise ValueError(fmsg)
# 
#     if not npall(isfinite(QS[1])):
#         raise ValueError(fmsg)
# 
#     return QS
# 
# def check_outcome(y, lik):
#     if not isinstance(lik, (list, tuple)):
#         lik = (lik,)
# 
#     str_err = "The first item of ``lik`` has to be a string."
#     if not isinstance(lik[0], str):
#         raise ValueError(str_err)
# 
#     lik_name = lik[0].lower()
# 
#     y = ascontiguousarray(y, float)
#     lik = lik[:1] + tuple(ascontiguousarray(i, float) for i in lik[1:])
# 
#     if not npall(isfinite(y)):
#         raise ValueError("Outcome must be finite.")
# 
#     if lik_name == "poisson":
#         return _check_poisson_outcome(y)
# 
#     if lik_name in ("binomial", "normal"):
#         if len(lik) != 2:
#             msg = "``lik`` must be a tuple of two elements for"
#             msg += " {} likelihood.".format(lik_name[0].upper() + lik_name[1:])
#             raise ValueError(msg)
# 
#     return y
# 
# Path: glimix_core/_util/solve.py
# def hinv(a, b, d):
#     rcond = 1e-7
# 
#     b = atleast_1d(b)
#     d = atleast_1d(d)
#     a = float(a)
#     cond = heigvals(a, b, d)
#     norm = a * d - b * b
#     with errstate(invalid="ignore", divide="ignore"):
#         ai = d / norm
#         bi = -b / norm
#         di = a / norm
# 
#     nok = cond > 1 / rcond
#     ai[nok], bi[nok], di[nok] = _hinv_svd(a, b[nok], d[nok])
#     return ai, bi, di
# 
# Path: glimix_core/_util/solve.py
# def heigvals(a, b, d):
#     a = float(a)
#     b = atleast_1d(b)
#     d = atleast_1d(d)
#     T = a + d
#     D = a * d - b * b
# 
#     t0 = T / 2
#     t1 = sqrt(maximum(T * T / 4 - D, 0))
#     eig0 = t0 + t1
#     eig1 = t0 - t1
# 
#     eig0 = absolute(eig0)
#     eig1 = absolute(eig1)
# 
#     with errstate(invalid="ignore", divide="ignore"):
#         return nan_to_num(maximum(eig0, eig1) / minimum(eig0, eig1))
# 
# def hsvd(a, b, d):
#     a = atleast_1d(a)
#     b = atleast_1d(b)
#     d = atleast_1d(d)
# 
#     aa = a * a
#     bb = b * b
#     dd = d * d
#     ab = a * b
#     bd = b * d
# 
#     e = aa - dd
#     s1 = aa + 2 * bb + dd
#     s2 = sqrt(e ** 2 + 4 * (ab + bd) ** 2)
# 
#     t = 2 * ab + 2 * bd
#     theta = arctan2(t, e) / 2
#     psi = arctan2(t, aa - dd) / 2
# 
#     Ct = cos(theta)
#     St = sin(theta)
#     Cp = cos(psi)
#     Sp = sin(psi)
# 
#     s11 = (a * Ct + b * St) * Cp + (b * Ct + d * St) * Sp
#     s22 = (a * St - b * Ct) * Sp + (-b * St + d * Ct) * Cp
# 
#     U = [[Ct, -St], [St, Ct]]
#     S = [sqrt((s1 + s2) / 2), sqrt(maximum((s1 - s2) / 2, 0.0))]
# 
#     VT = [[sign(s11) * Cp, sign(s11) * Sp], [-sign(s22) * Sp, sign(s22) * Cp]]
# 
#     # U S V.T
#     return U, S, VT


# Path: glimix_core/_util/test/test_util.py
import pytest
from numpy import array, asarray, block, inf, nan, ones
from numpy.linalg import inv, pinv, svd
from numpy.testing import assert_allclose
from numpy_sugar.linalg import ddot
from glimix_core._util import check_covariates, check_economic_qs, check_outcome, hinv
from glimix_core._util.solve import heigvals, hsvd




def test_util_check_economic_qs():

    A = ones((3, 2))
    B = ones((3, 1))
    C = ones(2)

    with pytest.raises(ValueError):
        check_economic_qs(A)

    with pytest.raises(ValueError):
        check_economic_qs((A, C))

    A[0, 0] = inf
    QS = ((A, B), C)

    with pytest.raises(ValueError):

        check_economic_qs(QS)