-# Compute the synchrones curves (sum of clusters elements) from a clustering result
-computeSynchrones = function(indices, getSeries, getSeriesForSynchrones)
-{
- #les getSeries(indices) sont les medoides --> init vect nul pour chacun, puis incr avec les
- #courbes (getSeriesForSynchrones) les plus proches... --> au sens de la norme L2 ?
- series = getSeries(indices)
- #...........
- #sapply(seq_along(inds), colMeans(getSeries(inds[[i]]$indices,inds[[i]]$ids)))
+ K = nrow(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=ncol(medoids),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()
+
+ if (parll)
+ {
+ cl = parallel::makeCluster(ncores_clust)
+ parallel::clusterExport(cl,
+ varlist=c("synchrones","counts","verbose","medoids","getRefSeries"),
+ envir=environment())
+ }
+
+ indices_workers = .spreadIndices(seq_len(nb_ref_curves), nb_series_per_chunk)
+ ignored <-
+ if (parll)
+ parallel::parLapply(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,]