Skip to content

Commit

Permalink
Use stars 3D array
Browse files Browse the repository at this point in the history
  • Loading branch information
vwmaus committed Sep 21, 2023
1 parent 020d53a commit 0880fc1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
13 changes: 7 additions & 6 deletions R/train.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' This function prepares a KNN-1 model with the Time Warp Dynamic Time Warping (TWDTW) algorithm.
#' If a formula is provided, the training samples are resampled using Generalized Additive Models (GAM).
#'
#' @param x A two-dimensional stars object (x, y) with time and bands as attributes.
#' @param x A three-dimensional stars object (x, y, time) with bands as attributes.
#' @param y An sf object with the coordinates of the training points.
#' @param time_weight A numeric vector with length two (steepness and midpoint of logistic weight) or a function.
#' See details in \link[twdtw]{twdtw}.
Expand Down Expand Up @@ -41,14 +41,13 @@
#' acquisition_date <- regmatches(tif_files, regexpr("[0-9]{8}", tif_files)) |>
#' as.Date(format = "%Y%m%d")
#'
#' # Create a 2D datacube
#' # Create a 3D datacube
#' dc <- read_stars(tif_files,
#' proxy = FALSE,
#' along = list(time = acquisition_date),
#' RasterIO = list(bands = 1:6)) |>
#' st_set_dimensions(3, c("EVI", "NDVI", "RED", "BLUE", "NIR", "MIR")) |>
#' split(c("band")) |>
#' split(c("time"))
#' split(c("band"))
#'
#' # Create a knn1-twdtw model
#' m <- twdtw_knn1(x = dc,
Expand Down Expand Up @@ -77,10 +76,12 @@ twdtw_knn1 <- function(x, y, time_weight, cycle_length, time_scale,
sampling_freq = NULL, ...){

# Check if x is a stars object with a time dimension
if (!inherits(x, "stars") || length(dim(x)) != 2) {
stop("x must be a stars object with two dimensions")
if (!inherits(x, "stars") || dim(x)['time'] < 1 || length(dim(x)) != 3) {
stop("x must be a three-dimensional stars object with a 'time' dimension")
}

x <- split(x, c("time"))

# Check if y is an sf object with point geometry
if (!inherits(y, "sf") || !all(st_is(y, "POINT"))) {
stop("y must be an sf object with point geometry")
Expand Down
5 changes: 2 additions & 3 deletions man/plot.twdtw_knn1.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions man/predict.twdtw_knn1.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions man/twdtw_knn1.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions tests/testthat/test-twdtw_classify.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ acquisition_date <- regmatches(tif_files, regexpr("[0-9]{8}", tif_files)) |>
as.Date(format = "%Y%m%d")

# Read the data as a stars object setting the time/date for each observation
# using along. This will prodcue a 4D array (data-cube) which will then be to
# a 2D array by spliting 'band' and 'time' dimensions
# using along. This will prodcue a 4D array (data-cube) which will then be converted
# to a 3D array by spliting the 'band' dimension
dc <- read_stars(tif_files,
proxy = FALSE,
along = list(time = acquisition_date),
RasterIO = list(bands = 1:6)) |>
st_set_dimensions(3, c("EVI", "NDVI", "RED", "BLUE", "NIR", "MIR")) |>
split(c("band")) |>
split(c("time"))
split(c("band"))

# Create a knn1-twdtw model
system.time(
Expand All @@ -44,15 +43,13 @@ ggplot() +
geom_stars(data = lu) +
theme_minimal()


### OTHER TESTS
# split time first
dc <- read_stars(tif_files,
proxy = FALSE,
along = list(time = acquisition_date),
RasterIO = list(bands = 1:6)) |>
st_set_dimensions(3, c("EVI", "NDVI", "RED", "BLUE", "NIR", "MIR")) |>
split(c("time")) |>
split(c("band"))

m <- twdtw_knn1(x = dc,
Expand Down

0 comments on commit 0880fc1

Please sign in to comment.