tests/testthat/test-review-fixes.R

## Regression tests for the defects found in the external review of the
## accompanying article (0.2.0). Each test fails on the pre-0.2.0 code.

test_that("the data loss is a single term by default (the 4.2 cm split is gone)", {
  data(swrc_example)
  d  <- prepare_swrc_data(swrc_example[1:400, ], depth_col = "depth")
  x  <- c("clay", "silt", "soc")
  sc <- fit_minmax(d, x)
  m  <- suppressMessages(make_obs_matrices(d, x, sc, K = 8L))
  ## pre-0.2.0: every observation got w_dry because h > 4.2 cm always
  expect_true(all(m$w == 1))
  ## an explicit split in centimetres still separates the two ends
  m2 <- suppressMessages(make_obs_matrices(d, x, sc, K = 8L, wet_split_cm = 10^4.2,
                                           w_wet = 1, w_dry = 5))
  h  <- as.numeric(d$matric_head)[attr(m2, "rows_kept")]
  expect_equal(m2$w, ifelse(h <= 10^4.2, 1, 5))
})

test_that("collocation covariates are real soils: texture sums to 100", {
  set.seed(1)
  df <- data.frame(sand = runif(50, 0, 60))
  df$clay <- runif(50, 0, 100 - df$sand)
  df$silt <- 100 - df$sand - df$clay
  rs <- build_residual_sets(df, c("sand", "silt", "clay"),
                            S1 = 200L, S2 = 50L, S3 = 50L, S4 = 50L)
  for (s in rs)
    expect_equal(rowSums(s[, c("sand", "silt", "clay")]), rep(100, nrow(s)),
                 tolerance = 1e-8)
  ## the pre-0.2.0 behaviour remains available, and breaks closure
  ru <- build_residual_sets(df, c("sand", "silt", "clay"),
                            S1 = 200L, S2 = 0L, S3 = 0L, S4 = 0L,
                            covariates = "uniform")
  expect_gt(max(abs(rowSums(ru$set1[, c("sand", "silt", "clay")]) - 100)), 1)
})

test_that("S1 sees a change of slope at a knot (TensorFlow)", {
  skip_if_not(reticulate::py_module_available("tensorflow"))
  tf <- tensorflow::tf
  K  <- 16L
  ## a toy theta_model: piecewise-linear in pF with ONE kink inside the dry
  ## range, linear everywhere else. The autodiff second derivative of this
  ## is zero almost everywhere; the discrete operator must not be.
  kink <- (6.4 - (-2)) / 9.6
  toy <- function(inputs, training = TRUE) {
    pf <- inputs[[2]]
    th <- 0.4 - 0.05 * pf * 9.6 - 0.10 * tf$nn$relu(pf - kink) * 9.6
    tf$concat(list(th, th), axis = 1L)
  }
  X  <- array(runif(10 * K * 2), c(10L, K, 2L))
  rt <- list(set1 = list(Xseq = tf$constant(X, "float32"),
                         pf = tf$constant(matrix(runif(10), ncol = 1), "float32")),
             set2 = list(Xseq = tf$zeros(c(0L, K, 2L)), pf = tf$zeros(c(0L, 1L))),
             set3 = list(Xseq = tf$zeros(c(0L, K, 2L)), pf = tf$zeros(c(0L, 1L))),
             set4 = list(Xseq = tf$zeros(c(0L, K, 2L)), pf = tf$zeros(c(0L, 1L))))
  L <- compute_physics_loss(toy, rt)
  expect_gt(as.numeric(L$L_lin), 1e-3)

  ## and a curve that IS linear across the dry range scores zero
  lin <- function(inputs, training = TRUE) {
    th <- 0.4 - 0.05 * inputs[[2]] * 9.6
    tf$concat(list(th, th), axis = 1L)
  }
  expect_lt(as.numeric(compute_physics_loss(lin, rt)$L_lin), 1e-5)
})

Try the soilFlux package in your browser

Any scripts or data that you put into this service are public.

soilFlux documentation built on Dec. 4, 2026, 5:10 p.m.