#' 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,
#' @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.
#' 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)
#'
#' 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)
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)
{
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.
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())
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))
}
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(