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