progress in main.R
[epclust.git] / code / draft_R_pkg / R / main.R
CommitLineData
7f0781b7 1#' @include defaults.R
3dcbfeef 2
7f0781b7
BA
3#' @title Cluster power curves with PAM in parallel
4#'
5#' @description Groups electricity power curves (or any series of similar nature) by applying PAM
6#' algorithm in parallel to chunks of size \code{nbSeriesPerChunk}
7#'
8#' @param data Access to the data, which can be of one of the three following types:
9#' \itemize{
10#' \item data.frame: each line contains its ID in the first cell, and all values after
11#' \item connection: any R connection object (e.g. a file) providing lines as described above
12#' \item function: a custom way to retrieve the curves; it has two arguments: the start index
13#' (start) and number of curves (n); see example in package vignette.
14#' }
15#' @param K Number of clusters
16#' @param nbSeriesPerChunk Number of series in each group
17#' @param writeTmp Function to write temporary wavelets coefficients (+ identifiers);
18#' see defaults in defaults.R
19#' @param readTmp Function to read temporary wavelets coefficients (see defaults.R)
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
22#' @param ncores number of parallel processes; if NULL, use parallel::detectCores()
23#'
24#' @return A data.frame of the final medoids curves (identifiers + values)
25epclust = function(data, K, nbSeriesPerChunk, writeTmp=ref_writeTmp, readTmp=ref_readTmp,
26 WER="end", ncores=NULL)
ac1d4231 27{
7f0781b7
BA
28 #TODO: setRefClass(...) to avoid copy data:
29 #http://stackoverflow.com/questions/2603184/r-pass-by-reference
ac1d4231 30
7f0781b7
BA
31 #0) check arguments
32 if (!is.data.frame(data) && !is.function(data))
6ecf5c2d
BA
33 tryCatch(
34 {
35 if (is.character(data))
36 {
37 dataCon = file(data, open="r")
38 } else if (!isOpen(data))
39 {
40 open(data)
41 dataCon = data
42 }
43 },
7f0781b7
BA
44 error="data should be a data.frame, a function or a valid connection")
45 if (!is.integer(K) || K < 2)
46 stop("K should be an integer greater or equal to 2")
47 if (!is.integer(nbSeriesPerChunk) || nbSeriesPerChunk < K)
48 stop("nbSeriesPerChunk should be an integer greater or equal to K")
49 if (!is.function(writeTmp) || !is.function(readTmp))
50 stop("read/writeTmp should be functional (see defaults.R)")
51 if (WER!="end" && WER!="mix")
52 stop("WER takes values in {'end','mix'}")
53 #concerning ncores, any non-integer type will be treated as "use parallel:detectCores()"
ac1d4231 54
3d061515 55 #1) acquire data (process curves, get as coeffs)
7f0781b7 56 index = 1
6ecf5c2d
BA
57 nbCurves = 0
58 repeat
ac1d4231 59 {
7f0781b7 60 if (is.data.frame(data))
3dcbfeef 61 {
7f0781b7 62 #full data matrix
b9f1c0c7
BA
63 if (index < nrow(data))
64 {
65 writeTmp( getCoeffs( data[index:(min(index+nbSeriesPerChunk-1,nrow(data))),] ) )
66 } else
67 {
68 break
69 }
7f0781b7
BA
70 } else if (is.function(data))
71 {
72 #custom user function to retrieve next n curves, probably to read from DB
b9f1c0c7
BA
73 coeffs_chunk = getCoeffs( data(index, nbSeriesPerChunk) )
74 if (!is.null(coeffs_chunk))
75 {
76 writeTmp(coeffs_chunk)
77 } else
78 {
79 break
80 }
7f0781b7
BA
81 } else
82 {
83 #incremental connection
84 #TODO: find a better way to parse than using a temp file
85 ascii_lines = readLines(dataCon, nbSeriesPerChunk)
b9f1c0c7
BA
86 if (length(ascii_lines > 0))
87 {
88 seriesChunkFile = ".tmp/seriesChunk"
89 writeLines(ascii_lines, seriesChunkFile)
90 writeTmp( getCoeffs( read.csv(seriesChunkFile) ) )
91 } else
92 {
93 break
94 }
3dcbfeef 95 }
7f0781b7 96 index = index + nbSeriesPerChunk
8e6accca 97 }
7f0781b7
BA
98 if (exists(dataCon))
99 close(dataCon)
8e6accca
BA
100
101 library(parallel)
7f0781b7 102 ncores = ifelse(is.integer(ncores), ncores, parallel::detectCores())
8e6accca 103 cl = parallel::makeCluster(ncores)
7f0781b7 104 parallel::clusterExport(cl=cl, varlist=c("X", "Y", "K", "p"), envir=environment())
6ecf5c2d 105 library(cluster)
b9f1c0c7 106 li = parallel::parLapply(cl, 1:B, )
3d061515 107
6ecf5c2d
BA
108 #2) process coeffs (by nbSeriesPerChunk) and cluster them in parallel
109 #TODO: be careful of writing to a new temp file, then flush initial one, then re-use it...
8e6accca
BA
110 repeat
111 {
112 completed = rep(FALSE, ............)
113 #while there is jobs to do (i.e. size of tmp "file" is greater than nbSeriesPerChunk),
114 #A) determine which tasks which processor will do (OK)
115 #B) send each (sets of) tasks in parallel
116 #C) flush tmp file (current parallel processes will write in it)
117 #always check "complete" flag (array, as I did in MPI) to know if "slaves" finished
118 }
b9f1c0c7 119pam(x, k)
7f0781b7 120 parallel::stopCluster(cl)
3d061515 121
8e6accca 122 #3) readTmp last results, apply PAM on it, and return medoids + identifiers
ac1d4231 123
8e6accca
BA
124 #4) apply stage 2 (in parallel ? inside task 2) ?)
125 if (WER == "end")
126 {
127 #from center curves, apply stage 2...
128 }
ac1d4231 129}