Skip to content

Commit

Permalink
lintr cleanup of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rfiorella committed Nov 27, 2023
1 parent 0fcf605 commit 3c69c19
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 188 deletions.
4 changes: 2 additions & 2 deletions R/calibrate_ambient_water_isotopes.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ calibrate_ambient_water_linreg <- function(amb_data_list,
# determine which cal period each ambient data belongs to.
var_inds_in_calperiod <- list()

for (i in 1:nrow(caldf)) {
for (i in seq_len(nrow(caldf))) {
int <- lubridate::interval(caldf$timeBgn[i], caldf$timeEnd[i])
var_inds_in_calperiod[[i]] <- which(amb_end_times %within% int)
}
Expand All @@ -76,7 +76,7 @@ calibrate_ambient_water_linreg <- function(amb_data_list,
oxydf$mean_cal <- oxydf$mean
oxydf$max_cal <- oxydf$max
oxydf$min_cal <- oxydf$min

for (i in 1:length(var_inds_in_calperiod)) {
if (!is.na(caldf$r2_18O[i]) & caldf$r2_18O[i] > r2_thres) {

Expand Down
41 changes: 20 additions & 21 deletions R/calibrate_carbon.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,30 +199,29 @@ calibrate_carbon <- function(inname,

ciso_subset_cal <-
lapply(names(ciso_subset),
function(x) {
calibrate_ambient_carbon_Bowling2003(
amb_data_list = ciso_subset[[x]],
caldf = cal_df,
site = site,
filter_data = filter_ambient,
force_to_end = force_cal_to_end,
force_to_beginning = force_cal_to_beginning,
r2_thres = r2_thres)
})
function(x) {
calibrate_ambient_carbon_Bowling2003(amb_data_list = ciso_subset[[x]],
caldf = cal_df,
site = site,
filter_data = filter_ambient,
force_to_end = force_cal_to_end,
force_to_beginning = force_cal_to_beginning,
r2_thres = r2_thres)
})

} else if (method == "linreg") {

ciso_subset_cal <- lapply(names(ciso_subset),
function(x) {
calibrate_ambient_carbon_linreg(
amb_data_list = ciso_subset[[x]],
caldf = cal_df,
site = site,
filter_data = filter_ambient,
force_to_end = force_cal_to_end,
force_to_beginning = force_cal_to_beginning,
r2_thres = r2_thres)
})
ciso_subset_cal <-
lapply(names(ciso_subset),
function(x) {
calibrate_ambient_carbon_linreg(amb_data_list = ciso_subset[[x]],
caldf = cal_df,
site = site,
filter_data = filter_ambient,
force_to_end = force_cal_to_end,
force_to_beginning = force_cal_to_beginning,
r2_thres = r2_thres)
})
}

names(ciso_subset_cal) <- names(ciso_subset)
Expand Down
2 changes: 1 addition & 1 deletion R/excised.R
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,4 @@ copy_ucrt_group <- function(data_list, outname, site, file, species) {

}

# nocov end
# nocov end
55 changes: 11 additions & 44 deletions tests/testthat/test-data_extraction.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

fin <- system.file("extdata",
"NEON.D15.ONAQ.DP4.00200.001.nsae.2019-05.basic.packed.h5",
package = 'NEONiso', mustWork = TRUE)
package = "NEONiso", mustWork = TRUE)
# 1) for CO2:
co2test <- ingest_data(fin, analyte = "Co2", amb_avg = 9, ref_avg = 9)

Expand All @@ -22,19 +22,19 @@ test_that("extract_carbon_calibration_data output has correct structure", {
})

test_that("extract_carbon_calibration_data removes standards correctly", {

expect_lt(nrow(extract_carbon_cal_data(co2test$refe_stacked,
standards = c("co2Low", "co2Med"))),
nrow(extract_carbon_cal_data(co2test$refe_stacked)))
standards = c("co2Low", "co2Med"))),
nrow(extract_carbon_cal_data(co2test$refe_stacked)))

expect_lt(nrow(extract_carbon_cal_data(co2test$refe_stacked,
standards = c("co2Low", "co2High"))),
nrow(extract_carbon_cal_data(co2test$refe_stacked)))
standards = c("co2Low", "co2High"))),
nrow(extract_carbon_cal_data(co2test$refe_stacked)))

