drop enercast submodule; drop Rcpp requirement; fix doc, complete code, fix fix fix
[epclust.git] / epclust / R / clustering.R
CommitLineData
3c5a4b08 1#' Two-stage clustering, within one task (see \code{claws()})
4bcfdbee 2#'
3c5a4b08
BA
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.
4bcfdbee 9#'
4bcfdbee 10#' @param getContribs Function to retrieve contributions from initial series indices:
40f12a2f 11#' \code{getContribs(indices)} outputs a contributions matrix
4bcfdbee 12#' @inheritParams claws
40f12a2f 13#' @inheritParams computeSynchrones
3c5a4b08
BA
14#' @inheritParams computeWerDists
15#'
16#' @return The indices of the computed (resp. K1 and K2) medoids.
4bcfdbee 17#'
3c5a4b08
BA
18#' @name clustering
19#' @rdname clustering
20#' @aliases clusteringTask1 clusteringTask2
4bcfdbee
BA
21NULL
22
23#' @rdname clustering
24#' @export
282342ba 25clusteringTask1 <- function(indices, getContribs, K1, algoClust1, nb_items_clust,
37c82bba 26 ncores_clust=1, verbose=FALSE, parll=TRUE)
5c652979 27{
492cd9e7 28 if (parll)
7b13d0c2 29 {
282342ba
BA
30 # outfile=="" to see stderr/stdout on terminal
31 cl <- parallel::makeCluster(ncores_clust, outfile = "")
d9bb53c5 32 parallel::clusterExport(cl, c("getContribs","K1","verbose"), envir=environment())
7b13d0c2 33 }
d9bb53c5 34 # Iterate clustering algorithm 1 until K1 medoids are found
492cd9e7
BA
35 while (length(indices) > K1)
36 {
d9bb53c5 37 # Balance tasks by splitting the indices set - as evenly as possible
282342ba 38 indices_workers <- .splitIndices(indices, nb_items_clust, min_size=K1+1)
0486fbad
BA
39 if (verbose)
40 cat(paste("*** [iterated] Clustering task 1 on ",length(indices)," series\n", sep=""))
e161499b
BA
41 indices <-
42 if (parll)
43 {
44 unlist( parallel::parLapply(cl, indices_workers, function(inds) {
45 require("epclust", quietly=TRUE)
0486fbad 46 inds[ algoClust1(getContribs(inds), K1) ]
e161499b
BA
47 }) )
48 }
49 else
50 {
51 unlist( lapply(indices_workers, function(inds)
0486fbad 52 inds[ algoClust1(getContribs(inds), K1) ]
e161499b
BA
53 ) )
54 }
492cd9e7
BA
55 }
56 if (parll)
57 parallel::stopCluster(cl)
58
56857861 59 indices #medoids
5c652979
BA
60}
61
4bcfdbee
BA
62#' @rdname clustering
63#' @export
282342ba
BA
64clusteringTask2 <- function(indices, getSeries, K2, algoClust2, nb_series_per_chunk,
65 smooth_lvl, nvoice, nbytes, endian, ncores_clust=1, verbose=FALSE, parll=TRUE)
5c652979 66{
e161499b 67 if (verbose)
3c5a4b08 68 cat(paste("*** Clustering task 2 on ",length(indices)," medoids\n", sep=""))
d9bb53c5 69
3c5a4b08
BA
70 if (length(indices) <= K2)
71 return (indices)
d9bb53c5 72
3c5a4b08 73 # A) Compute the WER distances (Wavelets Extended coefficient of deteRmination)
282342ba
BA
74 distances <- computeWerDists(indices, getSeries, nb_series_per_chunk,
75 smooth_lvl, nvoice, nbytes, endian, ncores_clust, verbose, parll)
d9bb53c5 76
3c5a4b08 77 # B) Apply clustering algorithm 2 on the WER distances matrix
e161499b 78 if (verbose)
a52836b2 79 cat(paste("*** algoClust2() on ",nrow(distances)," items\n", sep=""))
3c5a4b08 80 indices[ algoClust2(distances,K2) ]
e161499b 81}