Skip to content

Commit

Permalink
fix problems on open_prediction_grid.R (function + test + vignette)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marques committed Dec 18, 2023
1 parent dfcbbfa commit 4c396b2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
16 changes: 5 additions & 11 deletions R/open_data_heatmodel.R → R/open_prediction_grid.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
#' Open prediction grid for a given period
#'
#' @param period A vector of "Date" objects
#' @param data_path a character to data folder
#' @returns a data.table of prediction points
#' (each row corresponds to a lat, lon, date)
open_pred_period <- function(period) {
open_pred_period <- function(period, data_path) {
if (class(period) != "Date") {
stop("period is not a Date")
}
period <- as.character(period)
for (d in period) {
file <- paste0(
"../input/prediction-grid/",
"prediction_grid_points_urb_rur_space_time_covariates_",
d,
".csv"
)
file <- paste0(data_path, "pred_grid_", d, ".tif")
if (!file.exists(file)) {
stop(paste0("date ", d, " is not in files"))
}
}
list_pred <- list()
for (d in period) {
pred_d <- fread(file)
list_pred <- append(list_pred, list(pred_d))
list_pred[[d]] <- terra::rast(file)
}
pred_p <- rbindlist(list_pred, fill = TRUE)
return(pred_p = pred_p)
return(list_pred = list_pred)
}
23 changes: 0 additions & 23 deletions tests/testthat/test-open_data_heatmodel.R

This file was deleted.

20 changes: 20 additions & 0 deletions tests/testthat/test-open_prediction_grid.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
test_that("Check open_pred_period works", {
data_path <- "../testdata/"

# -- 1st example: period is not a Date object
p <- c("2022-06-01", "2022-06-03")
expect_error(open_pred_period(period = p, data_path = data_path),
"period is not a Date")

# -- 2nd example: date is not in files
p <- c(as.Date("2022-08-01"), as.Date("2100-06-03"))
expect_error(open_pred_period(period = p, data_path = data_path),
"date 2100-06-03 is not in files")

# -- 3rd example: should work
p <- c(as.Date("2022-08-01"), as.Date("2022-08-02"))
expect_no_error(open_pred_period(period = p, data_path = data_path))
output <- open_pred_period(period = p, data_path = data_path)
expect_equal(class(output), "list")
expect_equal(names(output), as.character(p))
})
24 changes: 24 additions & 0 deletions vignettes/prediction_grid.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: "Prediction grid creation"
output: html_document
date: "2023-12-18"
---

# Create and store prediction grid

```{r}
getwd()
cfl <- list_covar_testdata(covar_folder = "../tests/testdata/")
create_pred_rds(borders_path = "../tests/testdata/rtp_counties.shp",
covar_files_list = cfl,
output_path = "../tests/testdata/")
```

# Open prediction grid

```{r}
data_path <- "../tests/testdata/"
period <- seq(as.Date("2022-08-01"), as.Date("2022-08-02"), by = "1 day")
grid <- open_pred_period(period = period, data_path = data_path)
```

0 comments on commit 4c396b2

Please sign in to comment.