# Path: attitude/orientation/linear/regression.py
# class Regression(object):
#     def __init__(self, coordinates):
#         """
#         Solves plane equation Z = a(C[0])+b(C[1])+c by solving linear equation Cm=Z where m=[a,b,c]
#         C and Z are the dependent and independent variables, respectively.
#         """
#         coordinates = centered(N.array(coordinates))
#         if len(coordinates[0]) > 3:
#             coordinates = N.swapaxes(coordinates, 0, 1)
# 
#         self.C = add_ones(coordinates[:,0:2])
#         self.Z = coordinates[:,-1]
#         self.__coefficients__ = None
#         self.__covariance__ = None
# 
#         self.nobs = self.Z.shape[0]                     # number of observations
#         self.ncoef = self.C.shape[1]                    # number of coef.
#         self.df_e = self.nobs - self.ncoef              # degrees of freedom, error 
#         self.df_r = self.ncoef - 1                      # degrees of freedom, regression 
#         self.n,self.k = self.C.shape
# 
#     @property
#     def coefficients(self):
#         if self.__coefficients__ is not None:
#             return self.__coefficients__
#         self.xTx = N.linalg.inv(matrix_squared(self.C))
#         self.xy = N.dot(self.C.transpose(),self.Z)
#         self.__coefficients__ = N.dot(self.xTx,self.xy)
#         return self.__coefficients__
# 
#     @property
#     def predictions(self):
#         return self.C[:,0], self.C[:,1], N.dot(self.C,self.coefficients)
# 
#     @property
#     def residuals(self):
#         return self.Z - self.predictions[2]
# 
#     @property
#     def covariance_matrix(self):
#         if self.__covariance__ is not None:
#             return self.__covariance__
#         if self.n == self.k:
#             # solution is fully defined (i.e. only three points)
#             self.__covariance__ = N.zeros((self.n,self.k))
#         else:
#             #rTr = matrix_squared(self.residuals())
#             e = self.residuals
#             sse = N.dot(e,e) #/self.df_e #SSE
#             self.__covariance__ = N.dot(sse,self.xTx)
#         return self.__covariance__
# 
#     def standard_errors(self):
#         """Gets the standard errors of the coefficients"""
#         return N.sqrt(N.diagonal(self.covariance_matrix))
# 
# Path: attitude/orientation/tests.py
# class TestCase(object):
#     def __init__(self, array, strike_dip, id):
# def check_orientation(a):
# def test_directions():
# def test_coordinates():
# def test_covariance():
# def test_rotation_matrix():


from . import Regression
from ..tests import random_plane,scattered_plane
import numpy as N

def test_coordinates():
    """Tests coordinate length"""
    plane,coefficients = random_plane()
    fit = Regression(plane)
    assert N.column_stack(plane).shape[0] == fit.C.shape[0]

def test_regression():
    """Make sure we can fit a simple plane"""
    plane,coefficients = random_plane()
    fit = Regression(plane)
    assert N.allclose(fit.coefficients(), coefficients)

def test_covariance():
    """Make sure we don't get empty covariance matrices"""
    plane,coefficients = scattered_plane()

# Path: attitude/orientation/linear/regression.py
# class Regression(object):
#     def __init__(self, coordinates):
#         """
#         Solves plane equation Z = a(C[0])+b(C[1])+c by solving linear equation Cm=Z where m=[a,b,c]
#         C and Z are the dependent and independent variables, respectively.
#         """
#         coordinates = centered(N.array(coordinates))
#         if len(coordinates[0]) > 3:
#             coordinates = N.swapaxes(coordinates, 0, 1)
# 
#         self.C = add_ones(coordinates[:,0:2])
#         self.Z = coordinates[:,-1]
#         self.__coefficients__ = None
#         self.__covariance__ = None
# 
#         self.nobs = self.Z.shape[0]                     # number of observations
#         self.ncoef = self.C.shape[1]                    # number of coef.
#         self.df_e = self.nobs - self.ncoef              # degrees of freedom, error 
#         self.df_r = self.ncoef - 1                      # degrees of freedom, regression 
#         self.n,self.k = self.C.shape
# 
#     @property
#     def coefficients(self):
#         if self.__coefficients__ is not None:
#             return self.__coefficients__
#         self.xTx = N.linalg.inv(matrix_squared(self.C))
#         self.xy = N.dot(self.C.transpose(),self.Z)
#         self.__coefficients__ = N.dot(self.xTx,self.xy)
#         return self.__coefficients__
# 
#     @property
#     def predictions(self):
#         return self.C[:,0], self.C[:,1], N.dot(self.C,self.coefficients)
# 
#     @property
#     def residuals(self):
#         return self.Z - self.predictions[2]
# 
#     @property
#     def covariance_matrix(self):
#         if self.__covariance__ is not None:
#             return self.__covariance__
#         if self.n == self.k:
#             # solution is fully defined (i.e. only three points)
#             self.__covariance__ = N.zeros((self.n,self.k))
#         else:
#             #rTr = matrix_squared(self.residuals())
#             e = self.residuals
#             sse = N.dot(e,e) #/self.df_e #SSE
#             self.__covariance__ = N.dot(sse,self.xTx)
#         return self.__covariance__
# 
#     def standard_errors(self):
#         """Gets the standard errors of the coefficients"""
#         return N.sqrt(N.diagonal(self.covariance_matrix))
# 
# Path: attitude/orientation/tests.py
# class TestCase(object):
#     def __init__(self, array, strike_dip, id):
# def check_orientation(a):
# def test_directions():
# def test_coordinates():
# def test_covariance():
# def test_rotation_matrix():


# Path: attitude/orientation/linear/tests.py
from . import Regression
from ..tests import random_plane,scattered_plane
import numpy as N


def test_coordinates():
    """Tests coordinate length"""
    plane,coefficients = random_plane()
    fit = Regression(plane)
    assert N.column_stack(plane).shape[0] == fit.C.shape[0]

def test_regression():
    """Make sure we can fit a simple plane"""
    plane,coefficients = random_plane()
    fit = Regression(plane)
    assert N.allclose(fit.coefficients(), coefficients)

def test_covariance():
    """Make sure we don't get empty covariance matrices"""
    plane,coefficients = scattered_plane()

    fit = Regression(plane)