Skip to content

Commit

Permalink
test: Fix testing path for R CMD CHECK
Browse files Browse the repository at this point in the history
  • Loading branch information
fabern committed Sep 25, 2024
1 parent d06d211 commit 9c6bdf1
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 36 deletions.
4 changes: 2 additions & 2 deletions analysis/pmodel_use_newdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ library(tidyr)
library(khroma)

## ----------------------------------------------------------------------------------------------------------------------
p_model_drivers = readRDS(file = here::here("data/p_model_drivers_newformat.rds"))
p_model_validation = readRDS(file = here::here("data/p_model_validation_newformat.rds"))
p_model_drivers <- rsofun::p_model_drivers_format2024_08 # TODO: NOT YET UPDATED FOR PHYDRO (still add default phydro_* parameters)
# p_model_validation <- rsofun::p_model_validation_format2024_08

## ----------------------------------------------------------------------------------------------------------------------
# define model parameter values from previous work
Expand Down
Binary file added data/p_model_drivers_format2024_08.rda
Binary file not shown.
Binary file removed data/p_model_drivers_newformat.rds
Binary file not shown.
Binary file added data/p_model_validation_format2024_08.rda
Binary file not shown.
Binary file removed data/p_model_validation_newformat.rds
Binary file not shown.
12 changes: 4 additions & 8 deletions tests/testthat/test-calibration-pmodel.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ set.seed(10)