expect_lt(nrow(extract_carbon_cal_data(co2test$refe_stacked,
standards = "co2Low")),
nrow(extract_carbon_cal_data(co2test$refe_stacked,
standards = c("co2High", "co2Med"))))
standards = "co2Low")),
nrow(extract_carbon_cal_data(co2test$refe_stacked,
standards = c("co2High", "co2Med"))))
})

# 2) for H2O:
Expand All @@ -55,36 +55,3 @@ test_that("extract_water_calibration_data output has correct structure", {
expect_s3_class(tmp$timeBgn, "POSIXct")
expect_s3_class(tmp$timeEnd, "POSIXct")
})


# wiso_ref <- neonUtilities::stackEddy(fin, level = "dp01", avg = 3)
#
# high <- subset(wiso_ref[["ONAQ"]],
# wiso_ref[["ONAQ"]]$verticalPosition == "h2oHigh")
# med <- subset(wiso_ref[["ONAQ"]],
# wiso_ref[["ONAQ"]]$verticalPosition == "h2oMed")
# low <- subset(wiso_ref[["ONAQ"]],
# wiso_ref[["ONAQ"]]$verticalPosition == "h2oLow")
#
# test_that("extract_water_calibration_data returns 16 columns for by_site method", {
# expect_equal(ncol(extract_water_calibration_data(high,
# standard = "high",
# method = "by_site")), 16)
# expect_equal(ncol(extract_water_calibration_data(med,
# standard = "med",
# method = "by_site")), 16)
# expect_equal(ncol(extract_water_calibration_data(low,
# standard = "low",
# method = "by_site")), 16)
# })
#
# test_that("extract_water_calibration_data returns error if invalid standard provided", {
# expect_error(extract_water_calibration_data(low,
# standard = "cheese",
# method = "by_site"))
# expect_error(extract_water_calibration_data(low,
# standard = "cheese",
# method = "by_month"))
# })
#

