Skip to content

Commit

Permalink
Pretty model print. Fixes vignette.
Browse files Browse the repository at this point in the history
  • Loading branch information
vwmaus committed Sep 21, 2023
1 parent 6336a71 commit 9ef5f1a
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

S3method(plot,twdtw_knn1)
S3method(predict,twdtw_knn1)
S3method(print,twdtw_knn1)
export(shift_dates)
export(twdtw_knn1)
import(ggplot2)
Expand Down
74 changes: 74 additions & 0 deletions R/train.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,80 @@ twdtw_knn1 <- function(x, y, time_weight, cycle_length, time_scale,

}

#' Print method for objects of class twdtw_knn1
#'
#' This method provides a structured printout of the important components
#' of a `twdtw_knn1` object.
#'
#' @param x An object of class `twdtw_knn1`.
#' @param ... ignored
#'
#' @return Invisible `twdtw_knn1` object.
#'
#' @export
print.twdtw_knn1 <- function(x, ...) {
cat("\nModel of class 'twdtw_knn1'\n")
cat("-----------------------------\n")

# Printing the call
cat("Call:\n")
print(x$call)

# Printing the formula, if available
cat("\nFormula:\n")
print(x$formula)

# Printing the data summary
cat("\nData:\n")
print(x$data)

# Printing twdtw arguments
cat("\nTWDTW Arguments:\n")
pretty_arguments(x$twdtw_args)

invisible(x) # Returns the object invisibly, so it doesn't print twice
}

#' Print Pretty Arguments
#'
#' Display a list of arguments of a given function in a human-readable format.
#'
#' @param args A list of named arguments to display.
#'
#' @return Invisible NULL. The function is mainly used for its side effect of printing.
#'
#' @examples
#' \dontrun{
#' pretty_arguments(formals(twdtw_knn1))
#' }
#'
pretty_arguments <- function(args) {

if (is.null(args)) {
cat("Arguments are missing.\n")
return(invisible(NULL))
}

for (name in names(args)) {
default_value <- args[[name]]

if (is.symbol(default_value)) {
default_value <- as.character(default_value)

} else if (is.null(default_value)) {
default_value <- "NULL"

} else if (is.vector(default_value) && !is.null(names(default_value))) {
# Handle named vectors
values <- paste(names(default_value), default_value, sep = "=", collapse = ", ")
default_value <- paste0("c(", values, ")")
}
cat(paste0(" - ", name, ": ", default_value, "\n"))
}
}



#' Compute the Most Common Sampling Frequency across all observations
#'
#' This function calculates the most common difference between consecutive time points.
Expand Down
23 changes: 23 additions & 0 deletions man/pretty_arguments.Rd

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

20 changes: 20 additions & 0 deletions man/print.twdtw_knn1.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-twdtw_classify.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ system.time(
time_weight = c(steepness = 0.1, midpoint = 50),
formula = band ~ s(time))
)

m
# Visualize model patterns
plot(m)

Expand Down
5 changes: 4 additions & 1 deletion vignettes/landuse-mapping.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ twdtw_model <- twdtw_knn1(x = dc,
time_scale = 'day',
time_weight = c(steepness = 0.1, midpoint = 50),
formula = band ~ s(time))
print(twdtw_model)
```

In addition to the mandatory arguments `x` (satellite data-cube) and `y` (training samples),
Expand All @@ -102,9 +104,10 @@ Finally, we predict the land-use classes for each pixel location in the data-cub

```{r , echo = TRUE, eval = TRUE, warning = FALSE, message = FALSE}
lu_map <- predict(dc, model = twdtw_model)
print(lu_map)
```

And visualize the results using `ggplot`:
The 'time' dimension was reduced to a single map. We can now visualize it using `ggplot`:

```{r , echo = TRUE, eval = TRUE, warning = FALSE, message = FALSE}
ggplot() +
Expand Down

0 comments on commit 9ef5f1a

Please sign in to comment.