# Path: facerec_py/facerec/distance.py
# class EuclideanDistance(AbstractDistance):
#     def __init__(self):
#         AbstractDistance.__init__(self, "EuclideanDistance")
# 
#     def __call__(self, p, q):
#         p = np.asarray(p).flatten()
#         q = np.asarray(q).flatten()
#         return np.sqrt(np.sum(np.power(p-q, 2)))
# 
# Path: facerec_py/facerec/normalization.py
# def gaussian_kernel(X, mu=None, sig=None):
#     """
#     gaussian kernel.
#     convert distance to similarity by setting mu=0
#     :param X:
#     :param mu:
#     :param sig:
#     :return:
#     """
#     X = np.asarray(X)
#     if mu is None:
#         mu = X.mean()
#     if sig is None:
#         sig = X.std()
# 
#     return np.exp(-np.power(X-mu, 2)/(2*sig**2))
# 
# def inverse_dissim(X):
#     """
# 
#     :param X: int or np.array
#     :return:
#     """
#     X = np.asarray(X)
#     X = zscore(X)
#     X = minmax(X, 0, 10)
#     return 1./(1+X)
# 
# Path: facerec_py/facerec/util.py
# def asRowMatrix(X):
#     """
#     Creates a row-matrix from multi-dimensional data items in list l.
#     
#     X [list] List with multi-dimensional data.
#     """
#     if len(X) == 0:
#         return np.array([])
#     total = 1
#     for i in range(0, np.ndim(X[0])):
#         total = total * X[0].shape[i]
#     mat = np.empty([0, total], dtype=X[0].dtype)
#     for row in X:
#         mat = np.append(mat, row.reshape(1, -1), axis=0)  # same as vstack
#     return np.asmatrix(mat)


from facerec_py.facerec.distance import EuclideanDistance
from facerec_py.facerec.normalization import gaussian_kernel, inverse_dissim
from facerec_py.facerec.util import asRowMatrix
    from svmutil import *
from StringIO import StringIO
from svmutil import *
import logging
import numpy as np
import operator as op
import sys
    """
    This class is just a simple wrapper to use libsvm in the 
    CrossValidation module. If you don't use this framework
    use the validation methods coming with LibSVM, they are
    much easier to access (simply pass the correct class 
    labels in svm_predict and you are done...).

    The grid search method in this class is somewhat similar
    to libsvm grid.py, as it performs a parameter search over
    a logarithmic scale.    Again if you don't use this framework, 
    use the libsvm tools as they are much easier to access.

    Please keep in mind to normalize your input data, as expected
    for the model. There's no way to assume a generic normalization
    step.
    """

    def __init__(self, param=None):
        AbstractClassifier.__init__(self)
        self.logger = logging.getLogger("facerec.classifier.SVM")
        self.param = param
        self.svm = svm_model()
        self.param = param
        if self.param is None:
            self.param = svm_parameter("-q")

    def compute(self, X, y):
        self.logger.debug("SVM TRAINING (C=%.2f,gamma=%.2f,p=%.2f,nu=%.2f,coef=%.2f,degree=%.2f)" % (
            self.param.C, self.param.gamma, self.param.p, self.param.nu, self.param.coef0, self.param.degree))
        # turn data into a row vector (needed for libsvm)

# Path: facerec_py/facerec/distance.py
# class EuclideanDistance(AbstractDistance):
#     def __init__(self):
#         AbstractDistance.__init__(self, "EuclideanDistance")
# 
#     def __call__(self, p, q):
#         p = np.asarray(p).flatten()
#         q = np.asarray(q).flatten()
#         return np.sqrt(np.sum(np.power(p-q, 2)))
# 
# Path: facerec_py/facerec/normalization.py
# def gaussian_kernel(X, mu=None, sig=None):
#     """
#     gaussian kernel.
#     convert distance to similarity by setting mu=0
#     :param X:
#     :param mu:
#     :param sig:
#     :return:
#     """
#     X = np.asarray(X)
#     if mu is None:
#         mu = X.mean()
#     if sig is None:
#         sig = X.std()
# 
#     return np.exp(-np.power(X-mu, 2)/(2*sig**2))
# 
# def inverse_dissim(X):
#     """
# 
#     :param X: int or np.array
#     :return:
#     """
#     X = np.asarray(X)
#     X = zscore(X)
#     X = minmax(X, 0, 10)
#     return 1./(1+X)
# 
# Path: facerec_py/facerec/util.py
# def asRowMatrix(X):
#     """
#     Creates a row-matrix from multi-dimensional data items in list l.
#     
#     X [list] List with multi-dimensional data.
#     """
#     if len(X) == 0:
#         return np.array([])
#     total = 1
#     for i in range(0, np.ndim(X[0])):
#         total = total * X[0].shape[i]
#     mat = np.empty([0, total], dtype=X[0].dtype)
#     for row in X:
#         mat = np.append(mat, row.reshape(1, -1), axis=0)  # same as vstack
#     return np.asmatrix(mat)


# Path: facerec_py/facerec/classifier.py
from facerec_py.facerec.distance import EuclideanDistance
from facerec_py.facerec.normalization import gaussian_kernel, inverse_dissim
from facerec_py.facerec.util import asRowMatrix
    from svmutil import *
from StringIO import StringIO
from svmutil import *
import logging
import numpy as np
import operator as op
import sys

    """
    This class is just a simple wrapper to use libsvm in the 
    CrossValidation module. If you don't use this framework
    use the validation methods coming with LibSVM, they are
    much easier to access (simply pass the correct class 
    labels in svm_predict and you are done...).

    The grid search method in this class is somewhat similar
    to libsvm grid.py, as it performs a parameter search over
    a logarithmic scale.    Again if you don't use this framework, 
    use the libsvm tools as they are much easier to access.

    Please keep in mind to normalize your input data, as expected
    for the model. There's no way to assume a generic normalization
    step.
    """

    def __init__(self, param=None):
        AbstractClassifier.__init__(self)
        self.logger = logging.getLogger("facerec.classifier.SVM")
        self.param = param
        self.svm = svm_model()
        self.param = param
        if self.param is None:
            self.param = svm_parameter("-q")

    def compute(self, X, y):
        self.logger.debug("SVM TRAINING (C=%.2f,gamma=%.2f,p=%.2f,nu=%.2f,coef=%.2f,degree=%.2f)" % (
            self.param.C, self.param.gamma, self.param.p, self.param.nu, self.param.coef0, self.param.degree))
        # turn data into a row vector (needed for libsvm)

        X = asRowMatrix(X)