-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement transformer composition (#335)
Fixes #287
- Loading branch information
Showing
13 changed files
with
175 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#' Compose two or more transformations together | ||
#' | ||
#' This transformer provides a general mechanism for composing two or more | ||
#' transformers together. The most important use case is to combine reverse | ||
#' with other transformations. | ||
#' | ||
#' @param ... One or more transformers, either specified with string or | ||
#' as individual transformer objects. | ||
#' @export | ||
#' @examples | ||
#' demo_continuous(10^c(-2:4), trans = "log10", labels = label_log()) | ||
#' demo_continuous(10^c(-2:4), trans = c("log10", "reverse"), labels = label_log()) | ||
compose_trans <- function(...) { | ||
trans_list <- lapply(list2(...), as.trans) | ||
if (length(trans_list) == 0) { | ||
abort("Must include at least 1 transformer to compose") | ||
} | ||
|
||
# Resolve domains | ||
suppressWarnings( | ||
domain <- compose_fwd(trans_list[[1]]$domain, trans_list[-1]) | ||
) | ||
if (any(is.na(domain))) { | ||
abort("Sequence of transformations yields invalid domain") | ||
} | ||
domain <- range(domain) | ||
|
||
names <- vapply(trans_list, "[[", "name", FUN.VALUE = character(1)) | ||
|
||
trans_new( | ||
paste0("composition(", paste0(names, collapse = ","), ")"), | ||
transform = function(x) compose_fwd(x, trans_list), | ||
inverse = function(x) compose_rev(x, trans_list), | ||
breaks = function(x) trans_list[[1]]$breaks(x), | ||
domain = domain | ||
) | ||
} | ||
|
||
compose_fwd <- function(x, trans_list) { | ||
for (trans in trans_list) { | ||
x <- trans$transform(x) | ||
} | ||
x | ||
} | ||
|
||
compose_rev <- function(x, trans_list) { | ||
for (trans in rev(trans_list)) { | ||
x <- trans$inverse(x) | ||
} | ||
x | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# produces informative errors | ||
|
||
Code | ||
compose_trans() | ||
Condition | ||
Error in `compose_trans()`: | ||
! Must include at least 1 transformer to compose | ||
Code | ||
compose_trans("reverse", "log10") | ||
Condition | ||
Error in `compose_trans()`: | ||
! Sequence of transformations yields invalid domain | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# as.trans generates informative error | ||
|
||
Code | ||
as.trans(1) | ||
Condition | ||
Error in `as.trans()`: | ||
! `1` must be a character vector or a transformer object | ||
Code | ||
as.trans("x") | ||
Condition | ||
Error in `get()`: | ||
! object 'x_trans' of mode 'function' was not found | ||
|
||
# trans has useful print method | ||
|
||
Code | ||
trans_new("test", transform = identity, inverse = identity) | ||
Output | ||
Transformer: test [-Inf, Inf] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
test_that("composes transforms correctly", { | ||
t <- compose_trans("log10", "reverse") | ||
expect_equal(t$transform(100), -2) | ||
expect_equal(t$inverse(-2), 100) | ||
}) | ||
|
||
test_that("uses breaks from first transformer", { | ||
t <- compose_trans("log10", "reverse") | ||
expect_equal(t$breaks(c(1, 1000)), log_breaks()(c(1, 1000))) | ||
}) | ||
|
||
test_that("produces informative errors", { | ||
expect_snapshot(error = TRUE, { | ||
compose_trans() | ||
compose_trans("reverse", "log10") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters