<commit_before>cimport cython
import numpy as np
cimport numpy as np
from math import sqrt
import sys

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.profile(False)

## Regularisers for NNLSLp
#####  #####   ####  #    # # #    #   ##   #       ####
#    # #    # #    #  #  #  # ##  ##  #  #  #      #
#    # #    # #    #   ##   # # ## # #    # #       ####
#####  #####  #    #   ##   # #    # ###### #           #
#      #   #  #    #  #  #  # #    # #    # #      #    #
#      #    #  ####  #    # # #    # #    # ######  ####
cpdef non_negativity(np.ndarray[np.float64_t] x, int compartment_start, int compartment_size):
    cdef:
        np.ndarray[np.float64_t] v
        size_t i
    v = x.copy()
    for i in range(compartment_start, compartment_size):
        if v[i] < 0.0:
            v[i] = 0.0
    return v

cpdef soft_thresholding(np.ndarray[np.float64_t] x, double lam, int compartment_start, int compartment_size) :
    """
    Proximal of L1 norm

    Author: <NAME> - athena @ Inria
    """
    # NB: this preserves non-negativity
    cdef:
        np.ndarray[np.float64_t] v
        size_t i
    v = x.copy()
    for i in range(compartment_start, compartment_size):
        if v[i] <= lam:
            v[i] = 0.0
        else:
            v[i] -= lam
    return v

cpdef projection_onto_l2_ball(np.ndarray[np.float64_t] x, double lam, int compartment_start, int compartment_size) :
    """
    Proximal of L2 norm

    Author: <NAME> - athena @ Inria
    """
    # NB: this preserves non-negativity
    cdef:
        np.float64_t xn
        np.ndarray[np.float64_t] v
        size_t i
    v = x.copy()
    xn = sqrt(sum(v[compartment_start:compartment_size]**2))
    if xn > lam
        for i in range(compartment_start, compartment_size):
            v[i] = v[i]/xn*lam
    return v


## Regularisers for HNNLS
# Penalty term
cpdef omega_hierarchical(np.ndarray[np.float64_t] v, np.ndarray[object] subtree, np.ndarray[np.float64_t] weight, double lam, double n) :
    """
    Author: <NAME> - athena @ Inria
    References:
        [1] Jenatton et al. - `Proximal Methods for Hierarchical Sparse Coding`
    """
    cdef:
        int nG = weight.size
        size_t k, i
        double xn, tmp = 0.0

    if lam != 0:
        if n == 2:
            for k in range(nG):
                idx = subtree[k]
                xn = 0.0
                for i in idx:
                    xn += v[i]*v[i]
                    tmp += weight[k] * sqrt( xn )
        elif n == np.Inf:
            for k in range(nG):
                idx = subtree[k]
                tmp += weight[k] * max( v[idx] )
    return lam*tmp

# Proximal operator of the penalty term
cpdef prox_hierarchical( np.ndarray[np.float64_t] x, np.ndarray[object] subtree, np.ndarray[np.float64_t] weight, double lam, double n ) :
    """
    Author: <NAME> - athena @ Inria
    References:
        [1] Jenatton et al. - `Proximal Methods for Hierarchical Sparse Coding`
    """
    cdef:
        np.ndarray[np.float64_t] v
        int nG = weight.size, N, rho
        size_t k, i<commit_msg>cleaned the code of the proximals<commit_after>cimport cython
import numpy as np
cimport numpy as np
from math import sqrt
import sys

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.profile(False)

#####  #####   ####  #    # # #    #   ##   #       ####
#    # #    # #    #  #  #  # ##  ##  #  #  #      #
#    # #    # #    #   ##   # # ## # #    # #       ####
#####  #####  #    #   ##   # #    # ###### #           #
#      #   #  #    #  #  #  # #    # #    # #      #    #
#      #    #  ####  #    # # #    # #    # ######  ####
cpdef non_negativity(np.ndarray[np.float64_t] x, int compartment_start, int compartment_size):
    """
    POCS for the first orthant (non-negativity)

    Author: <NAME> - athena @ Inria
    """
    cdef:
        np.ndarray[np.float64_t] v
        size_t i
    v = x.copy()
    for i in range(compartment_start, compartment_size):
        if v[i] < 0.0:
            v[i] = 0.0
    return v

cpdef soft_thresholding(np.ndarray[np.float64_t] x, double lam, int compartment_start, int compartment_size) :
    """
    Proximal of L1 norm

    Author: <NAME> - athena @ Inria
    """
    # NB: this preserves non-negativity
    cdef:
        np.ndarray[np.float64_t] v
        size_t i
    v = x.copy()
    for i in range(compartment_start, compartment_size):
        if v[i] <= lam:
            v[i] = 0.0
        else:
            v[i] -= lam
    return v

cpdef projection_onto_l2_ball(np.ndarray[np.float64_t] x, double lam, int compartment_start, int compartment_size) :
    """
    Proximal of L2 norm

    Author: <NAME> - athena @ Inria
    """
    # NB: this preserves non-negativity
    cdef:
        np.float64_t xn
        np.ndarray[np.float64_t] v
        size_t i
    v = x.copy()
    xn = sqrt(sum(v[compartment_start:compartment_size]**2))
    if xn > lam
        for i in range(compartment_start, compartment_size):
            v[i] = v[i]/xn*lam
    return v

cpdef omega_hierarchical(np.ndarray[np.float64_t] v, np.ndarray[object] subtree, np.ndarray[np.float64_t] weight, double lam, double n) :
    """
    Author: <NAME> - athena @ Inria
    References:
        [1] Jenatton et al. - `Proximal Methods for Hierarchical Sparse Coding`
    """
    cdef:
        int nG = weight.size
        size_t k, i
        double xn, tmp = 0.0

    if lam != 0:
        if n == 2:
            for k in range(nG):
                idx = subtree[k]
                xn = 0.0
                for i in idx:
                    xn += v[i]*v[i]
                    tmp += weight[k] * sqrt( xn )
        elif n == np.Inf:
            for k in range(nG):
                idx = subtree[k]
                tmp += weight[k] * max( v[idx] )
    return lam*tmp

cpdef prox_hierarchical( np.ndarray[np.float64_t] x, np.ndarray[object] subtree, np.ndarray[np.float64_t] weight, double lam, double n ) :
    """
    Author: <NAME> - athena @ Inria
    References:
        [1] Jenatton et al. - `Proximal Methods for Hierarchical Sparse Coding`
    """
    cdef:
        np.ndarray[np.float64_t] v
        int nG = weight.size, N, rho
        size_t k, i
{'fraction_non_alphanumeric': 0.13869086195722619, 'fraction_numerical': 0.013609850939727802, 'mean_word_length': 1.808462238398544, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2183608', 'n_tokens_mistral': 2362, 'n_tokens_neox': 2243, 'n_words': 520}