#--------------------------------------------------------------------------
# Copyright @ Ehsan Elhamifar, 2012
#--------------------------------------------------------------------------

import numpy as np
import scipy
    
def processC(C = None,ro = 1): 
    if isinstance(C,scipy.sparse.csr_matrix):
        C = C.toarray()
    if (ro < 1):
        m,N = C.shape
        Cp = np.zeros((m,N))
        Ind = np.zeros([m,N],dtype=int)
        S = np.zeros([m,N])
        for i in range(N):
            tmp = (np.abs(C[:,i])).argsort()
            tmp1 = tmp[::-1]
            Ind[:,i] = tmp1.astype(int)
            S[:,i]= C[Ind[:,i],i]
        for i in range(N):
            cL1 = sum(S[:,i])
            stop = False
            cSum = 0
            t = 0
            while ((not stop)  and t < N):

                t = t + 1
                cSum = cSum + S[t-1,i]
                if (cSum >= ro * cL1):
                    stop = True
                    Cp[Ind[0:t,i],i] = C[Ind[0:t,i],i]

    else:
        Cp = C
    
    return Cp