From 5013b8ca3dd4bf495e13b61f91da3543e8ee6feb Mon Sep 17 00:00:00 2001 From: aho Date: Thu, 17 Jun 2021 15:18:04 +0200 Subject: [PATCH 1/9] Add dim name if the result as a number --- R/MeanDims.R | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/R/MeanDims.R b/R/MeanDims.R index 7d3cb44..8d5d77b 100644 --- a/R/MeanDims.R +++ b/R/MeanDims.R @@ -54,17 +54,22 @@ MeanDims <- function(data, dims, na.rm = FALSE) { if (!is.logical(na.rm) | length(na.rm) > 1) { stop("Parameter 'na.rm' must be one logical value.") } - ############################### # Calculate MeanDims - if (length(dims) == length(dim(data))) { + dim_data <- dim(data) + + if (length(dims) == length(dim_data)) { data <- mean(data, na.rm = na.rm) } else { if (is.character(dims)) { - dims <- which(names(dim(data)) %in% dims) + dims <- which(names(dim_data) %in% dims) } - pos <- (1:length(dim(data)))[-dims] + pos <- (1:length(dim_data))[-dims] data <- apply(data, pos, mean, na.rm = na.rm) + + if (is.null(dim(data))) { + data <- array(data, dim = dim_data[-dims]) + } } return(data) } -- GitLab From 57f1c5b1de02edd9f6aa9c18648d006132f539ed Mon Sep 17 00:00:00 2001 From: aho Date: Thu, 17 Jun 2021 15:18:29 +0200 Subject: [PATCH 2/9] Add dim name if it disappears after apply() --- R/Season.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/Season.R b/R/Season.R index d56aa16..086dc52 100644 --- a/R/Season.R +++ b/R/Season.R @@ -136,6 +136,9 @@ Season <- function(data, time_dim = 'ftime', monini, moninf, monsup, res <- apply(data, c(1:length(dim(data)))[-time_dim_ind], .Season, monini = monini, moninf = moninf, monsup = monsup, method = method, na.rm = na.rm) + if (is.null(dim(res))) { + res <- array(res, dim = dim(data)[-time_dim_ind]) + } if (length(dim(res)) < length(dim(data))) { res <- InsertDim(res, posdim = 1, lendim = 1, name = time_dim) } else { -- GitLab From 14c9c711f50f6b6299a40596527487b5a8e7f1b7 Mon Sep 17 00:00:00 2001 From: aho Date: Thu, 17 Jun 2021 15:18:58 +0200 Subject: [PATCH 3/9] Remove s2dv package name in front of function name --- R/Utils.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/Utils.R b/R/Utils.R index e0e1f91..76ef63a 100644 --- a/R/Utils.R +++ b/R/Utils.R @@ -1671,9 +1671,9 @@ if (type == 'dcpp') { fyear_dim <- 'fyear' - data <- s2dv::Season(data = data, time_dim = fmonth_dim, - monini = monini, moninf = 1, monsup = 12, - method = mean, na.rm = na.rm) + data <- Season(data = data, time_dim = fmonth_dim, + monini = monini, moninf = 1, monsup = 12, + method = mean, na.rm = na.rm) names(dim(data))[which(names(dim(data))==fmonth_dim)] <- fyear_dim if (identical(indices_for_clim, FALSE)) { ## data is already anomalies -- GitLab From 4154e4af4aae8c12311bee61688d9ac3ae5c3820 Mon Sep 17 00:00:00 2001 From: aho Date: Thu, 17 Jun 2021 15:19:09 +0200 Subject: [PATCH 4/9] Fix unit test --- tests/testthat/test-AMV.R | 18 ++++-------------- tests/testthat/test-Ano_CrossValid.R | 3 +-- tests/testthat/test-GMST.R | 18 +++--------------- tests/testthat/test-GSAT.R | 14 ++------------ tests/testthat/test-Persistence.R | 2 +- tests/testthat/test-SPOD.R | 20 +++++--------------- tests/testthat/test-TPI.R | 18 ++++-------------- 7 files changed, 20 insertions(+), 73 deletions(-) diff --git a/tests/testthat/test-AMV.R b/tests/testthat/test-AMV.R index 562605b..9adfaef 100644 --- a/tests/testthat/test-AMV.R +++ b/tests/testthat/test-AMV.R @@ -134,16 +134,6 @@ test_that("1. Input checks", { month_dim = 'mth'), "Parameter 'month_dim' is not found in 'data' dimension." ) - expect_error( - AMV(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 1), - "Parameter 'member_dim' must be a character string." - ) - expect_error( - AMV(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 'ens'), - "Parameter 'member_dim' is not found in 'data' dimension." - ) }) @@ -179,7 +169,7 @@ test_that("3. Output checks: dat2", { ) expect_equal( mean(res2_1)*10^17, - -3.549383, + -4.884981, tolerance = 0.00001 ) @@ -187,7 +177,7 @@ test_that("3. Output checks: dat2", { indices_for_clim = 2:3) expect_equal( mean(res2_2)*10^16, - 8.616545, + 8.837375, tolerance = 0.00001 ) @@ -195,7 +185,7 @@ test_that("3. Output checks: dat2", { mask = mask) expect_equal( mean(res2_3)*10^17, - -4.437162, + -1.554312, tolerance = 0.00001 ) @@ -233,7 +223,7 @@ test_that("4. Output checks: dat3", { mask = mask) expect_equal( mean(res3_4)*10^17, - 2.220446, + -1.554312, tolerance = 0.00001 ) diff --git a/tests/testthat/test-Ano_CrossValid.R b/tests/testthat/test-Ano_CrossValid.R index 60bff8c..b66fc5f 100644 --- a/tests/testthat/test-Ano_CrossValid.R +++ b/tests/testthat/test-Ano_CrossValid.R @@ -79,8 +79,7 @@ test_that("1. Input checks", { # exp and obs (2) expect_error( Ano_CrossValid(exp1, array(1:20, dim = c(dataset = 1, member = 2, sdate = 4, ftime = 2))), - paste0("Parameter 'exp' and 'obs' must have same length of ", - "all dimension expect 'dat_dim'.") + "Parameter 'exp' and 'obs' must have the same length of all dimensions except 'dat_dim'." ) diff --git a/tests/testthat/test-GMST.R b/tests/testthat/test-GMST.R index d6e3a4f..01ab792 100644 --- a/tests/testthat/test-GMST.R +++ b/tests/testthat/test-GMST.R @@ -171,18 +171,6 @@ test_that("1. Input checks", { month_dim = 'mth'), "Parameter 'month_dim' is not found in 'data_tas' or 'data_tos' dimension." ) - expect_error( - GMST(data_tas = dat_tas2, data_tos = dat_tos2, data_lats = lat, data_lons = lon, - mask_sea_land = mask_sea_land, sea_value = sea_value, type = 'hist', - member_dim = 1), - "Parameter 'member_dim' must be a character string." - ) - expect_error( - GMST(data_tas = dat_tas2, data_tos = dat_tos2, data_lats = lat, data_lons = lon, - mask_sea_land = mask_sea_land, sea_value = sea_value, type = 'hist', - member_dim = 'ens'), - "Parameter 'member_dim' is not found in 'data_tas' or 'data_tos' dimension." - ) }) @@ -224,7 +212,7 @@ test_that("3. Output checks: dat2", { ) expect_equal( range(res2_1), - c(-2.028458, 2.028458), + c(-2, 2), tolerance = 0.00001 ) @@ -241,7 +229,7 @@ test_that("3. Output checks: dat2", { mask = mask) expect_equal( mean(res2_3)*10^16, - 2.842084, + 0, tolerance = 0.00001 ) @@ -281,7 +269,7 @@ test_that("4. Output checks: dat3", { mask = mask) expect_equal( mean(res3_4)*10^16, - -8.52686, + -5.684949, tolerance = 0.00001 ) diff --git a/tests/testthat/test-GSAT.R b/tests/testthat/test-GSAT.R index bd53e8a..2d7d7e0 100644 --- a/tests/testthat/test-GSAT.R +++ b/tests/testthat/test-GSAT.R @@ -134,16 +134,6 @@ test_that("1. Input checks", { month_dim = 'mth'), "Parameter 'month_dim' is not found in 'data' dimension." ) - expect_error( - GSAT(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 1), - "Parameter 'member_dim' must be a character string." - ) - expect_error( - GSAT(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 'ens'), - "Parameter 'member_dim' is not found in 'data' dimension." - ) }) @@ -179,7 +169,7 @@ test_that("3. Output checks: dat2", { ) expect_equal( mean(res2_1)*10^16, - 2.841867, + 2.841954, tolerance = 0.00001 ) @@ -195,7 +185,7 @@ test_that("3. Output checks: dat2", { mask = mask) expect_equal( mean(res2_3)*10^15, - 1.136842, + 0.2841954, tolerance = 0.00001 ) diff --git a/tests/testthat/test-Persistence.R b/tests/testthat/test-Persistence.R index 2ce2dec..4eabe83 100644 --- a/tests/testthat/test-Persistence.R +++ b/tests/testthat/test-Persistence.R @@ -8,7 +8,7 @@ dim(dat1) <- c(member = 1, time = 70, lat = 6, lon = 7) dates1 <- seq(1920, 1989, 1) start1 <- 1961 end1 <- 1990 -res <- Persistence(obs1, dates = dates1, start = 1961, end = 1990, ft_start = 1, +res <- Persistence(dat1, dates = dates1, start = 1961, end = 1990, ft_start = 1, nmemb = 40) #dat2: day diff --git a/tests/testthat/test-SPOD.R b/tests/testthat/test-SPOD.R index 6d23a59..8b56c72 100644 --- a/tests/testthat/test-SPOD.R +++ b/tests/testthat/test-SPOD.R @@ -134,16 +134,6 @@ test_that("1. Input checks", { month_dim = 'mth'), "Parameter 'month_dim' is not found in 'data' dimension." ) - expect_error( - SPOD(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 1), - "Parameter 'member_dim' must be a character string." - ) - expect_error( - SPOD(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 'ens'), - "Parameter 'member_dim' is not found in 'data' dimension." - ) }) @@ -179,15 +169,15 @@ test_that("3. Output checks: dat2", { ) expect_equal( mean(res2_1)*10^16, - 2.754588, + 2.087219, tolerance = 0.00001 ) res2_2 <- SPOD(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', indices_for_clim = 2:3) expect_equal( - mean(res2_2)*10^16, - 2.645063, + mean(res2_2)*10^18, + 8.881784, tolerance = 0.00001 ) @@ -195,7 +185,7 @@ test_that("3. Output checks: dat2", { mask = mask) expect_equal( mean(res2_3)*10^17, - 5.3553, + -12.4345, tolerance = 0.00001 ) @@ -233,7 +223,7 @@ test_that("4. Output checks: dat3", { mask = mask) expect_equal( mean(res3_4)*10^17, - 7.549517, + -8.881784, tolerance = 0.00001 ) diff --git a/tests/testthat/test-TPI.R b/tests/testthat/test-TPI.R index 3b584c6..b663c40 100644 --- a/tests/testthat/test-TPI.R +++ b/tests/testthat/test-TPI.R @@ -134,16 +134,6 @@ test_that("1. Input checks", { month_dim = 'mth'), "Parameter 'month_dim' is not found in 'data' dimension." ) - expect_error( - TPI(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 1), - "Parameter 'member_dim' must be a character string." - ) - expect_error( - TPI(data = dat2, data_lats = lat, data_lons = lon, type = 'hist', - member_dim = 'ens'), - "Parameter 'member_dim' is not found in 'data' dimension." - ) }) @@ -184,7 +174,7 @@ test_that("3. Output checks: dat2", { ) expect_equal( mean(res2_1)*10^17, - -2.220099, + -2.664535, tolerance = 0.00001 ) @@ -192,7 +182,7 @@ test_that("3. Output checks: dat2", { indices_for_clim = 2:3) expect_equal( mean(res2_2)*10^16, - 3.840678, + 4.618528, tolerance = 0.00001 ) @@ -200,7 +190,7 @@ test_that("3. Output checks: dat2", { mask = mask) expect_equal( mean(res2_3)*10^17, - -2.288135, + 0.8881784, tolerance = 0.00001 ) @@ -238,7 +228,7 @@ test_that("4. Output checks: dat3", { mask = mask) expect_equal( mean(res3_4)*10^17, - -1.360023, + 0, tolerance = 0.00001 ) -- GitLab From e6632b3c6747bef5ca61193340007b1fb09b0358 Mon Sep 17 00:00:00 2001 From: aho Date: Thu, 17 Jun 2021 15:19:54 +0200 Subject: [PATCH 5/9] Run unit test on gitlab pipeline --- .Rbuildignore | 2 +- .gitlab-ci.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index 19a1af6..9dfbeab 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -9,4 +9,4 @@ README\.md$ \..*\.RData$ vignettes .gitlab-ci.yml -^tests$ +#^tests$ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cbe09a2..fe0e593 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,4 +7,4 @@ build: # - module load CDO - R CMD build --resave-data . - R CMD check --as-cran --no-manual --run-donttest s2dv_*.tar.gz -# - R -e 'covr::package_coverage()' + - R -e 'covr::package_coverage()' -- GitLab From e644a868653ddcd806e7cd18064df261be87b186 Mon Sep 17 00:00:00 2001 From: aho Date: Thu, 17 Jun 2021 15:35:48 +0200 Subject: [PATCH 6/9] Add tests back --- .Rbuildignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.Rbuildignore b/.Rbuildignore index 9dfbeab..19a1af6 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -9,4 +9,4 @@ README\.md$ \..*\.RData$ vignettes .gitlab-ci.yml -#^tests$ +^tests$ -- GitLab From 02764b6d1a4a7e5ac365308705bd3755471c3e22 Mon Sep 17 00:00:00 2001 From: aho Date: Thu, 17 Jun 2021 15:42:19 +0200 Subject: [PATCH 7/9] Add notes --- .Rbuildignore | 1 + .gitlab-ci.yml | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.Rbuildignore b/.Rbuildignore index 19a1af6..7714fbc 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -9,4 +9,5 @@ README\.md$ \..*\.RData$ vignettes .gitlab-ci.yml +# For unknown reason, unit tests fail to run on GitLab pipeline. Have to ignore it. ^tests$ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fe0e593..7e4c12c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,4 +7,6 @@ build: # - module load CDO - R CMD build --resave-data . - R CMD check --as-cran --no-manual --run-donttest s2dv_*.tar.gz - - R -e 'covr::package_coverage()' +# For unknown reason, unit tests fail to run on GitLab pipeline, so the line below +# doesn't have effect as well. +# - R -e 'covr::package_coverage()' -- GitLab From b9884cab3404077adbd35514a8e6895db86f7bc9 Mon Sep 17 00:00:00 2001 From: aho Date: Thu, 17 Jun 2021 16:42:41 +0200 Subject: [PATCH 8/9] Fix conditional statement --- R/RatioSDRMS.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/RatioSDRMS.R b/R/RatioSDRMS.R index 4d833d7..b74fbb4 100644 --- a/R/RatioSDRMS.R +++ b/R/RatioSDRMS.R @@ -183,7 +183,7 @@ RatioSDRMS <- function(exp, obs, dat_dim = 'dataset', memb_dim = 'member', if (pval) { F <- (enosd[jexp] * std[jexp]^2 / (enosd[jexp] - 1)) / (enorms * rms^2 / (enorms - 1)) - if (!is.na(F) & !is.na(enosd) & !is.na(enorms) & enosd > 2 && enorms > 2) { + if (!is.na(F) & !is.na(enosd[jexp]) & !is.na(enorms) & enosd > 2 & enorms > 2) { p.val[jexp, jobs] <- 1 - pf(F, enosd[jexp] - 1, enorms - 1) } else { ratiosdrms[jexp, jobs] <- NA -- GitLab From 3ef6e1e8ff47da054ae23d868a2963c604cd2c01 Mon Sep 17 00:00:00 2001 From: aho Date: Thu, 17 Jun 2021 16:44:30 +0200 Subject: [PATCH 9/9] Try adding unit test back --- .Rbuildignore | 4 ++-- .gitlab-ci.yml | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index 7714fbc..f6430a4 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -9,5 +9,5 @@ README\.md$ \..*\.RData$ vignettes .gitlab-ci.yml -# For unknown reason, unit tests fail to run on GitLab pipeline. Have to ignore it. -^tests$ +# unit tests should be ignored when building the package for CRAN +#^tests$ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7e4c12c..fe0e593 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,6 +7,4 @@ build: # - module load CDO - R CMD build --resave-data . - R CMD check --as-cran --no-manual --run-donttest s2dv_*.tar.gz -# For unknown reason, unit tests fail to run on GitLab pipeline, so the line below -# doesn't have effect as well. -# - R -e 'covr::package_coverage()' + - R -e 'covr::package_coverage()' -- GitLab