Skip to content

Commit

Permalink
determine compose_trans domains by pushing domain forward/back throug…
Browse files Browse the repository at this point in the history
…h transforms
  • Loading branch information
mjskay committed Nov 6, 2023
1 parent b885cf1 commit cc3c61c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
20 changes: 16 additions & 4 deletions R/trans-compose.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,22 @@ compose_trans <- function(...) {
cli::cli_abort("{.fun compose_trans} must include at least 1 transformer to compose")
}

# Resolve domains
suppressWarnings(
domain <- compose_fwd(trans_list[[1]]$domain, trans_list[-1])
)
# Resolve domains. First push the domain of the first transformation all the
# way forward through the sequence of transformations, intersecting it with
# all domains along the way, to get the range. Then push the range back
# through the inverses to get the domain.
range <- trans_list[[1]]$transform(trans_list[[1]]$domain)
for (trans in trans_list[-1]) {
lower <- max(min(trans$domain), min(range))
upper <- min(max(trans$domain), max(range))
if (isTRUE(lower <= upper)) {
range <- trans$transform(c(lower, upper))
} else {
range <- c(NA_real_, NA_real_)
break
}
}
domain <- compose_rev(range, trans_list)
if (any(is.na(domain))) {
cli::cli_abort("Sequence of transformations yields invalid domain")
}
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-trans-compose.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ test_that("produces informative errors", {
compose_trans("reverse", "log10")
})
})

test_that("produces correct domains", {
expect_equal(compose_trans("sqrt", "reverse")$domain, c(0, Inf))
expect_equal(compose_trans("sqrt", "log")$domain, c(0, Inf))
expect_equal(compose_trans("log", "log")$domain, c(1, Inf))
expect_equal(compose_trans("reverse", "log")$domain, c(-Inf, 0))
expect_equal(compose_trans("reverse", "logit", "log")$domain, c(-1, -0.5))
expect_error(compose_trans("sqrt", "reverse", "log")$domain, "invalid domain")
})

0 comments on commit cc3c61c

Please sign in to comment.