Skip to content

Commit dde574f

Browse files
authored
feat: add get_instance method to Resampling (#1313)
* ResamplingGroupOrStrata * rm Remotes * rm ResamplingGroupOrStrata * add back space * private$.get_instance * section Inherting
1 parent 2c49b8e commit dde574f

2 files changed

Lines changed: 26 additions & 18 deletions

File tree

R/Resampling.R

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
#' Next, the grouping information is replaced with the respective row ids to generate training and test sets.
4747
#' The sets can be accessed via `$train_set(i)` and `$test_set(i)`, respectively.
4848
#'
49+
#' @section Inheriting:
50+
#' It is possible to overwrite both `private$.get_instance()` to have full control, or only `private$.sample()` when one wants to use the pre-defined mechanism for stratification and grouping.
4951
#'
5052
#' @template seealso_resampling
5153
#' @export
@@ -173,25 +175,8 @@ Resampling = R6Class("Resampling",
173175
#' the object in its previous state.
174176
instantiate = function(task) {
175177
task = assert_task(as_task(task))
176-
strata = task$strata
177-
groups = task$groups
178-
179-
if (is.null(strata)) {
180-
if (is.null(groups)) {
181-
instance = private$.sample(task$row_ids, task = task)
182-
} else {
183-
private$.groups = groups
184-
instance = private$.sample(unique(groups$group), task = task)
185-
}
186-
} else {
187-
if (!is.null(groups)) {
188-
stopf("Cannot combine stratification with grouping")
189-
}
190-
instance = private$.combine(lapply(strata$row_id, private$.sample, task = task))
191-
}
192-
193178
private$.hash = NULL
194-
self$instance = instance
179+
self$instance = private$.get_instance(task)
195180
self$task_hash = task$hash
196181
self$task_row_hash = task$row_hash
197182
self$task_nrow = task$nrow
@@ -261,6 +246,24 @@ Resampling = R6Class("Resampling",
261246
.hash = NULL,
262247
.groups = NULL,
263248

249+
.get_instance = function(task) {
250+
strata = task$strata
251+
groups = task$groups
252+
if (is.null(strata)) {
253+
if (is.null(groups)) {
254+
private$.sample(task$row_ids, task = task)
255+
} else {
256+
private$.groups = groups
257+
private$.sample(unique(groups$group), task = task)
258+
}
259+
} else {
260+
if (!is.null(groups)) {
261+
stopf("Cannot combine stratification with grouping")
262+
}
263+
private$.combine(lapply(strata$row_id, private$.sample, task = task))
264+
}
265+
},
266+
264267
.get_set = function(getter, i) {
265268
if (!self$is_instantiated) {
266269
stopf("Resampling '%s' has not been instantiated yet", self$id)

man/Resampling.Rd

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)