# 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

class LGBPHS(ChainedFeature):
    def __init__(self):
        """
        Un-weighted Local Gabor Binary Pattern Histogram Sequence
        :return:
        """
        gabor = GaborFilterSki(theta_r=2, sigma_tuple=(1, ))
        gabor.filter = gabor.raw_convolve
        lbp_hist = MultiScaleSpatialHistogram()
        super(LGBPHS, self).__init__(gabor, lbp_hist)


class LGBPHS2(ChainedFeature):
    def __init__(self, n_orient=4, n_scale=2, lbp_operator=ExtendedLBP(radius=3)):  # alternatively LPQ
        gabor = GaborFilterCv2(n_orient, n_scale)
        lbp_hist = ConcatenatedSpatialHistogram(lbp_operator=lbp_operator)
        super(LGBPHS2, self).__init__(gabor, lbp_hist)


class GaborFisher(ChainedFeature):
    def __init__(self):
        gabor = GaborFilterSki(theta_r=2, sigma_tuple=(1, ))  # decrease param; otherwise memory issue
        gabor.filter = gabor.convolve_to_col  # replace
        fisher = Fisherfaces(14)
        super(GaborFisher, self).__init__(gabor, fisher)


class LbpFisher(ChainedFeature):
    def __init__(self, lbp_operator=ExtendedLBP(radius=11)):  # (6, 11, 14, 15, 19)

# 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


class LGBPHS(ChainedFeature):
    def __init__(self):
        """
        Un-weighted Local Gabor Binary Pattern Histogram Sequence
        :return:
        """
        gabor = GaborFilterSki(theta_r=2, sigma_tuple=(1, ))
        gabor.filter = gabor.raw_convolve
        lbp_hist = MultiScaleSpatialHistogram()
        super(LGBPHS, self).__init__(gabor, lbp_hist)


class LGBPHS2(ChainedFeature):
    def __init__(self, n_orient=4, n_scale=2, lbp_operator=ExtendedLBP(radius=3)):  # alternatively LPQ
        gabor = GaborFilterCv2(n_orient, n_scale)
        lbp_hist = ConcatenatedSpatialHistogram(lbp_operator=lbp_operator)
        super(LGBPHS2, self).__init__(gabor, lbp_hist)


class GaborFisher(ChainedFeature):
    def __init__(self):
        gabor = GaborFilterSki(theta_r=2, sigma_tuple=(1, ))  # decrease param; otherwise memory issue
        gabor.filter = gabor.convolve_to_col  # replace
        fisher = Fisherfaces(14)
        super(GaborFisher, self).__init__(gabor, fisher)


class LbpFisher(ChainedFeature):
    def __init__(self, lbp_operator=ExtendedLBP(radius=11)):  # (6, 11, 14, 15, 19)

        lbp = LBPPreprocessing(lbp_operator=lbp_operator)  # preprocessing, not histogram