R/ac_manager.R

Defines functions check_concept.concept_pool_inactive add_concept.concept_pool_inactive concept_pool_inactive init_history.concept_pool update_concept.concept_pool check_concept.concept_pool add_concept.concept_pool init_history.default init_history update_concept.default update_concept check_concept add_concept concept_pool

Documented in add_concept check_concept concept_pool concept_pool_inactive init_history update_concept

#' @title Concept pool
#' @description Stores concepts in slots and, on drift, asks its selector for a stored concept that
#'  fits the current batch (`check_concept()`). It also decides when the selector records the
#'  current batch in the learned limits of the active concept (`history_order`) and how the
#'  limits of a new concept start (`history_init`). Both only apply to learned limits: with a fixed
#'  limit (e.g. `slct_psi(max_psi = 0.25)`) the selector keeps no history.
#' @param model_selector Selector that chooses the stored concept to reuse.
#' @param model_deleter Evictor that chooses the slot to replace when every slot is used.
#' @param n_slots Number of slots.
#' @param history_order `"before"` (default): the active concept records the batch before the drift
#'  decision. `"after"`: the decision uses the previous batches only; the batch is then recorded in
#'  the concept that stays active (if it already has a model). Requires `history_init = "blocks"`
#'  (the learned limits must start with values from the training window).
#' @param history_init `"none"` (default): the history of a new concept starts empty. `"blocks"`: it
#'  starts with the PSI of each batch-sized block of the concept's training window against the
#'  rest of the window. Applies to the PSI limits of the selector and of the REMIND check, and
#'  requires a REMIND `train_size` of at least two batches. The error tolerance of
#'  `slct_error_metrics()` (alone or inside `slct_psi_then_error()`) also starts by blocks only
#'  when the selector is created with `init_blocks = TRUE` (one model per block; see
#'  `init_history.slct_error_metrics`).
#' @return A `concept_pool` object.
#' @import daltoolbox
#' @export
concept_pool <- function(model_selector, model_deleter, n_slots = 1,
                    history_order = c("before", "after"), history_init = c("none", "blocks")) {
  obj <- dal_base()
  obj$model_selector <- model_selector
  obj$model_deleter <- model_deleter
  obj$n_slots <- n_slots
  obj$history_order <- match.arg(history_order)
  obj$history_init <- match.arg(history_init)
  # "after" only learns from accepted batches: it needs limits started by blocks.
  if (obj$history_order == "after" && obj$history_init == "none")
    stop("history_order = 'after' requires history_init = 'blocks'")
  obj$concept_storage <- lapply(seq_len(n_slots), function(i) list(qty = 0))
  class(obj) <- append("concept_pool", class(obj))
  obj
}

#' @title Store a concept in the pool
#' @description Generic called on drift: store the concept that was active into the pool (a no-op for
#'  [concept_pool_inactive()], which never reuses concepts).
#' @param obj A [concept_pool()] (or [concept_pool_inactive()]) object.
#' @param concept The [new_concept()] to store.
#' @return The updated pool.
#' @export
add_concept <- function(obj, concept) {
  UseMethod("add_concept")
}

#' @title Reuse a stored concept for the current batch
#' @description Generic called on drift: ask the pool's selector for a stored concept that fits the
#'  current batch, or none (so a new concept is created). [concept_pool_inactive()] never reuses.
#' @param obj A [concept_pool()] (or [concept_pool_inactive()]) object.
#' @param X Features of the current batch.
#' @param y Labels of the current batch.
#' @return `list(mm = <updated pool>, sc = <chosen concept or NULL>)`.
#' @export
check_concept <- function(obj, X, y) {
  UseMethod("check_concept")
}

