|
| 1 | +# Mock species SDM wrapper function for testing |
| 2 | +wrapper_sdmtmb <- function(x, y, z, dir_spp, spp_info, n_knots) { |
| 3 | + # Return a list with mock components |
| 4 | + list( |
| 5 | + fit = list(), # Empty object, to be handled by sdmTMB::tidy and sanity |
| 6 | + index = data.frame(year = 1:3, index = c(1,2,3)) |
| 7 | + ) |
| 8 | +} |
| 9 | + |
| 10 | +# Mock sdmTMB::tidy and sdmTMB::sanity |
| 11 | +sdmTMB::tidy <- function(fit, conf.int = FALSE, effects = NULL) { |
| 12 | + # Return a simple tidy data frame |
| 13 | + data.frame(term = "intercept", estimate = 1.23) |
| 14 | +} |
| 15 | +sdmTMB::sanity <- function(fit) { |
| 16 | + # Return a simple sanity data frame |
| 17 | + data.frame(sane = TRUE) |
| 18 | +} |
| 19 | + |
| 20 | +# Setup test data |
| 21 | +result <- tibble::tibble( |
| 22 | + source = c("0.1_2", "0.1_4", "0.1_4", "0.2_2", "0.2_3", "0.2_4", "0.2_4", "0.3_2", "0.3_3"), |
| 23 | + trawlid = c(5, 1, 2, 5, 5, 1, 2, 5, 5), |
| 24 | + RandomAssignment = rep(1, 9), |
| 25 | + srvy = rep("CA", 9), |
| 26 | + common_name = rep("arrowtooth flounder", 9), |
| 27 | + total_catch_numbers = c(130, 100, 120, 130, 130, 100, 120, 130, 130), |
| 28 | + total_catch_wt_kg = c(40, 10, 20, 40, 40, 10, 20, 40, 40), |
| 29 | + latitude_dd = c(38, 35, 36, 38, 38, 35, 36, 38, 38), |
| 30 | + longitude_dd = c(-125.3, -124.8, -124.9, -125.3, -125.3, -124.8, -124.9, -125.3, -125.3), |
| 31 | + year = c(2001, 2001, 2002, 2001, 2001, 2001, 2002, 2001, 2001), |
| 32 | + pass = c(1, 1, 2, 1, 1, 1, 2, 1, 1), |
| 33 | + depth_m = c(150, 270, 130, 150, 150, 270, 130, 150, 150), |
| 34 | + area_swept_ha = c(1.6, 1.2, 1.5, 1.6, 1.6, 1.2, 1.5, 1.6, 1.6) |
| 35 | +) |
| 36 | +spp_dfs <- split(result, result$source) |
| 37 | + |
| 38 | + |
| 39 | +spp_info <- data.frame( |
| 40 | + srvy = "CA", |
| 41 | + common_name = "arrowtooth flounder", |
| 42 | + filter_lat_gt = 34, |
| 43 | + filter_lat_lt = NA, |
| 44 | + filter_depth = NA, |
| 45 | + model_fn = "total_catch_wt_kg ~ 0 + factor(year) + pass", |
| 46 | + model_family = "delta_gamma", |
| 47 | + model_anisotropy = TRUE, |
| 48 | + model_spatiotemporal = "iid, iid", |
| 49 | + stringsAsFactors = FALSE |
| 50 | + ) |
| 51 | + |
| 52 | +grid_yrs <- data.frame( |
| 53 | + longitude_dd = c(-124.81, -124.85, -125.32, -125.02, -124.55, -123.99, -125.76, -125.46, -124.36, -124.08), |
| 54 | + latitude_dd = c(46.85, 47.60, 48.25, 47.81, 46.42, 45.59, 48.08, 47.94, 43.75, 44.67), |
| 55 | + pass = rep(0, 10), |
| 56 | + depth_m = c(-159, -112, -97, -30, -191, -35, -655, -335, -110, -11), |
| 57 | + area_km2 = rep(0, 10), |
| 58 | + srvy = rep("CA", 10) |
| 59 | +) |
| 60 | + |
| 61 | +dir_out <- tempdir() |
| 62 | + |
| 63 | +test_that("resample_tests runs and creates output files", { |
| 64 | + # Remove any prior test directory |
| 65 | + test_dir <- paste0(dir_out, paste0(spp_info$srvy, "_Test_Species/")) |
| 66 | + if (dir.exists(test_dir)) unlink(test_dir, recursive = TRUE) |
| 67 | + |
| 68 | + expect_no_error({ |
| 69 | + resample_tests( |
| 70 | + spp_dfs = spp_dfs, |
| 71 | + spp_info = spp_info, |
| 72 | + grid_yrs = grid_yrs, |
| 73 | + dir_out = dir_out, |
| 74 | + test = TRUE, |
| 75 | + parallel = FALSE, |
| 76 | + n_knots = NULL, |
| 77 | + model_type = "wrapper_sdmtmb" |
| 78 | + ) |
| 79 | + }) |
| 80 | + |
| 81 | + # Check output files exist |
| 82 | + expect_true(file.exists(file.path(test_dir, "fit_df.csv"))) |
| 83 | + expect_true(file.exists(file.path(test_dir, "fit_pars.csv"))) |
| 84 | + expect_true(file.exists(file.path(test_dir, "fit_check.csv"))) |
| 85 | + expect_true(file.exists(file.path(test_dir, "index.csv"))) |
| 86 | + |
| 87 | + # Check that parquet files exist |
| 88 | + expect_true(file.exists(file.path(test_dir, "df_1.parquet"))) |
| 89 | + expect_true(file.exists(file.path(test_dir, "df_2.parquet"))) |
| 90 | + |
| 91 | + # Read and check CSV contents |
| 92 | + fit_df <- read.csv(file.path(test_dir, "fit_df.csv")) |
| 93 | + expect_true("estimate" %in% names(fit_df)) |
| 94 | + fit_index <- read.csv(file.path(test_dir, "index.csv")) |
| 95 | + expect_true(all(c("year", "index") %in% names(fit_index))) |
| 96 | +}) |
| 97 | + |
| 98 | +test_that("select_n_knots returns expected values", { |
| 99 | + expect_equal(select_n_knots(500), 50) |
| 100 | + expect_equal(select_n_knots(3000), 100) |
| 101 | + expect_equal(select_n_knots(9000), 200) |
| 102 | + expect_equal(select_n_knots(20000), 500) |
| 103 | + expect_equal(select_n_knots(100000), 1000) |
| 104 | +}) |
| 105 | + |
0 commit comments