16
16
# ' band of width \code{h}). The method is fully described in (Dehman, 2015) and
17
17
# ' based on a kernel version of the algorithm. The different options for the
18
18
# ' implementation are available in the package vignette entitled
19
- # ' \href{https://pneuvial.github.io/adjclust/articles/notesCHAC.html}{"Notes on CHAC implementation in adjclust}.
19
+ # ' \href{https://pneuvial.github.io/adjclust/articles/notesCHAC.html}{"Notes on CHAC implementation in adjclust" }.
20
20
# '
21
21
# ' @param mat A similarity matrix or a dist object. Most sparse formats from
22
22
# ' \code{\link[Matrix]{sparseMatrix}} are allowed
28
28
# ' @param strictCheck Logical (default to \code{TRUE}) to systematically check
29
29
# ' default of positivity in input similarities. Can be disabled to avoid
30
30
# ' computationally expensive checks when the number of features is large.
31
+ # ' @param nthreads Integer (default to \code{1L}). Number of threads use for
32
+ # ' matrix precomputations.
31
33
# '
32
34
# ' @returns An object of class \code{\link{chac}} which describes the tree
33
35
# ' produced by the clustering process. The object is a list with the same
82
84
# '
83
85
# ' @examples
84
86
# ' sim <- matrix(
85
- # ' c(1.0, 0.1, 0.2, 0.3,
86
- # ' 0.1, 1.0 ,0.4 ,0.5,
87
- # ' 0.2, 0.4, 1.0, 0.6,
88
- # ' 0.3, 0.5, 0.6, 1.0), nrow = 4)
87
+ # ' c(1.0, 0.1, 0.2, 0.3,
88
+ # ' 0.1, 1.0 ,0.4 ,0.5,
89
+ # ' 0.2, 0.4, 1.0, 0.6,
90
+ # ' 0.3, 0.5, 0.6, 1.0),
91
+ # ' nrow = 4)
89
92
# '
90
93
# ' ## similarity, full width
91
94
# ' fit1 <- adjClust(sim, "similarity")
@@ -112,112 +115,124 @@ NULL
112
115
# ' @importFrom Matrix diag
113
116
# ' @importFrom Matrix t
114
117
adjClust <- function (mat , type = c(" similarity" , " dissimilarity" ),
115
- h = ncol(mat ) - 1 , strictCheck = TRUE ) {
118
+ h = ncol(mat ) - 1 , strictCheck = TRUE , nthreads = 1L ) {
116
119
UseMethod(" adjClust" )
117
120
}
118
121
119
122
# ' @importFrom Matrix isSymmetric forceSymmetric
120
123
# ' @export
121
124
adjClust.matrix <- function (mat , type = c(" similarity" , " dissimilarity" ),
122
- h = ncol(mat ) - 1 , strictCheck = TRUE ) {
125
+ h = ncol(mat ) - 1 , strictCheck = TRUE ,
126
+ nthreads = 1L ) {
123
127
if (! is.numeric(mat ))
124
128
stop(" Input matrix is not numeric" )
125
129
if (! (isSymmetric(mat )))
126
130
stop(" Input matrix is not symmetric" )
127
- res <- run.adjclust(mat , type = type , h = h , strictCheck = strictCheck )
131
+ res <- run.adjclust(mat , type = type , h = h , strictCheck = strictCheck ,
132
+ nthreads = nthreads )
128
133
x <- sys.call()
129
134
res $ call <- update_call(x , " adjClust" )
130
135
return (res )
131
136
}
132
137
133
138
# ' @export
134
139
adjClust.dsyMatrix <- function (mat , type = c(" similarity" , " dissimilarity" ),
135
- h = ncol(mat ) - 1 , strictCheck = TRUE ) {
140
+ h = ncol(mat ) - 1 , strictCheck = TRUE ,
141
+ nthreads = 1L ) {
136
142
# RcppArmadillo functions don't support dsyMatrix, so convert to matrix
137
143
res <- run.adjclust(as.matrix(mat ), type = type , h = h ,
138
- strictCheck = strictCheck )
144
+ strictCheck = strictCheck , nthreads = nthreads )
139
145
x <- sys.call()
140
146
res $ call <- update_call(x , " adjClust" )
141
147
return (res )
142
148
}
143
149
144
150
# ' @export
145
151
adjClust.dgeMatrix <- function (mat , type = c(" similarity" , " dissimilarity" ),
146
- h = ncol(mat ) - 1 , strictCheck = TRUE ) {
152
+ h = ncol(mat ) - 1 , strictCheck = TRUE ,
153
+ nthreads = 1L ) {
147
154
type <- match.arg(type )
148
155
if (! (isSymmetric(mat ))) {
149
156
stop(" Input matrix is not symmetric" )
150
- } else {
151
- mat <- forceSymmetric(mat )
152
- }
153
- res <- adjClust(mat , type = type , h = h , strictCheck = strictCheck )
157
+ } else mat <- forceSymmetric(mat )
158
+ res <- adjClust(mat , type = type , h = h , strictCheck = strictCheck ,
159
+ nthreads = nthreads )
154
160
x <- sys.call()
155
161
res $ call <- update_call(x , " adjClust" )
156
162
return (res )
157
163
}
158
164
159
165
# ' @export
160
166
adjClust.dsCMatrix <- function (mat , type = c(" similarity" , " dissimilarity" ),
161
- h = ncol(mat ) - 1 , strictCheck = TRUE ) {
167
+ h = ncol(mat ) - 1 , strictCheck = TRUE ,
168
+ nthreads = 1L ) {
162
169
type <- match.arg(type )
163
170
if (type == " dissimilarity" )
164
171
stop(" 'type' can only be 'similarity' with sparse Matrix inputs" )
165
- res <- run.adjclust(mat , type = type , h = h , strictCheck = strictCheck )
172
+ res <- run.adjclust(mat , type = type , h = h , strictCheck = strictCheck ,
173
+ nthreads = nthreads )
166
174
x <- sys.call()
167
175
res $ call <- update_call(x , " adjClust" )
168
176
return (res )
169
177
}
170
178
171
179
# ' @export
172
180
adjClust.dgCMatrix <- function (mat , type = c(" similarity" , " dissimilarity" ),
173
- h = ncol(mat ) - 1 , strictCheck = TRUE ) {
181
+ h = ncol(mat ) - 1 , strictCheck = TRUE ,
182
+ nthreads = 1L ) {
174
183
if (! (isSymmetric(mat ))) {
175
184
stop(" Input matrix is not symmetric" )
176
185
} else {
177
186
mat <- forceSymmetric(mat )
178
187
}
179
- res <- adjClust(mat , type = type , h = h , strictCheck = strictCheck )
188
+ res <- adjClust(mat , type = type , h = h , strictCheck = strictCheck ,
189
+ nthreads = nthreads )
180
190
x <- sys.call()
181
191
res $ call <- update_call(x , " adjClust" )
182
192
return (res )
183
193
}
184
194
185
195
# ' @export
186
196
adjClust.dsTMatrix <- function (mat , type = c(" similarity" , " dissimilarity" ),
187
- h = ncol(mat ) - 1 , strictCheck = TRUE ) {
197
+ h = ncol(mat ) - 1 , strictCheck = TRUE ,
198
+ nthreads = 1L ) {
188
199
type <- match.arg(type )
189
200
if (type == " dissimilarity" )
190
201
stop(" 'type' can only be 'similarity' with sparse Matrix inputs" )
191
- res <- run.adjclust(mat , type = type , h = h , strictCheck = strictCheck )
202
+ res <- run.adjclust(mat , type = type , h = h , strictCheck = strictCheck ,
203
+ nthreads = nthreads )
192
204
x <- sys.call()
193
205
res $ call <- update_call(x , " adjClust" )
194
206
return (res )
195
207
}
196
208
197
209
# ' @export
198
210
adjClust.dgTMatrix <- function (mat , type = c(" similarity" , " dissimilarity" ),
199
- h = ncol(mat ) - 1 , strictCheck = TRUE ) {
211
+ h = ncol(mat ) - 1 , strictCheck = TRUE ,
212
+ nthreads = 1L ) {
200
213
type <- match.arg(type )
201
214
if (! (isSymmetric(mat ))) {
202
215
stop(" Input matrix is not symmetric" )
203
216
} else {
204
217
mat <- forceSymmetric(mat )
205
218
}
206
- res <- adjClust(mat , type = type , h = h , strictCheck = strictCheck )
219
+ res <- adjClust(mat , type = type , h = h , strictCheck = strictCheck ,
220
+ nthreads = nthreads )
207
221
x <- sys.call()
208
222
res $ call <- update_call(x , " adjClust" )
209
223
return (res )
210
224
}
211
225
212
226
# ' @export
213
227
adjClust.dist <- function (mat , type = c(" similarity" , " dissimilarity" ),
214
- h = ncol(mat ) - 1 , strictCheck = TRUE ) {
228
+ h = ncol(mat ) - 1 , strictCheck = TRUE ,
229
+ nthreads = 1L ) {
215
230
type <- match.arg(type )
216
231
if (type != " dissimilarity" )
217
232
message(" Note: input class is 'dist' so 'type' is supposed to be 'dissimilarity'" )
218
233
mat <- as.matrix(mat )
219
234
res <- adjClust.matrix(mat , type = " dissimilarity" , h = h ,
220
- strictCheck = strictCheck )
235
+ strictCheck = strictCheck , nthreads = nthreads )
221
236
x <- sys.call()
222
237
res $ call <- update_call(x , " adjClust" )
223
238
return (res )
@@ -226,7 +241,7 @@ adjClust.dist <- function(mat, type = c("similarity", "dissimilarity"),
226
241
# ' @importFrom methods is
227
242
# ' @import Rcpp
228
243
run.adjclust <- function (mat , type = c(" similarity" , " dissimilarity" ), h ,
229
- strictCheck = TRUE ) {
244
+ strictCheck = TRUE , nthreads = 1L ) {
230
245
# sanity checks
231
246
type <- match.arg(type )
232
247
if (any(is.na(mat )))
@@ -261,24 +276,24 @@ run.adjclust <- function(mat, type = c("similarity", "dissimilarity"), h,
261
276
262
277
if (is(mat , " sparseMatrix" )) {
263
278
# left
264
- rCumL <- matL_sparse_rowCumsums(mat , h )
279
+ rCumL <- matL_sparse_rowCumsums(mat , h , nthreads = nthreads )
265
280
rcCumL <- colCumsums(rCumL , useNames = FALSE ) # p x (h+1) matrix
266
281
rm(rCumL )
267
282
268
283
# right
269
- rCumR <- matR_sparse_rowCumsums(mat , h )
284
+ rCumR <- matR_sparse_rowCumsums(mat , h , nthreads = nthreads )
270
285
rcCumR <- colCumsums(rCumR , useNames = FALSE ) # p x (h+1) matrix
271
286
rm(rCumR )
272
287
273
288
out_matL <- matL_sparse(mat , 2 )
274
289
} else {
275
290
# left
276
- rCumL <- matL_full_rowCumsums(mat , h )
291
+ rCumL <- matL_full_rowCumsums(mat , h , nthreads = nthreads )
277
292
rcCumL <- colCumsums(rCumL , useNames = FALSE ) # p x (h+1) matrix
278
293
rm(rCumL )
279
294
280
295
# right
281
- rCumR <- matR_full_rowCumsums(mat , h )
296
+ rCumR <- matR_full_rowCumsums(mat , h , nthreads = nthreads )
282
297
rcCumR <- colCumsums(rCumR , useNames = FALSE ) # p x (h+1) matrix
283
298
rm(rCumR )
284
299
0 commit comments