<commit_before>import sys
from expr.kernelpca_ski import KPCA

from facerec_py.facerec.distance import *
from facerec_py.facerec.classifier import NearestNeighbor
from facerec_py.facerec.model import PredictableModel, FeaturesEnsemblePredictableModel
from facerec_py.facerec.validation import KFoldCrossValidation
from facerec_py.facerec.visual import subplot
from facerec_py.facerec.util import minmax_normalize
from expr.read_dataset import read_images
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from expr.feature import *
from util.commons_util.logger_utils.logger_factory import LoggerFactory
from scipy.interpolate import spline
import numpy as np

__author__ = 'Danyang'


class Drawer(object):
    def __init__(self, smooth=False):
        plt.figure("ROC")
        plt.axis([0, 0.5, 0.5, 1.001])
        # ax = pyplot.gca()
        # ax.set_autoscale_on(False)
        plt.xlabel('FPR')
        plt.ylabel('TPR')
        # colors: http://stackoverflow.com/questions/22408237/named-colors-in-matplotlib
        plt.rc('axes', color_cycle=['r', 'g', 'b', 'c', 'm', 'y', 'k', 'darkgreen', 'chocolate', 'darksalmon', 'darkseagreen', 'yellowgreen'])
        self.is_smooth = smooth
        self._rocs = []

    def show(self):
        plt.legend(handles=self._rocs)
        plt.show()

    def plot_roc(self, cv):
        """
        :type cv: KFoldCrossValidation
        :param cv:
        :return:
        """
        # Extract FPR
        FPRs = [r.FPR for r in cv.validation_results]
        TPRs = [r.TPR for r in cv.validation_results]
        if self.is_smooth:
            FPRs, TPRs = self.smooth(FPRs, TPRs)

        # Plot ROC
        roc, = plt.plot(FPRs, TPRs, label=cv.model.feature.short_name())
        self._rocs.append(roc)

    def smooth(self, x, y):
        x = np.array(x)
        y = np.array(y)
        x, idx = np.unique(x, return_index=True)  # otherwise singular matrix
        y = y[idx]

        x_sm = np.linspace(x.min(), x.max(), 60)  # evenly spaced numbers over a specified interval.
        y_sm = spline(x, y, x_sm)
        return x_sm, y_sm


class Experiment(object):
    def __init__(self):
        self.logger = LoggerFactory().getConsoleLogger("facerec")
        self._drawer = Drawer()

    def plot_fisher(self, X, model):
        E = []
        for i in xrange(min(model.feature.eigenvectors.shape[1], 16)):
            e = model.feature.eigenvectors[:, i].reshape(X[0].shape)
            E.append(minmax_normalize(e, 0, 255, dtype=np.uint8))
        # Plot them and store the plot to "python_fisherfaces_fisherfaces.pdf"<commit_msg>add tail and head for ROC curves<commit_after>import sys
from expr.kernelpca_ski import KPCA

from facerec_py.facerec.distance import *
from facerec_py.facerec.classifier import NearestNeighbor
from facerec_py.facerec.model import PredictableModel, FeaturesEnsemblePredictableModel
from facerec_py.facerec.validation import KFoldCrossValidation
from facerec_py.facerec.visual import subplot
from facerec_py.facerec.util import minmax_normalize
from expr.read_dataset import read_images
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from expr.feature import *
from util.commons_util.logger_utils.logger_factory import LoggerFactory
from scipy.interpolate import spline
import numpy as np

__author__ = 'Danyang'


class Drawer(object):
    def __init__(self, smooth=False):
        plt.figure("ROC")
        plt.axis([0, 0.5, 0.5, 1.001])
        # ax = pyplot.gca()
        # ax.set_autoscale_on(False)
        plt.xlabel('FPR')
        plt.ylabel('TPR')
        # colors: http://stackoverflow.com/questions/22408237/named-colors-in-matplotlib
        plt.rc('axes', color_cycle=['r', 'g', 'b', 'c', 'm', 'y', 'k', 'darkgreen', 'chocolate', 'darksalmon', 'darkseagreen', 'yellowgreen'])
        self.is_smooth = smooth
        self._rocs = []

    def show(self):
        plt.legend(handles=self._rocs)
        plt.show()

    def plot_roc(self, cv):
        """
        :type cv: KFoldCrossValidation
        :param cv:
        :return:
        """
        # Extract FPR
        FPRs = [r.FPR for r in cv.validation_results]
        TPRs = [r.TPR for r in cv.validation_results]

        # add (0, 0), and (1, 1)
        FPRs.append(0.0)
        TPRs.append(0.0)
        FPRs.append(1.0)
        TPRs.append(1.0)

        if self.is_smooth:
            FPRs, TPRs = self.smooth(FPRs, TPRs)

        # Plot ROC
        roc, = plt.plot(FPRs, TPRs, label=cv.model.feature.short_name())
        self._rocs.append(roc)

    def smooth(self, x, y):
        x = np.array(x)
        y = np.array(y)
        x, idx = np.unique(x, return_index=True)  # otherwise singular matrix
        y = y[idx]

        x_sm = np.linspace(x.min(), x.max(), 60)  # evenly spaced numbers over a specified interval.
        y_sm = spline(x, y, x_sm)
        return x_sm, y_sm


class Experiment(object):
    def __init__(self, smooth=False):
        self.logger = LoggerFactory().getConsoleLogger("facerec")
        self._drawer = Drawer(smooth)

    def plot_fisher(self, X, model):
        E = []
        for i in xrange(min(model.feature.eigenvectors.shape[1], 16)):
            e = model.feature.eigenvectors[:, i].reshape(X[0].shape)
            E.append(minmax_normalize(e, 0, 255, dtype=np.uint8))
        # Plot them and store the plot to "python_fisherfaces_fisherfaces.pdf"
{'fraction_non_alphanumeric': 0.11959761549925485, 'fraction_numerical': 0.012667660208643815, 'mean_word_length': 2.8105039034776436, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7214568', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1815, 'n_words': 416}