intermediate: R6, too slow
[talweg.git] / pkg / R / plot.R
index 5cb55b0..9a7a1ee 100644 (file)
@@ -63,21 +63,19 @@ computeFilaments <- function(data, index, limit=60, plot=TRUE)
        L = length(ref_serie)
 
        # Determine indices of no-NAs days followed by no-NAs tomorrows
-       first_day = ifelse(length(data$getCenteredSerie(1))<L, 2, 1)
-       fdays_indices = c()
-       for (i in first_day:(index-1))
+       fdays = c()
+       for (i in 1:(index-1))
        {
                if ( !any(is.na(data$getSerie(i)) | is.na(data$getSerie(i+1))) )
-                       fdays_indices = c(fdays_indices, i)
+                       fdays = c(fdays, i)
        }
 
-       distances = sapply(fdays_indices, function(i) {
+       distances = sapply(fdays, function(i) {
                sqrt( sum( (ref_serie - data$getCenteredSerie(i))^2 ) / L )
        })
        indices = sort(distances, index.return=TRUE)$ix[1:min(limit,length(distances))]
        yrange = quantile( c(ref_serie, sapply( indices, function(i) {
-               ii = fdays_indices[i]
-               serie = c(data$getCenteredSerie(ii), data$getCenteredSerie(ii+1))
+               serie = c(data$getCenteredSerie(fdays[i]), data$getCenteredSerie(fdays[i]+1))
                if (!all(is.na(serie)))
                        return (range(serie, na.rm=TRUE))
                c()
@@ -93,7 +91,7 @@ computeFilaments <- function(data, index, limit=60, plot=TRUE)
                par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=2)
                for ( i in seq_len(length(indices)+1) )
                {
-                       ii = ifelse(i<=length(indices), fdays_indices[ indices[plot_order[i]] ], index)
+                       ii = ifelse(i<=length(indices), fdays[ indices[plot_order[i]] ], index)
                        plot(c(data$getCenteredSerie(ii),data$getCenteredSerie(ii+1)),
                                ylim=yrange, type="l", col=colors[i],
                                xlab=ifelse(i==1,"Temps (en heures)",""), ylab=ifelse(i==1,"PM10 centrĂ©",""))
@@ -102,7 +100,7 @@ computeFilaments <- function(data, index, limit=60, plot=TRUE)
                }
                abline(v=24, lty=2, col=colors()[56])
        }
-       list("indices"=c(fdays_indices[ indices[plot_order] ],index), "colors"=colors)
+       list("indices"=c(fdays[ indices[plot_order] ],index), "colors"=colors)
 }
 
 #' @title Plot similarities
@@ -187,17 +185,8 @@ plotFbox <- function(data, filter=function(index) TRUE, plot_bivariate=TRUE)
        if (!requireNamespace("rainbow", quietly=TRUE))
                stop("Functional boxplot requires the rainbow package")
 
-       start_index = 1
-       end_index = data$getSize()
-       if (length(data$getCenteredSerie(1)) < length(data$getCenteredSerie(2)))
-       {
-               # Shifted start (7am, or 1pm, or...)
-               start_index = 2
-               end_index = data$getSize() - 1
-       }
-
        L = length(data$getCenteredSerie(2))
-       series_matrix = sapply(start_index:end_index, function(index) {
+       series_matrix = sapply(1:data$getSize(), function(index) {
                if (filter(index))
                        as.matrix(data$getSerie(index))
                else
@@ -256,19 +245,18 @@ plotRelativeVariability = function(data, indices, ...)
        ref_var = apply(ref_series, 2, sd)
 
        # Determine indices of no-NAs days followed by no-NAs tomorrows
-       first_day = ifelse(length(data$getCenteredSerie(1))<length(ref_series[1,]), 2, 1)
-       fdays_indices = c()
-       for (i in first_day:(tail(indices,1)-1))
+       fdays = c()
+       for (i in 1:(tail(indices,1)-1))
        {
                if ( !any(is.na(data$getSerie(i)) | is.na(data$getSerie(i+1))) )
-                       fdays_indices = c(fdays_indices, i)
+                       fdays = c(fdays, i)
        }
 
        # TODO: 3 == magic number
        random_var = matrix(nrow=3, ncol=48)
        for (mc in seq_len(nrow(random_var)))
        {
-               random_indices = sample(fdays_indices, length(indices))
+               random_indices = sample(fdays, length(indices))
                random_series = t( sapply(random_indices, function(i) {
                        c( data$getSerie(i), data$getSerie(i+1) )
                }) )