#' \item{\code{getStdHorizon()}}{
 #'   Number of time steps from serie[1] until midnight}
 #' \item{\code{append(time, serie, exo, exo_hat)}}{
-#'   Measured data for given vector of times + exogenous predictions from last midgnight.
+#'   Measured data for given vector of times + exogenous predictions from
+#'   last midgnight.}
 #' \item{\code{getTime(index)}}{
 #'   Times (vector) at specified index.}
 #' \item{\code{getCenteredSerie(index)}}{
 
 #'
 #' @docType class
 #' @format R6 class, inherits Forecaster
-#' @alias F_Average
+#' @aliases F_Average
 #'
 AverageForecaster = R6::R6Class("AverageForecaster",
        inherit = Forecaster,
 
 #'
 #' @docType class
 #' @format R6 class, inherits Forecaster
-#' @alias F_Neighbors
+#' @aliases F_Neighbors
 #'
 NeighborsForecaster = R6::R6Class("NeighborsForecaster",
        inherit = Forecaster,
                                return (NA)
 
                        # Determine indices of no-NAs days followed by no-NAs tomorrows
-                       fdays = getNoNA2(data, max(today-memory,1), today-1)
+                       fdays = .getNoNA2(data, max(today-memory,1), today-1)
 
                        # Get optional args
                        local = ifelse(hasArg("local"), list(...)$local, TRUE) #same level + season?
 
                        if (local)
                        {
-                               # Neighbors: days in "same season"; TODO: 60 == magic number...
+                               # TODO: 60 == magic number
                                fdays = getSimilarDaysIndices(today, data, limit=60, same_season=TRUE,
                                        days_in=fdays_cut)
                                if (length(fdays) <= 1)
                                        return (NA)
-                               levelToday = data$getLevel(today)
-                               distances = sapply(fdays, function(i) abs(data$getLevel(i)-levelToday))
-                               #TODO: 2, 10, 3, 12 magic numbers here...
-                               dist_thresh = 2
-                               min_neighbs = min(10,length(fdays))
-                               repeat
-                               {
-                                       same_pollution = (distances <= dist_thresh)
-                                       nb_neighbs = sum(same_pollution)
-                                       if (nb_neighbs >= min_neighbs) #will eventually happen
-                                               break
-                                       dist_thresh = dist_thresh + 3
-                               }
-                               fdays = fdays[same_pollution]
-                               max_neighbs = 12
-                               if (nb_neighbs > max_neighbs)
-                               {
-                                       # Keep only max_neighbs closest neighbors
-                                       fdays = fdays[
-                                               sort(distances[same_pollution],index.return=TRUE)$ix[1:max_neighbs] ]
-                               }
-                               if (length(fdays) == 1) #the other extreme...
+                               # TODO: 10, 12 == magic numbers
+                               fdays = .getConstrainedNeighbs(today,data,fdays,min_neighbs=10,max_neighbs=12)
+                               if (length(fdays) == 1)
                                {
                                        if (final_call)
                                        {
                                        mean(delta^2)
                                })
 
-                               sd_dist = sd(distances2)
-                               if (sd_dist < .25 * sqrt(.Machine$double.eps))
-                               {
-#                                      warning("All computed distances are very close: stdev too small")
-                                       sd_dist = 1 #mostly for tests... FIXME:
-                               }
-                               simils_endo = exp(-distances2/(sd_dist*window_endo^2))
+                               simils_endo <- .computeSimils(distances2, window_endo)
                        }
 
                        if (simtype == "exo" || simtype == "mix")
                                        delta %*% sigma_inv %*% delta
                                })
 
-                               sd_dist = sd(distances2)
-                               if (sd_dist < .25 * sqrt(.Machine$double.eps))
-                               {
-#                                      warning("All computed distances are very close: stdev too small")
-                                       sd_dist = 1 #mostly for tests... FIXME:
-                               }
-                               simils_exo = exp(-distances2/(sd_dist*window_exo^2))
+                               simils_exo <- .computeSimils(distances2, window_exo)
                        }
 
                        similarities =
                }
        )
 )
