<commit_before>from numpy.linalg import norm
from numpy import zeros

class composite(object):
    """
    A generic way to specify a problem in composite form.
    """

    def __init__(self, smooth_objective, nonsmooth_objective, proximal, initial, smooth_multiplier=1):
        # Do we need to store this?
        #self.initial = initial.copy()
        self.coefs = initial.copy()
        self.nonsmooth_objective = nonsmooth_objective
        self._smooth_objective = smooth_objective
        self.proximal = proximal
        self.smooth_multiplier = smooth_multiplier

    def smooth_objective(self, x, mode='both'):
        output = self._smooth_objective(x, mode=mode)
        if mode == 'both':
            return self.smooth_multiplier * output[0], self.smooth_multiplier * output[1]
        elif mode == 'grad' or mode == 'func':
            return self.smooth_multiplier * output
        else:
            raise ValueError("Mode incorrectly specified")

    def objective(self, x):
        return self.smooth_objective(x,mode='func') + self.nonsmooth_objective(x)

    def proximal_optimum(self, x, lipschitz=1):
        """
        Returns
        
        .. math::

           \inf_{v \in \mathbb{R}^p} \frac{L}{2}
           \|x-v\|^2_2 + \lambda h(v)

        where *p*=x.shape[0] and :math:`h(v)` = self.seminorm(v).

        """
        argmin = self.proximal(x, lipschitz)
        return argmin, lipschitz * norm(x-argmin)**2 / 2. + self.nonsmooth_objective(argmin)


    def proximal_step(self, x, grad, lipshitz, prox_control=None):
        """
        Compute the proximal optimization

        prox_control: If not None, then a dictionary of parameters for the prox procedure
        """
        z = x - grad / lipshitz
        
        if prox_control is None:
            return self.proximal(z, lipshitz)
        else:
            return self.proximal(z, lipshitz, **prox_control)

class nonsmooth(composite):
    """
    A composite subclass that explicitly 0
    as smooth_objective.
    """

    def smooth_objective(self, x):
        if mode == 'both':
            return 0., zeros(x.shape)
        elif mode == 'func':
            return 0.
        elif mode == 'grad':
            return zeros(x.shape)
        raise ValueError("Mode not specified correctly")

class smooth(composite):
<commit_msg>BUG - missing mode parameter<commit_after>from numpy.linalg import norm
from numpy import zeros

class composite(object):
    """
    A generic way to specify a problem in composite form.
    """

    def __init__(self, smooth_objective, nonsmooth_objective, proximal, initial, smooth_multiplier=1):
        # Do we need to store this?
        #self.initial = initial.copy()
        self.coefs = initial.copy()
        self.nonsmooth_objective = nonsmooth_objective
        self._smooth_objective = smooth_objective
        self.proximal = proximal
        self.smooth_multiplier = smooth_multiplier

    def smooth_objective(self, x, mode='both'):
        output = self._smooth_objective(x, mode=mode)
        if mode == 'both':
            return self.smooth_multiplier * output[0], self.smooth_multiplier * output[1]
        elif mode == 'grad' or mode == 'func':
            return self.smooth_multiplier * output
        else:
            raise ValueError("Mode incorrectly specified")

    def objective(self, x):
        return self.smooth_objective(x,mode='func') + self.nonsmooth_objective(x)

    def proximal_optimum(self, x, lipschitz=1):
        """
        Returns

        .. math::

           \inf_{v \in \mathbb{R}^p} \frac{L}{2}
           \|x-v\|^2_2 + \lambda h(v)

        where *p*=x.shape[0] and :math:`h(v)` = self.seminorm(v).

        """
        argmin = self.proximal(x, lipschitz)
        return argmin, lipschitz * norm(x-argmin)**2 / 2. + self.nonsmooth_objective(argmin)


    def proximal_step(self, x, grad, lipshitz, prox_control=None):
        """
        Compute the proximal optimization

        prox_control: If not None, then a dictionary of parameters for the prox procedure
        """
        z = x - grad / lipshitz
        
        if prox_control is None:
            return self.proximal(z, lipshitz)
        else:
            return self.proximal(z, lipshitz, **prox_control)


class nonsmooth(composite):
    """
    A composite subclass that explicitly 0
    as smooth_objective.
    """

    def smooth_objective(self, x, mode='both'):
        if mode == 'both':
            return 0., zeros(x.shape)
        elif mode == 'func':
            return 0.
        elif mode == 'grad':
            return zeros(x.shape)
        raise ValueError("Mode not specified correctly")


class smooth(composite):

{'fraction_non_alphanumeric': 0.10956856044425459, 'fraction_numerical': 0.005553182400683468, 'mean_word_length': 2.328358208955224, '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': '5866370', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1394, 'n_words': 368}