source("cstools/R/CST_Anomaly.R")
library(s2dverification)
library(testthat)
mod <- 1 : (2 * 3 * 4 * 5 * 6 * 7)
dim(mod) <- c(dataset = 2, member = 3, sdate = 4, ftime = 5, lat = 6, lon = 7)
obs2 <- 1 : (1 * 1 * 4 * 5 * 6 * 7)
dim(obs2) <- c(dataset = 1, member = 1, sdate = 4, ftime = 5, lat = 6, lon = 7)
exp <- list(data = mod)
obs <- list(data = obs2)
attr(exp, 'class') <- 's2dv_cube'
attr(obs, 'class') <- 's2dv_cube'

# cross validation return the same result:
a_mem <- CST_Anomaly(exp = exp, obs = obs, cross = TRUE)
str(a_mem)
res_mem <- Ano_CrossValid(var_exp = mod, var_obs = obs2, memb = TRUE)
str(res_mem)
expect_equal(a_mem$exp$data, res_mem$ano_exp)
dim(a_mem$exp$data)
dim(res_mem$ano_exp)
a_mem$exp$data[1,,,1,1,1]
res_mem$ano_exp[1,,,1,1,1]

sum(a_mem$exp$data != res_mem$ano_exp)
#-----------------------------------------

# cross validation return the same result
a <- CST_Anomaly(exp = exp, obs = obs,
                    cross = TRUE, memb = FALSE)
str(a)
res <- Ano_CrossValid(var_exp = mod, var_obs = obs2, memb = FALSE)

expect_equal(a$exp$data, res$ano_exp)
dim(a$exp$data)
dim(res$ano_exp)
a$exp$data[1,,,1,1,1]
res$ano_exp[1,,,1,1,1]

sum(a$exp$data != res$ano_exp)

dim(a$exp$data)
dim(a_mem$exp$data)
a$exp$data[1,,1,1,1,1]
a_mem$exp$data[1,,1,1,1,1] # all members are equal in this case
sum(a_mem$exp$data[ , 1, , , , ] != a_mem$exp$data[ , 2, , , , ])
#-------------------------------------------------------


# Computation of anomalies without cross-validation member = TRUE
ano <- CST_Anomaly(exp = exp, obs = obs, 
                        cross = FALSE, memb = TRUE)
str(ano)
clim <- Clim(mod, obs2)
res_exp <- Ano(var = mod, clim$clim_exp)
res_obs <- Ano(var = obs2, clim$clim_obs)

sum(ano$exp$data != res_exp)
sum(ano$obs$data != res_obs)

# without cross-validation and member = FALSE
ano_ens <- CST_Anomaly(exp = exp, obs = obs,
                      cross = FALSE, memb = FALSE)    
str(ano)
clim <- Clim(mod, obs2, memb = FALSE)
res_exp_ens <- Ano(var = mod, clim$clim_exp)
res_obs_ens <- Ano(var = obs2, clim$clim_obs)

sum(ano_ens$exp$data != res_exp_ens)
sum(ano_ens$obs$data != res_obs_ens)

# -------------------------------------------------------

# Filtering can be applied only without cross-validation
## .Loess function is already included in the code CST_Anomaly.
# New sample data is necessary as filtering doesn' affect linear values
library(CSTools)
# Computation of anomalies without cross-validation member = TRUE
library(abind)
dimnames <- names(dim(CSTools::lonlat_data$exp$data))
lonlat_data$exp$data <- abind(lonlat_data$exp$data,
                              lonlat_data$exp$data,
                              lonlat_data$exp$data, along = 4)
names(dim(lonlat_data$exp$data)) <- dimnames
lonlat_data$obs$data <- abind(lonlat_data$obs$data,
                              lonlat_data$obs$data,
                              lonlat_data$obs$data, along = 4)
names(dim(lonlat_data$obs$data)) <- dimnames

ano <- CST_Anomaly(exp = lonlat_data$exp, obs = lonlat_data$obs, 
                        cross = FALSE, memb = TRUE)
ano_f1 <- CST_Anomaly(exp = lonlat_data$exp, obs = lonlat_data$obs,
                      cross = FALSE, memb = TRUE,
                      filter_span = 1)
expect_equal(ano, ano_f1)
str(ano$exp$data)
str(ano_f1$exp$data)
dim(ano$exp$data)
dim(ano_f1$exp$data)
ano$exp$data[1,1,,,1,1]
ano_f1$exp$data[1,1,,,1,1]

# Reproduce with s2dverification:
clim <- Clim(lonlat_data$exp$data, lonlat_data$obs$data)
res_exp <- Ano(var = lonlat_data$exp$data, clim$clim_exp)
res_obs <- Ano(var = lonlat_data$obs$data, clim$clim_obs)

sum(ano$exp$data != res_exp) # reproducible results
sum(ano$obs$data != res_obs) # reproducible results for CST_Anom..

filter_span = 1
pos_dims <- names(dim(clim$clim_exp))
clim_f1 <- clim
reorder <- match(pos_dims, c('ftime',
                             pos_dims[-which(pos_dims == 'ftime')]))
clim_f1$clim_obs <- aperm(apply(clim$clim_obs, c(1 : 
      length(dim(clim$clim_obs)))[-which(names(dim(clim$clim_obs)) == 'ftime')],
               .Loess, loess_span = filter_span), reorder)
clim_f1$clim_exp <- aperm(apply(clim$clim_exp, c(1 :
      length(dim(clim$clim_exp)))[-which(names(dim(clim$clim_exp)) == 'ftime')],
               .Loess, loess_span = filter_span), reorder)
names(dim(clim_f1$clim_exp)) <- names(dim(clim$clim_exp))
names(dim(clim_f1$clim_obs)) <- names(dim(clim$clim_obs))
res_exp_f1 <- Ano(var = lonlat_data$exp$data, clim_f1$clim_exp)
res_obs_f1 <- Ano(var = lonlat_data$obs$data, clim_f1$clim_obs)

sum(ano_f1$exp$data != res_exp_f1) # reproducible results
sum(ano_f1$obs$data != res_obs_f1) # reproducible results for CST_Anom..

ano_f2 <- CST_Anomaly(exp = lonlat_data$exp, obs = lonlat_data$obs,
                      cross = FALSE,
                      memb = TRUE, filter_span = 2)
expect_equal(ano, ano_f2)
expect_equal(ano_f1, ano_f2)


