# 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
            assumed, that the predicted class is always returned as first
            element. Moreover, this class returns the distances for the 
            first k-Nearest Neighbors. 
            
            Example:
            
                [ 0, 
                   { 'labels'    : [ 0,      0,      1      ],
                     'distances' : [ 10.132, 10.341, 13.314 ]
                   }
                ]
            
            So if you want to perform a thresholding operation, you could 
            pick the distances in the second array of the generic classifier
            output.    
                    
        """
        distances = []
        for xi in self.X:
            xi = xi.reshape(-1, 1)
            d = self.dist_metric(xi, q)
            distances.append(d)
        if len(distances) > len(self.y):
            raise Exception("More distances than classes. Is your distance metric correct?")
        distances = np.asarray(distances)
        # Get the indices in an ascending sort order:
        idx = np.argsort(distances)
        # Sort the labels and distances accordingly:
        sorted_y = self.y[idx]
        sorted_distances = distances[idx]

# 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

            assumed, that the predicted class is always returned as first
            element. Moreover, this class returns the distances for the 
            first k-Nearest Neighbors. 
            
            Example:
            
                [ 0, 
                   { 'labels'    : [ 0,      0,      1      ],
                     'distances' : [ 10.132, 10.341, 13.314 ]
                   }
                ]
            
            So if you want to perform a thresholding operation, you could 
            pick the distances in the second array of the generic classifier
            output.    
                    
        """
        distances = []
        for xi in self.X:
            xi = xi.reshape(-1, 1)
            d = self.dist_metric(xi, q)
            distances.append(d)
        if len(distances) > len(self.y):
            raise Exception("More distances than classes. Is your distance metric correct?")
        distances = np.asarray(distances)
        # Get the indices in an ascending sort order:
        idx = np.argsort(distances)
        # Sort the labels and distances accordingly:
        sorted_y = self.y[idx]
        sorted_distances = distances[idx]

        sorted_sim = inverse_dissim(sorted_distances)