+
+#' getNoNA2
+#'
+#' Get indices in data of no-NA series followed by no-NA, within [first,last] range.
+#'
+#' @inheritParams dateIndexToInteger
+#' @param first First index (included)
+#' @param last Last index (included)
+#'
+.getNoNA2 = function(data, first, last)
+{
+       (first:last)[ sapply(first:last, function(i)
+               !any( is.na(data$getCenteredSerie(i)) | is.na(data$getCenteredSerie(i+1)) )
+       ) ]
+}
+
+#' getConstrainedNeighbs
+#'
+#' Get indices of neighbors of similar pollution level (among same season + day type).
+#'
+#' @param today Index of current day
+#' @param data Object of class Data
+#' @param fdays Current set of "first days" (no-NA pairs)
+#' @param min_neighbs Minimum number of points in a neighborhood
+#' @param max_neighbs Maximum number of points in a neighborhood
+#'
+.getConstrainedNeighbs = function(today, data, fdays, min_neighbs=10, max_neighbs=12)
+{
+       levelToday = data$getLevel(today)
+       distances = sapply(fdays, function(i) abs(data$getLevel(i)-levelToday))
+       #TODO: 2, +3 : magic numbers
+       dist_thresh = 2
+       min_neighbs = min(min_neighbs,length(fdays))
+       repeat
+       {
+               same_pollution = (distances <= dist_thresh)
+               nb_neighbs = sum(same_pollution)
+               if (nb_neighbs >= min_neighbs) #will eventually happen
+                       break
+               dist_thresh = dist_thresh + 3
+       }
+       fdays = fdays[same_pollution]
+       max_neighbs = 12
+       if (nb_neighbs > max_neighbs)
+       {
+               # Keep only max_neighbs closest neighbors
+               fdays = fdays[
+                       sort(distances[same_pollution],index.return=TRUE)$ix[1:max_neighbs] ]
+       }
+       fdsays
+}
+
+#' compute similarities
+#'
+#' Apply the gaussian kernel on computed squared distances.
+#'
+#' @param distances2 Squared distances
+#' @param window Window parameter for the kernel
+#'
+.computeSimils <- function(distances2, window)
+{
+       sd_dist = sd(distances2)
+       if (sd_dist < .25 * sqrt(.Machine$double.eps))
+       {
+#              warning("All computed distances are very close: stdev too small")
+               sd_dist = 1 #mostly for tests... FIXME:
+       }
+       exp(-distances2/(sd_dist*window^2))
+}
 
 #'
 #' @docType class
 #' @format R6 class, inherits Forecaster
