# Path: dev/bayesLib.py
# def int2flat(cameraMatrix, distCoeffs, model):
# def flat2CamMatrix(kFlat, model):
# def flat2int(X, Ns, model):
# def ext2flat(rVec, tVec):
# def flat2ext(X):
# def errorCuadraticoImagen(Xext, Xint, Ns, params, j, mahDist=False):
# def etotalExt(Xext, Xint, Ns, params, j):
# def errorCuadraticoInt(Xint, Ns, XextList, params, mahDist=False):
# def etotalInt(Xint, Ns, XextList, params):
#     def __init__(self, Ns, XextList, params, sampleador):
#     def nuevo(self, old, oldE):
#     def __init__(self, Xint, Ns, params, sampleador, priorExt, W):
#     def ePrior(self, Xext):
#     def etotalExt(self, Xext):
#     def nuevo(self, old, oldE):
# def procJint(Xint, Ns, XextList, params, ret):
# def procHint(Xint, Ns, XextList, params, ret):
# def procJext(Xext, Xint, Ns, params, j, ret):
# def procHext(Xext, Xint, Ns, params, j, ret):
# def jacobianos(Xint, Ns, XextList, params, hessianos=True):
# def varVar2(c, N):
# def varMahal(c1, n, c2, rank=False):
# def trasPerMAt(k,l):
# def varVarN(c, Nsamples):
#     X = np.concatenate((kFlat, dFlat))
#     X = np.concatenate((rFlat, tFlat))
#         S = np.linalg.inv(Cm)  # inverse of covariance matrix
#     A = vecA.reshape((k,l))
# class metHas:
# class metHasExt:


import numpy as np
import scipy.stats as sts
import matplotlib.pyplot as plt
import corner
import time
import sys
from copy import deepcopy as dc
from dev import bayesLib as bl
from numpy.random import rand as rn
from sklearn.decomposition import PCA
intrinsicParamsOutFile = intrinsicParamsOutFile + str(std) + ".npy"

# pongo en forma flat los valores iniciales
Xint, Ns = bl.int2flat(cameraMatrix, distCoeffs)
XextList = [bl.ext2flat(rVecs[i], tVecs[i])for i in range(n)]

Ci = np.repeat([ std**2 * np.eye(2)],n*m, axis=0).reshape(n,m,2,2)
params = dict()
params["n"] = n
params["m"] = m
params["imagePoints"] = imagePoints
params["model"] = model
params["chessboardModel"] = chessboardModel
params["Cccd"] = Ci
params["Cf"] = False
params["Ck"] = False
params["Crt"] = False
#params = [n, m, imagePoints, model, chessboardModel, Ci]

# pruebo con una imagen
j = 0
ErIm = bl.errorCuadraticoImagen(XextList[j], Xint, Ns, params, j, mahDist=False)
print(ErIm.sum())

# pruebo el error total
def etotal(Xint, Ns, XextList, params):
    '''
    calcula el error total como la suma de los errore de cada punto en cada
    imagen
    '''

# Path: dev/bayesLib.py
# def int2flat(cameraMatrix, distCoeffs, model):
# def flat2CamMatrix(kFlat, model):
# def flat2int(X, Ns, model):
# def ext2flat(rVec, tVec):
# def flat2ext(X):
# def errorCuadraticoImagen(Xext, Xint, Ns, params, j, mahDist=False):
# def etotalExt(Xext, Xint, Ns, params, j):
# def errorCuadraticoInt(Xint, Ns, XextList, params, mahDist=False):
# def etotalInt(Xint, Ns, XextList, params):
#     def __init__(self, Ns, XextList, params, sampleador):
#     def nuevo(self, old, oldE):
#     def __init__(self, Xint, Ns, params, sampleador, priorExt, W):
#     def ePrior(self, Xext):
#     def etotalExt(self, Xext):
#     def nuevo(self, old, oldE):
# def procJint(Xint, Ns, XextList, params, ret):
# def procHint(Xint, Ns, XextList, params, ret):
# def procJext(Xext, Xint, Ns, params, j, ret):
# def procHext(Xext, Xint, Ns, params, j, ret):
# def jacobianos(Xint, Ns, XextList, params, hessianos=True):
# def varVar2(c, N):
# def varMahal(c1, n, c2, rank=False):
# def trasPerMAt(k,l):
# def varVarN(c, Nsamples):
#     X = np.concatenate((kFlat, dFlat))
#     X = np.concatenate((rFlat, tFlat))
#         S = np.linalg.inv(Cm)  # inverse of covariance matrix
#     A = vecA.reshape((k,l))
# class metHas:
# class metHasExt:


# Path: dev/intrinsicCalibMHnoFull.py
import numpy as np
import scipy.stats as sts
import matplotlib.pyplot as plt
import corner
import time
import sys
from copy import deepcopy as dc
from dev import bayesLib as bl
from numpy.random import rand as rn
from sklearn.decomposition import PCA

intrinsicParamsOutFile = intrinsicParamsOutFile + str(std) + ".npy"

# pongo en forma flat los valores iniciales
Xint, Ns = bl.int2flat(cameraMatrix, distCoeffs)
XextList = [bl.ext2flat(rVecs[i], tVecs[i])for i in range(n)]

Ci = np.repeat([ std**2 * np.eye(2)],n*m, axis=0).reshape(n,m,2,2)
params = dict()
params["n"] = n
params["m"] = m
params["imagePoints"] = imagePoints
params["model"] = model
params["chessboardModel"] = chessboardModel
params["Cccd"] = Ci
params["Cf"] = False
params["Ck"] = False
params["Crt"] = False
#params = [n, m, imagePoints, model, chessboardModel, Ci]

# pruebo con una imagen
j = 0
ErIm = bl.errorCuadraticoImagen(XextList[j], Xint, Ns, params, j, mahDist=False)
print(ErIm.sum())

# pruebo el error total
def etotal(Xint, Ns, XextList, params):
    '''
    calcula el error total como la suma de los errore de cada punto en cada
    imagen
    '''

    return bl.errorCuadraticoInt(Xint, Ns, XextList, params).sum()