Commit | Line | Data |
---|---|---|
7f0781b7 BA |
1 | #' @title Cluster power curves with PAM in parallel |
2 | #' | |
3 | #' @description Groups electricity power curves (or any series of similar nature) by applying PAM | |
cea14f3a | 4 | #' algorithm in parallel to chunks of size \code{nb_series_per_chunk} |
7f0781b7 BA |
5 | #' |
6 | #' @param data Access to the data, which can be of one of the three following types: | |
7 | #' \itemize{ | |
8 | #' \item data.frame: each line contains its ID in the first cell, and all values after | |
9 | #' \item connection: any R connection object (e.g. a file) providing lines as described above | |
c33af7e4 BA |
10 | #' \item function: a custom way to retrieve the curves; it has two arguments: the ranks to be |
11 | #' retrieved, and the IDs - at least one of them must be present (priority: ranks). | |
7f0781b7 | 12 | #' } |
1c6f223e BA |
13 | #' @param K1 Number of super-consumers to be found after stage 1 (K1 << N) |
14 | #' @param K2 Number of clusters to be found after stage 2 (K2 << K1) | |
15 | #' @param ntasks Number of tasks (parallel iterations to obtain K1 medoids); default: 1. | |
16 | #' Note: ntasks << N, so that N is "roughly divisible" by N (number of series) | |
17 | #' @param nb_series_per_chunk (Maximum) number of series in each group, inside a task | |
cea14f3a | 18 | #' @param min_series_per_chunk Minimum number of series in each group |
3465b246 | 19 | #' @param wf Wavelet transform filter; see ?wt.filter. Default: haar |
7f0781b7 BA |
20 | #' @param WER "end" to apply stage 2 after stage 1 has iterated and finished, or "mix" |
21 | #' to apply it after every stage 1 | |
5c652979 BA |
22 | #' @param ncores_tasks "MPI" number of parallel tasks (1 to disable: sequential tasks) |
23 | #' @param ncores_clust "OpenMP" number of parallel clusterings in one task | |
24 | #' @param random Randomize chunks repartition | |
7f0781b7 BA |
25 | #' |
26 | #' @return A data.frame of the final medoids curves (identifiers + values) | |
1c6f223e BA |
27 | #' |
28 | #' @examples | |
29 | #' getData = function(start, n) { | |
30 | #' con = dbConnect(drv = RSQLite::SQLite(), dbname = "mydata.sqlite") | |
31 | #' df = dbGetQuery(con, paste( | |
32 | #' "SELECT * FROM times_values GROUP BY id OFFSET ",start, | |
33 | #' "LIMIT ", n, " ORDER BY date", sep="")) | |
34 | #' return (df) | |
35 | #' } | |
5c652979 BA |
36 | #' #TODO: 3 examples, data.frame / binary file / DB sqLite |
37 | #' + sampleCurves : wavBootstrap de package wmtsa | |
1c6f223e BA |
38 | #' cl = epclust(getData, K1=200, K2=15, ntasks=1000, nb_series_per_chunk=5000, WER="mix") |
39 | #' @export | |
5c652979 BA |
40 | epclust = function(data, K1, K2, ntasks=1, nb_series_per_chunk=50*K1, min_series_per_chunk=5*K1, |
41 | wf="haar", WER="end", ncores_tasks=1, ncores_clust=4, random=TRUE) | |
ac1d4231 | 42 | { |
7b13d0c2 | 43 | # Check arguments |
7f0781b7 | 44 | if (!is.data.frame(data) && !is.function(data)) |
5c652979 | 45 | { |
6ecf5c2d BA |
46 | tryCatch( |
47 | { | |
48 | if (is.character(data)) | |
cea14f3a | 49 | data_con = file(data, open="r") |
5c652979 | 50 | else if (!isOpen(data)) |
6ecf5c2d BA |
51 | { |
52 | open(data) | |
cea14f3a | 53 | data_con = data |
6ecf5c2d BA |
54 | } |
55 | }, | |
5c652979 BA |
56 | error=function(e) "data should be a data.frame, a function or a valid connection" |
57 | ) | |
58 | } | |
59 | K1 = toInteger(K1, function(x) x>=2) | |
60 | K2 = toInteger(K2, function(x) x>=2) | |
c33af7e4 | 61 | ntasks = toInteger(ntasks, function(x) x>=1) |
5c652979 BA |
62 | nb_series_per_chunk = toInteger(nb_series_per_chunk, function(x) x>=K1) |
63 | min_series_per_chunk = toInteger(K1, function(x) x>=K1 && x<=nb_series_per_chunk) | |
64 | ncores_tasks = toInteger(ncores_tasks, function(x) x>=1) | |
65 | ncores_clust = toInteger(ncores_clust, function(x) x>=1) | |
7f0781b7 BA |
66 | if (WER!="end" && WER!="mix") |
67 | stop("WER takes values in {'end','mix'}") | |
ac1d4231 | 68 | |
7b13d0c2 | 69 | # Serialize all wavelets coefficients (+ IDs) onto a file |
5c652979 | 70 | coeffs_file = ".coeffs" |
23844f60 | 71 | ids_files = ".ids" |
7f0781b7 | 72 | index = 1 |
cea14f3a | 73 | nb_curves = 0 |
5c652979 | 74 | nb_coeffs = NA |
6ecf5c2d | 75 | repeat |
ac1d4231 | 76 | { |
5c652979 | 77 | coeffs_chunk = computeCoeffs(data, index, nb_series_per_chunk, wf) |
cea14f3a BA |
78 | if (is.null(coeffs_chunk)) |
79 | break | |
c33af7e4 | 80 | serialize(coeffs_chunk, coeffs_file, append=TRUE) |
cea14f3a | 81 | index = index + nb_series_per_chunk |
5c652979 BA |
82 | nb_curves = nb_curves + nrow(coeffs_chunk) |
83 | if (is.na(nb_coeffs)) | |
84 | nb_coeffs = ncol(coeffs_chunk)-1 | |
8e6accca BA |
85 | } |
86 | ||
5c652979 BA |
87 | if (nb_curves < min_series_per_chunk) |
88 | stop("Not enough data: less rows than min_series_per_chunk!") | |
89 | nb_series_per_task = round(nb_curves / ntasks) | |
90 | if (nb_series_per_task < min_series_per_chunk) | |
91 | stop("Too many tasks: less series in one task than min_series_per_chunk!") | |
ac1d4231 | 92 | |
7b13d0c2 BA |
93 | # Cluster coefficients in parallel (by nb_series_per_chunk) |
94 | indices = if (random) sample(nb_curves) else seq_len(nb_curves) #all indices | |
95 | indices_tasks = list() #indices to be processed in each task | |
5c652979 | 96 | for (i in seq_len(ntasks)) |
8e6accca | 97 | { |
5c652979 BA |
98 | upper_bound = ifelse( i<ntasks, min(nb_series_per_task*i,nb_curves), nb_curves ) |
99 | indices_task[[i]] = indices[((i-1)*nb_series_per_task+1):upper_bound] | |
8e6accca | 100 | } |
5c652979 BA |
101 | library(parallel, quietly=TRUE) |
102 | cl_tasks = parallel::makeCluster(ncores_tasks) | |
7b13d0c2 BA |
103 | #parallel::clusterExport(cl=cl_tasks, varlist=c("ncores_clust", ...), envir=environment()) |
104 | indices = parallel::parLapply(cl_tasks, indices_tasks, clusteringStep12, ) | |
5c652979 | 105 | parallel::stopCluster(cl_tasks) |
3465b246 | 106 | |
7b13d0c2 BA |
107 | ##TODO: passer data ?! |
108 | ||
109 | # Run step1+2 step on resulting ranks | |
110 | ranks = clusteringStep12() | |
5c652979 | 111 | return (list("ranks"=ranks, "medoids"=getSeries(data, ranks))) |
cea14f3a | 112 | } |