test_that("test GPP calibration routine p-model (BT, likelihood maximization)", {
skip_on_cran()
#drivers <- rsofun::p_model_drivers # TODO: NOT YET UPDATED FOR PHYDRO
drivers <- readRDS(file = here::here("data/p_model_drivers_newformat.rds"))
drivers <- rsofun::p_model_drivers_format2024_08 # TODO: NOT YET UPDATED FOR PHYDRO (still add default phydro_* parameters)
drivers$params_siml[[1]]$use_gs <- TRUE


Expand Down Expand Up @@ -60,8 +59,7 @@ test_that("test GPP calibration routine p-model (BT, likelihood maximization)",

test_that("test GPP calibration routine p-model (GenSA, rmse, all params)", {
skip_on_cran()
#drivers <- rsofun::p_model_drivers # TODO: NOT YET UPDATED FOR PHYDRO
drivers <- readRDS(file = here::here("data/p_model_drivers_newformat.rds"))
drivers <- rsofun::p_model_drivers_format2024_08 # TODO: NOT YET UPDATED FOR PHYDRO (still add default phydro_* parameters)
drivers$params_siml[[1]]$use_gs <- TRUE
obs <- rsofun::p_model_validation

Expand Down Expand Up @@ -218,8 +216,7 @@ test_that("test Vcmax25 calibration routine p-model (GenSA, rmse)", {
test_that("test joint calibration routine p-model (BT, likelihood maximization)", {
skip_on_cran()
drivers <- rbind(
gpp = # TODO: rsofun::p_model_drivers is NOT YET UPDATED FOR PHYDRO
readRDS(file = here::here("data/p_model_drivers_newformat.rds")),
gpp = rsofun::p_model_drivers_format2024_08, # TODO: NOT YET UPDATED FOR PHYDRO (still add default phydro_* parameters)
vcmax25 = rsofun::p_model_drivers_vcmax25 |>
# TODO: NOT YET UPDATED FOR PHYDRO
# # specify additionally needed params_siml flags:
Expand Down Expand Up @@ -270,7 +267,6 @@ test_that("test joint calibration routine p-model (BT, likelihood maximization)"
err_vcmax25 = list(lower = 0.0001, upper = 0.1, init = 0.005)
)
)
# debug(rsofun::runread_pmodel_f)
pars <- rsofun::calib_sofun(
drivers = drivers,
obs = obs,
Expand All @@ -284,4 +280,4 @@ test_that("test joint calibration routine p-model (BT, likelihood maximization)"

# test for correctly returned values
expect_type(pars, "list")
})
})
16 changes: 6 additions & 10 deletions tests/testthat/test-model-runs.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
context("test models and their parameters")
set.seed(10)

test_that("run_pmodel_f_bysite()", {
skip_on_cran()

Expand All @@ -18,8 +17,7 @@ test_that("run_pmodel_f_bysite()", {
)

# read in demo data
#df_drivers <- p_model_drivers # TODO: NOT YET UPDATED FOR PHYDRO (a newformat, b add phydro_ parameters)
df_drivers <- readRDS(file = here::here("data/p_model_drivers_newformat.rds"))
df_drivers <- rsofun::p_model_drivers_format2024_08 # TODO: NOT YET UPDATED FOR PHYDRO (still add default phydro_* parameters)

# check run_pmodel_f_bysite() ##########################
# run the SOFUN Fortran P-model using the internal function `run_pmodel_f_bysite`
Expand Down Expand Up @@ -154,9 +152,9 @@ test_that("run_pmodel_f_bysite()", {
dpsi = c(0, 0, 0, 0, 0, 0),
psi_leaf = c(0, 0, 0, 0, 0, 0)
)
expect_equal(slice(tibble(mod1), c(1, 70, 1200, 1400, 2000, 2180)), ref1, tolerance = 1e-7)
expect_equal(slice(tibble(mod2), c(1, 70, 1200, 1400, 2000, 2180)), ref2, tolerance = 1e-7)
expect_equal(slice(tibble(mod3), c(1, 70, 1200, 1400, 2000, 2180)), ref3, tolerance = 1e-7)
expect_equal(slice(tibble(mod1), c(1, 70, 1200, 1400, 2000, 2180)), ref1, tolerance = 1e-6)
expect_equal(slice(tibble(mod2), c(1, 70, 1200, 1400, 2000, 2180)), ref2, tolerance = 1e-6)
expect_equal(slice(tibble(mod3), c(1, 70, 1200, 1400, 2000, 2180)), ref3, tolerance = 1e-6)

})
test_that("runread_pmodel_f()", {
Expand All @@ -176,8 +174,7 @@ test_that("runread_pmodel_f()", {
)

# read in demo data
#df_drivers <- p_model_drivers # TODO: NOT YET UPDATED FOR PHYDRO (a newformat, b add phydro_ parameters)
df_drivers <- readRDS(file = here::here("data/p_model_drivers_newformat.rds"))
df_drivers <- rsofun::p_model_drivers_format2024_08 # TODO: NOT YET UPDATED FOR PHYDRO (still add default phydro_* parameters)

df_output_singlecore <- rsofun::runread_pmodel_f(
df_drivers,
Expand Down Expand Up @@ -290,8 +287,7 @@ test_that("phydro-model run check LE and AET", {
)

# read in demo data
#df_drivers <- p_model_drivers # TODO: NOT YET UPDATED FOR PHYDRO
df_drivers <- readRDS(file = here::here("data/p_model_drivers_newformat.rds"))
df_drivers <- rsofun::p_model_drivers_format2024_08 # TODO: NOT YET UPDATED FOR PHYDRO (still add default phydro_* parameters)
df_drivers$params_siml[[1]]$use_gs <- TRUE

# run the SOFUN Fortran PHYDRO-model
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-quantitative-validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ test_that("p-model quantitative check versus observations (FR-Pue)", {
whc = 2000 # site info, water holding capacity in mm: TODO: does this make sense with soilm_thetastar in mm units?
)

#df_drivers <- p_model_drivers # TODO: NOT YET UPDATED FOR PHYDRO (a newformat, b add phydro_ parameters)
df_drivers <- readRDS(file = here::here("data/p_model_drivers_newformat.rds"))

df_drivers <- rsofun::p_model_drivers_format2024_08 # TODO: NOT YET UPDATED FOR PHYDRO (still add default phydro_* parameters)

# run the model for these parameters
res <- rsofun::runread_pmodel_f(
drivers = df_drivers,
Expand Down
18 changes: 10 additions & 8 deletions vignettes/pmodel_use_newdata.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,20 @@ The package includes two demo datasets to run and validate pmodel output using G
# lubridate::year(date) <= 2012) |>
# list()
# ))
#
# saveRDS(p_model_drivers, file = here::here("data/p_model_drivers_newformat.rds"))
#
# saveRDS(p_model_validation, file = here::here("data/p_model_validation_newformat.rds"))
#
# Save to subfolder data/ and export it to make it available for package users:
# p_model_drivers_format2024_08 <- p_model_drivers
# usethis::use_data(p_model_drivers_format2024_08) # TODO: NOT YET UPDATED FOR PHYDRO (still add default phydro_* parameters)
# p_model_validation_format2024_08 <- p_model_validation
# usethis::use_data(p_model_validation_format2024_08)
```

### Read drivers

```{r}
p_model_drivers = readRDS(file = here::here("data/p_model_drivers_newformat.rds"))
p_model_drivers <- rsofun::p_model_drivers_format2024_08 # TODO: NOT YET UPDATED FOR PHYDRO (still add default phydro_* parameters)
p_model_validation = readRDS(file = here::here("data/p_model_validation_newformat.rds"))
p_model_validation <- rsofun::p_model_validation_format2024_08
```

### Visualize drivers
Expand Down Expand Up @@ -243,9 +245,9 @@ These are real data from the French FR-Pue fluxnet site. Information about data
Another two datasets are provided as an example to validate the model against leaf traits data, rather than fluxes. Measurements of Vcmax25 (aggregated over species) for a subset of 4 sites from the GlobResp database (Atkin et al., 2015) are given in `p_model_validation_vcmax25` and the corresponding forcing for the P-model is given in `p_model_drivers_vcmax25`. Since leaf traits are only measured once per site, the forcing used is a single year of average climate (the average measurements between 2001 and 2015 on each day of the year).

```{r}
p_model_drivers_vcmax25
rsofun::p_model_drivers_vcmax25
p_model_validation_vcmax25
rsofun::p_model_validation_vcmax25
```

For the remainder of this vignette, we will use the GPP flux datasets. The workflow is exactly the same for leaf traits data.
Expand Down
11 changes: 6 additions & 5 deletions vignettes/sensitivity_analysis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ ll_pmodel <- function(
rsofun::cost_likelihood_pmodel( # reuse likelihood cost function
as.list(par_v), # must be a named list
obs = rsofun::p_model_validation,
drivers = readRDS(file = here::here("data/p_model_drivers_newformat.rds")), #TODO rsofun::p_model_drivers is NOT YET UPDATED FOR PHYDRO (a newformat, b add phydro_ parameters)
drivers = rsofun::p_model_drivers_format2024_08, #TODO rsofun::p_model_drivers is NOT YET UPDATED FOR PHYDRO (a newformat, b add phydro_ parameters)
targets = "gpp"
)
}
# Compute log-likelihood for a given set of parameters
ll_pmodel(
par_v = c(
Expand Down Expand Up @@ -269,7 +270,7 @@ settings_calib <- list(
# Calibrate kphio-related parameters and err_gpp
par_calib <- calib_sofun(
drivers = readRDS(file = here::here("data/p_model_drivers_newformat.rds")), #TODO rsofun::p_model_drivers is NOT YET UPDATED FOR PHYDRO (a newformat, b add phydro_ parameters)
drivers = rsofun::p_model_drivers_format2024_08, # TODO: NOT YET UPDATED FOR PHYDRO (still add default phydro_* parameters)
obs = p_model_validation,
settings = settings_calib,
par_fixed = list(
Expand Down Expand Up @@ -309,7 +310,7 @@ settings_calib <- list(
)
par_calib <- calib_sofun(
drivers = readRDS(file = here::here("data/p_model_drivers_newformat.rds")), #TODO rsofun::p_model_drivers is NOT YET UPDATED FOR PHYDRO (a newformat, b add phydro_ parameters)
drivers = rsofun::p_model_drivers_format2024_08, # TODO: NOT YET UPDATED FOR PHYDRO (still add default phydro_* parameters)
obs = p_model_validation,
settings = settings_calib,
par_fixed = list(
Expand Down Expand Up @@ -405,7 +406,7 @@ run_pmodel <- function(sample_par){
# and also adds the new observation error
out <- runread_pmodel_f(
drivers = readRDS(file = here::here("data/p_model_drivers_newformat.rds")), #TODO rsofun::p_model_drivers is NOT YET UPDATED FOR PHYDRO (a newformat, b add phydro_ parameters)
drivers = rsofun::p_model_drivers_format2024_08, # TODO: NOT YET UPDATED FOR PHYDRO (still add default phydro_* parameters)
par = list( # copied from par_fixed above
kphio = sample_par$kphio,
kphio_par_a = sample_par$kphio_par_a,
Expand Down Expand Up @@ -514,7 +515,7 @@ plot_gpp_error +
#
# Plot observed and predicted GPP, with a 95% confidence interval using err_gpp
plot_gpp_error <- ggplot(data = runread_pmodel_f(
drivers = readRDS(file = here::here("data/p_model_drivers_newformat.rds")), #TODO rsofun::p_model_drivers is NOT YET UPDATED FOR PHYDRO (a newformat, b add phydro_ parameters)
drivers = rsofun::p_model_drivers_format2024_08, # TODO: NOT YET UPDATED FOR PHYDRO (still add default phydro_* parameters)
par = list(
kphio = par_calib$par[1], # values from posterior
kphio_par_a = par_calib$par[2],
Expand Down

0 comments on commit 9c6bdf1

Please sign in to comment.