a b/R/utils.R
1
is_almostInteger <- function(X){
2
    if(!is.numeric(X) & !is.vector(X)) return(FALSE)
3
    if(length(X) != 1) return(FALSE)
4
    if(!is.finite(X)) return(FALSE)
5
    X.round <- round(X)
6
    if(X == X.round) return(TRUE)
7
    return(FALSE)
8
}
9
10
is_almostInteger_vector <- function(X){
11
    if(!is.vector(X) || is.list(X)){
12
        return(FALSE)
13
    }
14
    # if(!is.numeric(X) & !is.vector(X)) return(FALSE)
15
    #return(all(sapply(X, is_almostInteger)))
16
    return(all(vapply(X, is_almostInteger, logical(1))))
17
}
18
19
is_almostInteger_list <- function(X){
20
    if(!is.list(X)) return(FALSE)
21
    # if(!is.numeric(X) & !is.vector(X)) return(FALSE)
22
    # return(all(sapply(X, is_almostInteger)))
23
    return(all(vapply(X, is_almostInteger, logical(1))))
24
}
25
26
27
28
check_matrix <- function(X){
29
    # add rownames and colnames if absent, cast into matrix
30
    if(!(is.matrix(X) || is.data.frame(X))) return(FALSE)
31
32
    if(is.data.frame(X)){
33
        X <- as.matrix(X)
34
    }
35
    if(is.null(rownames(X))){
36
        rownames(X) <- 1:nrow(X)
37
    }
38
    if(is.null(colnames(X))){
39
        colnames(X) <- paste0("V", 1:ncol(X))
40
    }
41
    return(X)
42
}
43
44
45
validate_matrix_X <- function(X){
46
    # X should be a numeric matrix
47
    X <- check_matrix(X)
48
    if(!is.numeric(X)){
49
        stop("X must be a numeric matrix/data.frame")
50
    }
51
    # if(any(!X)) stop("X must be a numeric matrix/data.frame")
52
    return(X)
53
}
54
55
validate_matrix_Y <- function(Y){
56
    # X should be a numeric matrix
57
    Y <- check_matrix(Y)
58
    if(!is.numeric(Y)){
59
        stop("Y must be a numeric matrix/data.frame")
60
    }
61
    # if(any(!Y)) stop("Y must be a numeric matrix/data.frame")
62
    return(Y)
63
}
64
65
validate_list_matrix_X <- function(X){
66
    if(!is.list(X)){
67
        stop("X must be a list of matrix/data.frame")
68
    }
69
    X <- lapply(X, validate_matrix_X)
70
    return(X)
71
}
72
73
validate_ncomp <- function(ncomp, X){
74
    # ncomp should be a positive non-null integer
75
    # lower than ncol(X)
76
    nrow_X <- ifelse(is.list(X), nrow(X[[1]]), nrow(X))
77
    ncomp.max <- min(unlist(lapply(X,function(x)ncol(x))), nrow_X)
78
    if(is.list(X)){
79
        ncomp.max <- min(ncomp.max, ncol(X))
80
    }
81
    if(!is_almostInteger(ncomp)){
82
        stop(paste0("'ncomp' should be an integer between 1 and ", ncomp.max))
83
    }
84
    if(ncomp > ncomp.max || ncomp==0){
85
        stop(paste0("'ncomp' should be an integer between 1 and ", ncomp.max))
86
    }
87
    ncomp <- round(ncomp)
88
    return(ncomp)
89
}
90
91
validate_test_keepX <- function(test.keepX, X){
92
    # test.keepX should be a vecter of positive integer of size > 1
93
    # every value of test.keepX should be lower than ncol(X)
94
    # ncomp and X have already been validate
95
    if(is.null(test.keepX)){
96
        test.keepX <- ncol(X)
97
    }
98
    if(!is_almostInteger_vector(test.keepX)){
99
        stop("'test.keepX' should be numeric")
100
    }
101
    if(any(test.keepX>ncol(X))){
102
        stop(paste0("'test.keepX' must be lower than ", ncol(X), ", ncol(X)"))
103
    }
104
    return(sort(unique(test.keepX)))
105
}
106
107
validate_test_keepY <- function(test.keepY, Y){
108
    # test.keepX should be a vecter of positive integer of size > 1
109
    # every value of test.keepX should be lower than ncol(X)
110
    # ncomp and X have already been validate
111
    if(is.null(Y)){  # case of keepY null in block spls
112
        return(NULL)
113
    } else {
114
        if(is.null(test.keepY)){
115
            test.keepY <- ncol(Y)
116
        }
117
        if(!is_almostInteger_vector(test.keepY)){
118
            stop("'test.keepY' should be numeric")
119
        }
120
        if(any(test.keepY>ncol(Y))){
121
            stop(paste0("'test.keepY' must be lower than ", ncol(Y), ", ncol(Y)"))
122
        }
123
    }
124
    return(sort(unique(test.keepY)))
125
}
126
127
validate_test_list_keepX <- function(test.keepX, ncomp, X){
128
    # for block spls
129
    # same length of X (list)
130
    # if (is.null(test.keepX)) {
131
    #     test.keepX = lapply(seq_along(X), function(x) {
132
    #         c(5, 10, 15)[which(c(5, 10, 15) < ncol(X[[x]]))]
133
    #     })
134
    #     names(test.keepX) = names(X)
135
    # }
136
    if(is.null(test.keepX)){
137
        stop(paste0("'test.list.keepX' must be a list of numeric of size ", length(X), "."))
138
    }
139
    if(is_almostInteger_list(test.keepX)){
140
        stop(paste0("'test.list.keepX' must be a list of numeric of size ", length(X), "."))
141
    }
142
    if(!(all(names(test.keepX) %in% names(X)) && all(names(X) %in% names(test.keepX)))){
143
        stop("'list.test.keepX' should have the same names as X")
144
    }
145
    lapply(1:length(X), function(i){
146
        if(any(ncol(X[[i]]) < test.keepX[[i]])){
147
            stop(paste0("'test.list.keepX[[",i,"]] sould be lower than ",ncol(X[[i]]),", ncol(X[[",i,"]])."))
148
        }
149
    })
150
    test.keepX <- lapply(test.keepX, function(x){x})
151
    return(test.keepX)
152
}
153
154
validate_indY <- function(indY, X){
155
    # X already checked
156
    if(is.null(indY)){
157
        stop(paste0("'indY' must be a numeric value lower or equal to ", length(X), ", the number of blocks in X."))
158
    }
159
    if(!is_almostInteger(indY) | !(indY %in% c(1:length(X))) ){
160
        stop(paste0("'indY' must be a numeric value lower or equal to ", length(X), ", the number of blocks in X."))
161
    }
162
    return(indY)
163
}
164
165
sd_new <- function(x, ...){
166
    if(length(x) == 1){
167
        return(0)
168
    }else{
169
        return(sd(x, ...))
170
    }
171
}
172
173
return_true_false <- function(x, default){
174
    if(is.logical(x)){
175
        if(is.finite(x)){
176
            return(x)
177
        } else { #NA
178
            return(default)
179
        }
180
    } else {
181
        return(default)
182
    }
183
}
184
185
check_legend.block.name <- function(legend.block.name, cluster){
186
    stopifnot(is(legend.block.name, "character"))
187
    stopifnot(is(legend.block.name, "vector"))
188
    
189
    # cluster <- getCluster(...)
190
    size_block <- unique(cluster$block)
191
    stopifnot(length(legend.block.name) == length(size_block))
192
}