Skip to content

add callback function to show progress in JASP#88

Open
vandenman wants to merge 1 commit intoBayesian-Graphical-Modelling-Lab:mainfrom
vandenman:feature/progress_bar_callback
Open

add callback function to show progress in JASP#88
vandenman wants to merge 1 commit intoBayesian-Graphical-Modelling-Lab:mainfrom
vandenman:feature/progress_bar_callback

Conversation

@vandenman
Copy link
Copy Markdown
Collaborator

Fixes https://github.com/Bayesian-Graphical-Modelling-Lab/issues/issues/69

Adds a callback function to forward progress to JASP.

IMPORTANT! This PR is incompatible with #87. It doesn't matter which is merged first, but afterward the remaining PR must be updated again.

To try it out, run the following code. It runs bgm on the Wenchuan data with the following progress bars:

  1. the default progress bar
  2. utils::txtProgressBar
  3. cli::cli_progress_bar
library(bgms)
data("Wenchuan")

iter <- 1000
warmup <- 500
chains <- 2
# shows default progress bar
fit <- bgm(Wenchuan[, 1:4], iter = iter, warmup = warmup, chains = chains, seed = 123)

make_callback <- function() {
  pb <<- NULL
  callback <- function(done, total_iter) {
    if (is.null(pb)) {
      pb <<- utils::txtProgressBar(min = 0, max = total_iter, style = 3)
    }
    utils::setTxtProgressBar(pb, done)
  }
  return(callback)
}

cc <- make_callback()
fit <- bgm(Wenchuan[, 1:4], iter = iter, warmup = warmup, chains = chains, seed = 123,
           progress_callback = cc)

make_callback <- function() {
  id <<- NULL
  env <- new.env(parent = environment())
  callback <- function(done, total_iter) {
    if (is.null(id)) {
      id <<- cli::cli_progress_bar("Progress", total = total_iter, format = "Custom cli {cli::pb_bar} {cli::pb_percent}", .envir = env, .auto_close  = FALSE)
    }
    cli::cli_progress_update(id = id, set = done, .envir = env, force = TRUE)
  }
  return(callback)
}


cc <- make_callback()
fit <- bgm(Wenchuan[, 1:4], iter = iter, warmup = warmup, chains = chains, seed = 123,
           progress_callback = cc)

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Performance optimization
  • Statistical / methodological update

Documentation and Release Notes

  • Updated function documentation for any signature or behaviour changes
  • Regenerated man/*.Rd files if roxygen comments changed
  • Updated _pkgdown.yml if adding a new exported function
  • Added or updated NEWS.md entry if the change affects users

Testing and Validation

Describe the checks you ran and the main results.

  • Added or updated tests in tests/testthat/ when needed
  • Ran the project styler
source("inst/styler/bgms_style.R")
styler::style_pkg(style = bgms_style)
  • Checked test files for styled result = captures that must be reverted to result <-
  • Ran lintr::lint_package()
  • Ran roxygen2::roxygenise() if roxygen comments changed
  • Ran rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran")) for non-trivial changes
  • Verified numerical behaviour where relevant

Copy link
Copy Markdown
Collaborator

@MaartenMarsman MaartenMarsman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R-CMD-check fails on all platforms with one test failure:

test-validate-sampler.R:398 — "return list has all 11 expected elements" now fails because validate_sampler() returns 12 elements (the new progress_callback). Fix:

test_that("return list has all 12 expected elements", {
  res = vs()
  expected_names = c(
    "update_method", "target_accept", "iter", "warmup",
    "hmc_num_leapfrogs", "nuts_max_depth", "learn_mass_matrix",
    "chains", "cores", "seed", "progress_type", "progress_callback"
  )
  expect_named(res, expected_names)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants