"""
Ridge regression
"""

# Author: Mathieu Blondel <mathieu@mblondel.org>
#         Reuben Fletcher-Costin <reuben.fletchercostin@gmail.com>
#         Fabian Pedregosa <fabian@fseoane.net>
#         Michael Eickenberg <michael.eickenberg@nsup.org>
# License: BSD 3 clause

from abc import ABCMeta, abstractmethod
import warnings

import numpy as np
from scipy import linalg
from scipy import sparse
from scipy.sparse import linalg as sp_linalg

from .base import LinearClassifierMixin, LinearModel
from ..base import RegressorMixin
from ..utils.extmath import safe_sparse_dot
from ..utils import check_X_y
from ..utils import compute_sample_weight
from ..utils import column_or_1d
from ..preprocessing import LabelBinarizer
from ..grid_search import GridSearchCV
from ..externals import six
from ..metrics.scorer import check_scoring

def _solve_sparse_cg(X, y, alpha, max_iter=None, tol=1e-3, verbose=0):
    n_samples, n_features = X.shape
    X1 = sp_linalg.aslinearoperator(X)
    coefs = np.empty((y.shape[1], n_features))

    if n_features > n_samples:
        def create_mv(curr_alpha):
            def _mv(x):
                return X1.matvec(X1.rmatvec(x)) + curr_alpha * x
            return _mv
    else:
        def create_mv(curr_alpha):
            def _mv(x):
                return X1.rmatvec(X1.matvec(x)) + curr_alpha * x
            return _mv

    for i in range(y.shape[1]):
        y_column = y[:, i]

        mv = create_mv(alpha[i])
        if n_features > n_samples:
            # kernel ridge
            # w = X.T * inv(X X^t + alpha*Id) y
            C = sp_linalg.LinearOperator(
                (n_samples, n_samples), matvec=mv, dtype=X.dtype)
            coef, info = sp_linalg.cg(C, y_column, tol=tol)
            coefs[i] = X1.rmatvec(coef)
        else:
            # linear ridge
            # w = inv(X^t X + alpha*Id) * X.T y
            y_column = X1.rmatvec(y_column)
            C = sp_linalg.LinearOperator(
                (n_features, n_features), matvec=mv, dtype=X.dtype)
            coefs[i], info = sp_linalg.cg(C, y_column, maxiter=max_iter,
                                          tol=tol)
        if info < 0:
            raise ValueError("Failed with error code %d" % info)

        if max_iter is None and info > 0 and verbose:
            warnings.warn("sparse_cg did not converge after %d iterations." %
                          info)

    return coefs

def _solve_lsqr(X, y, alpha, max_iter=None, tol=1e-3):
    n_samples, n_features = X.shape
    coefs = np.empty((y.shape[1], n_features))

    # According to the lsqr documentation, alpha = damp^2.
    sqrt_alpha = np.sqrt(alpha)

    for i in range(y.shape[1]):
        y_column = y[:, i]
        coefs[i] = sp_linalg.lsqr(X, y_column, damp=sqrt_alpha[i],
                                  atol=tol, btol=tol, iter_lim=max_iter)[0]

    return coefs

def _solve_cholesky(X, y, alpha):
    # w = inv(X^t X + alpha*Id) * X.T y
    n_samples, n_features = X.shape
    n_targets = y.shape[1]

    A = safe_sparse_dot(X.T, X, dense_output=True)
    Xy = safe_sparse_dot(X.T, y, dense_output=True)

    one_alpha = np.array_equal(alpha, len(alpha) * [alpha[0]])

    if one_alpha:
        A.flat[::n_features + 1] += alpha[0]
        return linalg.solve(A, Xy, sym_pos=True,
                            overwrite_a=True).T
    else:
        coefs = np.empty([n_targets, n_features])
        for coef, target, current_alpha in zip(coefs, Xy.T, alpha):
            A.flat[::n_features + 1] += current_alpha
            coef[:] = linalg.solve(A, target, sym_pos=True,
                                   overwrite_a=False).ravel()
            A.flat[::n_features + 1] -= current_alpha
        return coefs

def _solve_cholesky_kernel(K, y, alpha, sample_weight=None, copy=False):
    # dual_coef = inv(X X^t + alpha*Id) y
    n_samples = K.shape[0]
    n_targets = y.shape[1]

    if copy:
        K = K.copy()

    alpha = np.atleast_1d(alpha)
    one_alpha = (alpha == alpha[0]).all()
    has_sw = isinstance(sample_weight, np.ndarray) \
        or sample_weight not in [1.0, None]

    if has_sw:
        # Unlike other solvers, we need to support sample_weight directly
        # because K might be a pre-computed kernel.
        sw = np.sqrt(np.atleast_1d(sample_weight))
        y = y * sw[:, np.newaxis]
        K *= np.outer(sw, sw)

    if one_alpha:
        # Only one penalty, we can solve multi-target problems in one time.
        K.flat[::n_samples + 1] += alpha[0]

        try:
            # Note: we must use overwrite_a=False in order to be able to
            #       use the fall-back solution below in case a LinAlgError
            #       is raised
            dual_coef = linalg.solve(K, y, sym_pos=True,
                                     overwrite_a=False)
        except np.linalg.LinAlgError:
            warnings.warn("Singular matrix in solving dual problem. Using "
                          "least-squares solution instead.")
            dual_coef = linalg.lstsq(K, y)[0]

        # K is expensive to compute and store in memory so change it back in
        # case it was user-given.
        K.flat[::n_samples + 1] -= alpha[0]

        if has_sw:
            dual_coef *= sw[:, np.newaxis]

        return dual_coef
    else:
        # One penalty per target. We need to solve each target separately.
        dual_coefs = np.empty([n_targets, n_samples])

        for dual_coef, target, current_alpha in zip(dual_coefs, y.T, alpha):
            K.flat[::n_samples + 1] += current_alpha

            dual_coef[:] = linalg.solve(K, target, sym_pos=True,
                                        overwrite_a=False).ravel()

            K.flat[::n_samples + 1] -= current_alpha

        if has_sw:
            dual_coefs *= sw[np.newaxis, :]

        return dual_coefs.T

def _solve_svd(X, y, alpha):
    U, s, Vt = linalg.svd(X, full_matrices=False)
    idx = s > 1e-15  # same default value as scipy.linalg.pinv
    s_nnz = s[idx][:, np.newaxis]
    UTy = np.dot(U.T, y)
    d = np.zeros((s.size, alpha.size))
    d[idx] = s_nnz / (s_nnz ** 2 + alpha)
    d_UT_y = d * UTy
    return np.dot(Vt.T, d_UT_y).T

def _rescale_data(X, y, sample_weight):
    """Rescale data so as to support sample_weight"""
    n_samples = X.shape[0]
    sample_weight = sample_weight * np.ones(n_samples)
    sample_weight = np.sqrt(sample_weight)
    sw_matrix = sparse.dia_matrix((sample_weight, 0),
                                  shape=(n_samples, n_samples))
    X = safe_sparse_dot(sw_matrix, X)
    y = safe_sparse_dot(sw_matrix, y)
    return X, y

