export vars to nodes
[epclust.git] / epclust / R / computeCoeffs.R
CommitLineData
5c652979
BA
1computeCoeffs = function(data, index, nb_series_per_chunk, wf)
2{
3 coeffs_chunk = NULL
4 if (is.data.frame(data) && index < nrow(data))
5 {
6 #full data matrix
7 coeffs_chunk = curvesToCoeffs(
8 data[index:(min(index+nb_series_per_chunk-1,nrow(data))),], wf)
9 }
10 else if (is.function(data))
11 {
12 #custom user function to retrieve next n curves, probably to read from DB
13 coeffs_chunk = curvesToCoeffs( data(rank=(index-1)+seq_len(nb_series_per_chunk)), wf )
14 }
15 else if (exists(data_con))
16 {
17 #incremental connection ; TODO: more efficient way to parse than using a temp file
18 ascii_lines = readLines(data_con, nb_series_per_chunk)
19 if (length(ascii_lines > 0))
20 {
21 series_chunk_file = ".series_chunk"
22 writeLines(ascii_lines, series_chunk_file)
23 coeffs_chunk = curvesToCoeffs( read.csv(series_chunk_file), wf )
24 unlink(series_chunk_file)
25 }
26 }
27 coeffs_chunk
28}
29
5c652979
BA
30curvesToCoeffs = function(series, wf)
31{
32 if (!require(wavelets, quietly=TRUE))
33 stop("Couldn't load wavelets library")
34 L = length(series[1,])
23844f60 35 D = ceiling( log2(L) )
5c652979
BA
36 nb_sample_points = 2^D
37 #TODO: parallel::parApply() ?!
23844f60
BA
38 as.data.frame( apply(series, 1, function(x) {
39 interpolated_curve = spline(1:L, x, n=nb_sample_points)$y
5c652979 40 W = wavelets::dwt(interpolated_curve, filter=wf, D)@W
23844f60
BA
41 rev( sapply( W, function(v) ( sqrt( sum(v^2) ) ) ) )
42 }) )
5c652979 43}