X-Git-Url: https://git.auder.net/?p=epclust.git;a=blobdiff_plain;f=epclust%2FR%2Fmain.R;h=f6662678f9db2ae812054979a4173728ca08efd3;hp=bcc650abb8feee40cfc246ef3f95d27b13e478c4;hb=a52836b23adb4bfa6722642ec6426fb7b5f39650;hpb=d9bb53c5e1392018bf67f92140edb10137f3423c diff --git a/epclust/R/main.R b/epclust/R/main.R index bcc650a..f666267 100644 --- a/epclust/R/main.R +++ b/epclust/R/main.R @@ -27,7 +27,8 @@ #' series; the name was chosen because all types of arguments are converted to a function. #' When \code{getSeries} is given as a function, it must take a single argument, #' 'indices', integer vector equal to the indices of the curves to retrieve; -#' see SQLite example. The nature and role of other arguments should be clear +#' see SQLite example. The nature and role of other arguments should be clear. +#' WARNING: the return value must be a matrix (in columns), or NULL if no matches. #' \cr #' Note: Since we don't make assumptions on initial data, there is a possibility that #' even when serialized, contributions or synchrones do not fit in RAM. For example, @@ -61,6 +62,7 @@ #' @param contrib_type Type of contribution: "relative", "logit" or "absolute" (any prefix) #' @param WER "end" to apply stage 2 after stage 1 has fully iterated, or "mix" to apply #' stage 2 at the end of each task +#' @param nvoice Number of voices within each octave for CWT computations #' @param random TRUE (default) for random chunks repartition #' @param ntasks Number of tasks (parallel iterations to obtain K1 [if WER=="end"] #' or K2 [if WER=="mix"] medoids); default: 1. @@ -90,7 +92,7 @@ #' ref_series = matrix( c(cos(x),cos(2*x),cos(3*x),sin(x),sin(2*x),sin(3*x)), ncol=6 ) #' library(wmtsa) #' series = do.call( cbind, lapply( 1:6, function(i) -#' do.call(cbind, wmtsa::wavBootstrap(ref_series[i,], n.realization=400)) ) ) +#' do.call(cbind, wmtsa::wavBootstrap(ref_series[,i], n.realization=400)) ) ) #' #dim(series) #c(2400,10001) #' medoids_ascii = claws(series, K1=60, K2=6, 200, verbose=TRUE) #' @@ -130,7 +132,10 @@ #' request <- paste(request, indexToID_inDB[i], ",", sep="") #' request <- paste(request, ")", sep="") #' df_series <- dbGetQuery(series_db, request) -#' as.matrix(df_series[,"value"], nrow=serie_length) +#' if (length(df_series) >= 1) +#' as.matrix(df_series[,"value"], nrow=serie_length) +#' else +#' NULL #' } #' medoids_db = claws(getSeries, K1=60, K2=6, 200)) #' dbDisconnect(series_db) @@ -145,7 +150,7 @@ claws <- function(getSeries, K1, K2, nb_series_per_chunk, nb_items_clust1=7*K1, algoClust1=function(data,K) cluster::pam(t(data),K,diss=FALSE)$id.med, algoClust2=function(dists,K) cluster::pam(dists,K,diss=TRUE)$id.med, - wav_filt="d8", contrib_type="absolute", WER="end", random=TRUE, + wav_filt="d8", contrib_type="absolute", WER="end", nvoice=4, random=TRUE, ntasks=1, ncores_tasks=1, ncores_clust=4, sep=",", nbytes=4, endian=.Platform$endian, verbose=FALSE, parll=TRUE) { @@ -189,21 +194,32 @@ claws <- function(getSeries, K1, K2, nb_series_per_chunk, nb_items_clust1=7*K1, if (!is.function(getSeries)) { if (verbose) - cat("...Serialize time-series\n") - series_file = ".series.bin" ; unlink(series_file) - binarize(getSeries, series_file, nb_series_per_chunk, sep, nbytes, endian) + cat("...Serialize time-series (or retrieve past binary file)\n") + series_file = ".series.bin" + if (!file.exists(series_file)) + binarize(getSeries, series_file, nb_series_per_chunk, sep, nbytes, endian) getSeries = function(inds) getDataInFile(inds, series_file, nbytes, endian) } # Serialize all computed wavelets contributions into a file - contribs_file = ".contribs.bin" ; unlink(contribs_file) + contribs_file = ".contribs.bin" index = 1 nb_curves = 0 if (verbose) - cat("...Compute contributions and serialize them\n") - nb_curves = binarizeTransform(getSeries, - function(series) curvesToContribs(series, wav_filt, contrib_type), - contribs_file, nb_series_per_chunk, nbytes, endian) + cat("...Compute contributions and serialize them (or retrieve past binary file)\n") + if (!file.exists(contribs_file)) + { + nb_curves = binarizeTransform(getSeries, + function(series) curvesToContribs(series, wav_filt, contrib_type), + contribs_file, nb_series_per_chunk, nbytes, endian) + } + else + { + # TODO: duplicate from getDataInFile() in de_serialize.R + contribs_size = file.info(contribs_file)$size #number of bytes in the file + contrib_length = readBin(contribs_file, "integer", n=1, size=8, endian=endian) + nb_curves = (contribs_size-8) / (nbytes*contrib_length) + } getContribs = function(indices) getDataInFile(indices, contribs_file, nbytes, endian) # A few sanity checks: do not continue if too few data available. @@ -229,7 +245,7 @@ claws <- function(getSeries, K1, K2, nb_series_per_chunk, nb_items_clust1=7*K1, cl = parallel::makeCluster(ncores_tasks, outfile="") varlist = c("getSeries","getContribs","K1","K2","algoClust1","algoClust2", "nb_series_per_chunk","nb_items_clust1","ncores_clust", - "sep","nbytes","endian","verbose","parll") + "nvoice","sep","nbytes","endian","verbose","parll") if (WER=="mix" && ntasks>1) varlist = c(varlist, "medoids_file") parallel::clusterExport(cl, varlist, envir = environment()) @@ -253,7 +269,7 @@ claws <- function(getSeries, K1, K2, nb_series_per_chunk, nb_items_clust1=7*K1, require("bigmemory", quietly=TRUE) medoids1 = bigmemory::as.big.matrix( getSeries(indices_medoids) ) medoids2 = clusteringTask2(medoids1, K2, algoClust2, getSeries, nb_curves, - nb_series_per_chunk, nbytes, endian, ncores_clust, verbose, parll) + nb_series_per_chunk, nvoice, nbytes, endian, ncores_clust, verbose, parll) binarize(medoids2, medoids_file, nb_series_per_chunk, sep, nbytes, endian) return (vector("integer",0)) } @@ -314,7 +330,7 @@ claws <- function(getSeries, K1, K2, nb_series_per_chunk, nb_items_clust1=7*K1, nb_series_per_chunk, ncores_tasks*ncores_clust, verbose, parll) medoids1 = bigmemory::as.big.matrix( getSeries(indices_medoids) ) medoids2 = clusteringTask2(medoids1, K2, algoClust2, getRefSeries, nb_curves, - nb_series_per_chunk, nbytes, endian, ncores_tasks*ncores_clust, verbose, parll) + nb_series_per_chunk, nvoice, nbytes, endian, ncores_tasks*ncores_clust, verbose, parll) # Cleanup: remove temporary binary files tryCatch(