def ridge_regression(X, y, alpha, sample_weight=None, solver='auto',
                     max_iter=None, tol=1e-3, verbose=0):
    """Solve the ridge equation by the method of normal equations.

    Read more in the :ref:`User Guide <ridge_regression>`.

    Parameters
    ----------
    X : {array-like, sparse matrix, LinearOperator},
        shape = [n_samples, n_features]
        Training data

    y : array-like, shape = [n_samples] or [n_samples, n_targets]
        Target values

    alpha : {float, array-like},
        shape = [n_targets] if array-like
        The l_2 penalty to be used. If an array is passed, penalties are
        assumed to be specific to targets

    max_iter : int, optional
        Maximum number of iterations for conjugate gradient solver.
        The default value is determined by scipy.sparse.linalg.

    sample_weight : float or numpy array of shape [n_samples]
        Individual weights for each sample. If sample_weight is set, then
        the solver will automatically be set to 'cholesky'

    solver : {'auto', 'svd', 'cholesky', 'lsqr', 'sparse_cg'}
        Solver to use in the computational routines:

        - 'auto' chooses the solver automatically based on the type of data.

        - 'svd' uses a Singular Value Decomposition of X to compute the Ridge
          coefficients. More stable for singular matrices than
          'cholesky'.

        - 'cholesky' uses the standard scipy.linalg.solve function to
          obtain a closed-form solution via a Cholesky decomposition of
          dot(X.T, X)

        - 'sparse_cg' uses the conjugate gradient solver as found in
          scipy.sparse.linalg.cg. As an iterative algorithm, this solver is
          more appropriate than 'cholesky' for large-scale data
          (possibility to set `tol` and `max_iter`).

        - 'lsqr' uses the dedicated regularized least-squares routine
          scipy.sparse.linalg.lsqr. It is the fatest but may not be available
          in old scipy versions. It also uses an iterative procedure.

        All three solvers support both dense and sparse data.

    tol : float
        Precision of the solution.

    verbose : int
        Verbosity level. Setting verbose > 0 will display additional information
        depending on the solver used.

    Returns
    -------
    coef : array, shape = [n_features] or [n_targets, n_features]
        Weight vector(s).

    Notes
    -----
    This function won't compute the intercept.
    """

    n_samples, n_features = X.shape

    if y.ndim > 2:
        raise ValueError("Target y has the wrong shape %s" % str(y.shape))

    ravel = False
    if y.ndim == 1:
        y = y.reshape(-1, 1)
        ravel = True

    n_samples_, n_targets = y.shape

    if n_samples != n_samples_:
        raise ValueError("Number of samples in X and y does not correspond:"
                         " %d != %d" % (n_samples, n_samples_))

    has_sw = sample_weight is not None

    if solver == 'auto':
        # cholesky if it's a dense array and cg in
        # any other case
        if not sparse.issparse(X) or has_sw:
            solver = 'cholesky'
        else:
            solver = 'sparse_cg'

    elif solver == 'lsqr' and not hasattr(sp_linalg, 'lsqr'):
        warnings.warn("""lsqr not available on this machine, falling back
                      to sparse_cg.""")
        solver = 'sparse_cg'

    if has_sw:
        if np.atleast_1d(sample_weight).ndim > 1:
            raise ValueError("Sample weights must be 1D array or scalar")

        # Sample weight can be implemented via a simple rescaling.
        X, y = _rescale_data(X, y, sample_weight)

    # There should be either 1 or n_targets penalties
    alpha = np.asarray(alpha).ravel()
    if alpha.size not in [1, n_targets]:
        raise ValueError("Number of targets and number of penalties "
                         "do not correspond: %d != %d"
                         % (alpha.size, n_targets))

    if alpha.size == 1 and n_targets > 1:
        alpha = np.repeat(alpha, n_targets)

    if solver not in ('sparse_cg', 'cholesky', 'svd', 'lsqr'):
        raise ValueError('Solver %s not understood' % solver)

    if solver == 'sparse_cg':
        coef = _solve_sparse_cg(X, y, alpha, max_iter, tol, verbose)

    elif solver == "lsqr":
        coef = _solve_lsqr(X, y, alpha, max_iter, tol)

    elif solver == 'cholesky':
        if n_features > n_samples:
            K = safe_sparse_dot(X, X.T, dense_output=True)
            try:
                dual_coef = _solve_cholesky_kernel(K, y, alpha)

                coef = safe_sparse_dot(X.T, dual_coef, dense_output=True).T
            except linalg.LinAlgError:
                # use SVD solver if matrix is singular
                solver = 'svd'

        else:
            try:
                coef = _solve_cholesky(X, y, alpha)
            except linalg.LinAlgError:
                # use SVD solver if matrix is singular
                solver = 'svd'

    if solver == 'svd':
        if sparse.issparse(X):
            raise TypeError('SVD solver does not support sparse'
                            ' inputs currently')
        coef = _solve_svd(X, y, alpha)

    if ravel:
        # When y was passed as a 1d-array, we flatten the coefficients.
        coef = coef.ravel()

    return coef

class _BaseRidge(six.with_metaclass(ABCMeta, LinearModel)):

    @abstractmethod
    def __init__(self, alpha=1.0, fit_intercept=True, normalize=False,
                 copy_X=True, max_iter=None, tol=1e-3, solver="auto"):
        self.alpha = alpha
        self.fit_intercept = fit_intercept
        self.normalize = normalize
        self.copy_X = copy_X
        self.max_iter = max_iter
        self.tol = tol
        self.solver = solver

    def fit(self, X, y, sample_weight=None):
        X, y = check_X_y(X, y, ['csr', 'csc', 'coo'], dtype=np.float,
                         multi_output=True, y_numeric=True)

        if ((sample_weight is not None) and
                np.atleast_1d(sample_weight).ndim > 1):
            raise ValueError("Sample weights must be 1D array or scalar")

        X, y, X_mean, y_mean, X_std = self._center_data(
            X, y, self.fit_intercept, self.normalize, self.copy_X,
            sample_weight=sample_weight)

        self.coef_ = ridge_regression(X, y,
                                      alpha=self.alpha,
                                      sample_weight=sample_weight,
                                      max_iter=self.max_iter,
                                      tol=self.tol,
                                      solver=self.solver)
        self._set_intercept(X_mean, y_mean, X_std)
        return self

class Ridge(_BaseRidge, RegressorMixin):
    """Linear least squares with l2 regularization.

    This model solves a regression model where the loss function is
    the linear least squares function and regularization is given by
    the l2-norm. Also known as Ridge Regression or Tikhonov regularization.
    This estimator has built-in support for multi-variate regression
    (i.e., when y is a 2d-array of shape [n_samples, n_targets]).

    Read more in the :ref:`User Guide <ridge_regression>`.

    Parameters
    ----------
    alpha : {float, array-like}
        shape = [n_targets]
        Small positive values of alpha improve the conditioning of the problem
        and reduce the variance of the estimates.  Alpha corresponds to
        ``(2*C)^-1`` in other linear models such as LogisticRegression or
        LinearSVC. If an array is passed, penalties are assumed to be specific
        to the targets. Hence they must correspond in number.

    copy_X : boolean, optional, default True
        If True, X will be copied; else, it may be overwritten.

    fit_intercept : boolean
        Whether to calculate the intercept for this model. If set
        to false, no intercept will be used in calculations
        (e.g. data is expected to be already centered).

    max_iter : int, optional
        Maximum number of iterations for conjugate gradient solver.
        The default value is determined by scipy.sparse.linalg.

    normalize : boolean, optional, default False
        If True, the regressors X will be normalized before regression.

    solver : {'auto', 'svd', 'cholesky', 'lsqr', 'sparse_cg'}
        Solver to use in the computational routines:

        - 'auto' chooses the solver automatically based on the type of data.

        - 'svd' uses a Singular Value Decomposition of X to compute the Ridge
          coefficients. More stable for singular matrices than
          'cholesky'.

        - 'cholesky' uses the standard scipy.linalg.solve function to
          obtain a closed-form solution.

        - 'sparse_cg' uses the conjugate gradient solver as found in
          scipy.sparse.linalg.cg. As an iterative algorithm, this solver is
          more appropriate than 'cholesky' for large-scale data
          (possibility to set `tol` and `max_iter`).

        - 'lsqr' uses the dedicated regularized least-squares routine
          scipy.sparse.linalg.lsqr. It is the fatest but may not be available
          in old scipy versions. It also uses an iterative procedure.

        All three solvers support both dense and sparse data.

    tol : float
        Precision of the solution.

    Attributes
    ----------
    coef_ : array, shape = [n_features] or [n_targets, n_features]
        Weight vector(s).

    See also
    --------
    RidgeClassifier, RidgeCV, KernelRidge

    Examples
    --------
    >>> from sklearn.linear_model import Ridge
    >>> import numpy as np
    >>> n_samples, n_features = 10, 5
    >>> np.random.seed(0)
    >>> y = np.random.randn(n_samples)
    >>> X = np.random.randn(n_samples, n_features)
    >>> clf = Ridge(alpha=1.0)
    >>> clf.fit(X, y) # doctest: +NORMALIZE_WHITESPACE
    Ridge(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=None,
          normalize=False, solver='auto', tol=0.001)
    """
    def __init__(self, alpha=1.0, fit_intercept=True, normalize=False,
                 copy_X=True, max_iter=None, tol=1e-3, solver="auto"):
        super(Ridge, self).__init__(alpha=alpha, fit_intercept=fit_intercept,
                                    normalize=normalize, copy_X=copy_X,
                                    max_iter=max_iter, tol=tol, solver=solver)

    def fit(self, X, y, sample_weight=None):
        """Fit Ridge regression model

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Training data

        y : array-like, shape = [n_samples] or [n_samples, n_targets]
            Target values

        sample_weight : float or numpy array of shape [n_samples]
            Individual weights for each sample

        Returns
        -------
        self : returns an instance of self.
        """
        return super(Ridge, self).fit(X, y, sample_weight=sample_weight)

