def j_detrend_new_v2(data,deg,func_type,robust):

    mask = 0
    size = data.shape
    if len(size)==2:
        size = np.resize(size,len(size)+2)
        size[2] = 1
        size[3] = 1
    
    if size[3]==1:
        data2d = data.T
    else:
        data2d = (np.reshape(data,(size[0]*size[1]*size[2],size[3]))).T

    if mask!=1:
        mask1d = np.reshape(mask,((size[0]*size[1]*size[2]),1))
    else:
        mask1d = np.ones(((size[0]*size[1]*size[2]),1))

    index_mask = np.where(mask1d!=0)    
    nb_samples = data2d.shape[0]
    nb_vectors = len(index_mask)
    
    if func_type=='linear':
        D = (np.arange(-1,(1+2/(nb_samples-1)),2/(nb_samples-1))).T
    else:
        N = nb_samples
        K = deg
        d = 0
        n = np.arange(0,N)
        C = np.zeros((N,K))

        C[:,0] = np.ones(N)/math.sqrt(N)
        for k in range(1,K):
            C[:,k] = math.sqrt(2/N)*(np.cos(np.pi*(2*n+1)*(k-1)/(2*N)))
        
        D = C*(math.sqrt(nb_samples))

    data2d_d = np.zeros((size[0]*size[1]*size[2],size[3]))
    for i_vect in range(0,nb_vectors):
        data1d = data2d[index_mask[i_vect],:]
        
        if robust==1:
            l = 1
        else:
            l = np.dot(np.dot(np.linalg.pinv(np.dot(D.T,D)),D.T),data1d)
        Dl = np.dot(D,l)
        res_l = data1d - Dl        
        data1d_d = data1d - Dl
        data2d_d[index_mask[i_vect],:] = data1d_d

        #display progress...

    #data2d_d = data2d_d.T
    #data_d = np.reshape(data2d_d,(size[0],size[1],size[2],size[3]))

    data_d = data2d_d
    return data_d

#=======================================================================================================================
# usage
#=======================================================================================================================
dataset/ETHPy150Open neuropoly/spinalcordtoolbox/dev/vertebral_labeling/batch_labeling.py/j_detrend_new_v2/original