From 6f623c0950e40c856877cc31aa308073689457f3 Mon Sep 17 00:00:00 2001 From: aho Date: Tue, 28 Jun 2022 09:15:10 +0200 Subject: [PATCH 1/9] Create the folders and necessary file for automatic unit testing --- .gitlab-ci.yml | 9 +++++++++ tests/testthat.R | 7 +++++++ tests/testthat/test-decadal_monthly.R | 6 ++++++ 3 files changed, 22 insertions(+) create mode 100644 .gitlab-ci.yml create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-decadal_monthly.R diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 00000000..c1e690c2 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,9 @@ +stages: + - build +build: + stage: build + script: + - module load R/3.6.1-foss-2015a-bare + - module load CDO/1.9.8-foss-2015a + - R CMD build --resave-data . + - R CMD check --as-cran --no-manual --run-donttest startR_*.tar.gz diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 00000000..3d7dd459 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,7 @@ +library(testthat) +library(startR) +library(SpecsVerification) +library(s2dv) +library(CSTools) + +test_check("auto-S2S") diff --git a/tests/testthat/test-decadal_monthly.R b/tests/testthat/test-decadal_monthly.R new file mode 100644 index 00000000..6d342bf9 --- /dev/null +++ b/tests/testthat/test-decadal_monthly.R @@ -0,0 +1,6 @@ +context("Decadal monthly data") + +test_that("1. ", { + + +}) -- GitLab From fd5535fc91410baaa705db3361386b7b14892611 Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 29 Jun 2022 14:48:22 +0200 Subject: [PATCH 2/9] Create CI/CD --- .gitlab-ci.yml | 25 ++++++++++++++++++++----- tests/testthat.R | 3 ++- tests/testthat/test-decadal_monthly.R | 13 ++++++++++++- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c1e690c2..3000e522 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,9 +1,24 @@ -stages: +default: +# before_script: +# - R --version +# - cdo --version +# after_script: +# - echo "After script section" +# - echo "For example you might do some cleanup here" + +stages: # List of stages for jobs, and their order of execution - build -build: + - test + +build-job: stage: build script: - - module load R/3.6.1-foss-2015a-bare + - echo "Build: Loading modules..." + - module load R/4.1.2-foss-2015a-bare - module load CDO/1.9.8-foss-2015a - - R CMD build --resave-data . - - R CMD check --as-cran --no-manual --run-donttest startR_*.tar.gz + +unit-test-job: # This job runs in the test stage. + stage: test # It only starts when the job in the build stage completes successfully. + script: + - echo "Test: Running unit tests..." + - Rscript ./tests/testthat.R #/test-decadal_monthly.R diff --git a/tests/testthat.R b/tests/testthat.R index 3d7dd459..9e9b5a21 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -4,4 +4,5 @@ library(SpecsVerification) library(s2dv) library(CSTools) -test_check("auto-S2S") +print('In testthat.R. Running...') +source('./tests/testthat/test-decadal_monthly.R') diff --git a/tests/testthat/test-decadal_monthly.R b/tests/testthat/test-decadal_monthly.R index 6d342bf9..e3f85290 100644 --- a/tests/testthat/test-decadal_monthly.R +++ b/tests/testthat/test-decadal_monthly.R @@ -1,6 +1,17 @@ context("Decadal monthly data") -test_that("1. ", { +source("modules/Loading/Loading_decadal.R") +source("modules/Calibration/Calibration.R") +source("modules/Skill/Skill.R") +source("modules/Saving/Save_data.R") +recipe_file <- "modules/Loading/testing_recipes/recipe_decadal.yml" + +test_that("1. Loading", { + +print('In test_that 1.') +#suppressWarnings( +#data <- load_datasets(recipe_file) +#) }) -- GitLab From 9c81ee9c1533cc55d31445a6a958d6876466e2b8 Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 29 Jun 2022 14:56:59 +0200 Subject: [PATCH 3/9] Syntax fix --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3000e522..36590a09 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -13,12 +13,12 @@ stages: # List of stages for jobs, and their order of execution build-job: stage: build script: - - echo "Build: Loading modules..." + - echo "Build Loading modules..." - module load R/4.1.2-foss-2015a-bare - module load CDO/1.9.8-foss-2015a unit-test-job: # This job runs in the test stage. stage: test # It only starts when the job in the build stage completes successfully. script: - - echo "Test: Running unit tests..." + - echo "Test Running unit tests..." - Rscript ./tests/testthat.R #/test-decadal_monthly.R -- GitLab From 765495dc15e13d52017d34db8e19ddcb4f9e2bf7 Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 29 Jun 2022 15:14:35 +0200 Subject: [PATCH 4/9] Try fixing bugs --- .gitlab-ci.yml | 2 +- tests/testthat.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 36590a09..8707fee2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -14,7 +14,7 @@ build-job: stage: build script: - echo "Build Loading modules..." - - module load R/4.1.2-foss-2015a-bare + - module load R/3.6.1-foss-2015a-bare #R/4.1.2-foss-2015a-bare - module load CDO/1.9.8-foss-2015a unit-test-job: # This job runs in the test stage. diff --git a/tests/testthat.R b/tests/testthat.R index 9e9b5a21..e803bbe4 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -1,8 +1,8 @@ -library(testthat) library(startR) library(SpecsVerification) library(s2dv) library(CSTools) +library(testthat) print('In testthat.R. Running...') source('./tests/testthat/test-decadal_monthly.R') -- GitLab From f8b52a17edb32042030a3ae9e608db92ff5f50cd Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 29 Jun 2022 15:16:07 +0200 Subject: [PATCH 5/9] Try fixing CI/CD bugs - 2 --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8707fee2..80dcde6d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -21,4 +21,5 @@ unit-test-job: # This job runs in the test stage. stage: test # It only starts when the job in the build stage completes successfully. script: - echo "Test Running unit tests..." + - module list - Rscript ./tests/testthat.R #/test-decadal_monthly.R -- GitLab From 38f4694ee7cee346af46745508e7ad5ca3255de9 Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 29 Jun 2022 15:18:48 +0200 Subject: [PATCH 6/9] Try fixing CI/CD bugs - 3 --- .gitlab-ci.yml | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 80dcde6d..320ff66e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,19 +7,22 @@ default: # - echo "For example you might do some cleanup here" stages: # List of stages for jobs, and their order of execution - - build +# - build - test -build-job: - stage: build - script: - - echo "Build Loading modules..." - - module load R/3.6.1-foss-2015a-bare #R/4.1.2-foss-2015a-bare - - module load CDO/1.9.8-foss-2015a +#build-job: +# stage: build +# script: +# - echo "Build Loading modules..." +# - module load R/3.6.1-foss-2015a-bare #R/4.1.2-foss-2015a-bare +# - module load CDO/1.9.8-foss-2015a unit-test-job: # This job runs in the test stage. stage: test # It only starts when the job in the build stage completes successfully. script: + - echo "Build Loading modules..." + - module load R/4.1.2-foss-2015a-bare + - module load CDO/1.9.8-foss-2015a - echo "Test Running unit tests..." - module list - Rscript ./tests/testthat.R #/test-decadal_monthly.R -- GitLab From f2113ec931c660ee40ff65d3aca8fc5521539c89 Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 29 Jun 2022 15:45:26 +0200 Subject: [PATCH 7/9] Try having two test jobs --- .gitlab-ci.yml | 34 +++++++++++--------------- tests/test_decadal.R | 3 +++ tests/test_seasonal.R | 3 +++ tests/testthat.R | 8 ------ tests/testthat/test-seasonal_monthly.R | 17 +++++++++++++ 5 files changed, 37 insertions(+), 28 deletions(-) create mode 100644 tests/test_decadal.R create mode 100644 tests/test_seasonal.R delete mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-seasonal_monthly.R diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 320ff66e..4ea4685f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,28 +1,22 @@ -default: -# before_script: -# - R --version -# - cdo --version -# after_script: -# - echo "After script section" -# - echo "For example you might do some cleanup here" - stages: # List of stages for jobs, and their order of execution -# - build - test -#build-job: -# stage: build -# script: -# - echo "Build Loading modules..." -# - module load R/3.6.1-foss-2015a-bare #R/4.1.2-foss-2015a-bare -# - module load CDO/1.9.8-foss-2015a - unit-test-job: # This job runs in the test stage. - stage: test # It only starts when the job in the build stage completes successfully. + stage: test + script: + - echo "Loading modules..." + - module load R/4.1.2-foss-2015a-bare + - module load CDO/1.9.8-foss-2015a + - module list + - echo "Running unit tests..." + - Rscript ./tests/test_seasonal.R + +unit-test-decadal: # This job runs in the test stage. + stage: test script: - - echo "Build Loading modules..." + - echo "Loading modules..." - module load R/4.1.2-foss-2015a-bare - module load CDO/1.9.8-foss-2015a - - echo "Test Running unit tests..." - module list - - Rscript ./tests/testthat.R #/test-decadal_monthly.R + - echo "Running decadal unit tests..." + - Rscript ./tests/test_decadal.R diff --git a/tests/test_decadal.R b/tests/test_decadal.R new file mode 100644 index 00000000..26bc856e --- /dev/null +++ b/tests/test_decadal.R @@ -0,0 +1,3 @@ +library(testthat) + +source('./tests/testthat/test-decadal_monthly.R') diff --git a/tests/test_seasonal.R b/tests/test_seasonal.R new file mode 100644 index 00000000..37aa3ee4 --- /dev/null +++ b/tests/test_seasonal.R @@ -0,0 +1,3 @@ +library(testthat) + +source('./tests/testthat/test-seasonal_monthly.R') diff --git a/tests/testthat.R b/tests/testthat.R deleted file mode 100644 index e803bbe4..00000000 --- a/tests/testthat.R +++ /dev/null @@ -1,8 +0,0 @@ -library(startR) -library(SpecsVerification) -library(s2dv) -library(CSTools) -library(testthat) - -print('In testthat.R. Running...') -source('./tests/testthat/test-decadal_monthly.R') diff --git a/tests/testthat/test-seasonal_monthly.R b/tests/testthat/test-seasonal_monthly.R new file mode 100644 index 00000000..b4147fbf --- /dev/null +++ b/tests/testthat/test-seasonal_monthly.R @@ -0,0 +1,17 @@ +context("Decadal monthly data") + +source("modules/Loading/Loading_decadal.R") +source("modules/Calibration/Calibration.R") +source("modules/Skill/Skill.R") +source("modules/Saving/Save_data.R") + +recipe_file <- "modules/Loading/testing_recipes/recipe_4.yml" + +test_that("1. Loading", { + +print('In test_that 1.') +#suppressWarnings( +#data <- load_datasets(recipe_file) +#) + +}) -- GitLab From 51b9a5a980e2abb6e7a2b68f6b2d30fd15231407 Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 29 Jun 2022 17:31:14 +0200 Subject: [PATCH 8/9] Set up unit test structure and add first decadal monthly case --- .gitlab-ci.yml | 4 +- modules/Loading/Loading_decadal.R | 4 +- tests/recipes/recipe-decadal_monthly_1.yml | 46 +++++ tests/test_decadal.R | 7 +- tests/testthat/test-decadal_monthly.R | 192 ++++++++++++++++++++- 5 files changed, 242 insertions(+), 11 deletions(-) create mode 100644 tests/recipes/recipe-decadal_monthly_1.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4ea4685f..8c720372 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,14 +1,14 @@ stages: # List of stages for jobs, and their order of execution - test -unit-test-job: # This job runs in the test stage. +unit-test-seasonal: # This job runs in the test stage. stage: test script: - echo "Loading modules..." - module load R/4.1.2-foss-2015a-bare - module load CDO/1.9.8-foss-2015a - module list - - echo "Running unit tests..." + - echo "Running seasonal unit tests..." - Rscript ./tests/test_seasonal.R unit-test-decadal: # This job runs in the test stage. diff --git a/modules/Loading/Loading_decadal.R b/modules/Loading/Loading_decadal.R index fe615594..f94d1343 100644 --- a/modules/Loading/Loading_decadal.R +++ b/modules/Loading/Loading_decadal.R @@ -2,11 +2,11 @@ # 1. archive.yml # 2. recipe.yml # 3. Load_decadal.R (V) -setwd('/esarchive/scratch/aho/git/auto-s2s/') +#setwd('/esarchive/scratch/aho/git/auto-s2s/') ## TODO: remove paths to personal scratchs source("/esarchive/scratch/vagudets/repos/csoperational/R/get_regrid_params.R") -source("/esarchive/scratch/vagudets/repos/cstools/R/s2dv_cube.R") +#source("/esarchive/scratch/vagudets/repos/cstools/R/s2dv_cube.R") # Load required libraries/funs source("modules/Loading/get_daily_time_ind.R") source("modules/Loading/check_latlon.R") diff --git a/tests/recipes/recipe-decadal_monthly_1.yml b/tests/recipes/recipe-decadal_monthly_1.yml new file mode 100644 index 00000000..5acd642d --- /dev/null +++ b/tests/recipes/recipe-decadal_monthly_1.yml @@ -0,0 +1,46 @@ +Description: + Author: An-Chi Ho + '': split version +Analysis: + Horizon: Decadal + Variables: + name: tas + freq: monthly_mean + Datasets: + System: + name: EC-Earth3-i4 #CanESM5 + member: r1i4p1f1,r2i4p1f1 + Multimodel: no + Reference: + name: ERA5 #JRA-55 + Time: + sdate: + fcst: 2021 +# fcst_sday: '1101' + hcst_start: 1991 #'1993' + hcst_end: 1994 #'2016' +# season: 'Annual' + leadtimemin: 0 + leadtimemax: 2 + Region: + latmin: 17 #-90 + latmax: 20 #90 + lonmin: 12 + lonmax: 15 #359.9 + Regrid: + method: bilinear + type: to_system #to_reference + Workflow: + Calibration: + method: bias + Skill: + metric: RPSS + Indicators: + index: FALSE + Output_format: S2S4E +Run: + Loglevel: INFO + Terminal: yes + output_dir: /esarchive/scratch/aho/git/auto-s2s/out-logs/ + code_dir: /esarchive/scratch/aho/git/auto-s2s/ + diff --git a/tests/test_decadal.R b/tests/test_decadal.R index 26bc856e..84a23b62 100644 --- a/tests/test_decadal.R +++ b/tests/test_decadal.R @@ -1,3 +1,8 @@ library(testthat) -source('./tests/testthat/test-decadal_monthly.R') +path_testthat <- file.path('./tests/testthat/') +files_testthat <- list.files('./tests/testthat/', pattern = 'decadal') + +for (i_file in 1:length(files_testthat)) { + source(paste0('./tests/testthat/', files_testthat[i_file])) +} diff --git a/tests/testthat/test-decadal_monthly.R b/tests/testthat/test-decadal_monthly.R index e3f85290..19560921 100644 --- a/tests/testthat/test-decadal_monthly.R +++ b/tests/testthat/test-decadal_monthly.R @@ -1,17 +1,197 @@ -context("Decadal monthly data") +context("Decadal monthly data - 1") + +########################################### source("modules/Loading/Loading_decadal.R") source("modules/Calibration/Calibration.R") source("modules/Skill/Skill.R") source("modules/Saving/Save_data.R") -recipe_file <- "modules/Loading/testing_recipes/recipe_decadal.yml" +recipe_file <- "tests/recipes/recipe-decadal_monthly_1.yml" + +# Load datasets +suppressWarnings({invisible(capture.output( +data <- load_datasets(recipe_file) +))}) + +recipe <- read_yaml(recipe_file) +# Calibrate datasets +suppressWarnings({invisible(capture.output( + calibrated_data <- calibrate_datasets(data, recipe) +))}) + +# Compute skill metrics +suppressWarnings({invisible(capture.output( +skill_metrics <- compute_skill_metrics(calibrated_data$hcst, data$obs, + recipe, na.rm = T, ncores = 4) +))}) + +#====================================== test_that("1. Loading", { -print('In test_that 1.') -#suppressWarnings( -#data <- load_datasets(recipe_file) -#) +expect_equal( +is.list(data), +TRUE +) +expect_equal( +names(data), +c("hcst", "fcst", "obs") +) +expect_equal( +class(data$hcst), +"s2dv_cube" +) +expect_equal( +class(data$fcst), +"s2dv_cube" +) +expect_equal( +class(data$obs), +"s2dv_cube" +) +expect_equal( +names(data$hcst), +c("data", "Variable", "Datasets", "Dates", "when", "source_files", "load_parameters") +) +expect_equal( +names(data$hcst), +names(data$fcst) +) +expect_equal( +names(data$hcst), +names(data$obs) +) +expect_equal( +dim(data$hcst$data), +c(dat = 1, var = 1, ensemble = 2, sday = 1, sweek = 1, syear = 4, time = 3, latitude = 5, longitude = 4) +) +expect_equal( +dim(data$fcst$data), +c(dat = 1, var = 1, ensemble = 2, sday = 1, sweek = 1, syear = 1, time = 3, latitude = 5, longitude = 4) +) +expect_equal( +dim(data$hcst$Dates$start), +c(sday = 1, sweek = 1, syear = 4, time = 3) +) +expect_equal( +as.vector(drop(data$hcst$data)[,1:2,1,2,3]), +c(291.3831, 291.6227, 292.3012, 290.9779), +tolerance = 0.0001 +) +expect_equal( +mean(data$hcst$data), +287.3804, +tolerance = 0.0001 +) +expect_equal( +range(data$hcst$data), +c(281.7395, 294.2467), +tolerance = 0.0001 +) +expect_equal( +(data$hcst$Dates$start)[1], +as.POSIXct("1991-11-16", tz = 'UTC') +) +expect_equal( +(data$hcst$Dates$start)[2], +as.POSIXct("1992-11-16", tz = 'UTC') +) +expect_equal( +(data$hcst$Dates$start)[5], +as.POSIXct("1991-12-16 12:00:00", tz = 'UTC') +) +expect_equal( +(data$hcst$Dates$start)[10], +as.POSIXct("1993-01-16 12:00:00", tz = 'UTC') +) + +}) + +#====================================== +test_that("2. Calibration", { + +expect_equal( +is.list(calibrated_data), +TRUE +) +expect_equal( +names(calibrated_data), +c("hcst", "fcst") +) +expect_equal( +class(calibrated_data$hcst), +"s2dv_cube" +) +expect_equal( +class(calibrated_data$fcst), +"s2dv_cube" +) +expect_equal( +dim(calibrated_data$hcst$data), +c(dat = 1, var = 1, ensemble = 2, sday = 1, sweek = 1, syear = 4, time = 3, latitude = 5, longitude = 4) +) +expect_equal( +dim(calibrated_data$fcst$data), +c(dat = 1, var = 1, ensemble = 2, sday = 1, sweek = 1, syear = 1, time = 3, latitude = 5, longitude = 4) +) +expect_equal( +mean(calibrated_data$fcst$data), +291.8375, +tolerance = 0.0001 +) +expect_equal( +mean(calibrated_data$hcst$data), +289.6679, +tolerance = 0.0001 +) +expect_equal( +as.vector(drop(calibrated_data$hcst$data)[1, , 2, 3, 4]), +c(286.3895, 286.6408, 290.6652, 288.3759), +tolerance = 0.0001 +) +expect_equal( +range(calibrated_data$fcst$data), +c(287.2173, 297.4578), +tolerance = 0.0001 +) + +}) + + +#====================================== +test_that("3. Metrics", { + +expect_equal( +is.list(skill_metrics), +TRUE +) +expect_equal( +names(skill_metrics), +c("rpss", "rpss_significance") +) +expect_equal( +class(skill_metrics$rpss[[1]]), +"array" +) +expect_equal( +dim(skill_metrics$rpss[[1]]), +c(dat = 1, var = 1, sday = 1, sweek = 1, time = 3, latitude = 5, longitude = 4) +) +expect_equal( +dim(skill_metrics$rpss_significance[[1]]), +dim(skill_metrics$rpss[[1]]) +) +expect_equal( +as.vector(drop(skill_metrics$rpss[[1]])[, 2, 3]), +c(-0.2857143, -1.2500000, -1.8928571), +tolerance = 0.0001 +) +expect_equal( +as.vector(drop(skill_metrics$rpss_significance[[1]])[, 2, 3]), +rep(FALSE, 3) +) }) + + -- GitLab From 580bb08123274aca8b3c06642b75dd4702cda9eb Mon Sep 17 00:00:00 2001 From: Victoria Agudetse Roures Date: Tue, 5 Jul 2022 11:26:30 +0200 Subject: [PATCH 9/9] Add monthly seasonal tests to CI/CD --- tests/recipes/recipe-seasonal_monthly_1.yml | 43 +++++ tests/test_seasonal.R | 8 +- tests/testthat/test-seasonal_monthly.R | 187 +++++++++++++++++++- 3 files changed, 230 insertions(+), 8 deletions(-) create mode 100644 tests/recipes/recipe-seasonal_monthly_1.yml diff --git a/tests/recipes/recipe-seasonal_monthly_1.yml b/tests/recipes/recipe-seasonal_monthly_1.yml new file mode 100644 index 00000000..3631c783 --- /dev/null +++ b/tests/recipes/recipe-seasonal_monthly_1.yml @@ -0,0 +1,43 @@ +Description: + Author: V. Agudetse + +Analysis: + Horizon: Seasonal + Variables: + name: tas + freq: monthly_mean + Datasets: + System: + name: system7c3s + Multimodel: False + Reference: + name: era5 + Time: + sdate: + fcst_syear: '2020' + fcst_sday: '1101' + hcst_start: '1993' + hcst_end: '1996' + leadtimemin: 0 + leadtimemax: 2 + Region: + latmin: 17 + latmax: 20 + lonmin: 12 + lonmax: 15 + Regrid: + method: bilinear + type: to_system + Workflow: + Calibration: + method: mse_min + Skill: + metric: RPSS + Indicators: + index: no + Output_format: S2S4E +Run: + Loglevel: INFO + Terminal: yes + output_dir: /esarchive/scratch/vagudets/repos/auto-s2s/out-logs/ + code_dir: /esarchive/scratch/vagudets/repos/auto-s2s/ diff --git a/tests/test_seasonal.R b/tests/test_seasonal.R index 37aa3ee4..4718e3d4 100644 --- a/tests/test_seasonal.R +++ b/tests/test_seasonal.R @@ -1,3 +1,9 @@ library(testthat) -source('./tests/testthat/test-seasonal_monthly.R') +path_testthat <- file.path('./tests/testthat/') +files_testthat <- list.files('./tests/testthat/', pattern = 'seasonal') + +for (i_file in 1:length(files_testthat)) { + source(paste0('./tests/testthat/', files_testthat[i_file])) +} + diff --git a/tests/testthat/test-seasonal_monthly.R b/tests/testthat/test-seasonal_monthly.R index b4147fbf..4bdf833d 100644 --- a/tests/testthat/test-seasonal_monthly.R +++ b/tests/testthat/test-seasonal_monthly.R @@ -1,17 +1,190 @@ -context("Decadal monthly data") +context("Seasonal monthly data") -source("modules/Loading/Loading_decadal.R") +source("modules/Loading/Loading.R") source("modules/Calibration/Calibration.R") source("modules/Skill/Skill.R") source("modules/Saving/Save_data.R") -recipe_file <- "modules/Loading/testing_recipes/recipe_4.yml" +recipe_file <- "tests/recipes/recipe-seasonal_monthly_1.yml" + +# Load datasets +suppressWarnings({invisible(capture.output( +data <- load_datasets(recipe_file) +))}) + +recipe <- read_yaml(recipe_file) + +suppressWarnings({invisible(capture.output( +calibrated_data <- calibrate_datasets(data, recipe) +))}) + +# Compute skill metrics +suppressWarnings({invisible(capture.output( +skill_metrics <- compute_skill_metrics(calibrated_data$hcst, data$obs, + recipe, na.rm = T, ncores = 4) +))}) test_that("1. Loading", { -print('In test_that 1.') -#suppressWarnings( -#data <- load_datasets(recipe_file) -#) +expect_equal( +is.list(data), +TRUE +) +expect_equal( +names(data), +c("hcst", "fcst", "obs") +) +expect_equal( +class(data$hcst), +"s2dv_cube" +) +expect_equal( +class(data$fcst), +"s2dv_cube" +) +expect_equal( +class(data$obs), +"s2dv_cube" +) +expect_equal( +names(data$hcst), +c("data", "lon", "lat", "Variable", "Datasets", "Dates", "when", "source_files", "load_parameters") +) +expect_equal( +names(data$hcst), +names(data$fcst) +) +expect_equal( +names(data$hcst), +names(data$obs) +) +expect_equal( +dim(data$hcst$data), +c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 4, time = 3, latitude = 3, longitude = 3, ensemble = 25) +) +expect_equal( +dim(data$fcst$data), +c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 1, time = 3, latitude = 3, longitude = 3, ensemble = 51) +) +expect_equal( +dim(data$hcst$Dates$start), +c(sday = 1, sweek = 1, syear = 4, time = 3) +) +expect_equal( +as.vector(drop(data$hcst$data)[1:2,1:2,1,2,3]), +c(293.9651, 295.9690, 290.6771, 290.7957), +tolerance = 0.0001 +) +expect_equal( +mean(data$hcst$data), +290.8758, +tolerance = 0.0001 +) +expect_equal( +range(data$hcst$data), +c(284.7413, 299.6219), +tolerance = 0.0001 +) +expect_equal( +(data$hcst$Dates$start)[1], +as.POSIXct("1993-12-01", tz = 'UTC') +) +expect_equal( +(data$hcst$Dates$start)[2], +as.POSIXct("1994-12-01", tz = 'UTC') +) +expect_equal( +(data$hcst$Dates$start)[5], +as.POSIXct("1994-01-01", tz = 'UTC') +) +expect_equal( +(data$obs$Dates$start)[10], +as.POSIXct("1995-02-14", tz = 'UTC') +) + +}) + +test_that("2. Calibration", { + +expect_equal( +is.list(calibrated_data), +TRUE +) +expect_equal( +names(calibrated_data), +c("hcst", "fcst") +) +expect_equal( +class(calibrated_data$hcst), +"s2dv_cube" +) +expect_equal( +class(calibrated_data$fcst), +"s2dv_cube" +) +expect_equal( +dim(calibrated_data$hcst$data), +c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 4, time = 3, latitude = 3, longitude = 3, ensemble = 25) +) +expect_equal( +dim(calibrated_data$fcst$data), +c(dat = 1, var = 1, sday = 1, sweek = 1, syear = 1, time = 3, latitude = 3, longitude = 3, ensemble = 51) +) +expect_equal( +mean(calibrated_data$fcst$data), +291.1218, +tolerance = 0.0001 +) +expect_equal( +mean(calibrated_data$hcst$data), +289.8596, +tolerance = 0.0001 +) +expect_equal( +as.vector(drop(calibrated_data$hcst$data)[1, , 2, 3, 4]), +c(287.7982, 287.0422, 290.4297), +tolerance = 0.0001 +) +expect_equal( +range(calibrated_data$fcst$data), +c(283.5374, 306.2353), +tolerance = 0.0001 +) + +}) + + +#====================================== +test_that("3. Metrics", { + +expect_equal( +is.list(skill_metrics), +TRUE +) +expect_equal( +names(skill_metrics), +c("rpss", "rpss_significance") +) +expect_equal( +class(skill_metrics$rpss[[1]]), +"array" +) +expect_equal( +dim(skill_metrics$rpss[[1]]), +c(dat = 1, var = 1, sday = 1, sweek = 1, time = 3, latitude = 3, longitude = 3) +) +expect_equal( +dim(skill_metrics$rpss_significance[[1]]), +dim(skill_metrics$rpss[[1]]) +) +expect_equal( +as.vector(drop(skill_metrics$rpss[[1]])[, 2, 3]), +c(-1.153829, -1.114743, -1.392457), +tolerance = 0.0001 +) +expect_equal( +as.vector(drop(skill_metrics$rpss_significance[[1]])[, 2, 3]), +rep(FALSE, 3) +) }) -- GitLab