add realtime option, slightly refactor data acquisition
[talweg.git] / pkg / R / Data.R
CommitLineData
f17665c7 1#' Data
3d69ff21 2#'
f17665c7 3#' Data encapsulation
3d69ff21 4#'
a66a84b5
BA
5#' @docType class
6#' @importFrom R6 R6Class
7#'
8#' @field .data List of \itemize{
3d69ff21
BA
9#' \item time: vector of times
10#' \item serie: centered series
11#' \item level: corresponding levels
f17665c7 12#' \item exo: exogenous variables
a66a84b5 13#' \item exo_hat: predicted exogenous variables}
f17665c7 14#'
98e958ca
BA
15#' @section Methods:
16#' \describe{
17#' \item{\code{getSize()}}{
18#' Return number of series in dataset.}
19#' \item{\code{getStdHorizon()}}{
20#' Return number of time steps from serie[1] until midnight}
72b9c501
BA
21#' \item{\code{appendHat(time, hat_serie, hat_exo)}}{
22#' New estimated data + time.}
23#' \item{\code{append(serie, exo)}}{
24#' New measured data; call *after* \code{appendHat()}}
98e958ca
BA
25#' \item{\code{getTime(index)}}{
26#' Get times at specified index.}
72b9c501
BA
27#' \item{\code{getCenteredSerie(index, hat=FALSE)}}{
28#' Get (measured or predicted) centered serie at specified index.}
29#' \item{\code{getCenteredSeries(indices, hat=FALSE)}}{
98e958ca 30#' Get centered series at specified indices (in columns).}
72b9c501 31#' \item{\code{getLevel(index, hat=FALSE)}}{
98e958ca 32#' Get level at specified index.}
72b9c501 33#' \item{\code{getSerie(index, hat=FALSE)}}{
98e958ca 34#' Get serie (centered+level) at specified index.}
72b9c501 35#' \item{\code{getSeries(indices, hat=FALSE)}}{
98e958ca 36#' Get series at specified indices (in columns).}
72b9c501 37#' \item{\code{getExo(index, hat=FALSE)}}{
98e958ca 38#' Get exogenous variables at specified index.}
98e958ca 39#' }
546b0cb6 40#'
25b75559 41Data = R6::R6Class("Data",
35eb1443 42 private = list(
25b75559 43 .data = list()
35eb1443 44 ),
f17665c7 45 public = list(
3d69ff21 46 getSize = function()
a66a84b5 47 length(private$.data)
35eb1443 48 ,
e030a6e3 49 getStdHorizon = function()
a66a84b5 50 24 - as.POSIXlt( private$.data[[1]]$time[1] )$hour + 1
35eb1443 51 ,
72b9c501 52 appendHat = function(time, hat_serie, hat_exo)
a66a84b5 53 {
72b9c501
BA
54 hat_level = mean(hat_serie, na.rm=TRUE)
55 hat_centered_serie = hat_serie - hat_level
a66a84b5 56 private$.data[[length(private$.data)+1]] <- list(
72b9c501
BA
57 "time"=time, "hat_centered_serie"=hat_centered_serie,
58 "hat_level"=hat_level, "hat_exo"=hat_exo )
59 },
60 append = function(serie, exo)
61 {
62 level = mean(serie, na.rm=TRUE)
63 centered_serie = serie - level
64 private$.data[[length(private$.data)]]$centered_serie <- centered_serie,
65 private$.data[[length(private$.data)]]$level <- level,
66 private$.data[[length(private$.data)]]$exo <- exo,
a66a84b5 67 },
3d69ff21 68 getTime = function(index)
a66a84b5
BA
69 {
70 index = dateIndexToInteger(index, self)
71 private$.data[[index]]$time
72 },
72b9c501 73 getCenteredSerie = function(index, hat=FALSE)
a66a84b5
BA
74 {
75 index = dateIndexToInteger(index, self)
72b9c501
BA
76 if (hat)
77 private$.data[[index]]$hat_centered_serie
78 else
79 private$.data[[index]]$centered_serie
a66a84b5 80 },
72b9c501
BA
81 getCenteredSeries = function(indices, hat=FALSE)
82 sapply(indices, function(i) self$getCenteredSerie(i, hat))
98e958ca 83 ,
72b9c501 84 getLevel = function(index, hat=FALSE)
a66a84b5
BA
85 {
86 index = dateIndexToInteger(index, self)
72b9c501
BA
87 if (hat)
88 private$.data[[index]]$hat_level
89 else
90 private$.data[[index]]$level
a66a84b5 91 },
72b9c501 92 getSerie = function(index, hat=FALSE)
a66a84b5
BA
93 {
94 index = dateIndexToInteger(index, self)
72b9c501
BA
95 if (hat)
96 private$.data[[index]]$hat_centered_serie + private$.data[[index]]$hat_level
97 else
98 private$.data[[index]]$centered_serie + private$.data[[index]]$level
a66a84b5 99 },
72b9c501
BA
100 getSeries = function(indices, hat=FALSE)
101 sapply(indices, function(i) self$getSerie(i, hat))
98e958ca 102 ,
72b9c501 103 getExo = function(index, hat=FALSE)
a66a84b5
BA
104 {
105 index = dateIndexToInteger(index, self)
72b9c501
BA
106 if (hat)
107 private$.data[[index]]$hat_exo
108 else
109 private$.data[[index]]$exo
a66a84b5
BA
110 },
111 removeFirst = function()
112 private$.data <- private$.data[2:length(private$.data)]
113 ,
114 removeLast = function()
115 private$.data <- private$.data[1:(length(private$.data)-1)]
3d69ff21
BA
116 )
117)