<commit_before># Copyright 2017 <NAME>. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

import os
import sys
sys.path.append(os.getcwd() + '/../')
import numpy as np
from PLDA import PLDA
from sklearn.decomposition import PCA
from skimage.io import imread
from skimage.transform import resize


def build_google_faces_dataset(load_dir, resized_shape):
    data_shape=resized_shape
    imgs = []
    lbls = []
    for fname in os.listdir(load_dir):
        if fname[-4:] == '.jpg':
            label = ''.join([i for i in fname if not i.isdigit()])
            label = label[:-4]
            if label != 'neutral':  # Ignore the 2 'neutral' face images.
                img = imread(load_dir + fname)
                img = resize(img, data_shape)
                img = img.flatten()
                imgs.append(img)
                lbls.append(label)

    return imgs, lbls

def check_equal_folds(*args, **kwargs):
    n = args[0].shape[0]
    k = kwargs['k']
    warn = n % k != 0
    if warn:
        print('\nWARNING')
        print('\'k\' did not divide the total number of data \'n\': ' +
              'n % k = {}.'.format(n % k))
        print('{} subsamples of {} data '.format(k - 1, int(n / k)) + 
              'each are being tested during each k-folds iteration.')
       
def rerun(n_iterations):
    def wrap_CV(CV_func):
        k_folds_scores = []
        def wrapper(*args, **kwargs):
            check_equal_folds(*args, **kwargs)

            for x in range(n_iterations):
                scores = CV_func(*args, **kwargs)
                k_folds_scores.append(scores)
                scores = []

            return k_folds_scores
        return wrapper
    return wrap_CV

@rerun(n_iterations=10)  # Number of times to run k-folds function.
def k_folds_CV_PLDA(X, Y, k=5, MAP_estimate=True):
    """ Cross validation on Google Face emotions dataset, using k-folds.

    DESCRIPTION: If MAP_estimate is set to False, n_runs number of runs
                  are run for each of the k-subsamples runs as well. 
                  This function runs several iterations of k-folders such that
                  the k sets of data are randomly selected.
    ARGUMENTS
     X         (ndarray): Rows of data examaples. [n x n_dims]
     Y         (ndarray): Labels corresponding to the rows of X. [n x 1]
     k             (int): k in k-folds. The number of subsets/subsamples to
                           to break the data into. During each run, all but
                           one set is used as training data, and the remaining
                           is used to test the model's performance. Number
                           of data in the test set is (n // k).
     MAP_estimate (bool): True makes the PLDA model use the MAP estimate for
                           classifciation. If this is set to false, the model<commit_msg>Update cross_validation_PLDA.py<commit_after># Copyright 2017 <NAME>. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

import os
import sys
sys.path.append(os.getcwd() + '/../')
import numpy as np
from PLDA import PLDA
from sklearn.decomposition import PCA
from skimage.io import imread
from skimage.transform import resize


def build_google_faces_dataset(load_dir, resized_shape):
    data_shape=resized_shape
    imgs = []
    lbls = []
    for fname in os.listdir(load_dir):
        if fname[-4:] == '.jpg':
            label = ''.join([i for i in fname if not i.isdigit()])
            label = label[:-4]
            if label != 'neutral':  # Ignore the 2 'neutral' face images.
                img = imread(load_dir + fname)
                img = resize(img, data_shape)
                img = img.flatten()
                imgs.append(img)
                lbls.append(label)

    return imgs, lbls

def check_equal_folds(*args, **kwargs):
    n = args[0].shape[0]
    k = kwargs['k']
    warn = n % k != 0
    if warn:
        print('\nWARNING')
        print('\'k\' did not divide the total number of data \'n\': ' +
              'n % k = {}.'.format(n % k))
        print('{} subsamples of {} data '.format(k - 1, int(n / k)) + 
              'each are being tested during each k-folds iteration.')
       
def rerun(n_iterations):
    def wrap_CV(CV_func):
        k_folds_scores = []
        def wrapper(*args, **kwargs):
            check_equal_folds(*args, **kwargs)

            for x in range(n_iterations):
                scores = CV_func(*args, **kwargs)
                k_folds_scores.append(scores)
                scores = []

            return k_folds_scores
        return wrapper
    return wrap_CV

@rerun(n_iterations=10)  # Number of times to run k-folds function.
def k_folds_CV_PLDA(X, Y, k=5, MAP_estimate=True):
    """ Cross validation on Google Face emotions dataset, using k-folds.

    DESCRIPTION: If MAP_estimate is set to False, n_runs number of runs
                  are conducted for each of the k-subsamples. 
                 This function is entirely rerun via the decorator such
                  that the k sets of data are randomly selected each time.
    ARGUMENTS
     X         (ndarray): Rows of data examaples. [n x n_dims]
     Y         (ndarray): Labels corresponding to the rows of X. [n x 1]
     k             (int): k in k-folds. The number of subsets/subsamples to
                           to break the data into. During each run, all but
                           one set is used as training data, and the remaining
                           is used to test the model's performance. Number
                           of data in the test set is (n // k).
     MAP_estimate (bool): True makes the PLDA model use the MAP estimate for
                           classifciation. If this is set to false, the model
{'fraction_non_alphanumeric': 0.10997257901573099, 'fraction_numerical': 0.005484196853802858, 'mean_word_length': 2.1658291457286434, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5551334', 'n_tokens_mistral': 2021, 'n_tokens_neox': 1915, 'n_words': 750}