#' @rdname clustering
#' @export
clusteringTask1 <- function(indices, getContribs, K1, algoClust1, nb_items_clust,
- ncores_clust=3, verbose=FALSE, parll=TRUE)
+ ncores_clust=3, verbose=FALSE)
{
if (verbose)
cat(paste("*** Clustering task 1 on ",length(indices)," series [start]\n", sep=""))
if (length(indices) <= K1)
return (indices)
+ parll <- (ncores_clust > 1)
if (parll)
{
# outfile=="" to see stderr/stdout on terminal
#' @rdname clustering
#' @export
clusteringTask2 <- function(indices, getSeries, K2, algoClust2, nb_series_per_chunk,
- smooth_lvl, nvoice, nbytes, endian, ncores_clust=3, verbose=FALSE, parll=TRUE)
+ smooth_lvl, nvoice, nbytes, endian, ncores_clust=3, verbose=FALSE)
{
if (verbose)
cat(paste("*** Clustering task 2 on ",length(indices)," medoids\n", sep=""))
# A) Compute the WER distances (Wavelets Extended coefficient of deteRmination)
distances <- computeWerDists(indices, getSeries, nb_series_per_chunk,
- smooth_lvl, nvoice, nbytes, endian, ncores_clust, verbose, parll)
+ smooth_lvl, nvoice, nbytes, endian, ncores_clust, verbose)
# B) Apply clustering algorithm 2 on the WER distances matrix
if (verbose)
#' @param nbytes 4 or 8 bytes to (de)serialize a floating-point number
#' @param endian Endianness for (de)serialization: "little" or "big"
#' @param verbose FALSE: nothing printed; TRUE: some execution traces
-#' @param parll TRUE: run in parallel. FALSE: run sequentially
#'
#' @return A list:
#' \itemize{
#' @examples
#' \dontrun{
#' # WER distances computations are too long for CRAN (for now)
-#' parll = FALSE #on this small example, sequential run is faster
+#' # Note: on this small example, sequential run is faster
#'
#' # Random series around cos(x,2x,3x)/sin(x,2x,3x)
#' x <- seq(0,50,0.05)
#' series = series[,permut]
#' #dim(series) #c(240,1001)
#' res_ascii <- claws(series, K1=30, K2=6, nb_series_per_chunk=500,
-#' nb_items_clust=100, random=FALSE, verbose=TRUE, parll=parll)
+#' nb_items_clust=100, random=FALSE, verbose=TRUE, ncores_clust=1)
#'
#' # Same example, from CSV file
#' csv_file <- tempfile(pattern="epclust_series.csv_")
#' write.table(t(series), csv_file, sep=",", row.names=FALSE, col.names=FALSE)
-#' res_csv <- claws(csv_file, 30, 6, 500, 100, random=FALSE, parll=parll)
+#' res_csv <- claws(csv_file, 30, 6, 500, 100, random=FALSE, ncores_clust=1)
#'
#' # Same example, from binary file
#' bin_file <- tempfile(pattern="epclust_series.bin_")
#' endian <- "little"
#' binarize(csv_file, bin_file, 500, ",", nbytes, endian)
#' getSeries <- function(indices) getDataInFile(indices, bin_file, nbytes, endian)
-#' res_bin <- claws(getSeries, 30, 6, 500, 100, random=FALSE, parll=parll)
+#' res_bin <- claws(getSeries, 30, 6, 500, 100, random=FALSE, ncores_clust=1)
#' unlink(csv_file)
#' unlink(bin_file)
#'
#' df_series <- dbGetQuery(series_db, request)
#' matrix(df_series[,"value"], nrow=serie_length)
#' }
-#' res_db <- claws(getSeries, 30, 6, 500, 100, random=FALSE, parll=parll)
+#' res_db <- claws(getSeries, 30, 6, 500, 100, random=FALSE, ncores_clust=1)
#' dbDisconnect(series_db)
#'
#' # All results should be equal:
algoClust2=function(dists,K) cluster::pam(dists,K,diss=TRUE,pamonce=1)$id.med,
wav_filt="d8", contrib_type="absolute", WER="end", smooth_lvl=3, nvoice=4,
random=TRUE, ntasks=1, ncores_tasks=1, ncores_clust=3, sep=",", nbytes=4,
- endian=.Platform$endian, verbose=FALSE, parll=TRUE)
+ endian=.Platform$endian, verbose=FALSE)
{
# Check/transform arguments
if (!is.matrix(series) && !bigmemory::is.big.matrix(series)
stop("'sep': character")
nbytes <- .toInteger(nbytes, function(x) x==4 || x==8)
verbose <- .toLogical(verbose)
- parll <- .toLogical(parll)
# Binarize series if it is not a function; the aim is to always use a function,
# to uniformize treatments. An equally good alternative would be to use a file-backed
indices_all[((i-1)*nb_series_per_task+1):upper_bound]
})
+ parll <- (ncores_tasks > 1)
if (parll && ntasks>1)
{
# Initialize parallel runs: outfile="" allow to output verbose traces in the console
parallel::makeCluster(ncores_tasks, outfile="")
else
parallel::makeCluster(ncores_tasks)
- varlist <- c("ncores_clust","verbose","parll", #task 1 & 2
+ varlist <- c("ncores_clust","verbose", #task 1 & 2
"K1","getContribs","algoClust1","nb_items_clust") #task 1
if (WER=="mix")
{
if (parll && ntasks>1)
require("epclust", quietly=TRUE)
indices_medoids <- clusteringTask1(inds, getContribs, K1, algoClust1,
- nb_items_clust, ncores_clust, verbose, parll)
+ nb_items_clust, ncores_clust, verbose)
if (WER=="mix")
{
indices_medoids <- clusteringTask2(indices_medoids, getSeries, K2, algoClust2,
- nb_series_per_chunk,smooth_lvl,nvoice,nbytes,endian,ncores_clust,verbose,parll)
+ nb_series_per_chunk,smooth_lvl,nvoice,nbytes,endian,ncores_clust,verbose)
}
indices_medoids
}
if (verbose)
cat("...Run final // stage 1 + stage 2\n")
indices_medoids <- clusteringTask1(indices_medoids_all, getContribs, K1, algoClust1,
- nb_items_clust, ncores_tasks*ncores_clust, verbose, parll)
+ nb_items_clust, ncores_tasks*ncores_clust, verbose)
indices_medoids <- clusteringTask2(indices_medoids, getSeries, K2, algoClust2,
- nb_series_per_chunk,smooth_lvl,nvoice,nbytes,endian,ncores_last_stage,verbose,parll)
+ nb_series_per_chunk,smooth_lvl,nvoice,nbytes,endian,ncores_last_stage,verbose)
# Compute synchrones, that is to say the cumulated power consumptions for each of the K2
# final groups.
medoids <- getSeries(indices_medoids)
synchrones <- computeSynchrones(medoids, getSeries, nb_curves, nb_series_per_chunk,
- ncores_last_stage, verbose, parll)
+ ncores_last_stage, verbose)
# NOTE: no need to use big.matrix here, since there are only K2 << K1 << N remaining curves
list("medoids"=medoids, "ranks"=indices_medoids, "synchrones"=synchrones)