export vars to nodes
[epclust.git] / epclust / R / clustering.R
index 42e894c..578b2f3 100644 (file)
@@ -1,20 +1,19 @@
 # Cluster one full task (nb_curves / ntasks series)
-clusteringTask = function(K1, K2, WER, nb_series_per_chunk, indices_tasks, ncores_clust)
+clusteringTask = function(indices_clust)
 {
        cl_clust = parallel::makeCluster(ncores_clust)
-       #parallel::clusterExport(cl=cl_clust, varlist=c("fonctions_du_package"), envir=environment())
-       indices_clust = indices_task[[i]]
+       parallel::clusterExport(cl_clust,
+               varlist=c("K1","K2","WER"),
+               envir=environment())
        repeat
        {
                nb_workers = max( 1, round( length(indices_clust) / nb_series_per_chunk ) )
-               indices_workers = list()
-               for (i in 1:nb_workers)
-               {
+               indices_workers = lapply(seq_len(nb_workers), function(i) {
                        upper_bound = ifelse( i<nb_workers,
                                min(nb_series_per_chunk*i,length(indices_clust)), length(indices_clust) )
-                       indices_workers[[i]] = indices_clust[(nb_series_per_chunk*(i-1)+1):upper_bound]
-               }
-               indices_clust = parallel::parLapply(cl, indices_workers, clusterChunk, K1, K2*(WER=="mix"))
+                       indices_clust[(nb_series_per_chunk*(i-1)+1):upper_bound]
+               })
+               indices_clust = parallel::parLapply(cl, indices_workers, clusterChunk)
                # TODO: soft condition between K2 and K1, before applying final WER step
                if ((WER=="end" && length(indices_clust)==K1) || (WER=="mix" && length(indices_clust)==K2))
                        break
@@ -24,17 +23,17 @@ clusteringTask = function(K1, K2, WER, nb_series_per_chunk, indices_tasks, ncore
 }
 
 # Cluster a chunk of series inside one task (~max nb_series_per_chunk)
-clusterChunk = function(indices, K1, K2)
+clusterChunk = function(indices_chunk)
 {
-       coeffs = getCoeffs(indices)
+       coeffs = readCoeffs(indices_chunk)
        cl = computeClusters(as.matrix(coeffs[,2:ncol(coeffs)]), K1, diss=FALSE)
-       if (K2 > 0)
+       if (WER=="mix" > 0)
        {
                curves = computeSynchrones(cl)
                dists = computeWerDists(curves)
                cl = computeClusters(dists, K2, diss=TRUE)
        }
-       indices[cl]
+       indices_chunk[cl]
 }
 
 # Apply the clustering algorithm (PAM) on a coeffs or distances matrix