class RidgeClassifier(LinearClassifierMixin, _BaseRidge):
    """Classifier using Ridge regression.

    Read more in the :ref:`User Guide <ridge_regression>`.

    Parameters
    ----------
    alpha : float
        Small positive values of alpha improve the conditioning of the problem
        and reduce the variance of the estimates.  Alpha corresponds to
        ``(2*C)^-1`` in other linear models such as LogisticRegression or
        LinearSVC.

    class_weight : dict or 'balanced', optional
        Weights associated with classes in the form ``{class_label: weight}``.
        If not given, all classes are supposed to have weight one.

        The "balanced" mode uses the values of y to automatically adjust
        weights inversely proportional to class frequencies in the input data
        as ``n_samples / (n_classes * np.bincount(y))``

    copy_X : boolean, optional, default True
        If True, X will be copied; else, it may be overwritten.

    fit_intercept : boolean
        Whether to calculate the intercept for this model. If set to false, no
        intercept will be used in calculations (e.g. data is expected to be
        already centered).

    max_iter : int, optional
        Maximum number of iterations for conjugate gradient solver.
        The default value is determined by scipy.sparse.linalg.

    normalize : boolean, optional, default False
        If True, the regressors X will be normalized before regression.

    solver : {'auto', 'svd', 'cholesky', 'lsqr', 'sparse_cg'}
        Solver to use in the computational
        routines. 'svd' will use a Singular value decomposition to obtain
        the solution, 'cholesky' will use the standard
        scipy.linalg.solve function, 'sparse_cg' will use the
        conjugate gradient solver as found in
        scipy.sparse.linalg.cg while 'auto' will chose the most
        appropriate depending on the matrix X. 'lsqr' uses
        a direct regularized least-squares routine provided by scipy.

    tol : float
        Precision of the solution.

    Attributes
    ----------
    coef_ : array, shape = [n_features] or [n_classes, n_features]
        Weight vector(s).

    See also
    --------
    Ridge, RidgeClassifierCV

    Notes
    -----
    For multi-class classification, n_class classifiers are trained in
    a one-versus-all approach. Concretely, this is implemented by taking
    advantage of the multi-variate response support in Ridge.
    """
    def __init__(self, alpha=1.0, fit_intercept=True, normalize=False,
                 copy_X=True, max_iter=None, tol=1e-3, class_weight=None,
                 solver="auto"):
        super(RidgeClassifier, self).__init__(
            alpha=alpha, fit_intercept=fit_intercept, normalize=normalize,
            copy_X=copy_X, max_iter=max_iter, tol=tol, solver=solver)
        self.class_weight = class_weight

    def fit(self, X, y, sample_weight=None):
        """Fit Ridge regression model.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples,n_features]
            Training data

        y : array-like, shape = [n_samples]
            Target values

        sample_weight : float or numpy array of shape (n_samples,)
            Sample weight.

        Returns
        -------
        self : returns an instance of self.
        """
        self._label_binarizer = LabelBinarizer(pos_label=1, neg_label=-1)
        Y = self._label_binarizer.fit_transform(y)
        if not self._label_binarizer.y_type_.startswith('multilabel'):
            y = column_or_1d(y, warn=True)

        if self.class_weight:
            if sample_weight is None:
                sample_weight = 1.
            # modify the sample weights with the corresponding class weight
            sample_weight = (sample_weight *
                             compute_sample_weight(self.class_weight, y))

        super(RidgeClassifier, self).fit(X, Y, sample_weight=sample_weight)
        return self

    @property
    def classes_(self):
        return self._label_binarizer.classes_

class _RidgeGCV(LinearModel):
    """Ridge regression with built-in Generalized Cross-Validation

    It allows efficient Leave-One-Out cross-validation.

    This class is not intended to be used directly. Use RidgeCV instead.

    Notes
    -----

    We want to solve (K + alpha*Id)c = y,
    where K = X X^T is the kernel matrix.

    Let G = (K + alpha*Id)^-1.

    Dual solution: c = Gy
    Primal solution: w = X^T c

    Compute eigendecomposition K = Q V Q^T.
    Then G = Q (V + alpha*Id)^-1 Q^T,
    where (V + alpha*Id) is diagonal.
    It is thus inexpensive to inverse for many alphas.

    Let loov be the vector of prediction values for each example
    when the model was fitted with all examples but this example.

    loov = (KGY - diag(KG)Y) / diag(I-KG)

    Let looe be the vector of prediction errors for each example
    when the model was fitted with all examples but this example.

    looe = y - loov = c / diag(G)

    References
    ----------
    http://cbcl.mit.edu/projects/cbcl/publications/ps/MIT-CSAIL-TR-2007-025.pdf
    http://www.mit.edu/~9.520/spring07/Classes/rlsslides.pdf
    """

    def __init__(self, alphas=(0.1, 1.0, 10.0),
                 fit_intercept=True, normalize=False,
                 scoring=None, copy_X=True,
                 gcv_mode=None, store_cv_values=False):
        self.alphas = np.asarray(alphas)
        self.fit_intercept = fit_intercept
        self.normalize = normalize
        self.scoring = scoring
        self.copy_X = copy_X
        self.gcv_mode = gcv_mode
        self.store_cv_values = store_cv_values

    def _pre_compute(self, X, y):
        # even if X is very sparse, K is usually very dense
        K = safe_sparse_dot(X, X.T, dense_output=True)
        v, Q = linalg.eigh(K)
        QT_y = np.dot(Q.T, y)
        return v, Q, QT_y

    def _decomp_diag(self, v_prime, Q):
        # compute diagonal of the matrix: dot(Q, dot(diag(v_prime), Q^T))
        return (v_prime * Q ** 2).sum(axis=-1)

    def _diag_dot(self, D, B):
        # compute dot(diag(D), B)
        if len(B.shape) > 1:
            # handle case where B is > 1-d
            D = D[(slice(None), ) + (np.newaxis, ) * (len(B.shape) - 1)]
        return D * B

    def _errors(self, alpha, y, v, Q, QT_y):
        # don't construct matrix G, instead compute action on y & diagonal
        w = 1.0 / (v + alpha)
        c = np.dot(Q, self._diag_dot(w, QT_y))
        G_diag = self._decomp_diag(w, Q)
        # handle case where y is 2-d
        if len(y.shape) != 1:
            G_diag = G_diag[:, np.newaxis]
        return (c / G_diag) ** 2, c

    def _values(self, alpha, y, v, Q, QT_y):
        # don't construct matrix G, instead compute action on y & diagonal
        w = 1.0 / (v + alpha)
        c = np.dot(Q, self._diag_dot(w, QT_y))
        G_diag = self._decomp_diag(w, Q)
        # handle case where y is 2-d
        if len(y.shape) != 1:
            G_diag = G_diag[:, np.newaxis]
        return y - (c / G_diag), c

    def _pre_compute_svd(self, X, y):
        if sparse.issparse(X):
            raise TypeError("SVD not supported for sparse matrices")
        U, s, _ = linalg.svd(X, full_matrices=0)
        v = s ** 2
        UT_y = np.dot(U.T, y)
        return v, U, UT_y

    def _errors_svd(self, alpha, y, v, U, UT_y):
        w = ((v + alpha) ** -1) - (alpha ** -1)
        c = np.dot(U, self._diag_dot(w, UT_y)) + (alpha ** -1) * y
        G_diag = self._decomp_diag(w, U) + (alpha ** -1)
        if len(y.shape) != 1:
            # handle case where y is 2-d
            G_diag = G_diag[:, np.newaxis]
        return (c / G_diag) ** 2, c

    def _values_svd(self, alpha, y, v, U, UT_y):
        w = ((v + alpha) ** -1) - (alpha ** -1)
        c = np.dot(U, self._diag_dot(w, UT_y)) + (alpha ** -1) * y
        G_diag = self._decomp_diag(w, U) + (alpha ** -1)
        if len(y.shape) != 1:
            # handle case when y is 2-d
            G_diag = G_diag[:, np.newaxis]
        return y - (c / G_diag), c

    def fit(self, X, y, sample_weight=None):
        """Fit Ridge regression model

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Training data

        y : array-like, shape = [n_samples] or [n_samples, n_targets]
            Target values

        sample_weight : float or array-like of shape [n_samples]
            Sample weight

        Returns
        -------
        self : Returns self.
        """
        X, y = check_X_y(X, y, ['csr', 'csc', 'coo'], dtype=np.float,
                         multi_output=True, y_numeric=True)

        n_samples, n_features = X.shape

        X, y, X_mean, y_mean, X_std = LinearModel._center_data(
            X, y, self.fit_intercept, self.normalize, self.copy_X,
            sample_weight=sample_weight)

        gcv_mode = self.gcv_mode
        with_sw = len(np.shape(sample_weight))

        if gcv_mode is None or gcv_mode == 'auto':
            if sparse.issparse(X) or n_features > n_samples or with_sw:
                gcv_mode = 'eigen'
            else:
                gcv_mode = 'svd'
        elif gcv_mode == "svd" and with_sw:
            # FIXME non-uniform sample weights not yet supported
            warnings.warn("non-uniform sample weights unsupported for svd, "
                          "forcing usage of eigen")
            gcv_mode = 'eigen'

        if gcv_mode == 'eigen':
            _pre_compute = self._pre_compute
            _errors = self._errors
            _values = self._values
        elif gcv_mode == 'svd':
            # assert n_samples >= n_features
            _pre_compute = self._pre_compute_svd
            _errors = self._errors_svd
            _values = self._values_svd
        else:
            raise ValueError('bad gcv_mode "%s"' % gcv_mode)

        v, Q, QT_y = _pre_compute(X, y)
        n_y = 1 if len(y.shape) == 1 else y.shape[1]
        cv_values = np.zeros((n_samples * n_y, len(self.alphas)))
        C = []

        scorer = check_scoring(self, scoring=self.scoring, allow_none=True)
        error = scorer is None

        for i, alpha in enumerate(self.alphas):
            weighted_alpha = (sample_weight * alpha
                              if sample_weight is not None
                              else alpha)
            if error:
                out, c = _errors(weighted_alpha, y, v, Q, QT_y)
            else:
                out, c = _values(weighted_alpha, y, v, Q, QT_y)
            cv_values[:, i] = out.ravel()
            C.append(c)

        if error:
            best = cv_values.mean(axis=0).argmin()
        else:
            # The scorer want an object that will make the predictions but
            # they are already computed efficiently by _RidgeGCV. This
            # identity_estimator will just return them
            def identity_estimator():
                pass
            identity_estimator.decision_function = lambda y_predict: y_predict
            identity_estimator.predict = lambda y_predict: y_predict

            out = [scorer(identity_estimator, y.ravel(), cv_values[:, i])
                   for i in range(len(self.alphas))]
            best = np.argmax(out)

        self.alpha_ = self.alphas[best]
        self.dual_coef_ = C[best]
        self.coef_ = safe_sparse_dot(self.dual_coef_.T, X)

        self._set_intercept(X_mean, y_mean, X_std)

        if self.store_cv_values:
            if len(y.shape) == 1:
                cv_values_shape = n_samples, len(self.alphas)
            else:
                cv_values_shape = n_samples, n_y, len(self.alphas)
            self.cv_values_ = cv_values.reshape(cv_values_shape)

        return self

