Skip to content

Commit

Permalink
Catch errors from CVXR calibration with bad data
Browse files Browse the repository at this point in the history
* DS-3458 [revdep skip]
  • Loading branch information
mwmclean committed Aug 12, 2021
1 parent 4c92b84 commit 246671a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: flipData
Type: Package
Title: Functions for extracting and describing data
Version: 1.5.0
Version: 1.5.1
Author: Displayr <opensource@displayr.com>
Maintainer: Displayr <opensource@displayr.com>
Description: Functions for extracting data from formulas and
Expand Down
15 changes: 15 additions & 0 deletions R/calibrate.R
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ computeCalibrate <- function(adjustment.variables, margins, input.weight, raking
Phi_R = Minimize(sum(input.weight * (-entr(g) - g + 1)))
p = Problem(Phi_R, constraints)
res = solve(p)
checkSolverStatus(res)
as.numeric(input.weight * res$getValue(g))
}
)
Expand Down Expand Up @@ -375,3 +376,17 @@ print.Calibrate <- function (x, ...)
"Largest weight is ", FormatAsReal(rng[2], decimals = 3), " (", FormatAsReal(rng[2] / rng[1], decimals = 3), " times the smallest weight)",
instruction.for.getting.variable))
}

#' Check for errors from running CVXR::solve
#' @noRd
checkSolverStatus <- function(solve.output)
{
if (is.list(solve.output) && is.character(solve.output[["status"]]))
{
status <- solve.output[["status"]]
if (status == "solver_error" || status == "infeasible")
stop("Calibration could not be performed for the given input data. ",
"Please check that the supplied targets are appropriate for your data.")
}
return(invisible())
}
10 changes: 10 additions & 0 deletions tests/testthat/test-calibrate.R
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,13 @@ test_that("Output contains the right class for extension buttons", {

expect_true(inherits(wgt, "Calibrate"))
})

test_that("DS-3458: Catch CVXR solver errors from bad input data",
{
numeric.vars <- list(Coke = c(10, 5, 0),
Pepsi = c(0, 5, 2))
targets <- c(.5, .5)
expect_error(Calibrate(numeric.variables = numeric.vars,
numeric.targets = targets),
"check that the supplied targets are appropriate for your data.")
})

0 comments on commit 246671a

Please sign in to comment.