Skip to content

Commit

Permalink
Enforces twdtw arguments in model build. Adds stars vignette.
Browse files Browse the repository at this point in the history
  • Loading branch information
vwmaus committed Sep 21, 2023
1 parent ad5ed5b commit 49495ac
Show file tree
Hide file tree
Showing 44 changed files with 242 additions and 1,633 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ examples_x64
^LICENSE\.md$
^dtwSat\.Rproj$
^\.github$
^doc$
^Meta$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ CRAN-SUBMISSION
src/symbols.rds
*.o
*.so
/doc/
/Meta/
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ License: GPL (>= 3)
URL: https://github.com/vwmaus/dtwSat/
BugReports: https://github.com/vwmaus/dtwSat/issues/
Maintainer: Victor Maus <[email protected]>
VignetteBuilder:
knitr
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Expand All @@ -47,10 +49,9 @@ Imports:
mgcv,
stats,
tidyr,
rlang,
proxy
Suggests:
rbenchmark,
knitr,
testthat (>= 3.0.0)
Config/testthat/edition: 3
Collate:
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ importFrom(mgcv,gam)
importFrom(mgcv,predict.gam)
importFrom(mgcv,s)
importFrom(proxy,dist)
importFrom(rlang,.data)
importFrom(stats,as.formula)
importFrom(stats,predict)
importFrom(stats,setNames)
Expand Down
7 changes: 6 additions & 1 deletion R/predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param newdata A data frame or similar object containing the new observations
#' (time series data) to be predicted.
#' @param ... Additional arguments passed to the \link[twdtw]{twdtw} function.
#' If provided, they will overwrite twdtw arguments previously passed to \link[dtwSat]{twdtw_knn1}.
#'
#' @return A vector of predicted classes for the `newdata`.
#'
Expand All @@ -18,14 +19,18 @@
#' @export
predict.twdtw_knn1 <- function(object, newdata, ...){

# Update twdtw_args with new arguments passed via ...
new_twdtw_args <- list(...)
matching_twdtw_args <- intersect(names(new_twdtw_args), names(object$twdtw_args))
object$twdtw_args[matching_twdtw_args] <- new_twdtw_args[matching_twdtw_args]

# Convert newdata to time series
newdata_ts <- prepare_time_series(newdata)

# Compute TWDTW distances
distances <- sapply(object$data$observations, function(pattern){
sapply(newdata_ts$observations, function(ts) {
proxy::dist(x = as.data.frame(ts), y = as.data.frame(pattern), method = 'twdtw', ...)
do.call(proxy::dist, c(list(x = as.data.frame(ts), y = as.data.frame(pattern), method = 'twdtw'), object$twdtw_args))
})
})

Expand Down
42 changes: 32 additions & 10 deletions R/train.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
#'
#' @param x A two-dimensional stars object (x, y) with time and 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}.
#' @param cycle_length The length of the cycle, e.g. phenological cycles. Details in \link[twdtw]{twdtw}.
#' @param time_scale Specifies the time scale for the observations. Details in \link[twdtw]{twdtw}.
#' @param formula Either NULL or a formula to reduce samples of the same label using Generalized Additive Models (GAM).
#' Default is \code{band ~ s(time)}. See details.
#' @param start_column Name of the column in y that indicates the start date. Default is 'start_date'.
#' @param end_column Name of the column in y that indicates the end date. Default is 'end_date'.
#' @param label_colum Name of the column in y containing land use labels. Default is 'label'.
#' @param sampling_freq The time frequency for sampling, including the unit (e.g., '16 day').
#' If NULL, the function will infer the frequency. This parameter is only used if a formula is provided.
#' @param ... Additional arguments passed to the GAM function.
#' @param ... Additional arguments passed to the \link[mgcv]{gam} function and to \link[twdtw]{twdtw} function.
#'
#' @details If \code{formula} is NULL, the KNN-1 model will retain all training samples. If a formula is passed (e.g., \code{band ~ \link[mgcv]{s}(time)}),
#' then samples of the same label (land cover class) will be resampled using GAM.
Expand Down Expand Up @@ -49,24 +53,26 @@
#' # Create a knn1-twdtw model
#' m <- twdtw_knn1(x = dc,
#' y = samples,
#' cycle_length = 'year',
#' time_scale = 'day',
#' time_weight = c(steepness = 0.1, midpoint = 50),
#' formula = band ~ s(time))
#'
#' # Visualize model patterns
#' plot(m)
#'
#' # Classify satellite images
#' system.time(
#' lu <- predict(dc,
#' model = m,
#' drop_dimensions = TRUE,
#' cycle_length = 'year',
#' time_scale = 'day',
#' time_weight = c(steepness = 0.1, midpoint = 50))
#' )
#' system.time(lu <- predict(dc, model = m))
#'
#' # Visualise land use classification
#' ggplot() +
#' geom_stars(data = lu) +
#' theme_minimal()
#'
#' }
#' @export
twdtw_knn1 <- function(x, y, formula = NULL, start_column = 'start_date',
twdtw_knn1 <- function(x, y, time_weight, cycle_length, time_scale,
formula = NULL, start_column = 'start_date',
end_column = 'end_date', label_colum = 'label',
sampling_freq = NULL, ...){

Expand All @@ -80,6 +86,11 @@ twdtw_knn1 <- function(x, y, formula = NULL, start_column = 'start_date',
stop("y must be an sf object with point geometry")
}

# check for minimum set of twdtw arguments
if (!(is.function(time_weight) || (is.numeric(time_weight) && length(time_weight) == 2))) stop("'time_weight' should be either a function or a numeric vector with length two")
if (is.null(cycle_length)) stop("The 'cycle_length' argument is missing.")
if (is.null(time_scale)) stop("The 'time_scale' argument is missing for 'cycle_length' type character.")

# Check for required columns in y
required_columns <- c(start_column, end_column, label_colum)
missing_columns <- setdiff(required_columns, names(y))
Expand Down Expand Up @@ -140,6 +151,17 @@ twdtw_knn1 <- function(x, y, formula = NULL, start_column = 'start_date',
model$call <- match.call()
model$formula <- formula
model$data <- ts_data
# add twdtw arguments to model
model$twdtw_args <- list(time_weight = time_weight,
cycle_length = cycle_length,
time_scale = time_scale,
origin = NULL,
max_elapsed = Inf,
version = "f90")
new_twdtw_args <- list(...)
matching_twdtw_args <- intersect(names(model$twdtw_args), names(new_twdtw_args))
model$twdtw_args[matching_twdtw_args] <- new_twdtw_args[matching_twdtw_args]

class(model) <- "twdtw_knn1"

return(model)
Expand Down
1 change: 0 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#' @import ggplot2
#' @importFrom stats as.formula predict setNames
#' @importFrom mgcv gam s predict.gam
#' @importFrom rlang .data
#' @importFrom tidyr pivot_longer pivot_wider nest unnest
#' @importFrom proxy dist
#'
Expand Down
41 changes: 0 additions & 41 deletions inst/REFERENCES.bib
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,6 @@ @article{Maus:2019
month={Jan}
}

@article{Olofsson:2014,
title = {Good Practices for Estimating Area and Assessing Accuracy of Land Change},
journal = {Remote Sensing of Environment},
volume = {148},
pages = {42--57},
year = {2014},
issn = {0034--4257},
doi = {10.1016/j.rse.2014.02.015},
author = {Pontus Olofsson and Giles M. Foody and Martin Herold and Stephen V. Stehman and Curtis E. Woodcock and Michael A. Wulder}
}

@article{Olofsson:2013,
author = {Pontus Olofsson and Giles M. Foody and Stephen V. Stehman and Curtis E. Woodcock},
title = {Making Better Use of Accuracy Aata in Land Change Studies: Estimating Accuracy and Area and Quantifying Uncertainty Using Stratified Estimation},
journal = {Remote Sensing of Environment},
volume = {129},
pages = {122--131},
year = {2013},
doi = {10.1016/j.rse.2012.10.031}
}

@article{Giorgino:2009,
author = {Toni Giorgino},
title = {Computing and Visualizing Dynamic Time Warping Alignments in {R}: The dtw Package},
journal = {Journal of Statistical Software},
year = {2009},
volume = {31},
pages = {1--24},
number = {7},
doi = {10.18637/jss.v031.i07}
}

@book{Muller:2007,
title = {Information Retrieval for Music and Motion},
publisher = {Springer-Verlag},
year = {2007},
author = {Meinard Muller},
pages = {64-84},
address = {London}
}

@misc{Didan:2015,
author = {Kamel Didan},
title = {{MOD13Q1 MODIS/Terra Vegetation Indices 16-Day L3 Global
Expand Down
Binary file removed inst/lucc_MT/assessment.RData
Binary file not shown.
Binary file removed inst/lucc_MT/cross_validation.RData
Binary file not shown.
Binary file removed inst/lucc_MT/data/blue.tif
Binary file not shown.
Binary file removed inst/lucc_MT/data/doy.tif
Binary file not shown.
Binary file removed inst/lucc_MT/data/evi.tif
Binary file not shown.
Binary file removed inst/lucc_MT/data/mir.tif
Binary file not shown.
Binary file removed inst/lucc_MT/data/ndvi.tif
Binary file not shown.
Binary file removed inst/lucc_MT/data/nir.tif
Binary file not shown.
Binary file removed inst/lucc_MT/data/red.tif
Binary file not shown.
Loading

0 comments on commit 49495ac

Please sign in to comment.