+ if (verbose)
+ cat(paste("--- Compute synchrones\n", sep=""))
+
+ computeSynchronesChunk = function(indices)
+ {
+ ref_series = getRefSeries(indices)
+ nb_series = nrow(ref_series)
+
+ if (parll)
+ {
+ require("bigmemory", quietly=TRUE)
+ require("synchronicity", quietly=TRUE)
+ require("epclust", quietly=TRUE)
+ synchrones <- bigmemory::attach.big.matrix(synchrones_desc)
+ medoids <- bigmemory::attach.big.matrix(medoids_desc)
+ m <- synchronicity::attach.mutex(m_desc)
+ }
+
+ #get medoids indices for this chunk of series
+ mi = computeMedoidsIndices(medoids@address, ref_series)
+# #R-equivalent, requiring a matrix (thus potentially breaking "fit-in-memory" hope)
+# mat_meds = medoids[,]
+# mi = rep(NA,nb_series)
+# for (i in 1:nb_series)
+# mi[i] <- which.min( rowSums( sweep(mat_meds, 2, ref_series[i,], '-')^2 ) )
+# rm(mat_meds); gc()
+
+ for (i in seq_len(nb_series))
+ {
+ if (parll)
+ synchronicity::lock(m)
+ synchrones[mi[i],] = synchrones[mi[i],] + ref_series[i,]
+#TODO: remove counts
+ counts[mi[i],1] = counts[mi[i],1] + 1
+ if (parll)
+ synchronicity::unlock(m)
+ }
+ }
+
+ K = nrow(medoids) ; L = ncol(medoids)
+ # Use bigmemory (shared==TRUE by default) + synchronicity to fill synchrones in //
+ # TODO: if size > RAM (not our case), use file-backed big.matrix
+ synchrones = bigmemory::big.matrix(nrow=K, ncol=L, type="double", init=0.)
+ counts = bigmemory::big.matrix(nrow=K, ncol=1, type="double", init=0)
+ # synchronicity is only for Linux & MacOS; on Windows: run sequentially
+ parll = (requireNamespace("synchronicity",quietly=TRUE)
+ && parll && Sys.info()['sysname'] != "Windows")
+ if (parll)
+ {
+ m <- synchronicity::boost.mutex()
+ m_desc <- synchronicity::describe(m)
+ synchrones_desc = bigmemory::describe(synchrones)
+ medoids_desc = bigmemory::describe(medoids)
+
+ cl = parallel::makeCluster(ncores_clust)
+ parallel::clusterExport(cl,
+ varlist=c("synchrones_desc","counts","verbose","m_desc","medoids_desc","getRefSeries"),
+ envir=environment())
+ }
+
+ indices_workers = .spreadIndices(seq_len(nb_ref_curves), nb_series_per_chunk)
+ ignored <-
+ if (parll)
+ parallel::parLapply(cl, indices_workers, computeSynchronesChunk)
+ else
+ lapply(indices_workers, computeSynchronesChunk)
+
+ if (parll)
+ parallel::stopCluster(cl)
+
+ #TODO: can we avoid this loop? ( synchrones = sweep(synchrones, 1, counts, '/') )
+ for (i in seq_len(K))
+ synchrones[i,] = synchrones[i,] / counts[i,1]
+ #NOTE: odds for some clusters to be empty? (when series already come from stage 2)
+ # ...maybe; but let's hope resulting K1' be still quite bigger than K2
+ noNA_rows = sapply(seq_len(K), function(i) all(!is.nan(synchrones[i,])))
+ if (all(noNA_rows))
+ return (synchrones)
+ # Else: some clusters are empty, need to slice synchrones
+ synchrones[noNA_rows,]
+}
+
+#' computeWerDists
+#'
+#' Compute the WER distances between the synchrones curves (in rows), which are
+#' returned (e.g.) by \code{computeSynchrones()}
+#'
+#' @param synchrones A big.matrix of synchrones, in rows. The series have same length
+#' as the series in the initial dataset
+#' @inheritParams claws
+#'
+#' @return A matrix of size K1 x K1
+#'
+#' @export
+computeWerDists = function(synchrones, ncores_clust=1,verbose=FALSE,parll=TRUE)
+{
+ if (verbose)
+ cat(paste("--- Compute WER dists\n", sep=""))
+
+ n <- nrow(synchrones)
+ delta <- ncol(synchrones)