csys.split <- function(data, labels, ratio = 2/3, mask = NULL)
{
    if (row(data) != length(labels)) {
        stop("the number of samples is diff. with the number of labels.")
    }

    nl = length(labels)
    cv = unique(labels)
    data1 = NULL;data2 = NULL;labels1 = NULL;labels2 = NULL;
    if (length(mask == 0)) {
        mask = logical(nl)
        if (ratio >= 1 | ratio <= 0) {
            stop("ratio must be in the range [0,1]")
        }
        nc = length(unique(labels)) ## number of unique classes
        if (2*nc >= n) { ## the number of classes is more than half of labels
            ndata1 = ratio * n
            idx = order(runif(n))
            mask[idx[1:ndata1]] = TRUE
        }else { ## the number of classes is low
            for (i in 1 : nc) {
                idx = which(cv[i] == labels)
                ndata1 = round(length(idx) * ratio)
                ord = order(runif(length(idx)))
                mask[idx[ord[1:ndata1]]] = TRUE
            }
        }
    }
    if (!is.logical(mask) | length(mask) != length(labels)) {
        if(!is.logical(mask)) {
            stop("The given mask is not logical")
        }else {
            stop("The number of logical value in mask is diff. with the number of labels")
        }
    }

    data1.idx = labels[mask]
    data2.idx = labels[!mask]

    data1 = train[data1.idx]
    labels1 = labels[data1.idx]

    data2 = train[data2.idx]
    labels2 = labels[data2.idx]

    return(list(data1 = data1, labels1 = labels1, data2 = data2, labels2 = lables2))
}
