3 #' @aliases clusteringTask1 clusteringTask2 computeClusters1 computeClusters2
5 #' @title Two-stage clustering, withing one task (see \code{claws()})
7 #' @description \code{clusteringTask1()} runs one full stage-1 task, which consists in
8 #' iterated stage 1 clustering (on nb_curves / ntasks energy contributions, computed
9 #' through discrete wavelets coefficients).
10 #' \code{clusteringTask2()} runs a full stage-2 task, which consists in synchrones
11 #' and then WER distances computations, before applying the clustering algorithm.
12 #' \code{computeClusters1()} and \code{computeClusters2()} correspond to the atomic
13 #' clustering procedures respectively for stage 1 and 2. The former applies the
14 #' first clustering algorithm on a contributions matrix, while the latter clusters
15 #' a set of series inside one task (~nb_items_clust1)
17 #' @param indices Range of series indices to cluster in parallel (initial data)
18 #' @param getContribs Function to retrieve contributions from initial series indices:
19 #' \code{getContribs(indices)} outpus a contributions matrix
20 #' @inheritParams computeSynchrones
21 #' @inheritParams claws
23 #' @return For \code{clusteringTask1()}, the indices of the computed (K1) medoids.
24 #' Indices are irrelevant for stage 2 clustering, thus \code{clusteringTask2()}
25 #' outputs a big.matrix of medoids (of size LxK2, K2 = final number of clusters)
30 clusteringTask1 = function(indices, getContribs, K1, algoClust1, nb_items_clust1,
31 ncores_clust=1, verbose=FALSE, parll=TRUE)
35 cl = parallel::makeCluster(ncores_clust, outfile = "")
36 parallel::clusterExport(cl, varlist=c("getContribs","K1","verbose"), envir=environment())
38 while (length(indices) > K1)
40 indices_workers = .spreadIndices(indices, nb_items_clust1)
42 cat(paste("*** [iterated] Clustering task 1 on ",length(indices)," series\n", sep=""))
46 unlist( parallel::parLapply(cl, indices_workers, function(inds) {
47 require("epclust", quietly=TRUE)
48 inds[ algoClust1(getContribs(inds), K1) ]
53 unlist( lapply(indices_workers, function(inds)
54 inds[ algoClust1(getContribs(inds), K1) ]
59 parallel::stopCluster(cl)
66 clusteringTask2 = function(medoids, K2, algoClust2, getRefSeries, nb_ref_curves,
67 nb_series_per_chunk, sync_mean, nbytes,endian,ncores_clust=1,verbose=FALSE,parll=TRUE)
70 cat(paste("*** Clustering task 2 on ",ncol(medoids)," synchrones\n", sep=""))
72 if (ncol(medoids) <= K2)
74 synchrones = computeSynchrones(medoids, getRefSeries, nb_ref_curves,
75 nb_series_per_chunk, sync_mean, ncores_clust, verbose, parll)
76 distances = computeWerDists(synchrones, nbytes, endian, ncores_clust, verbose, parll)
78 cat(paste(" algoClust2() on ",nrow(distances)," items\n", sep=""))
79 medoids[ algoClust2(distances,K2), ]
84 #' Compute the synchrones curves (sum of clusters elements) from a matrix of medoids,
85 #' using L2 distances.
87 #' @param medoids big.matrix of medoids (curves of same length as initial series)
88 #' @param getRefSeries Function to retrieve initial series (e.g. in stage 2 after series
89 #' have been replaced by stage-1 medoids)
90 #' @param nb_ref_curves How many reference series? (This number is known at this stage)
91 #' @inheritParams claws
93 #' @return A big.matrix of size L x K1 where L = length of a serie
96 computeSynchrones = function(medoids, getRefSeries, nb_ref_curves,
97 nb_series_per_chunk, sync_mean, ncores_clust=1,verbose=FALSE,parll=TRUE)
99 computeSynchronesChunk = function(indices)
103 require("bigmemory", quietly=TRUE)
104 requireNamespace("synchronicity", quietly=TRUE)
105 require("epclust", quietly=TRUE)
106 synchrones <- bigmemory::attach.big.matrix(synchrones_desc)
108 counts <- bigmemory::attach.big.matrix(counts_desc)
109 medoids <- bigmemory::attach.big.matrix(medoids_desc)
110 m <- synchronicity::attach.mutex(m_desc)
113 ref_series = getRefSeries(indices)
114 nb_series = nrow(ref_series)
116 # Get medoids indices for this chunk of series
117 mi = computeMedoidsIndices(medoids@address, ref_series)
119 for (i in seq_len(nb_series))
122 synchronicity::lock(m)
123 synchrones[, mi[i] ] = synchrones[, mi[i] ] + ref_series[,i]
125 counts[ mi[i] ] = counts[ mi[i] ] + 1
127 synchronicity::unlock(m)
131 K = ncol(medoids) ; L = nrow(medoids)
132 # Use bigmemory (shared==TRUE by default) + synchronicity to fill synchrones in //
133 # TODO: if size > RAM (not our case), use file-backed big.matrix
134 synchrones = bigmemory::big.matrix(nrow=L, ncol=K, type="double", init=0.)
136 counts = bigmemory::big.matrix(nrow=K, ncol=1, type="double", init=0)
137 # synchronicity is only for Linux & MacOS; on Windows: run sequentially
138 parll = (requireNamespace("synchronicity",quietly=TRUE)
139 && parll && Sys.info()['sysname'] != "Windows")
142 m <- synchronicity::boost.mutex()
143 m_desc <- synchronicity::describe(m)
144 synchrones_desc = bigmemory::describe(synchrones)
146 counts_desc = bigmemory::describe(counts)
147 medoids_desc = bigmemory::describe(medoids)
148 cl = parallel::makeCluster(ncores_clust)
149 varlist=c("synchrones_desc","sync_mean","m_desc","medoids_desc","getRefSeries")
151 varlist = c(varlist, "counts_desc")
152 parallel::clusterExport(cl, varlist, envir=environment())
158 cat(paste("--- Compute ",K," synchrones with ",nb_ref_curves," series\n", sep=""))
160 indices_workers = .spreadIndices(seq_len(nb_ref_curves), nb_series_per_chunk)
163 parallel::parLapply(cl, indices_workers, computeSynchronesChunk)
165 lapply(indices_workers, computeSynchronesChunk)
168 parallel::stopCluster(cl)
173 #TODO: can we avoid this loop? ( synchrones = sweep(synchrones, 2, counts, '/') )
174 for (i in seq_len(K))
175 synchrones[,i] = synchrones[,i] / counts[i]
176 #NOTE: odds for some clusters to be empty? (when series already come from stage 2)
177 # ...maybe; but let's hope resulting K1' be still quite bigger than K2
178 noNA_rows = sapply(seq_len(K), function(i) all(!is.nan(synchrones[,i])))
181 # Else: some clusters are empty, need to slice synchrones
182 bigmemory::as.big.matrix(synchrones[,noNA_rows])
187 #' Compute the WER distances between the synchrones curves (in rows), which are
188 #' returned (e.g.) by \code{computeSynchrones()}
190 #' @param synchrones A big.matrix of synchrones, in rows. The series have same length
191 #' as the series in the initial dataset
192 #' @inheritParams claws
194 #' @return A matrix of size K1 x K1
197 computeWerDists = function(synchrones, nbytes,endian,ncores_clust=1,verbose=FALSE,parll=TRUE)
199 n <- nrow(synchrones)
200 delta <- ncol(synchrones)
201 #TODO: automatic tune of all these parameters ? (for other users)
203 # noctave = 2^13 = 8192 half hours ~ 180 days ; ~log2(ncol(synchrones))
205 # 4 here represent 2^5 = 32 half-hours ~ 1 day
206 #NOTE: default scalevector == 2^(0:(noctave * nvoice) / nvoice) * s0 (?)
207 scalevector <- 2^(4:(noctave * nvoice) / nvoice + 1)
208 #condition: ( log2(s0*w0/(2*pi)) - 1 ) * nvoice + 1.5 >= 1
212 s0log = as.integer( (log2( s0*w0/(2*pi) ) - 1) * nvoice + 1.5 )
213 totnoct = noctave + as.integer(s0log/nvoice) + 1
215 Xwer_dist <- bigmemory::big.matrix(nrow=n, ncol=n, type="double")
217 cwt_file = ".epclust_bin/cwt"
218 #TODO: args, nb_per_chunk, nbytes, endian
220 # Generate n(n-1)/2 pairs for WER distances computations
226 pairs = c(pairs, lapply(V, function(v) c(i,v)))
229 computeSaveCWT = function(index)
231 ts <- scale(ts(synchrones[index,]), center=TRUE, scale=scaled)
232 totts.cwt = Rwave::cwt(ts, totnoct, nvoice, w0, plot=FALSE)
233 ts.cwt = totts.cwt[,s0log:(s0log+noctave*nvoice)]
235 sqs <- sqrt(2^(0:(noctave*nvoice)/nvoice)*s0)
236 sqres <- sweep(ts.cwt,2,sqs,'*')
237 res <- sqres / max(Mod(sqres))
238 #TODO: serializer les CWT, les récupérer via getDataInFile ;
239 #--> OK, faut juste stocker comme séries simples de taille delta*ncol (53*17519)
240 binarize(c(as.double(Re(res)),as.double(Im(res))), cwt_file, ncol(res), ",", nbytes, endian)
245 cl = parallel::makeCluster(ncores_clust)
246 synchrones_desc <- bigmemory::describe(synchrones)
247 Xwer_dist_desc <- bigmemory::describe(Xwer_dist)
248 parallel::clusterExport(cl, varlist=c("synchrones_desc","Xwer_dist_desc","totnoct",
249 "nvoice","w0","s0log","noctave","s0","verbose","getCWT"), envir=environment())
254 cat(paste("--- Compute WER dists\n", sep=""))
255 # precompute save all CWT........
257 #precompute and serialize all CWT
260 parallel::parLapply(cl, 1:n, computeSaveCWT)
262 lapply(1:n, computeSaveCWT)
264 getCWT = function(index)
267 res <- getDataInFile(c(2*index-1,2*index), cwt_file, nbytes, endian)
271 # Distance between rows i and j
272 computeDistancesIJ = function(pair)
276 require("bigmemory", quietly=TRUE)
277 require("epclust", quietly=TRUE)
278 synchrones <- bigmemory::attach.big.matrix(synchrones_desc)
279 Xwer_dist <- bigmemory::attach.big.matrix(Xwer_dist_desc)
282 i = pair[1] ; j = pair[2]
283 if (verbose && j==i+1)
284 cat(paste(" Distances (",i,",",j,"), (",i,",",j+1,") ...\n", sep=""))
288 num <- epclustFilter(Mod(cwt_i * Conj(cwt_j)))
289 WX <- epclustFilter(Mod(cwt_i * Conj(cwt_i)))
290 WY <- epclustFilter(Mod(cwt_j * Conj(cwt_j)))
291 wer2 <- sum(colSums(num)^2) / sum(colSums(WX) * colSums(WY))
292 Xwer_dist[i,j] <- sqrt(delta * ncol(cwt_i) * max(1 - wer2, 0.)) #FIXME: wer2 should be < 1
293 Xwer_dist[j,i] <- Xwer_dist[i,j]
299 cat(paste("--- Compute WER dists\n", sep=""))
303 parallel::parLapply(cl, pairs, computeDistancesIJ)
305 lapply(pairs, computeDistancesIJ)
308 parallel::stopCluster(cl)
311 distances <- Xwer_dist[,]
313 distances #~small matrix K1 x K1
316 # Helper function to divide indices into balanced sets
317 .spreadIndices = function(indices, nb_per_set)
320 nb_workers = floor( L / nb_per_set )
321 rem = L %% max_nb_per_set
322 if (nb_workers == 0 || (nb_workers==1 && rem==0))
324 # L <= max_nb_per_set, simple case
325 indices_workers = list(indices)
329 indices_workers = lapply( seq_len(nb_workers), function(i)
330 indices[(nb_per_chunk*(i-1)+1):(nb_per_set*i)] )
331 # Spread the remaining load among the workers
332 rem = L %% nb_per_set
335 index = rem%%nb_workers + 1
336 indices_workers[[index]] = c(indices_workers[[index]], indices[L-rem+1])