1 #' Two-stage clustering, within one task (see \code{claws()})
3 #' \code{clusteringTask1()} runs one full stage-1 task, which consists in iterated
4 #' stage 1 clustering on nb_curves / ntasks energy contributions, computed through
5 #' discrete wavelets coefficients.
6 #' \code{clusteringTask2()} runs a full stage-2 task, which consists in WER distances
7 #' computations between medoids (indices) output from stage 1, before applying
8 #' the second clustering algorithm on the distances matrix.
10 #' @param getContribs Function to retrieve contributions from initial series indices:
11 #' \code{getContribs(indices)} outputs a contributions matrix
12 #' @inheritParams claws
13 #' @inheritParams computeSynchrones
14 #' @inheritParams computeWerDists
16 #' @return The indices of the computed (resp. K1 and K2) medoids.
20 #' @aliases clusteringTask1 clusteringTask2
25 clusteringTask1 = function(indices, getContribs, K1, algoClust1, nb_items_clust,
26 ncores_clust=1, verbose=FALSE, parll=TRUE)
30 cl = parallel::makeCluster(ncores_clust, outfile = "")
31 parallel::clusterExport(cl, c("getContribs","K1","verbose"), envir=environment())
33 # Iterate clustering algorithm 1 until K1 medoids are found
34 while (length(indices) > K1)
36 # Balance tasks by splitting the indices set - as evenly as possible
37 indices_workers = .splitIndices(indices, nb_items_clust, min_size=K1+1)
39 cat(paste("*** [iterated] Clustering task 1 on ",length(indices)," series\n", sep=""))
43 unlist( parallel::parLapply(cl, indices_workers, function(inds) {
44 require("epclust", quietly=TRUE)
45 inds[ algoClust1(getContribs(inds), K1) ]
50 unlist( lapply(indices_workers, function(inds)
51 inds[ algoClust1(getContribs(inds), K1) ]
56 parallel::stopCluster(cl)
63 clusteringTask2 = function(indices, getSeries, K2, algoClust2, nb_series_per_chunk,
64 nvoice, nbytes, endian, ncores_clust=1, verbose=FALSE, parll=TRUE)
67 cat(paste("*** Clustering task 2 on ",length(indices)," medoids\n", sep=""))
69 if (length(indices) <= K2)
72 # A) Compute the WER distances (Wavelets Extended coefficient of deteRmination)
73 distances = computeWerDists(indices, getSeries, nb_series_per_chunk,
74 nvoice, nbytes, endian, ncores_clust, verbose, parll)
76 # B) Apply clustering algorithm 2 on the WER distances matrix
78 cat(paste("*** algoClust2() on ",nrow(distances)," items\n", sep=""))
79 indices[ algoClust2(distances,K2) ]