-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix problems on open_prediction_grid.R (function + test + vignette)
- Loading branch information
Marques
committed
Dec 18, 2023
1 parent
dfcbbfa
commit 4c396b2
Showing
4 changed files
with
49 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` | ||
|