-
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.
feat: dynamic target in TransitionClassification (#53)
* feat: add dynamic target #52 * test: fix check_target * feat: add print format for Target * test: fix Target * feat: use Target in the inner working of Transition * feat: add print format for Generic * feat: check_target knows Target * test: update TransitionClassifiication * fix: Target did not accept NULL * docs: update man * docs: update news * test: fix check_target test * docs: export Target * fix: checkmate::makeExpectation is missing makeExpectation is imported to be used internally in our customised checkmate functions * docs: fix wrong alias of Target * test: fix Target tests * docs: fix Target example * docs: fix Target example in man * Increment version number * docs: update pkgdown
- Loading branch information
1 parent
e050db3
commit 87794dd
Showing
17 changed files
with
507 additions
and
51 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,7 +1,7 @@ | ||
Package: dymiumCore | ||
Type: Package | ||
Title: The core functions of a Dynamic Microsimulation framework for Integrated Urban Models | ||
Version: 0.1.2.9000 | ||
Version: 0.1.3 | ||
Authors@R: c( | ||
person("Amarin", "Siripanich", email = "[email protected]", role = c("aut", "cre")), | ||
person("Taha", "Rashidi", role = c("aut"))) | ||
|
@@ -81,6 +81,7 @@ Collate: | |
'Network.R' | ||
'Population.R' | ||
'Pipeline.R' | ||
'Target.R' | ||
'Transition.R' | ||
'TransitionClassification.R' | ||
'TransitionRegression.R' | ||
|
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
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
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
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,115 @@ | ||
#' @title Target | ||
#' | ||
#' @usage NULL | ||
#' @include Generic.R | ||
#' @format [R6::R6Class] object inheriting [Generic]. | ||
#' | ||
#' @description | ||
#' | ||
#' `Target` is to be used within `TransitionClassification` or supply to event | ||
#' functions. If the target is dynamic then its `get` will return its target | ||
#' value at the current time or its closest time to the current time. | ||
#' | ||
#' @section Construction: | ||
#' | ||
#' ``` | ||
#' Target$new(x) | ||
#' ``` | ||
#' | ||
#' * `x` :: any object that passes `check_target()`\cr | ||
#' A target object or `NULL`. | ||
#' | ||
#' @section Active Field (read-only): | ||
#' | ||
#' * `data`:: a target object\cr | ||
#' A target object. | ||
#' | ||
#' * `dynamic`:: `logical(1)`\cr | ||
#' A logical flag which indicates whether the target object is dynamic or not. | ||
#' | ||
#' @section Public Methods: | ||
#' | ||
#' * `get(time = .get_sim_time())`\cr | ||
#' (`integer(1)`) -> a named `list()`\cr | ||
#' Get a alignment target as a named list. | ||
#' | ||
#' @aliases Targets | ||
#' @export | ||
#' | ||
#' @examples | ||
#' | ||
#' # static target | ||
#' TrgtStatic <- Target$new(list(yes = 10)) | ||
#' TrgtStatic$data | ||
#' TrgtStatic$dynamic | ||
#' TrgtStatic$get() | ||
#' | ||
#' # dynamic target | ||
#' target_dynamic <- data.frame(time = 1:10, yes = 1:10) | ||
#' TrgtDynamic <- Target$new(list(yes = 10)) | ||
#' TrgtDynamic$data | ||
#' TrgtDynamic$dynamic | ||
#' | ||
#' # if the `time` argument in `get()` is not specified then it will rely on | ||
#' # the time step from the simulation clock from `.get_sim_time()`. | ||
#' TrgtDynamic$get() | ||
#' TrgtDynamic$get(1) | ||
#' TrgtDynamic$get(10) | ||
Target <- R6::R6Class( | ||
classname = "Target", | ||
inherit = dymiumCore::Generic, | ||
public = list( | ||
initialize = function(x) { | ||
assert_target(x, null.ok = TRUE) | ||
if (is.data.frame(x)) { | ||
if (!is.data.table(x)) { | ||
private$.data <- as.data.table(x) | ||
} else { | ||
private$.data <- data.table::copy(x) | ||
} | ||
if ("time" %in% names(x)) { | ||
private$.dynamic <- TRUE | ||
} | ||
} | ||
private$.data <- x | ||
return(invisible(self)) | ||
}, | ||
|
||
get = function(time = .get_sim_time()) { | ||
if (private$.dynamic) { | ||
closest_time_index <- which.min(abs(private$.data[['time']] - time)) | ||
return(as.list(private$.data[closest_time_index, -c("time")])) | ||
} | ||
if (is.data.table(private$.data)) { | ||
return(copy(private$.data)) | ||
} | ||
return(private$.data) | ||
}, | ||
|
||
print = function() { | ||
msg <- glue::glue("dynamic: {private$.dynamic}") | ||
if (private$.dynamic) { | ||
period <- c(min(private$.data[["time"]]), | ||
max(private$.data[["time"]])) | ||
msg <- glue::glue(msg, | ||
"period: {period[1]} to {period[2]}", .sep = "\n- ") | ||
} | ||
super$print(msg) | ||
} | ||
), | ||
|
||
active = list( | ||
data = function() { | ||
base::get(".data", envir = private) | ||
}, | ||
dynamic = function() { | ||
base::get(".dynamic", envir = private) | ||
} | ||
), | ||
|
||
private = list( | ||
.data = NULL, | ||
.dynamic = FALSE | ||
) | ||
|
||
) |
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
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
Oops, something went wrong.