-#' @alias F_Persistence
+#' @aliases F_Persistence
 #'
 PersistenceForecaster = R6::R6Class("PersistenceForecaster",
        inherit = Forecaster,
 
 #'
 #' @docType class
 #' @format R6 class, inherits Forecaster
-#' @alias F_Zero
+#' @aliases F_Zero
 #'
 ZeroForecaster = R6::R6Class("ZeroForecaster",
        inherit = Forecaster,
 
 #' @inheritParams computeForecast
 #' @inheritParams getZeroJumpPredict
 #'
-#' @alias J_Neighbors
+#' @aliases J_Neighbors
 #'
 getNeighborsJumpPredict = function(data, today, memory, horizon, params, ...)
 {
 
 #' @inheritParams computeForecast
 #' @inheritParams getZeroJumpPredict
 #'
-#' @alias J_Persistence
+#' @aliases J_Persistence
 #'
 getPersistenceJumpPredict = function(data, today, memory, horizon, params, ...)
 {
 
 #' @param today Index of the current day (predict tomorrow)
 #' @param params Optional parameters computed by the main forecaster
 #'
-#' @alias J_Zero
+#' @aliases J_Zero
 #'
 getZeroJumpPredict = function(data, today, memory, horizon, params, ...)
 {
 
 #' @examples
 #' ts_data <- system.file("extdata","pm10_mesures_H_loc.csv",package="talweg")
 #' exo_data <- system.file("extdata","meteo_extra_noNAs.csv",package="talweg")
-#' data <- getData(ts_data, exo_data, input_tz="GMT", working_tz="GMT", predict_at=7)
-#' pred <- computeForecast(data, 2200:2230, "Persistence", "Zero",
-#'   memory=500, horizon=12, ncores=1)
+#' data <- getData(ts_data, exo_data, input_tz="GMT", working_tz="GMT",
+#'   predict_at=7, limit=200)
+#' pred <- computeForecast(data, 100:130, "Persistence", "Zero",
+#'   memory=50, horizon=12, ncores=1)
 #' \dontrun{#Sketch for real-time mode:
 #' data <- Data$new()
 #' forecaster <- MyForecaster$new(myJumpPredictFunc)
 
                return (day <= 4)
        return (day == day_ref)
 }
-
-#' getNoNA2
-#'
-#' Get indices in data of no-NA series followed by no-NA, within [first,last] range.
-#'
-#' @inheritParams dateIndexToInteger
-#' @param first First index (included)
-#' @param last Last index (included)
-#'
-#' @export
-getNoNA2 = function(data, first, last)
-{
-       (first:last)[ sapply(first:last, function(i)
-               !any( is.na(data$getCenteredSerie(i)) | is.na(data$getCenteredSerie(i+1)) )
-       ) ]
-}
 
 library(testthat)
-library(talweg)
+
+load_all() #because some non-exported functions
+#library(talweg)
 
 test_check("talweg")
 
-context("Check that date <--> integer indexes conversions work")
+context("Date <--> integer conversions")
 
 ts_data = system.file("testdata","ts_test.csv",package="talweg")
 exo_data = system.file("testdata","exo_test.csv",package="talweg")
 data7 <<- getData(ts_data, exo_data, input_tz="GMT", date_format="%Y-%m-%d %H:%M",
        working_tz="GMT", predict_at=7, limit=Inf)
 
-test_that("dateIndexToInteger",
+test_that("dateIndexToInteger works as expected",
 {
        expect_identical( dateIndexToInteger("2007-01-01",data0),   1 )
        expect_identical( dateIndexToInteger("2007-01-02",data0),   2 )
        expect_identical( dateIndexToInteger("2007-05-31",data7), 151 )
 })
 
-test_that("integerIndexToDate",
+test_that("integerIndexToDate works as expected",
 {
        expect_identical( integerIndexToDate(  1,data0), as.Date("2007-01-01") )
        expect_identical( integerIndexToDate(  2,data0), as.Date("2007-01-02") )
 
 test_that("integerIndexToDate(dateIndexToInteger) == Id",
 {
-       expect_identical(
-               integerIndexToDate(dateIndexToInteger("2007-01-01",data0),data0), as.Date("2007-01-01") )
-       expect_identical(
-               integerIndexToDate(dateIndexToInteger("2007-01-01",data7),data7), as.Date("2007-01-01") )
-       expect_identical(
-               integerIndexToDate(dateIndexToInteger("2007-01-02",data0),data0), as.Date("2007-01-02") )
-       expect_identical(
-               integerIndexToDate(dateIndexToInteger("2007-01-02",data7),data7), as.Date("2007-01-02") )
-       expect_identical(
-               integerIndexToDate(dateIndexToInteger("2007-02-01",data0),data0), as.Date("2007-02-01") )
-       expect_identical(
-               integerIndexToDate(dateIndexToInteger("2007-02-01",data0),data0), as.Date("2007-02-01") )
-       expect_identical(
-               integerIndexToDate(dateIndexToInteger("2007-03-01",data0),data0), as.Date("2007-03-01") )
-       expect_identical(
-               integerIndexToDate(dateIndexToInteger("2007-03-01",data0),data0), as.Date("2007-03-01") )
-       expect_identical(
-               integerIndexToDate(dateIndexToInteger("2007-05-31",data0),data0), as.Date("2007-05-31") )
-       expect_identical(
-               integerIndexToDate(dateIndexToInteger("2007-05-31",data0),data0), as.Date("2007-05-31") )
+       expect_identical(integerIndexToDate(dateIndexToInteger("2007-01-01",data0),data0),
+               as.Date("2007-01-01") )
+       expect_identical(integerIndexToDate(dateIndexToInteger("2007-01-01",data7),data7),
+               as.Date("2007-01-01") )
+       expect_identical(integerIndexToDate(dateIndexToInteger("2007-01-02",data0),data0),
+               as.Date("2007-01-02") )
+       expect_identical(integerIndexToDate(dateIndexToInteger("2007-01-02",data7),data7),
+               as.Date("2007-01-02") )
+       expect_identical(integerIndexToDate(dateIndexToInteger("2007-02-01",data0),data0),
+               as.Date("2007-02-01") )
+       expect_identical(integerIndexToDate(dateIndexToInteger("2007-02-01",data0),data0),
+               as.Date("2007-02-01") )
+       expect_identical(integerIndexToDate(dateIndexToInteger("2007-03-01",data0),data0),
+               as.Date("2007-03-01") )
+       expect_identical(integerIndexToDate(dateIndexToInteger("2007-03-01",data0),data0),
+               as.Date("2007-03-01") )
+       expect_identical(integerIndexToDate(dateIndexToInteger("2007-05-31",data0),data0),
+               as.Date("2007-05-31") )
+       expect_identical(integerIndexToDate(dateIndexToInteger("2007-05-31",data0),data0),
+               as.Date("2007-05-31") )
 })
 
 test_that("Persistence method behave as expected",
 {
        #Situation A: +Zero; (generally) correct if jump, wrong otherwise
-       pred00_sd = computeForecast(data00, indices, "Persistence", "Zero", Inf, 24, same_day=TRUE)
-       pred00_dd = computeForecast(data00, indices, "Persistence", "Zero", Inf, 24, same_day=FALSE)
+       pred00_sd = computeForecast(data00, indices, "Persistence", "Zero", Inf, 24,
+               same_day=TRUE)
+       pred00_dd = computeForecast(data00, indices, "Persistence", "Zero", Inf, 24,
+               same_day=FALSE)
        for (i in 1:7)
        {
                expect_equal(pred00_sd$getSerie(i), rep(pred_order[i],24))
                expect_equal(pred00_dd$getSerie(i), rep(pred_order[i],24))
        }
 
-       pred13_sd = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24, same_day=TRUE)
-       pred13_dd = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24, same_day=FALSE)
+       pred13_sd = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24,
+               same_day=TRUE)
+       pred13_dd = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24,
+               same_day=FALSE)
        for (i in 2:6)
        {
                expect_equal(pred13_sd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
 test_that("Neighbors method behave as expected",
 {
        #Situation A: +Zero; correct if jump, wrong otherwise
-       pred00 = computeForecast(data00, indices, "Neighbors", "Zero", Inf, 24, simtype="mix")
+       pred00 = computeForecast(data00, indices, "Neighbors", "Zero", Inf, 24,
+               simtype="mix")
        for (i in 1:7)
                expect_equal(pred00$getSerie(i), rep(pred_order[i],24))
 
-       pred13 = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24, simtype="mix")
+       pred13 = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24,
+               simtype="mix")
        for (i in 1:7)
                expect_equal(pred13$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
 
        #Situation B: +Neighbors, always predict bad (small, averaged) jump
-       pred00 = computeForecast(data00, indices, "Neighbors", "Neighbors", Inf, 24, simtype="endo")
-       #Concerning weights, there are 12+(1 if i>=2) gaps at -6 and 90-12+(i-2 if i>=3) gaps at 1
-       #Thus, predicted jump is respectively
+       pred00 = computeForecast(data00, indices, "Neighbors", "Neighbors", Inf, 24,
+               simtype="endo")
+       #Concerning weights, there are 12+(1 if i>=2) gaps at -6 and 90-12+(i-2 if i>=3) gaps
+       #at 1. Thus, predicted jump is respectively
        #  (12*-6+78)/90 = 0.06666667
        #  (13*-6+78)/91 = 0
        #  (13*-6+79)/92 = 0.01086957
        for (i in 1:7)
                expect_equal(pred00$getSerie(i), rep(pred_order[i]+jumps[i],24))
 
-       #Next lines commented out because too unpredictable results (tendency to flatten everything...)
-#      pred13 = computeForecast(data13, indices, "Neighbors", "Neighbors", Inf, 24, simtype="endo")
+       #Next lines commented out because too unpredictable results
+       #(tendency to flatten everything...)
+#      pred13 = computeForecast(data13, indices, "Neighbors", "Neighbors", Inf, 24,
+#              simtype="endo")
 #      for (i in 1:7)
 #              expect_equal(pred13$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
 
 
-context("Check that computeFilaments behaves as expected")
+context("computeFilaments")
 
 #shorthand: map 1->1, 2->2, 3->3, 4->1, ..., 149->2, 150->3
 I = function(i)
 
--- /dev/null
+context("Get similar days")
+
+itestthat("getSimilarDaysIndices works as expected",
+{
+       getSimilarDaysIndices(index, data, limit, same_season, days_in=NULL)
+       #...
+})
+{
+       index = dateIndexToInteger(index, data)
+
+testthat("getConstrainedNeighbs works as expected",
+{
+       .getConstrainedNeighbs(today, data, fdays, min_neighbs=10, max_neighbs=12)
+       #...
+})
+