From a3344f7591f6f4b3d337a69e4a568e9b16e33415 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Sun, 2 Jul 2017 23:02:24 +0200
Subject: [PATCH] fix bugs

---
 pkg/R/F_Neighbors.R | 15 +++++++++------
 pkg/R/Forecaster.R  |  2 +-
 pkg/R/J_LastValue.R |  3 ++-
 pkg/R/J_Zero.R      |  2 +-
 pkg/R/getData.R     |  2 +-
 5 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/pkg/R/F_Neighbors.R b/pkg/R/F_Neighbors.R
index 0295cd5..ea27d5c 100644
--- a/pkg/R/F_Neighbors.R
+++ b/pkg/R/F_Neighbors.R
@@ -137,7 +137,7 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster",
 					days_in=tdays_cut, operational=opera)
 				nb_neighbs <- round( window[length(window)] )
 				# TODO: 10 == magic number
-				tdays <- .getConstrainedNeighbs(today, data, tdays, min_neighbs=nb_neighbs)
+				tdays <- .getConstrainedNeighbs(today, data, tdays, nb_neighbs, opera)
 				if (length(tdays) == 1)
 				{
 					if (final_call)
@@ -171,7 +171,7 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster",
 
 			if (simtype == "exo" || simtype == "mix")
 			{
-				distances2 <- .computeDistsExo(data, today, tdays)
+				distances2 <- .computeDistsExo(data, today, tdays, opera)
 
 				# Compute exogen similarities using the given window
 				window_exo = ifelse(simtype=="mix", window[2], window[1])
@@ -218,9 +218,9 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster",
 # @param min_neighbs Minimum number of points in a neighborhood
 # @param max_neighbs Maximum number of points in a neighborhood
 #
-.getConstrainedNeighbs = function(today, data, tdays, min_neighbs=10)
+.getConstrainedNeighbs = function(today, data, tdays, min_neighbs, opera)
 {
-	levelToday = data$getLevelHat(today)
+	levelToday = ifelse(opera, tail(data$getLevelHat(today),1), data$getLevel(today))
 	distances = sapply( tdays, function(i) abs(data$getLevel(i) - levelToday) )
 	#TODO: 1, +1, +3 : magic numbers
 	dist_thresh = 1
@@ -265,10 +265,13 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster",
 	})
 }
 
-.computeDistsExo <- function(data, today, tdays)
+.computeDistsExo <- function(data, today, tdays, opera)
 {
 	M = matrix( ncol=1+length(tdays), nrow=1+length(data$getExo(1)) )
-	M[,1] = c( data$getLevelHat(today), as.double(data$getExoHat(today)) )
+	if (opera)
+		M[,1] = c( tail(data$getLevelHat(today),1), as.double(data$getExoHat(today)) )
+	else
+		M[,1] = c( data$getLevel(today), as.double(data$getExo(today)) )
 	for (i in seq_along(tdays))
 		M[,i+1] = c( data$getLevel(tdays[i]), as.double(data$getExo(tdays[i])) )
 
diff --git a/pkg/R/Forecaster.R b/pkg/R/Forecaster.R
index a10f0bd..ef270b1 100644
--- a/pkg/R/Forecaster.R
+++ b/pkg/R/Forecaster.R
@@ -59,7 +59,7 @@ Forecaster = R6::R6Class("Forecaster",
 			# Parameters (potentially) computed during shape prediction stage
 			predicted_shape <- self$predictShape(data,today,memory,predict_from,horizon,...)
 
-			if (is.na(predicted_shape))
+			if (is.na(predicted_shape[1]))
 				return (NA)
 
 			predicted_delta <- private$.pjump(data, today, memory, predict_from,
diff --git a/pkg/R/J_LastValue.R b/pkg/R/J_LastValue.R
index 4190b9c..9b1b5e3 100644
--- a/pkg/R/J_LastValue.R
+++ b/pkg/R/J_LastValue.R
@@ -7,7 +7,8 @@
 #'
 #' @aliases J_LastValue
 #'
-getLastValueJumpPredict = function(data, today, memory, horizon, params, ...)
+getLastValueJumpPredict = function(data, today, memory, predict_from,
+	horizon, params, ...)
 {
 	0
 }
diff --git a/pkg/R/J_Zero.R b/pkg/R/J_Zero.R
index 16b6ed5..01ba9e3 100644
--- a/pkg/R/J_Zero.R
+++ b/pkg/R/J_Zero.R
@@ -8,7 +8,7 @@
 #'
 #' @aliases J_Zero
 #'
-getZeroJumpPredict = function(data, today, memory, horizon, params, ...)
+getZeroJumpPredict = function(data, today, memory, predict_from, horizon, params, ...)
 {
 	list(...)$first_pred - ifelse( predict_from >= 2,
 		data$getSerie(today)[predict_from-1], tail(data$getSerie(today-1),1) )
diff --git a/pkg/R/getData.R b/pkg/R/getData.R
index d0e69d7..8e44a52 100644
--- a/pkg/R/getData.R
+++ b/pkg/R/getData.R
@@ -68,7 +68,7 @@ getData = function(ts_data, exo_data, date_format="%d/%m/%Y %H:%M", limit=Inf)
 			{
 				time = c(time, ts_df[line,1])
 				serie = c(serie, ts_df[line,2])
-				level_hat = c(level_hat, #in case of data file is incomplete...
+				level_hat = c(level_hat, #if data file is incomplete...
 					ifelse(ncol(ts_df) > 2, ts_df[line,3], mean(serie,na.rm=TRUE)))
 				line = line + 1
 			};
-- 
2.44.0