'update'
[epclust.git] / epclust / R / clustering.R
CommitLineData
3c5a4b08 1#' Two-stage clustering, within one task (see \code{claws()})
4bcfdbee 2#'
3c5a4b08 3#' \code{clusteringTask1()} runs one full stage-1 task, which consists in iterated
3fb6e823 4#' clustering on nb_curves / ntasks energy contributions, computed through
3c5a4b08
BA
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:
3fb6e823 11#' \code{getContribs(indices)} outputs a contributions matrix, in columns
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,
074a48c4 26 ncores_clust=3, verbose=FALSE)
5c652979 27{
dc86eb0c 28 if (verbose)
e0154a59 29 cat(paste("*** Clustering task 1 on ",length(indices)," series [start]\n", sep=""))
dc86eb0c
BA
30
31 if (length(indices) <= K1)
32 return (indices)
33
074a48c4 34 parll <- (ncores_clust > 1)
492cd9e7 35 if (parll)
7b13d0c2 36 {
282342ba 37 # outfile=="" to see stderr/stdout on terminal
3fb6e823
BA
38 cl <-
39 if (verbose)
40 parallel::makeCluster(ncores_clust, outfile = "")
41 else
42 parallel::makeCluster(ncores_clust)
d9bb53c5 43 parallel::clusterExport(cl, c("getContribs","K1","verbose"), envir=environment())
7b13d0c2 44 }
d9bb53c5 45 # Iterate clustering algorithm 1 until K1 medoids are found
492cd9e7
BA
46 while (length(indices) > K1)
47 {
d9bb53c5 48 # Balance tasks by splitting the indices set - as evenly as possible
282342ba 49 indices_workers <- .splitIndices(indices, nb_items_clust, min_size=K1+1)
e161499b
BA
50 indices <-
51 if (parll)
52 {
53 unlist( parallel::parLapply(cl, indices_workers, function(inds) {
54 require("epclust", quietly=TRUE)
0486fbad 55 inds[ algoClust1(getContribs(inds), K1) ]
e161499b
BA
56 }) )
57 }
58 else
59 {
60 unlist( lapply(indices_workers, function(inds)
0486fbad 61 inds[ algoClust1(getContribs(inds), K1) ]
e161499b
BA
62 ) )
63 }
dc86eb0c
BA
64 if (verbose)
65 {
e0154a59 66 cat(paste("*** Clustering task 1 on ",length(indices)," medoids [iter]\n", sep=""))
dc86eb0c 67 }
492cd9e7
BA
68 }
69 if (parll)
70 parallel::stopCluster(cl)
71
56857861 72 indices #medoids
5c652979
BA
73}
74
4bcfdbee
BA
75#' @rdname clustering
76#' @export
282342ba 77clusteringTask2 <- function(indices, getSeries, K2, algoClust2, nb_series_per_chunk,
074a48c4 78 smooth_lvl, nvoice, nbytes, endian, ncores_clust=3, verbose=FALSE)
5c652979 79{
e161499b 80 if (verbose)
3c5a4b08 81 cat(paste("*** Clustering task 2 on ",length(indices)," medoids\n", sep=""))
d9bb53c5 82
3c5a4b08
BA
83 if (length(indices) <= K2)
84 return (indices)
d9bb53c5 85
3c5a4b08 86 # A) Compute the WER distances (Wavelets Extended coefficient of deteRmination)
282342ba 87 distances <- computeWerDists(indices, getSeries, nb_series_per_chunk,
074a48c4 88 smooth_lvl, nvoice, nbytes, endian, ncores_clust, verbose)
d9bb53c5 89
3c5a4b08 90 # B) Apply clustering algorithm 2 on the WER distances matrix
e161499b 91 if (verbose)
a52836b2 92 cat(paste("*** algoClust2() on ",nrow(distances)," items\n", sep=""))
3c5a4b08 93 indices[ algoClust2(distances,K2) ]
e161499b 94}