From bf4e38620be087a0a0487cdda9c21cfe5c34ed23 Mon Sep 17 00:00:00 2001 From: Edvin Fuglebakk Date: Thu, 11 Jan 2024 22:23:42 +0100 Subject: [PATCH] Fixed suggests dependent tests --- .github/workflows/check-full.yaml | 4 +- .github/workflows/test-coverage.yaml | 1 + R/RecaWrap.R | 6 +++ inst/tinytest/test-ConvergenceAnalysis.R | 45 ++++++++++++++++++++++ inst/tinytest/test-RecaFormatChecks.R | 2 +- inst/tinytest/test-RecaWrap.R | 3 +- inst/tinytest/test-StoxAnalysisFunctions.R | 2 +- inst/tinytest/test-StoxDataTypes.R | 3 ++ inst/tinytest/test-StoxReportFunctions.R | 44 --------------------- inst/tinytest/test-ecaOuputputConversion.R | 5 ++- inst/tinytest/test-lengthGroupCollapse.R | 6 +++ tests/testAgainstStoxPrep.R | 4 +- 12 files changed, 71 insertions(+), 54 deletions(-) create mode 100644 inst/tinytest/test-ConvergenceAnalysis.R diff --git a/.github/workflows/check-full.yaml b/.github/workflows/check-full.yaml index 82178795..357a4565 100644 --- a/.github/workflows/check-full.yaml +++ b/.github/workflows/check-full.yaml @@ -182,7 +182,7 @@ jobs: shell: Rscript {0} # turn off testing of suggestions for configurations where Reca is not provided in StoX package repositories - - name: Check without suggested dependencies + - name: Check without suggest-dependencies if: runner.os == 'macOS' && (matrix.config.r== 'next' || matrix.config.r== '4.3') env: _R_CHECK_CRAN_INCOMING_: false @@ -190,7 +190,7 @@ jobs: run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "error", check_dir = "check") shell: Rscript {0} - - name: Check with suggested dependencies + - name: Check with suggest-dependencies if: runner.os != 'macOS' || (matrix.config.r!= 'next' && matrix.config.r!= '4.3') env: _R_CHECK_CRAN_INCOMING_: false diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index 2806a966..964b3d0e 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -2,6 +2,7 @@ on: push: branches: - master + - testing pull_request: branches: - testing diff --git a/R/RecaWrap.R b/R/RecaWrap.R index 224dec76..e5e9825f 100644 --- a/R/RecaWrap.R +++ b/R/RecaWrap.R @@ -1108,3 +1108,9 @@ rEcaDataReport <- function(samples, landings, covariates){ out <- data.table::as.data.table(out) return(out) } + +#' Runs Reca::eca.estimate. Provided so that tinytest unittest can be implemented in a way that is dependent on Reca being available +#' @noRd +eca.estimate <- function(AgeLength, WeightLength, Landings, GlobalParameters){ + return(Reca::eca.estimate(AgeLength, WeightLength, Landings, GlobalParameters)) +} diff --git a/inst/tinytest/test-ConvergenceAnalysis.R b/inst/tinytest/test-ConvergenceAnalysis.R new file mode 100644 index 00000000..0f9d5d5e --- /dev/null +++ b/inst/tinytest/test-ConvergenceAnalysis.R @@ -0,0 +1,45 @@ +# ECA tests are only run if Reca is installed. +if (nchar(system.file(package="Reca"))>0){ +#context("Test StoxReportFunctions: ReportRecaParameterStatistics") +StoxLandingFile <- system.file("testresources","StoxLandingData.rds", package="RstoxFDA") +StoxLandingData <- readRDS(StoxLandingFile) +StoxBioticFile <- system.file("testresources","StoxBioticData.rds", package="RstoxFDA") +StoxBioticData <- readRDS(StoxBioticFile) +StoxBioticData$Individual <- StoxBioticData$Individual[StoxBioticData$Individual$IndividualAge<4,] +StoxBioticData$Haul$Gear <- StoxLandingData$Landing$Gear[c(1:20, 1:20, 1:5)] +StoxBioticData$Station$Area <- StoxLandingData$Landing$Area[c(1:20, 1:20, 1:5)] +prep <- RstoxFDA::PrepareRecaEstimate(StoxBioticData, StoxLandingData, FixedEffects = c(), RandomEffects = c("Gear", "Area"), CellEffect = c("All"), MinAge = 2, MaxAge = 3) + +fpath1 <- RstoxFDA:::makeTempDirReca("chain1") +fpath2 <- RstoxFDA:::makeTempDirReca("chain2") +fpath3 <- RstoxFDA:::makeTempDirReca("chain3") + +paramOut1 <- RstoxFDA::ParameterizeRecaModels(prep, 10, 50, 1, ResultDirectory = fpath1) +paramOut2 <- RstoxFDA::ParameterizeRecaModels(prep, 10, 50, 1, ResultDirectory = fpath2) + +paramSummary <- RstoxFDA::ReportRecaParameterStatistics(paramOut1) +paramSummary <- RstoxFDA::ReportRecaParameterStatistics(paramOut2, paramSummary, AppendReport = TRUE) +expect_true(RstoxFDA::is.ParameterizationSummaryData(paramSummary)) + +RstoxFDA:::removeTempDirReca(fpath1) +RstoxFDA:::removeTempDirReca(fpath2) +RstoxFDA:::removeTempDirReca(fpath3) + +convergence <- RstoxFDA::ReportParameterConvergence(paramSummary) + +expect_true(RstoxFDA::is.ParameterConvergenceData(convergence)) +expect_true(nrow(convergence$ConvergenceReport) < 433) +expect_true(nrow(convergence$ConvergenceReport) > 0) + +#construct three identical chains, should signal convergence +paramSummary <- RstoxFDA::ReportRecaParameterStatistics(paramOut1) +paramOut1$GlobalParameters$GlobalParameters$resultdir="B" +paramSummary <- RstoxFDA::ReportRecaParameterStatistics(paramOut1, paramSummary, AppendReport = T) +paramOut1$GlobalParameters$GlobalParameters$resultdir="C" +paramSummary <- RstoxFDA::ReportRecaParameterStatistics(paramOut1, paramSummary, AppendReport = T) + +#context("Check Gelman-Rubin for equal chains") +convergence <- RstoxFDA::ReportParameterConvergence(paramSummary, Tolerance = 0) +expect_equal(nrow(convergence$ConvergenceReport), 433) +expect_true(all(abs(convergence$ConvergenceReport$GelmanRubinR-1)<.1)) +} diff --git a/inst/tinytest/test-RecaFormatChecks.R b/inst/tinytest/test-RecaFormatChecks.R index b0f06349..3421f2d9 100644 --- a/inst/tinytest/test-RecaFormatChecks.R +++ b/inst/tinytest/test-RecaFormatChecks.R @@ -1,7 +1,7 @@ # ECA tests are not run for platforms where Reca is not available from StoX repositories. # ECA tests are only run if Reca is installed. -if (length(system.file(package="Reca"))>0){ +if (nchar(system.file(package="Reca"))>0){ StoxLandingFile <- system.file("testresources","StoxLandingData.rds", package="RstoxFDA") StoxLandingData <- readRDS(StoxLandingFile) diff --git a/inst/tinytest/test-RecaWrap.R b/inst/tinytest/test-RecaWrap.R index 2c486a99..8ad5129f 100644 --- a/inst/tinytest/test-RecaWrap.R +++ b/inst/tinytest/test-RecaWrap.R @@ -1,7 +1,6 @@ -# ECA tests are not run for platforms where Reca is not available from StoX repositories. # ECA tests are only run if Reca is installed. -if (length(system.file(package="Reca"))>0){ +if (nchar(system.file(package="Reca"))>0){ fishdata <- data.table::as.data.table(readRDS(system.file(package = "RstoxFDA", "testresources", "fishdata.rda"))) landings <- data.table::as.data.table(readRDS(system.file(package = "RstoxFDA", "testresources", "landings.rda"))) diff --git a/inst/tinytest/test-StoxAnalysisFunctions.R b/inst/tinytest/test-StoxAnalysisFunctions.R index 0dc4c1ee..eb7cbad4 100644 --- a/inst/tinytest/test-StoxAnalysisFunctions.R +++ b/inst/tinytest/test-StoxAnalysisFunctions.R @@ -1,7 +1,7 @@ # Tests StoX analysis functions that interfaces Reca. # ECA tests are only run if Reca is installed. -if (length(system.file(package="Reca"))>0){ +if (nchar(system.file(package="Reca"))>0){ #context("Test ParameterizeRecaModels cache") StoxBioticFile <- system.file("testresources","StoxBioticData.rds", package="RstoxFDA") StoxBioticData <- readRDS(StoxBioticFile) diff --git a/inst/tinytest/test-StoxDataTypes.R b/inst/tinytest/test-StoxDataTypes.R index 69cc6864..8739128a 100644 --- a/inst/tinytest/test-StoxDataTypes.R +++ b/inst/tinytest/test-StoxDataTypes.R @@ -11,5 +11,8 @@ expect_true(RstoxFDA:::is.POSIXct(as.POSIXct(Sys.Date()))) expect_true(RstoxFDA:::is.RecaPrediction(RstoxFDA::recaPrediction)) #context("Test is.RecaResult") +# ECA tests are only run if Reca is installed. +if (nchar(system.file(package="Reca"))>0){ suppressWarnings(ex<-RstoxFDA::RunRecaEstimate(RstoxFDA::recaDataExample, 100,100)) expect_true(RstoxFDA:::is.RecaResult(ex)) +} \ No newline at end of file diff --git a/inst/tinytest/test-StoxReportFunctions.R b/inst/tinytest/test-StoxReportFunctions.R index 5263fb48..a3e27183 100644 --- a/inst/tinytest/test-StoxReportFunctions.R +++ b/inst/tinytest/test-StoxReportFunctions.R @@ -10,50 +10,6 @@ expect_true(!any(is.na(decomp$MeanWeightByAge$SD))) expect_true(!any(is.na(decomp$MeanWeightByAge$Low))) expect_true(!any(is.na(decomp$MeanWeightByAge$High))) -#context("Test StoxReportFunctions: ReportRecaParameterStatistics") -StoxLandingFile <- system.file("testresources","StoxLandingData.rds", package="RstoxFDA") -StoxLandingData <- readRDS(StoxLandingFile) -StoxBioticFile <- system.file("testresources","StoxBioticData.rds", package="RstoxFDA") -StoxBioticData <- readRDS(StoxBioticFile) -StoxBioticData$Individual <- StoxBioticData$Individual[StoxBioticData$Individual$IndividualAge<4,] -StoxBioticData$Haul$Gear <- StoxLandingData$Landing$Gear[c(1:20, 1:20, 1:5)] -StoxBioticData$Station$Area <- StoxLandingData$Landing$Area[c(1:20, 1:20, 1:5)] -prep <- RstoxFDA::PrepareRecaEstimate(StoxBioticData, StoxLandingData, FixedEffects = c(), RandomEffects = c("Gear", "Area"), CellEffect = c("All"), MinAge = 2, MaxAge = 3) - -fpath1 <- RstoxFDA:::makeTempDirReca("chain1") -fpath2 <- RstoxFDA:::makeTempDirReca("chain2") -fpath3 <- RstoxFDA:::makeTempDirReca("chain3") - -paramOut1 <- RstoxFDA::ParameterizeRecaModels(prep, 10, 50, 1, ResultDirectory = fpath1) -paramOut2 <- RstoxFDA::ParameterizeRecaModels(prep, 10, 50, 1, ResultDirectory = fpath2) - -paramSummary <- RstoxFDA::ReportRecaParameterStatistics(paramOut1) -paramSummary <- RstoxFDA::ReportRecaParameterStatistics(paramOut2, paramSummary, AppendReport = TRUE) -expect_true(RstoxFDA::is.ParameterizationSummaryData(paramSummary)) - -RstoxFDA:::removeTempDirReca(fpath1) -RstoxFDA:::removeTempDirReca(fpath2) -RstoxFDA:::removeTempDirReca(fpath3) - -convergence <- RstoxFDA::ReportParameterConvergence(paramSummary) - -expect_true(RstoxFDA::is.ParameterConvergenceData(convergence)) -expect_true(nrow(convergence$ConvergenceReport) < 433) -expect_true(nrow(convergence$ConvergenceReport) > 0) - -#construct three identical chains, should signal convergence -paramSummary <- RstoxFDA::ReportRecaParameterStatistics(paramOut1) -paramOut1$GlobalParameters$GlobalParameters$resultdir="B" -paramSummary <- RstoxFDA::ReportRecaParameterStatistics(paramOut1, paramSummary, AppendReport = T) -paramOut1$GlobalParameters$GlobalParameters$resultdir="C" -paramSummary <- RstoxFDA::ReportRecaParameterStatistics(paramOut1, paramSummary, AppendReport = T) - -#context("Check Gelman-Rubin for equal chains") -convergence <- RstoxFDA::ReportParameterConvergence(paramSummary, Tolerance = 0) -expect_equal(nrow(convergence$ConvergenceReport), 433) -expect_true(all(abs(convergence$ConvergenceReport$GelmanRubinR-1)<.1)) - - #context("Test StoxReportFunctions: ReportRecaCatchStatistics") predictiondatafile <- system.file("testresources","stocksplitpred.rds", package="RstoxFDA") catchAtAgeFlat <- readRDS(system.file("testresources", "recaPredictionFlat.rds", package="RstoxFDA")) diff --git a/inst/tinytest/test-ecaOuputputConversion.R b/inst/tinytest/test-ecaOuputputConversion.R index 95aa9eba..49632db0 100644 --- a/inst/tinytest/test-ecaOuputputConversion.R +++ b/inst/tinytest/test-ecaOuputputConversion.R @@ -1,7 +1,7 @@ # ECA tests are only run if Reca is installed. -if (length(system.file(package="Reca"))>0){ +if (nchar(system.file(package="Reca"))>0){ #context("test-StoxAnalysisFunctions: tests RecaResult conversion") ecaResult <- readRDS(system.file("testresources","ecaResult.rds", package="RstoxFDA")) @@ -72,7 +72,8 @@ prep <- RstoxFDA:::PrepareRecaEstimate(StoxBioticData, StoxLandingData, FixedEff fpath <- RstoxFDA:::makeTempDirReca() RecaData <- RstoxFDA::convertRecaData(prep, nSamples = 10, burnin = 50, thin=1, resultdir = fpath, delta.age = .001, fitfile = "fit", seed = 42, lgamodel = "log-linear") sanitizeRecaInput(RecaData$AgeLength, RecaData$WeightLength, RecaData$Landings, RecaData$GlobalParameters, stage="parameterize") -est<-Reca::eca.estimate(RecaData$AgeLength, RecaData$WeightLength, RecaData$Landings, RecaData$GlobalParameters) + +est<-RstoxFDA:::eca.estimate(RecaData$AgeLength, RecaData$WeightLength, RecaData$Landings, RecaData$GlobalParameters) RstoxFDA:::removeTempDirReca(fpath) } diff --git a/inst/tinytest/test-lengthGroupCollapse.R b/inst/tinytest/test-lengthGroupCollapse.R index 46005730..104fd4cf 100644 --- a/inst/tinytest/test-lengthGroupCollapse.R +++ b/inst/tinytest/test-lengthGroupCollapse.R @@ -1,3 +1,7 @@ +# ECA tests are only run if Reca is installed. + +if (nchar(system.file(package="Reca"))>0){ + StoxLandingFile <- system.file("testresources","StoxLandingData.rds", package="RstoxFDA") StoxLandingData <- readRDS(StoxLandingFile) @@ -41,3 +45,5 @@ reportCAALwoLength <- RstoxFDA::ReportRecaCatchAtLengthAndAge(results) expect_equal(nrow(reportCAALwLength$NbyLength), 2158) expect_equal(nrow(reportCAALwoLength$NbyLength), 26) expect_equal(sum(reportCAALwoLength$NbyLength$CatchAtLength), sum(reportCAALwLength$NbyLength$CatchAtLength)) + +} diff --git a/tests/testAgainstStoxPrep.R b/tests/testAgainstStoxPrep.R index 198916a7..4205d8cc 100644 --- a/tests/testAgainstStoxPrep.R +++ b/tests/testAgainstStoxPrep.R @@ -7,7 +7,7 @@ library(data.table) # ECA tests are only run if Reca is installed. -if (length(system.file(package="Reca"))>0){ +if (nchar(system.file(package="Reca"))>0){ stoxRobj <- readRDS(system.file(package = "RstoxFDA", "testresources", "oldstoxprepreca")) @@ -43,4 +43,4 @@ tabStox <- RstoxFDA::makeResultTableRECA(stoxRecaResults$prediction) #RstoxFDA::plotCatchAtAge(prepRecaResults$prediction, title="RecaPrep results") #RstoxFDA::plotCatchAtAge(stoxRecaResults$prediction, title="StoxPrep results") -} \ No newline at end of file +}