14 changes: 5 additions & 9 deletions tests/testthat/test-data_ingestion.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ fin <- system.file("extdata",
#------------------------------------------
# various tests about output structure of ingest_data.

# TEMPORARY TESTS:
# expect failure if analyte = "H2o"
#test_that("ingest_data errors for H2o (TEMPORARY)", {
# expect_error(ingest_data(fin, analyte = "H2o"))
#})

# a) get data
co2test <- ingest_data(fin, analyte = "Co2", amb_avg = 9, ref_avg = 9)
h2otest <- ingest_data(fin, analyte = "H2o", amb_avg = 9, ref_avg = 3)
Expand All @@ -51,7 +45,9 @@ rest_raw <- neonUtilities::stackEddy(fin, avg = 9, level = "dp01")[[1]] %>%
dplyr::select("verticalPosition", "timeBgn",
"timeEnd", tidyselect::contains("isoCo2"))

rest_raw <- rest_raw[rowSums(is.na(rest_raw)) < 145, ] # Needed to remove merge error where times are slightly different between h2o and co2.
# Needed to remove merge error where times are
# slightly different between h2o and co2.
rest_raw <- rest_raw[rowSums(is.na(rest_raw)) < 145, ]

test_that("restructure_carbon_variables errors when invalid mode provided", {
expect_error(restructure_carbon_variables(rest_raw,
Expand Down Expand Up @@ -117,6 +113,6 @@ test2 <- ingest_data(all_files, analyte = "Co2", amb_avg = 9, ref_avg = 9)

test_that("stackEddy/ingest data works on multiple daily files", {
# would expect number of rows to be larger in test2 than test 1:
expect_gt(nrow(test2$ambient$`000_010_09m`$dlta13CCo2), nrow(test1$ambient$`000_010_09m`$dlta13CCo2))
expect_gt(nrow(test2$ambient$`000_010_09m`$dlta13CCo2),
nrow(test1$ambient$`000_010_09m`$dlta13CCo2))
})

74 changes: 40 additions & 34 deletions tests/testthat/test-data_regression.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
# CARBON FUNCTIONS
#----------------------

# test to make sure that the I/O structures across regression functions are correct.
# test to make sure that the I/O structures
# across regression functions are correct.
fin <- system.file("extdata",
"NEON.D15.ONAQ.DP4.00200.001.nsae.2019-05.basic.packed.h5",
package = "NEONiso", mustWork = TRUE)
co2test <- ingest_data(fin, analyte = "Co2", amb_avg = 9, ref_avg = 9)
co2data <- NEONiso:::extract_carbon_cal_data(co2test$refe_stacked,
standards = c("co2Low", "co2Med", "co2High"))
calDf_B03 <- NEONiso:::fit_carbon_regression(co2data,
standards = c("co2Low",
"co2Med",
"co2High"))
caldf_b03 <- NEONiso:::fit_carbon_regression(co2data,
method = "Bowling_2003",
calibration_half_width = 2)
calDf_LR <- NEONiso:::fit_carbon_regression(co2data,
caldf_lr <- NEONiso:::fit_carbon_regression(co2data,
method = "linreg",
calibration_half_width = 2)

Expand All @@ -32,8 +35,8 @@ test_that("fit_carbon_regression returns data.frame", {

test_that("calibration data frames have 14 columns", {
skip_on_cran()
expect_equal(ncol(calDf_B03), 14)
expect_equal(ncol(calDf_LR), 14)
expect_equal(ncol(caldf_b03), 14)
expect_equal(ncol(caldf_lr), 14)
})

test_that("carbon calibration data frames correct even when no input data", {
Expand All @@ -57,12 +60,12 @@ test_that("carbon calibration data frames correct even when no input data", {

# test carbon - bowling ambient calibration
temp <- calibrate_ambient_carbon_Bowling2003(co2test$ambient$`000_010_09m`,
calDf_B03,
caldf_b03,
site = "ONAQ")
temp_gf <- calibrate_ambient_carbon_Bowling2003(co2test$ambient$`000_010_09m`,
calDf_B03,
site = "ONAQ",
gap_fill_parameters = TRUE)
caldf_b03,
site = "ONAQ",
gap_fill_parameters = TRUE)

test_that("calibrate_ambient_carbon_Bowling2003 returns a list",{
skip_on_cran()
Expand All @@ -83,20 +86,22 @@ test_that("calibrate_ambient_carbon_Bowling2003 returns correct variables", {

test_that("calibrated d13C values have been added to calibrate_ambient_cabron_Bowling2003 output", {
skip_on_cran()
expect_gt(ncol(temp$dlta13CCo2), ncol(co2test$ambient$`000_010_09m`$dlta13CCo2))
expect_gt(ncol(temp_gf$dlta13CCo2), ncol(co2test$ambient$`000_010_09m`$dlta13CCo2))
expect_gt(ncol(temp$dlta13CCo2),
ncol(co2test$ambient$`000_010_09m`$dlta13CCo2))
expect_gt(ncol(temp_gf$dlta13CCo2),
ncol(co2test$ambient$`000_010_09m`$dlta13CCo2))
})

# test carbon - linreg ambient calibration
temp <- calibrate_ambient_carbon_linreg(co2test$ambient$`000_010_09m`,
calDf_LR,
site = "ONAQ")
caldf_lr,
site = "ONAQ")
temp_gf <- calibrate_ambient_carbon_linreg(co2test$ambient$`000_010_09m`,
calDf_LR,
site = "ONAQ",
gap_fill_parameters = TRUE)
caldf_lr,
site = "ONAQ",
gap_fill_parameters = TRUE)

test_that("calibrate_ambient_carbon_linreg returns a list",{
test_that("calibrate_ambient_carbon_linreg returns a list", {
skip_on_cran()
expect_equal(class(temp), "list")
expect_equal(class(temp_gf), "list")
Expand All @@ -115,8 +120,10 @@ test_that("calibrate_ambient_carbon_linreg returns correct variables", {

test_that("calibrated d13C values have been added to calibrate_ambient_cabron_linreg output", {
skip_on_cran()
expect_gt(ncol(temp$dlta13CCo2), ncol(co2test$ambient$`000_010_09m`$dlta13CCo2))
expect_gt(ncol(temp_gf$dlta13CCo2), ncol(co2test$ambient$`000_010_09m`$dlta13CCo2))
expect_gt(ncol(temp$dlta13CCo2),
ncol(co2test$ambient$`000_010_09m`$dlta13CCo2))
expect_gt(ncol(temp_gf$dlta13CCo2),
ncol(co2test$ambient$`000_010_09m`$dlta13CCo2))
})


Expand All @@ -131,35 +138,35 @@ fin <- system.file("extdata",
h2otest <- ingest_data(fin, analyte = "H2o", amb_avg = 9, ref_avg = 3)
h2odata <- NEONiso:::extract_water_calibration_data(h2otest$refe_stacked)

calDf <- NEONiso:::fit_water_regression(h2odata,
caldf <- NEONiso:::fit_water_regression(h2odata,
calibration_half_width = 2)

test_that("fit_water_regression returns data.frame", {
skip_on_cran()
expect_s3_class(fit_water_regression(h2odata,
calibration_half_width = 2),
calibration_half_width = 2),
"data.frame")
})

test_that("calibration data frames have 14 columns", {
skip_on_cran()
expect_equal(ncol(calDf), 14)
expect_equal(ncol(caldf), 14)
})


# work through ambient calibrations

# test carbon - bowling ambient calibration
temp <- calibrate_ambient_water_linreg(h2otest$ambient$`000_010_09m`,
calDf,
site = "ONAQ")
caldf,
site = "ONAQ")
# no gapfilling in the water functions yet!
#temp_gf <- calibrate_ambient_water_linreg(h2otest$ambient$`000_010_09m`,
# calDf,
# site = "ONAQ",
# gap_fill_parameters = TRUE)

test_that("calibrate_ambient_water_linreg returns a list",{
test_that("calibrate_ambient_water_linreg returns a list", {
skip_on_cran()
str(temp)
expect_equal(class(temp), "list")
Expand All @@ -169,17 +176,16 @@ test_that("calibrate_ambient_water_linreg returns a list",{
test_that("calibrate_ambient_water_linreg returns correct variables", {
skip_on_cran()
vars <- c("dlta18OH2o", "dlta2HH2o", "pres", "presEnvHut", "rhEnvHut",
"rtioMoleDryH2o", "rtioMoleWetH2o",
"rtioMoleWetH2oEnvHut", "temp", "tempEnvHut")
"rtioMoleDryH2o", "rtioMoleWetH2o",
"rtioMoleWetH2oEnvHut", "temp", "tempEnvHut")
expect_equal(names(temp), vars)
# expect_equal(names(temp_gf), vars)
#expect_equal(names(temp_gf), vars)
})

test_that("calibrated water isotope values have been added to calibrate_ambient_water_linreg output", {
skip_on_cran()
expect_gt(ncol(temp$dlta18OH2o), ncol(h2otest$ambient$`000_010_09m`$dlta18OH2o))
expect_gt(ncol(temp$dlta2HH2o), ncol(h2otest$ambient$`000_010_09m`$dlta2HH2o))
expect_gt(ncol(temp$dlta18OH2o),
ncol(h2otest$ambient$`000_010_09m`$dlta18OH2o))
expect_gt(ncol(temp$dlta2HH2o),
ncol(h2otest$ambient$`000_010_09m`$dlta2HH2o))
})



Loading

0 comments on commit 3c69c19

Please sign in to comment.