export vars to nodes
[epclust.git] / epclust / R / clustering.R
CommitLineData
7b13d0c2 1# Cluster one full task (nb_curves / ntasks series)
48108c39 2clusteringTask = function(indices_clust)
5c652979 3{
7b13d0c2 4 cl_clust = parallel::makeCluster(ncores_clust)
48108c39
BA
5 parallel::clusterExport(cl_clust,
6 varlist=c("K1","K2","WER"),
7 envir=environment())
7b13d0c2
BA
8 repeat
9 {
10 nb_workers = max( 1, round( length(indices_clust) / nb_series_per_chunk ) )
48108c39 11 indices_workers = lapply(seq_len(nb_workers), function(i) {
7b13d0c2
BA
12 upper_bound = ifelse( i<nb_workers,
13 min(nb_series_per_chunk*i,length(indices_clust)), length(indices_clust) )
48108c39
BA
14 indices_clust[(nb_series_per_chunk*(i-1)+1):upper_bound]
15 })
16 indices_clust = parallel::parLapply(cl, indices_workers, clusterChunk)
74f571a7 17 # TODO: soft condition between K2 and K1, before applying final WER step
7b13d0c2
BA
18 if ((WER=="end" && length(indices_clust)==K1) || (WER=="mix" && length(indices_clust)==K2))
19 break
20 }
21 parallel::stopCluster(cl_clust)
22 unlist(indices_clust)
5c652979
BA
23}
24
7b13d0c2 25# Cluster a chunk of series inside one task (~max nb_series_per_chunk)
48108c39 26clusterChunk = function(indices_chunk)
5c652979 27{
48108c39 28 coeffs = readCoeffs(indices_chunk)
7b13d0c2 29 cl = computeClusters(as.matrix(coeffs[,2:ncol(coeffs)]), K1, diss=FALSE)
48108c39 30 if (WER=="mix" > 0)
5c652979
BA
31 {
32 curves = computeSynchrones(cl)
33 dists = computeWerDists(curves)
7b13d0c2 34 cl = computeClusters(dists, K2, diss=TRUE)
5c652979 35 }
48108c39 36 indices_chunk[cl]
5c652979
BA
37}
38
7b13d0c2
BA
39# Apply the clustering algorithm (PAM) on a coeffs or distances matrix
40computeClusters = function(md, K, diss)
5c652979 41{
7b13d0c2
BA
42 if (!require(cluster, quietly=TRUE))
43 stop("Unable to load cluster library")
44 cluster::pam(md, K, diss=diss)$id.med
5c652979
BA
45}
46
7b13d0c2
BA
47# Compute the synchrones curves (sum of clusters elements) from a clustering result
48computeSynchrones = function(indices)
5c652979 49{
7b13d0c2 50 colSums( getData(indices) )
5c652979 51}
1c6f223e 52
7b13d0c2
BA
53# Compute the WER distance between the synchrones curves
54computeWerDist = function(curves)
d03c0621 55{
5c652979
BA
56 if (!require("Rwave", quietly=TRUE))
57 stop("Unable to load Rwave library")
7b13d0c2
BA
58 n <- nrow(curves)
59 delta <- ncol(curves)
db6fc17d 60 #TODO: automatic tune of all these parameters ? (for other users)
d03c0621 61 nvoice <- 4
7b13d0c2 62 # noctave = 2^13 = 8192 half hours ~ 180 days ; ~log2(ncol(curves))
d7d55bc1
BA
63 noctave = 13
64 # 4 here represent 2^5 = 32 half-hours ~ 1 day
db6fc17d
BA
65 #NOTE: default scalevector == 2^(0:(noctave * nvoice) / nvoice) * s0 (?)
66 scalevector <- 2^(4:(noctave * nvoice) / nvoice) * 2
67 #condition: ( log2(s0*w0/(2*pi)) - 1 ) * nvoice + 1.5 >= 1
68 s0=2
69 w0=2*pi
70 scaled=FALSE
71 s0log = as.integer( (log2( s0*w0/(2*pi) ) - 1) * nvoice + 1.5 )
72 totnoct = noctave + as.integer(s0log/nvoice) + 1
73
74 # (normalized) observations node with CWT
75 Xcwt4 <- lapply(seq_len(n), function(i) {
7b13d0c2 76 ts <- scale(ts(curves[i,]), center=TRUE, scale=scaled)
db6fc17d
BA
77 totts.cwt = Rwave::cwt(ts,totnoct,nvoice,w0,plot=0)
78 ts.cwt = totts.cwt[,s0log:(s0log+noctave*nvoice)]
79 #Normalization
80 sqs <- sqrt(2^(0:(noctave*nvoice)/nvoice)*s0)
81 sqres <- sweep(ts.cwt,MARGIN=2,sqs,'*')
82 sqres / max(Mod(sqres))
83 })
3ccd1e39 84
db6fc17d
BA
85 Xwer_dist <- matrix(0., n, n)
86 fcoefs = rep(1/3, 3) #moving average on 3 values (TODO: very slow! correct?!)
87 for (i in 1:(n-1))
1c6f223e 88 {
db6fc17d 89 for (j in (i+1):n)
d03c0621 90 {
db6fc17d
BA
91 #TODO: later, compute CWT here (because not enough storage space for 32M series)
92 # 'circular=TRUE' is wrong, should just take values on the sides; to rewrite in C
93 num <- filter(Mod(Xcwt4[[i]] * Conj(Xcwt4[[j]])), fcoefs, circular=TRUE)
94 WX <- filter(Mod(Xcwt4[[i]] * Conj(Xcwt4[[i]])), fcoefs, circular=TRUE)
95 WY <- filter(Mod(Xcwt4[[j]] * Conj(Xcwt4[[j]])), fcoefs, circular=TRUE)
96 wer2 <- sum(colSums(num)^2) / sum( sum(colSums(WX) * colSums(WY)) )
97 Xwer_dist[i,j] <- sqrt(delta * ncol(Xcwt4[[1]]) * (1 - wer2))
98 Xwer_dist[j,i] <- Xwer_dist[i,j]
d03c0621 99 }
1c6f223e 100 }
d03c0621 101 diag(Xwer_dist) <- numeric(n)
c6556868 102 Xwer_dist
1c6f223e 103}