# Path: facerec_py/facerec/normalization.py
# def minmax(X, low, high, minX=None, maxX=None, dtype=np.float):
# def zscore(X, mean=None, std=None):
# def gaussian(X, mu, sig):
# def inverse_dissim(X):
# def vector_normalize(x):
# def gaussian_kernel(X, mu=None, sig=None):
#     X = np.asarray(X)
#     X = X * (high - low)
#     X = X + low
#     X = np.asarray(X)
#     X = (X - mean) / std
#     X = np.asarray(X)
#     X = zscore(X)
#     X = minmax(X, 0, 10)
#     X = np.asarray(X)
# 
# Path: facerec_py/facerec/preprocessing.py
# class LBPPreprocessing(AbstractFeature):
#     def __init__(self, lbp_operator=ExtendedLBP(radius=1, neighbors=8)):
#         AbstractFeature.__init__(self)
#         self._lbp_operator = lbp_operator
# 
#     def compute(self, X, y):
#         Xp = []
#         for xi in X:
#             Xp.append(self.extract(xi))
#         return Xp
# 
#     def extract(self, X):
#         return self._lbp_operator(X)
# 
#     def __repr__(self):
#         return "LBPPreprocessing (lbp_operator=%s)" % (repr(self._lbp_operator))


import numpy as np
import cv2
from scipy import ndimage as nd
from skimage.filters import gabor_kernel  # from skimage.filter._gabor import gabor_kernel
from facerec_py.facerec import normalization
from facerec_py.facerec.feature import *
from facerec_py.facerec.lbp import *
from facerec_py.facerec.preprocessing import LBPPreprocessing
        """
        convention: cap X, small y
        :param X: The images, which is a Python list of numpy arrays.
        :param y: The corresponding labels (the unique number of the subject, person) in a Python list.
        :return:
        """
        # build the column matrix
        XC = asColumnMatrix(X)

        features = []
        for x in X:
            features.append(self.filter(x))
        return features

    def extract(self, X):
        """

        :param X: a single test data
        :return:
        """
        return self.filter(X)

    def filter(self, x):
        feats = np.zeros((len(self._kernels), 2), dtype=np.float32)
        for k, kernel in enumerate(self._kernels):
            filtered = nd.convolve(x, kernel, mode='wrap', cval=0)
            feats[k, 0] = filtered.mean()
            feats[k, 1] = filtered.var()

        for i in xrange(2):

# Path: facerec_py/facerec/normalization.py
# def minmax(X, low, high, minX=None, maxX=None, dtype=np.float):
# def zscore(X, mean=None, std=None):
# def gaussian(X, mu, sig):
# def inverse_dissim(X):
# def vector_normalize(x):
# def gaussian_kernel(X, mu=None, sig=None):
#     X = np.asarray(X)
#     X = X * (high - low)
#     X = X + low
#     X = np.asarray(X)
#     X = (X - mean) / std
#     X = np.asarray(X)
#     X = zscore(X)
#     X = minmax(X, 0, 10)
#     X = np.asarray(X)
# 
# Path: facerec_py/facerec/preprocessing.py
# class LBPPreprocessing(AbstractFeature):
#     def __init__(self, lbp_operator=ExtendedLBP(radius=1, neighbors=8)):
#         AbstractFeature.__init__(self)
#         self._lbp_operator = lbp_operator
# 
#     def compute(self, X, y):
#         Xp = []
#         for xi in X:
#             Xp.append(self.extract(xi))
#         return Xp
# 
#     def extract(self, X):
#         return self._lbp_operator(X)
# 
#     def __repr__(self):
#         return "LBPPreprocessing (lbp_operator=%s)" % (repr(self._lbp_operator))


# Path: expr/feature.py
import numpy as np
import cv2
from scipy import ndimage as nd
from skimage.filters import gabor_kernel  # from skimage.filter._gabor import gabor_kernel
from facerec_py.facerec import normalization
from facerec_py.facerec.feature import *
from facerec_py.facerec.lbp import *
from facerec_py.facerec.preprocessing import LBPPreprocessing

        """
        convention: cap X, small y
        :param X: The images, which is a Python list of numpy arrays.
        :param y: The corresponding labels (the unique number of the subject, person) in a Python list.
        :return:
        """
        # build the column matrix
        XC = asColumnMatrix(X)

        features = []
        for x in X:
            features.append(self.filter(x))
        return features

    def extract(self, X):
        """

        :param X: a single test data
        :return:
        """
        return self.filter(X)

    def filter(self, x):
        feats = np.zeros((len(self._kernels), 2), dtype=np.float32)
        for k, kernel in enumerate(self._kernels):
            filtered = nd.convolve(x, kernel, mode='wrap', cval=0)
            feats[k, 0] = filtered.mean()
            feats[k, 1] = filtered.var()

        for i in xrange(2):

            feats[:, i] = normalization.zscore(feats[:, i])  # needed?