R/ac_drift_workflow.R

Defines functions drift_step drift_workflow

Documented in drift_step drift_workflow

#' @title Drift-adaptation workflow
#' @description Ancestor of the drift-adaptation workflows: the family of predictive models that react
#'  to concept drift. Each concrete model is an *experiment line* — a workflow of activities
#'  (`detect -> manage -> train -> threshold -> record`) with its own backbone. This base holds the
#'  shared process state (`drift_method`, `warmup_size`, `verbose`, the sticky `drifted` flag) and the
#'  shared drift step ([drift_step()]); it does not own a concrete backbone. The two concrete lines are:
#'  \describe{
#'    \item{[adp_retrain()]}{a single model, retrained when a drift is reported (formerly `stealthy`).}
#'    \item{[adp_recurrent()]}{a repository of concepts that are stored and reused across recurring
#'      regimes (formerly `remind`).}
#'  }
#' @param drift_method The drift detector.
#' @param warmup_size Minimum number of rows before the detector runs.
#' @param verbose If `TRUE`, prints drift messages.
#' @return A `drift_workflow` object (the shared base; use [adp_retrain()] or [adp_recurrent()]).
#' @export
drift_workflow <- function(drift_method, warmup_size = 100, verbose = FALSE) {
  obj <- list()
  obj$drift_method <- drift_method
  obj$warmup_size <- warmup_size
  obj$verbose <- verbose
  obj$drifted <- FALSE
  class(obj) <- "drift_workflow"
  obj
}

#' @title Run the drift detector on a batch
#' @description The shared drift step of the drift-adaptation workflows: builds the signal the detector
#'  monitors with [drifter_input()], feeds the detector, reads whether it drifted and resets it on a
#'  drift. It keeps the model wrappers from duplicating the detect-and-reset logic.
#' @param drift_method The drift detector.
#' @param ctx The context passed to [drifter_input()]: `features` (the projected batch) and, for
#'  error-based detectors, `model`, `y` and `th` (and the wrapper's `target_uni`).
#' @return A list with the updated `drift_method` (reset when it drifted), the logical `drifted`, the
#'  `rows` the monitored signal covers and the `drifter_output` produced on this batch (captured
#'  before the reset, for diagnostics).
#' @export
drift_step <- function(drift_method, ctx) {
  inp <- drifter_input(drift_method, ctx)
  drift_method <- fit(drift_method, inp$value)
  drifted <- isTRUE(drift_method$drifted)
  output <- drift_method$drifter_output
  if (drifted) drift_method <- reset_state(drift_method)
  list(drift_method = drift_method, drifted = drifted, rows = inp$rows, drifter_output = output)
}

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.