class _BaseRidgeCV(LinearModel):
    def __init__(self, alphas=(0.1, 1.0, 10.0),
                 fit_intercept=True, normalize=False, scoring=None,
                 cv=None, gcv_mode=None,
                 store_cv_values=False):
        self.alphas = alphas
        self.fit_intercept = fit_intercept
        self.normalize = normalize
        self.scoring = scoring
        self.cv = cv
        self.gcv_mode = gcv_mode
        self.store_cv_values = store_cv_values

    def fit(self, X, y, sample_weight=None):
        """Fit Ridge regression model

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
            Training data

        y : array-like, shape = [n_samples] or [n_samples, n_targets]
            Target values

        sample_weight : float or array-like of shape [n_samples]
            Sample weight

        Returns
        -------
        self : Returns self.
        """
        if self.cv is None:
            estimator = _RidgeGCV(self.alphas,
                                  fit_intercept=self.fit_intercept,
                                  normalize=self.normalize,
                                  scoring=self.scoring,
                                  gcv_mode=self.gcv_mode,
                                  store_cv_values=self.store_cv_values)
            estimator.fit(X, y, sample_weight=sample_weight)
            self.alpha_ = estimator.alpha_
            if self.store_cv_values:
                self.cv_values_ = estimator.cv_values_
        else:
            if self.store_cv_values:
                raise ValueError("cv!=None and store_cv_values=True "
                                 " are incompatible")
            parameters = {'alpha': self.alphas}
            fit_params = {'sample_weight' : sample_weight}
            gs = GridSearchCV(Ridge(fit_intercept=self.fit_intercept),
                              parameters, fit_params=fit_params, cv=self.cv)
            gs.fit(X, y)
            estimator = gs.best_estimator_
            self.alpha_ = gs.best_estimator_.alpha

        self.coef_ = estimator.coef_
        self.intercept_ = estimator.intercept_

        return self

class RidgeCV(_BaseRidgeCV, RegressorMixin):
    """Ridge regression with built-in cross-validation.

    By default, it performs Generalized Cross-Validation, which is a form of
    efficient Leave-One-Out cross-validation.

    Read more in the :ref:`User Guide <ridge_regression>`.

    Parameters
    ----------
    alphas : numpy array of shape [n_alphas]
        Array of alpha values to try.
        Small positive values of alpha improve the conditioning of the
        problem and reduce the variance of the estimates.
        Alpha corresponds to ``(2*C)^-1`` in other linear models such as
        LogisticRegression or LinearSVC.

    fit_intercept : boolean
        Whether to calculate the intercept for this model. If set
        to false, no intercept will be used in calculations
        (e.g. data is expected to be already centered).

    normalize : boolean, optional, default False
        If True, the regressors X will be normalized before regression.

    scoring : string, callable or None, optional, default: None
        A string (see model evaluation documentation) or
        a scorer callable object / function with signature
        ``scorer(estimator, X, y)``.

    cv : integer or cross-validation generator, optional
        If None, Generalized Cross-Validation (efficient Leave-One-Out)
        will be used.
        If an integer is passed, it is the number of folds for KFold cross
        validation.  Specific cross-validation objects can be passed, see
        sklearn.cross_validation module for the list of possible objects

    gcv_mode : {None, 'auto', 'svd', eigen'}, optional
        Flag indicating which strategy to use when performing
        Generalized Cross-Validation. Options are::

            'auto' : use svd if n_samples > n_features or when X is a sparse
                     matrix, otherwise use eigen
            'svd' : force computation via singular value decomposition of X
                    (does not work for sparse matrices)
            'eigen' : force computation via eigendecomposition of X^T X

        The 'auto' mode is the default and is intended to pick the cheaper
        option of the two depending upon the shape and format of the training
        data.

    store_cv_values : boolean, default=False
        Flag indicating if the cross-validation values corresponding to
        each alpha should be stored in the `cv_values_` attribute (see
        below). This flag is only compatible with `cv=None` (i.e. using
        Generalized Cross-Validation).

    Attributes
    ----------
    cv_values_ : array, shape = [n_samples, n_alphas] or \
        shape = [n_samples, n_targets, n_alphas], optional
        Cross-validation values for each alpha (if `store_cv_values=True` and \
        `cv=None`). After `fit()` has been called, this attribute will \
        contain the mean squared errors (by default) or the values of the \
        `{loss,score}_func` function (if provided in the constructor).

    coef_ : array, shape = [n_features] or [n_targets, n_features]
        Weight vector(s).

    alpha_ : float
        Estimated regularization parameter.

    intercept_ : float | array, shape = (n_targets,)
        Independent term in decision function. Set to 0.0 if
        ``fit_intercept = False``.

    See also
    --------
    Ridge: Ridge regression
    RidgeClassifier: Ridge classifier
    RidgeClassifierCV: Ridge classifier with built-in cross validation
    """
    pass

class RidgeClassifierCV(LinearClassifierMixin, _BaseRidgeCV):
    """Ridge classifier with built-in cross-validation.

    By default, it performs Generalized Cross-Validation, which is a form of
    efficient Leave-One-Out cross-validation. Currently, only the n_features >
    n_samples case is handled efficiently.

    Read more in the :ref:`User Guide <ridge_regression>`.

    Parameters
    ----------
    alphas : numpy array of shape [n_alphas]
        Array of alpha values to try.
        Small positive values of alpha improve the conditioning of the
        problem and reduce the variance of the estimates.
        Alpha corresponds to ``(2*C)^-1`` in other linear models such as
        LogisticRegression or LinearSVC.

    fit_intercept : boolean
        Whether to calculate the intercept for this model. If set
        to false, no intercept will be used in calculations
        (e.g. data is expected to be already centered).

    normalize : boolean, optional, default False
        If True, the regressors X will be normalized before regression.

    scoring : string, callable or None, optional, default: None
        A string (see model evaluation documentation) or
        a scorer callable object / function with signature
        ``scorer(estimator, X, y)``.

    cv : cross-validation generator, optional
        If None, Generalized Cross-Validation (efficient Leave-One-Out)
        will be used.

    class_weight : dict or 'balanced', optional
        Weights associated with classes in the form ``{class_label: weight}``.
        If not given, all classes are supposed to have weight one.

        The "balanced" mode uses the values of y to automatically adjust
        weights inversely proportional to class frequencies in the input data
        as ``n_samples / (n_classes * np.bincount(y))``

    Attributes
    ----------
    cv_values_ : array, shape = [n_samples, n_alphas] or \
    shape = [n_samples, n_responses, n_alphas], optional
        Cross-validation values for each alpha (if `store_cv_values=True` and
    `cv=None`). After `fit()` has been called, this attribute will contain \
    the mean squared errors (by default) or the values of the \
    `{loss,score}_func` function (if provided in the constructor).

    coef_ : array, shape = [n_features] or [n_targets, n_features]
        Weight vector(s).

    alpha_ : float
        Estimated regularization parameter

    See also
    --------
    Ridge: Ridge regression
    RidgeClassifier: Ridge classifier
    RidgeCV: Ridge regression with built-in cross validation

    Notes
    -----
    For multi-class classification, n_class classifiers are trained in
    a one-versus-all approach. Concretely, this is implemented by taking
    advantage of the multi-variate response support in Ridge.
    """
    def __init__(self, alphas=(0.1, 1.0, 10.0), fit_intercept=True,
                 normalize=False, scoring=None, cv=None, class_weight=None):
        super(RidgeClassifierCV, self).__init__(
            alphas=alphas, fit_intercept=fit_intercept, normalize=normalize,
            scoring=scoring, cv=cv)
        self.class_weight = class_weight

    def fit(self, X, y, sample_weight=None):
        """Fit the ridge classifier.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            Training vectors, where n_samples is the number of samples
            and n_features is the number of features.

        y : array-like, shape (n_samples,)
            Target values.

        sample_weight : float or numpy array of shape (n_samples,)
            Sample weight.

        Returns
        -------
        self : object
            Returns self.
        """
        self._label_binarizer = LabelBinarizer(pos_label=1, neg_label=-1)
        Y = self._label_binarizer.fit_transform(y)
        if not self._label_binarizer.y_type_.startswith('multilabel'):
            y = column_or_1d(y, warn=True)

        if self.class_weight:
            if sample_weight is None:
                sample_weight = 1.
            # modify the sample weights with the corresponding class weight
            sample_weight = (sample_weight *
                             compute_sample_weight(self.class_weight, y))

        _BaseRidgeCV.fit(self, X, Y, sample_weight=sample_weight)
        return self

    @property
    def classes_(self):
        return self._label_binarizer.classes_


"""
========================================
Lasso and Elastic Net for Sparse Signals
========================================

Estimates Lasso and Elastic-Net regression models on a manually generated
sparse signal corrupted with an additive noise. Estimated coefficients are
compared with the ground-truth.

"""
print(__doc__)

import numpy as np
import matplotlib.pyplot as plt

from sklearn.metrics import r2_score

###############################################################################
# generate some sparse data to play with
np.random.seed(42)

n_samples, n_features = 50, 200
X = np.random.randn(n_samples, n_features)
coef = 3 * np.random.randn(n_features)
inds = np.arange(n_features)
np.random.shuffle(inds)
coef[inds[10:]] = 0  # sparsify coef
y = np.dot(X, coef)

# add noise
y += 0.01 * np.random.normal((n_samples,))

# Split data in train set and test set
n_samples = X.shape[0]
X_train, y_train = X[:n_samples / 2], y[:n_samples / 2]
X_test, y_test = X[n_samples / 2:], y[n_samples / 2:]

###############################################################################
# Lasso
from sklearn.linear_model import Lasso

alpha = 0.1
lasso = Lasso(alpha=alpha)

y_pred_lasso = lasso.fit(X_train, y_train).predict(X_test)
r2_score_lasso = r2_score(y_test, y_pred_lasso)
print(lasso)
print("r^2 on test data : %f" % r2_score_lasso)

###############################################################################
# ElasticNet
from sklearn.linear_model import ElasticNet

enet = ElasticNet(alpha=alpha, l1_ratio=0.7)

y_pred_enet = enet.fit(X_train, y_train).predict(X_test)
r2_score_enet = r2_score(y_test, y_pred_enet)
print(enet)
print("r^2 on test data : %f" % r2_score_enet)

plt.plot(enet.coef_, color='lightgreen', linewidth=2,
         label='Elastic net coefficients')
plt.plot(lasso.coef_, color='gold', linewidth=2,
         label='Lasso coefficients')
plt.plot(coef, '--', color='navy', label='original coefficients')
plt.legend(loc='best')
plt.title("Lasso R^2: %f, Elastic Net R^2: %f"
          % (r2_score_lasso, r2_score_enet))
plt.show()


"""
Measure global optimization performance of Levy function
"""

import sys, time
import numpy as np
import matplotlib.pyplot as pt
import multiprocessing as mp
import dfibers.traversal as tv
import dfibers.numerical_utilities as nu
import dfibers.logging_utilities as lu
import dfibers.fixed_points as fx
import dfibers.solvers as sv
import dfibers.examples.levy as lv
from mpl_toolkits.mplot3d import Axes3D

def run_trial(args):
    basename, sample, timeout = args
    stop_time = time.clock() + timeout

    logfile = open("%s_s%d.log"%(basename,sample),"w")

    # Set up fiber arguments
    np.random.seed()
    v = 20*np.random.rand(2,1) - 10 # random point in domain
    c = lv.f(v) # direction at that point
    c = c + 0.1*np.random.randn(2,1) # perturb for more variability
    fiber_kwargs = {
        "f": lv.f,
        "ef": lv.ef,
        "Df": lv.Df,
        "compute_step_amount": lambda trace: (0.0001, 0),
        "v": v,
        "c": c,
        "stop_time": stop_time,
        "terminate": lambda trace: (np.fabs(trace.x[:-1]) > 10).any(),
        "max_solve_iterations": 2**5,
    }

    solve_start = time.clock()

    # Run in one direction
    solution = sv.fiber_solver(
        logger=lu.Logger(logfile).plus_prefix("+: "),
        **fiber_kwargs)
    X1 = np.concatenate(solution["Fiber trace"].points, axis=1)
    V1 = solution["Fixed points"]
    z = solution["Fiber trace"].z_initial
    # print("Status: %s\n"%solution["Fiber trace"].status)

    # Run in other direction (negate initial tangent)
    solution = sv.fiber_solver(
        z= -z,
        logger=lu.Logger(logfile).plus_prefix("-: "),
        **fiber_kwargs)
    X2 = np.concatenate(solution["Fiber trace"].points, axis=1)
    V2 = solution["Fixed points"]
    # print("Status: %s\n"%solution["Fiber trace"].status)

    # Join fiber segments
    fiber = np.concatenate((np.fliplr(X1), X2), axis=1)

    # Union solutions
    fxpts = fx.sanitize_points(
        np.concatenate((V1, V2), axis=1),
        f = lv.f,
        ef = lv.ef,
        Df = lv.Df,
        duplicates = lambda V, v: (np.fabs(V - v) < 10**-6).all(axis=0),
    )

    # Save results
    with open("%s_s%d.npz"%(basename,sample), 'w') as rf: np.savez(rf, **{
        "fxpts": fxpts,
        "fiber": fiber,
        "runtime": time.clock() - solve_start })

    logfile.close()

def run_experiment(basename, num_samples, timeout, num_procs=0):

    pool_args = []
    for sample in range(num_samples):
        pool_args.append((basename, sample, timeout))

    if num_procs > 0:

        num_procs = min(num_procs, mp.cpu_count())
        print("using %d processes..."%num_procs)
        pool = mp.Pool(processes=num_procs)
        pool.map(run_trial, pool_args)
        pool.close()
        pool.join()

    else:

        for pa in pool_args: run_trial(pa)

def compile_results(basename, num_samples):

    L = []
    F = []
    runtimes = []
    for sample in range(num_samples):
        with open("%s_s%d.npz"%(basename,sample), 'r') as rf: data = dict(np.load(rf))
        fxpts = data["fxpts"]
        Fs = np.fabs(lv.f(fxpts)).max(axis=0)
        Ls = lv.levy(fxpts)
        within = (np.fabs(fxpts) < 10).all(axis=0)
        mean_within = Ls[within].mean() if within.any() else np.nan
        print("sample %d: %d secs, %d solns, mean %f, mean within %f, min %f"%(
            sample, data["runtime"], len(Ls), Ls.mean(), mean_within, Ls.min()))
        L.append(Ls)
        F.append(Fs)
        runtimes.append(data["runtime"])

    counts = np.array([len(Ls) for Ls in L])
    bests = np.array([Ls.min() for Ls in L])
    resids = np.array([Fs.max() for Fs in F])
    runtimes = np.array(runtimes)
    print("avg count = %d, avg best = %f, avg resid = %f, best best = %f"%(
        counts.mean(), bests.mean(), resids.mean(), bests.min()))
    return counts, bests, runtimes

def plot_results(basename, num_samples, counts, bests, runtimes, timeout):

    ### Optimization order stats
    pt.figure(figsize=(5,4))
    pt.subplot(2,1,1)
    pt.plot(np.sort(bests), '-k.')
    pt.xlabel("Ordered samples")
    pt.ylabel("Best objective value")

    ##### Work complexity
    pt.subplot(2,1,2)
    terms = (runtimes < timeout)
    pt.plot(runtimes[terms], bests[terms], 'k+', markerfacecolor='none')
    pt.plot(runtimes[~terms], bests[~terms], 'ko', markerfacecolor='none')
    pt.legend(["terminated","timed out"])
    pt.xlabel("Runtime (seconds)")
    pt.ylabel("Best objective value")

    pt.tight_layout()
    pt.show()

    ### Fiber visuals

    pt.figure(figsize=(4,7))
    # objective fun
    X_surface, Y_surface = np.mgrid[-10:10:100j,-10:10:100j]
    L = lv.levy(np.array([X_surface.flatten(), Y_surface.flatten()])).reshape(X_surface.shape)
    ax_surface = pt.gcf().add_subplot(2,1,1,projection="3d")
    ax_surface.plot_surface(X_surface, Y_surface, L, linewidth=0, antialiased=False, color='gray')
    ax_surface.set_xlabel("v0")
    ax_surface.set_ylabel("v1")
    ax_surface.set_zlabel("levy(v)")
    ax_surface.view_init(azim=-80, elev=20)

    # fibers
    ax = pt.gcf().add_subplot(2,1,2)
    X_grid, Y_grid = np.mgrid[-10:10:60j,-10:10:60j]
    XY = np.array([X_grid.flatten(), Y_grid.flatten()])
    C_XY = lv.f(XY)
    ax.quiver(XY[0,:],XY[1,:],C_XY[0,:],C_XY[1,:],color=0.5*np.ones((1,3)),
        scale=10,units='xy',angles='xy')

    num_plot_samples = 3
    sort_idx = np.argsort(bests)
    plot_idx = [0] + list(np.random.permutation(num_samples)[:num_plot_samples-1])
    samples = sort_idx[plot_idx]
    # samples = [41,73,20] # all through global
    # samples = [41, 97, 11] # two through global
    # samples = [41, 49, 13] # two through global, one horiz not through
    # samples = [41, 46, 70] # one through global, one horiz
    # samples = [41, 96, 27] # two through global, one almost horiz
    samples = [41, 63, 28] # two through global, all interesting
    print("samples:")
    print(samples)
    for i,sample in enumerate(samples[::-1]):
        with open("%s_s%d.npz"%(basename,sample), 'r') as rf: data = dict(np.load(rf))
        fxpts = data["fxpts"]
        fiber = data["fiber"][:,::]

        L = lv.levy(fxpts).min()
        col = 0.5*float(num_plot_samples-i-1)/num_plot_samples
        print(sample,col)
        ax.plot(fiber[0],fiber[1],color=(col,col,col,1), linestyle='-', linewidth=1)
        pt.plot(fxpts[0],fxpts[1], 'o', color=(col,col,col,1))

    pt.xlabel("v0")
    pt.ylabel("v1",rotation=0)
    pt.yticks(np.linspace(-10,10,5))
    pt.xlim([-10,10])
    pt.ylim([-10,10])
    pt.tight_layout()
    pt.show()

if __name__ == "__main__":

    basename = "levy_opt"
    num_samples = 100
    num_plot_samples = 3
    timeout = 60*30
    num_procs = 10

    # run_experiment(basename, num_samples=num_samples, timeout=timeout, num_procs=num_procs)
    counts, bests, runtimes = compile_results(basename, num_samples)
    plot_results(basename, num_samples, counts, bests, runtimes, timeout)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Watermarkero, Mario, Ariel
"""

from PIL import Image
import random
import matplotlib.pyplot as plt
import numpy as np

def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

def marcarPixel(color, bitporinsertar):
    if (color%2)==1:
        if bitporinsertar==0:
            color=color-1
    elif (color%2)==0:
        if bitporinsertar==1:
            color=color+1
    return color

def plotLsbRgb(img):
    fig, (ax1, ax2) = plt.subplots(2, 1)
    ax1.set_title('Imagen RGB')
    ax1.imshow(img)
    ax2.set_title('LSB RGB')
    img=255*(img%2)
    ax2.imshow(img)
    plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10,
                        right=0.95, hspace=0.3,wspace=0.35)

#imagen original
path="img3.jpg"
imgOriginal = np.array(Image.open(path))
nFilas, nCols, nCanales = imgOriginal.shape
#marca
key=41196
random.seed(key)
porcentajeDeimagenPorMarcar=50
sizeMarca = nCols*int(porcentajeDeimagenPorMarcar*(nFilas/100))
#marca = [random.randint(0,1) for i in range(sizeMarca)]
plotLsbRgb(imgOriginal)

#proceso de marcado
imgMarcada = imgOriginal.copy();
cont = 1 #contador del numero de bits inscrustados
#Proceso de incrustacion
for fila in range(0,nFilas):
    for columna in range(0,nCols):
        pixel=imgOriginal[fila,columna]
        newPixel = [marcarPixel(
            pixel[0],random.randint(0,1)),
            marcarPixel(pixel[1],random.randint(0,1)),
            marcarPixel(pixel[2],random.randint(0,1))]
        imgMarcada[fila,columna] = newPixel
        if cont >= sizeMarca:
            break
        cont = cont +1
    if cont >= sizeMarca:
        break

plotLsbRgb(imgMarcada)
image = Image.fromarray(imgMarcada, 'RGB')
image.save('ImagenMarcada.bmp')
print('Porciento de la imagen marcada: ' + str(porcentajeDeimagenPorMarcar)+'%')
print('bits incrustados: ' + str(sizeMarca*3))
print('Bytes incrustados: ' + str(sizeMarca*3/8))
print('KiloBytes incrustados: ' + str(sizeMarca*3/8/1024))
print('MegaBytes incrustados: ' + str(sizeMarca*3/8/1024/1024))


import numpy as np
import sklearn.linear_model
import sklearn.tree
import Simulators, Logger, Evaluators, Semibandits, Metrics
import warnings
import argparse
import pickle
import sys

class RegretExp(object):
    def __init__(self, weight=None, link="linear", K=10, L=5, T=1000, dataset="synth", feat_noise=0.25, reward_noise=1.0, policies="finite", structure='none'):
        self.T = T
        self.K = K
        self.L = L
        if weight == None:
            weight = np.arange(1,self.L+1)
        self.weight = weight
        self.link = link
        self.feat_noise = feat_noise
        self.reward_noise = reward_noise
        self.dataset = dataset
        self.policies = policies
        self.structure = structure
        if self.dataset == "synth":
            print("----Generating Semibandit Simulator----")
            self.Sim = Simulators.OrderedSBSim(100,100,self.K,
                                               self.L,self.feat_noise,
                                               w_vec=self.weight,
                                               link=self.link,
                                               one_pass=False)
            print("----Done----")
        elif self.dataset == "mq2007":
            print("----Generating MQ2007 Simulator----")
            self.Sim = Simulators.DatasetBandit(self.L,loop=True,
                                                dataset='mq2007',
                                                metric=Metrics.NavigationalTTS,
                                                ## metric=Metrics.NDCG,
                                                structure=self.structure)
            if self.policies == "finite":
                trees = pickle.load(open('./mq2007_trees.pkl', 'rb'))
                self.Sim.set_policies(trees)

            print("----Done----")
        elif self.dataset == "mq2008":
            print("----Generating MQ2008 Simulator----")
            self.Sim = Simulators.DatasetBandit(self.L,loop=True,
                                                dataset='mq2008',
                                                metric=Metrics.NavigationalTTS,
                                                structure=self.structure)
            if self.policies == "finite":
                trees = pickle.load(open('./mq2008_trees.pkl', 'rb'))
                self.Sim.set_policies(trees)
            print("----Done----")
        elif self.dataset == 'yahoo':
            print("----Generating Yahoo Simulator----")
            self.Sim = Simulators.DatasetBandit(self.L,loop=True,
                                                dataset='yahoo',
                                                ## metric=Metrics.NDCG,
                                                metric=Metrics.NavigationalTTS,
                                                structure=self.structure)
            if self.policies == "finite":
                trees = pickle.load(open('./yahoo_trees.pkl', 'rb'))
                self.Sim.set_policies(trees)
            print("----Done----")
        else:
            print("Error invalid dataset")
            sys.exit(1)

    def run_alg(self, Alg, params={}):
        A = Alg(self.Sim)
        (reward, regret) = A.play(self.T,params=params,verbose=False)
        return (reward, regret)

if __name__=='__main__':
    warnings.simplefilter("ignore")
    parser = argparse.ArgumentParser()
    parser.add_argument('--T', action='store',
                        default=1000,
                        help='number of rounds')
    parser.add_argument('--link', action='store', choices=['linear', 'logistic'], default='linear')
    parser.add_argument('--dataset', action='store', choices=['synth','mq2007','mq2008', 'yahoo'])
    parser.add_argument('--policies', action='store', choices=['finite', 'tree', 'linear'], default='linear')
    parser.add_argument('--K', action='store', default=10)
    parser.add_argument('--L', action='store', default=5)
    parser.add_argument('--structure', action='store', default='none', choices=['none','cluster'])

    Args = parser.parse_args(sys.argv[1:])
    print(Args)
    Args.T = int(Args.T)
    Args.K = int(Args.K)
    Args.L = int(Args.L)

    weight = np.arange(1,Args.L+1)[::-1]  ## np.arange(1,Args.L+1,1)[::-1] ## /np.sum(np.arange(1,Args.L+1))

    Algs = {
        ## 'EELS': Semibandits.EELS,
        'EELS2': Semibandits.EELS2,
        ## 'Eps': Semibandits.EpsGreedy,
        'EpsOracle': Semibandits.EpsGreedy,
        ## 'Random': Semibandits.Semibandit
        }

    Params = {
        'EELS': {
            'link': Args.link,
            },
        'EELS2': {
            'link': Args.link,
            },
        'Eps': {
            'reward': True,
            },
        'EpsOracle': {
            'reward': False,
            'weight': weight,
            'link': Args.link
            },
        'Random': {}
        }

    if Args.dataset != "synth" and Args.policies == 'tree':
        Params['EELS']['learning_alg'] = sklearn.tree.DecisionTreeRegressor
        Params['EELS2']['learning_alg'] = sklearn.tree.DecisionTreeRegressor
        Params['Eps']['learning_alg'] = sklearn.tree.DecisionTreeRegressor
        Params['EpsOracle']['learning_alg'] = sklearn.tree.DecisionTreeRegressor

    if Args.dataset != "synth" and Args.policies == 'linear':
        Params['EELS']['learning_alg'] = sklearn.linear_model.LinearRegression
        Params['EELS2']['learning_alg'] = sklearn.linear_model.LinearRegression
        Params['Eps']['learning_alg'] = sklearn.linear_model.LinearRegression
        Params['EpsOracle']['learning_alg'] = sklearn.linear_model.LinearRegression

    Out = {
        'EELS': [],
        'EELS_regret': [],
        'EELS2': [],
        'EELS2_regret': [],
        'Eps': [],
        'Eps_regret': [],
        'EpsOracle': [],
        'EpsOracle_regret': [],
        'Random': [],
        'Random_regret': []
        }

    Exp = RegretExp(weight = weight, link=Args.link, K=Args.K, L=Args.L, T=Args.T, dataset=Args.dataset, policies=Args.policies,structure=Args.structure)
    for i in range(10):
        print('----Iter %d----' % (i))
        for (k,v) in Algs.items():
            print('----Running %s with params %s----' % (k, Params[k]))
            (reward, regret) = Exp.run_alg(v, params=Params[k])
            Out[k].append(reward)
            Out[k+"_regret"].append(regret)
            print('%s final: %0.3f' % (k, reward[-1]))

    pickle.dump(Out, open("./data/%s_%s_%s_link=%s_T=%d_K=%d_L=%d.pkl" %(Args.dataset, Args.policies, Args.structure, Args.link, Args.T, Args.K, Args.L), "wb"))


from pycocotools.coco import COCO
from write_xml import write_to_file
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import os
import shutil

dataDir='..'
dataType='val2014'
annFile='%s/annotations/instances_%s.json'%(dataDir,dataType)

coco=COCO(annFile)	# load database
cats=coco.loadCats(coco.getCatIds())
print cats

foodCategory = []
foodCategoryId = []
foodImageId = []

for cat in cats:
	if cat['supercategory'] == 'food':
		foodCategory.append(cat['name'])
		print cat['name']

foodCategoryId = coco.getCatIds(foodCategory)
foodImageId = coco.getImgIds(catIds=foodCategoryId) #must add catIds=
#print len(foodImageId)

dstdir = './JPEGImages/'

# Get all food images and copy them to JPEGImages folders.(#JPEGImage#)
for cat in range(0, len(foodImageId)):
	img = coco.loadImgs(foodImageId[cat])[0]
	img_name = '%s/images/%s/%s'%(dataDir,dataType,img['file_name'])
	#print img_name
	shutil.copy(img_name, dstdir)

# Generate the SegmentationObject/SegmentationClass (#Segmentation#)
dstdir = './SegmentationClass'
dstdir_2 = './SegmentationObject'

# Generate the configuration files for Annotation folders(#Annotation#)
# Move to the above the share the loop of image_names.
for cat in range(0, len(foodImageId)):
	img = coco.loadImgs(foodImageId[cat])[0]
	img_name = os.path.splitext(img['file_name'])[0]
	img_annotation_xml_name ='./Annotations/%s.xml'%(img_name)
	img_annotation_jpg_name ='./JPEGImages/%s.jpg'%(img_name)
#	print img_annotation_xml_name
	file = open(img_annotation_xml_name, "wb")
	# def write_to_file(img_name,food_type, file_name, img_width, img_height,left_x, left_y, right_x, right_y):
	img_width = img['width']
	img_height = img['height']

	## Now load annotation in order to get bbox, food type
	ann_id = coco.getAnnIds(imgIds=img['id'])
	print "ann_id"
	print ann_id

	## Note: for one image, there are multiple labels, find the food_label
	ann = coco.loadAnns(ann_id)

	for ann_food in ann:
		ann_cat_id = ann_food['category_id']
		ann_cat = coco.loadCats(ann_cat_id)[0]
		if ann_cat['supercategory'] == 'food':
			print ann_cat['name']
			food_ann = ann_food
			break

	print "annotation"
	print ann_food
	bbox = ann_food['bbox']
	catId = ann_food['category_id']
	cat = coco.loadCats(catId)[0]
	left_x = bbox[0]
	left_y = bbox[1]
	right_x = left_x + bbox[2]
	right_y = left_y + bbox[3]
	food_type = cat['name']

	print img_annotation_jpg_name

	if food_type == 'donut':
		shutil.copy(img_annotation_jpg_name, "./donut/")
	elif food_type == 'cake':
		shutil.copy(img_annotation_jpg_name, "./cake/")
	elif food_type == 'hot dog':
		shutil.copy(img_annotation_jpg_name, "./hotdog/")
	elif food_type == 'sandwich':
		shutil.copy(img_annotation_jpg_name, "./sandwich/")
	elif food_type == 'carrot':
		shutil.copy(img_annotation_jpg_name, "./carrot/")
	elif food_type == 'apple':
		shutil.copy(img_annotation_jpg_name, "./apple/")
	elif food_type == 'orange':
		shutil.copy(img_annotation_jpg_name, "./orange/")
	elif food_type == 'banana':
		shutil.copy(img_annotation_jpg_name, "./banana/")
	elif food_type == 'pizza':
		shutil.copy(img_annotation_jpg_name, "./pizza/")

 	write_to_file(img_annotation_jpg_name, food_type, img_annotation_xml_name, str(img_width), str(img_height), str(left_x), str(left_y), str(right_x), str(right_y))
	file.close()

### ??????

# Generat the configuration for ImageSet

img = coco.loadImgs(foodImageId[5])[0]
img_name = '%s/images/%s/%s'%(dataDir,dataType,img['file_name'])
I = io.imread(img_name)
#plt.figure()
#plt.imshow(I)
#plt.show()

# JUST FOR DEBUGGING
print foodCategory
print foodCategoryId
print foodImageId
print img_name


from matplotlib.colors import LinearSegmentedColormap
from numpy import nan, inf
cm_data = [[1., 0., 0.282353],
[1., 0., 0.282353],
[1., 0., 0.290196],
[1., 0., 0.298039],
[1., 0., 0.305882],
[1., 0., 0.313725],
[1., 0., 0.321569],
[1., 0., 0.329412],
[1., 0., 0.337255],
[1., 0., 0.345098],
[1., 0., 0.352941],
[1., 0., 0.356863],
[1., 0., 0.364706],
[1., 0., 0.372549],
[1., 0., 0.380392],
[1., 0., 0.388235],
[1., 0., 0.396078],
[1., 0., 0.403922],
[1., 0., 0.411765],
[1., 0., 0.419608],
[1., 0., 0.427451],
[1., 0., 0.435294],
[1., 0., 0.443137],
[1., 0., 0.45098],
[1., 0., 0.458824],
[1., 0., 0.466667],
[1., 0., 0.47451],
[1., 0., 0.482353],
[1., 0., 0.490196],
[1., 0., 0.498039],
[1., 0., 0.505882],
[1., 0., 0.513725],
[1., 0., 0.521569],
[1., 0., 0.529412],
[1., 0., 0.537255],
[1., 0., 0.545098],
[1., 0., 0.552941],
[1., 0., 0.556863],
[1., 0., 0.564706],
[1., 0., 0.572549],
[1., 0., 0.580392],
[1., 0., 0.588235],
[1., 0., 0.596078],
[1., 0., 0.603922],
[1., 0., 0.611765],
[1., 0., 0.619608],
[1., 0., 0.627451],
[1., 0., 0.635294],
[1., 0., 0.643137],
[1., 0., 0.65098],
[1., 0., 0.658824],
[1., 0., 0.666667],
[1., 0., 0.67451],
[1., 0., 0.682353],
[1., 0., 0.690196],
[1., 0., 0.698039],
[1., 0., 0.705882],
[1., 0., 0.713725],
[1., 0., 0.721569],
[1., 0., 0.729412],
[1., 0., 0.737255],
[1., 0., 0.745098],
[1., 0., 0.74902],
[1., 0., 0.756863],
[1., 0., 0.764706],
[1., 0., 0.772549],
[1., 0., 0.780392],
[1., 0., 0.788235],
[1., 0., 0.796078],
[1., 0., 0.803922],
[1., 0., 0.811765],
[1., 0., 0.819608],
[1., 0., 0.827451],
[1., 0., 0.835294],
[1., 0., 0.843137],
[1., 0., 0.85098],
[1., 0., 0.858824],
[1., 0., 0.866667],
[1., 0., 0.87451],
[1., 0., 0.882353],
[1., 0., 0.890196],
[1., 0., 0.898039],
[1., 0., 0.905882],
[1., 0., 0.913725],
[1., 0., 0.921569],
[1., 0., 0.929412],
[1., 0., 0.937255],
[1., 0., 0.945098],
[1., 0., 0.94902],
[1., 0., 0.956863],
[1., 0., 0.964706],
[1., 0., 0.972549],
[1., 0., 0.980392],
[1., 0., 0.988235],
[1., 0., 0.996078],
[0.992157, 0., 1.],
[0.984314, 0., 1.],
[0.976471, 0., 1.],
[0.968627, 0., 1.],
[0.960784, 0., 1.],
[0.952941, 0., 1.],
[0.945098, 0., 1.],
[0.937255, 0., 1.],
[0.929412, 0., 1.],
[0.921569, 0., 1.],
[0.913725, 0., 1.],
[0.905882, 0., 1.],
[0.898039, 0., 1.],
[0.890196, 0., 1.],
[0.882353, 0., 1.],
[0.87451, 0., 1.],
[0.866667, 0., 1.],
[0.858824, 0., 1.],
[0.85098, 0., 1.],
[0.847059, 0., 1.],
[0.839216, 0., 1.],
[0.831373, 0., 1.],
[0.823529, 0., 1.],
[0.815686, 0., 1.],
[0.807843, 0., 1.],
[0.8, 0., 1.],
[0.792157, 0., 1.],
[0.784314, 0., 1.],
[0.776471, 0., 1.],
[0.768627, 0., 1.],
[0.760784, 0., 1.],
[0.752941, 0., 1.],
[0.745098, 0., 1.],
[0.737255, 0., 1.],
[0.729412, 0., 1.],
[0., 0.54902, 1.],
[0., 0.572549, 1.],
[0., 0.596078, 1.],
[0., 0.615686, 1.],
[0., 0.639216, 1.],
[0., 0.662745, 1.],
[0., 0.682353, 1.],
[0., 0.705882, 1.],
[0., 0.729412, 1.],
[0., 0.752941, 1.],
[0., 0.772549, 1.],
[0., 0.796078, 1.],
[0., 0.819608, 1.],
[0., 0.839216, 1.],
[0., 0.862745, 1.],
[0., 0.886275, 1.],
[0., 0.909804, 1.],
[0., 0.929412, 1.],
[0., 0.952941, 1.],
[0., 0.976471, 1.],
[0., 1., 1.],
[0., 1., 0.976471],
[0., 1., 0.952941],
[0., 1., 0.929412],
[0., 1., 0.909804],
[0., 1., 0.886275],
[0., 1., 0.862745],
[0., 1., 0.839216],
[0., 1., 0.819608],
[0., 1., 0.796078],
[0., 1., 0.772549],
[0., 1., 0.752941],
[0., 1., 0.729412],
[0., 1., 0.705882],
[0., 1., 0.682353],
[0., 1., 0.662745],
[0., 1., 0.639216],
[0., 1., 0.615686],
[0., 1., 0.596078],
[0., 1., 0.572549],
[0., 1., 0.54902],
[0., 1., 0.52549],
[0., 1., 0.505882],
[0., 1., 0.482353],
[0., 1., 0.458824],
[0., 1., 0.439216],
[0., 1., 0.415686],
[0., 1., 0.392157],
[0., 1., 0.368627],
[0., 1., 0.34902],
[0., 1., 0.32549],
[0., 1., 0.301961],
[0., 1., 0.278431],
[0., 1., 0.258824],
[0., 1., 0.235294],
[0., 1., 0.211765],
[0., 1., 0.192157],
[0., 1., 0.168627],
[0., 1., 0.145098],
[0., 1., 0.121569],
[0., 1., 0.101961],
[0., 1., 0.0784314],
[0., 1., 0.054902],
[0., 1., 0.0352941],
[0., 1., 0.0117647],
[0.00784314, 1., 0.],
[0.0313725, 1., 0.],
[0.0509804, 1., 0.],
[0.0745098, 1., 0.],
[0.0980392, 1., 0.],
[0.117647, 1., 0.],
[0.141176, 1., 0.],
[0.164706, 1., 0.],
[0.188235, 1., 0.],
[0.207843, 1., 0.],
[0.231373, 1., 0.],
[0.254902, 1., 0.],
[0.278431, 1., 0.],
[0.298039, 1., 0.],
[0.321569, 1., 0.],
[0.345098, 1., 0.],
[0.364706, 1., 0.],
[0.388235, 1., 0.],
[0.411765, 1., 0.],
[0.435294, 1., 0.],
[0.454902, 1., 0.],
[0.478431, 1., 0.],
[0.501961, 1., 0.],
[0.521569, 1., 0.],
[0.545098, 1., 0.],
[0.568627, 1., 0.],
[0.592157, 1., 0.],
[0.611765, 1., 0.],
[0.635294, 1., 0.],
[0.658824, 1., 0.],
[0.678431, 1., 0.],
[0.701961, 1., 0.],
[0.72549, 1., 0.],
[0.74902, 1., 0.],
[0.768627, 1., 0.],
[0.792157, 1., 0.],
[0.815686, 1., 0.],
[0.839216, 1., 0.],
[0.858824, 1., 0.],
[0.882353, 1., 0.],
[0.905882, 1., 0.],
[0.92549, 1., 0.],
[0.94902, 1., 0.],
[0.972549, 1., 0.],
[0.996078, 1., 0.],
[1., 0.980392, 0.],
[1., 0.956863, 0.],
[1., 0.933333, 0.],
[1., 0.913725, 0.],
[1., 0.890196, 0.],
[1., 0.866667, 0.],
[1., 0.843137, 0.],
[1., 0.823529, 0.],
[1., 0.8, 0.],
[1., 0.776471, 0.],
[1., 0.756863, 0.],
[1., 0.733333, 0.],
[1., 0.709804, 0.],
[1., 0.686275, 0.],
[1., 0.666667, 0.],
[1., 0.666667, 0.]]

test_cm = LinearSegmentedColormap.from_list(__file__, cm_data)

if __name__ == "__main__":
    import matplotlib.pyplot as plt
    import numpy as np

    try:
        from pycam02ucs.cm.viscm import viscm
        viscm(test_cm)
    except ImportError:
        print("pycam02ucs not found, falling back on simple display")
        plt.imshow(np.linspace(0, 100, 256)[None, :], aspect='auto',
                   cmap=test_cm)
    plt.show()