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