#' @title Update concept
#' @description Keeps the selector's per-concept state up to date with the current batch. REMIND
#'  calls it for the active concept at every batch, before (`stage = "before"`) and after
#'  (`stage = "after"`) the drift decision. Selectors receive `phase = "observe"` (record the batch
#'  in the learned limits, when `record = TRUE`) or `phase = "adapt"` (adapt the active concept to
#'  the batch, e.g. the autoencoder of `slct_ae`).
#' @param obj A concept_pool or selector object.
#' @param concept The active concept.
#' @param X Current batch (features).
#' @param y Current batch (target).
#' @param ... `stage`, `record` and `trainer` (concept_pool); `phase`, `record` and
#'  `trainer` (selector).
#' @return The updated concept.
#' @export
update_concept <- function(obj, concept, X, y, ...) {
  UseMethod("update_concept")
}

#' @export
update_concept.default <- function(obj, concept, X, y, ...) {
  concept
}

#' @title Initial history
#' @description Starts the learned limits of a newly trained concept (see `history_init`).
#' @param obj A concept_pool or selector object.
#' @param concept The newly trained concept.
#' @param block_size Rows per block (the batch size).
#' @param ... `trainer`: function(x, y) that trains a stand-alone concept with the REMIND steps
#'  (given by REMIND; used only by `slct_error_metrics(init_blocks = TRUE)`).
#' @return The concept.
#' @export
init_history <- function(obj, concept, block_size, ...) {
  UseMethod("init_history")
}

#' @export
init_history.default <- function(obj, concept, block_size, ...) {
  concept
}


#' @export
add_concept.concept_pool <- function(obj, concept) {
  slot <- concept$slot
  if (slot == 0) {
    empty <- which(vapply(obj$concept_storage, function(cn) cn$qty == 0, logical(1)))
    slot <- if (length(empty) > 0) empty[1] else evict(obj$model_deleter, obj$concept_storage)
  }
  concept$slot <- 0
  obj$concept_storage[[slot]] <- concept
  obj
}

#' @export
check_concept.concept_pool <- function(obj, X, y) {
  valid <- which(vapply(obj$concept_storage, function(cn) cn$qty > 0, logical(1)))
  if (length(valid) == 0) return(list(mm = obj, sc = NULL))
  sel <- select_model(obj$model_selector, obj$concept_storage, valid, X, y)
  obj$concept_storage <- sel$cl
  if (sel$sc == 0) return(list(mm = obj, sc = NULL))
  
  obj$concept_storage[[sel$sc]]$slot <- sel$sc
  list(mm = obj, sc = obj$concept_storage[[sel$sc]])
}

#' @export
update_concept.concept_pool <- function(obj, concept, X, y, stage = "before", record = TRUE, ...) {
  before <- obj$history_order == "before"
  if (stage == "before")
    return(update_concept(obj$model_selector, concept, X, y, phase = "observe",
                          record = record && before, ...))
  if (!before && record && !is.null(concept$model))
    concept <- update_concept(obj$model_selector, concept, X, y, phase = "observe", record = TRUE, ...)
  update_concept(obj$model_selector, concept, X, y, phase = "adapt", ...)
}

#' @export
init_history.concept_pool <- function(obj, concept, block_size, ...) {
  if (obj$history_init != "blocks") return(concept)
  init_history(obj$model_selector, concept, block_size, ...)
}


#' @title Inactive concept pool
#' @description Never stores nor reuses concepts: every drift creates a new concept.
#' @return A `concept_pool_inactive` object.
#' @export
concept_pool_inactive <- function() {
  obj <- concept_pool(model_selector = NULL, model_deleter = NULL, n_slots = 1)
  class(obj) <- append("concept_pool_inactive", class(obj))
  obj
}

#' @export
add_concept.concept_pool_inactive <- function(obj, concept) {
  obj
}

#' @export
check_concept.concept_pool_inactive <- function(obj, X, y) {
  list(mm = obj, sc = NULL)
}

Try the heimdall package in your browser

Any scripts or data that you put into this service are public.

heimdall documentation built on Dec. 2, 2026, 5:08 p.m.