# 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


class AbstractClassifier(object):
    def compute(self, X, y):
        raise NotImplementedError("Every AbstractClassifier must implement the compute method.")

    def predict(self, X):
        raise NotImplementedError("Every AbstractClassifier must implement the predict method.")

    def update(self, X, y):
        raise NotImplementedError("This Classifier is cannot be updated.")

    def binary_predict(self, q, lbl):
        """

        :param q: feature
        :param lbl: the specified class label
        :return conf: confidence interval [0, 1]
        :raise NotImplementedError:
        """
        raise NotImplementedError("The binary prediction is not implemented")


class NearestNeighbor(AbstractClassifier):
    """
    Implements a k-Nearest Neighbor Model with a generic distance metric.
    """


# 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



class AbstractClassifier(object):
    def compute(self, X, y):
        raise NotImplementedError("Every AbstractClassifier must implement the compute method.")

    def predict(self, X):
        raise NotImplementedError("Every AbstractClassifier must implement the predict method.")

    def update(self, X, y):
        raise NotImplementedError("This Classifier is cannot be updated.")

    def binary_predict(self, q, lbl):
        """

        :param q: feature
        :param lbl: the specified class label
        :return conf: confidence interval [0, 1]
        :raise NotImplementedError:
        """
        raise NotImplementedError("The binary prediction is not implemented")


class NearestNeighbor(AbstractClassifier):
    """
    Implements a k-Nearest Neighbor Model with a generic distance metric.
    """


    def __init__(self, dist_metric=EuclideanDistance(), k=1):