Skip to content

Commit 810b5b5

Browse files
Merge pull request #1526 from seb09/error-step-isomap-neighbor
2 parents 0543586 + 3619916 commit 810b5b5

File tree

5 files changed

+95
-19
lines changed

5 files changed

+95
-19
lines changed

R/isomap.R

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#' If `num_terms` is greater than the number of columns or the number of
1111
#' possible dimensions, a smaller value will be used.
1212
#' @param neighbors The number of neighbors.
13-
#' @param options A list of options to `dimRed::Isomap()``.
14-
#' @param res The `dimRed::Isomap()`` object is stored here once this
13+
#' @param options A list of options to `dimRed::embed()``.
14+
#' @param res The `dimRed::embed()`` object is stored here once this
1515
#' preprocessing step has be trained by [prep()].
1616
#' @template step-return
1717
#' @family multivariate transformation steps
@@ -161,6 +161,14 @@ step_isomap_new <-
161161
)
162162
}
163163

164+
embed_mod <- function(..., msg = FALSE) {
165+
if (msg) {
166+
utils::getFromNamespace("embed", ns = "dimRed")(...)
167+
} else {
168+
suppressMessages(utils::getFromNamespace("embed", ns = "dimRed")(...))
169+
}
170+
}
171+
164172
#' @export
165173
prep.step_isomap <- function(x, training, info = NULL, ...) {
166174
col_names <- recipes_eval_select(x$terms, training, info)
@@ -175,23 +183,15 @@ prep.step_isomap <- function(x, training, info = NULL, ...) {
175183

176184
iso_map <- try_fetch_eval_tidy(
177185
eval_dimred_call(
178-
"embed",
186+
"embed_mod",
179187
.data = dimred_data(training[, col_names, drop = FALSE]),
180188
.method = "Isomap",
181189
knn = x$neighbors,
182190
ndim = x$num_terms,
183-
.mute = x$options$.mute
191+
.mute = setdiff(x$options$.mute, "message"),
192+
msg = !"message" %in% x$options$.mute
184193
)
185194
)
186-
187-
if (inherits(iso_map, "try-error")) {
188-
cli::cli_abort(
189-
c(
190-
x = "Failed with error:",
191-
i = as.character(iso_map)
192-
)
193-
)
194-
}
195195
} else {
196196
iso_map <- NULL
197197
}

R/misc.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,12 @@ changelog <- function(show, before, after, x) {
828828
# ------------------------------------------------------------------------------
829829

830830
eval_dimred_call <- function(fn, ...) {
831-
cl <- rlang::call2(fn, .ns = "dimRed", ...)
831+
.ns <- if (exists(fn, where = asNamespace("dimRed"), inherits = FALSE)) {
832+
"dimRed"
833+
} else {
834+
NULL
835+
}
836+
cl <- rlang::call2(fn, .ns = .ns, ...)
832837
rlang::eval_tidy(cl)
833838
}
834839

man/step_isomap.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/isomap.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,49 @@
2323
Sepal.Width, deg_free = 1, degree = 1), Sepal.Length, deg_free = 1, degree = 1),
2424
Species, threshold = 1e-09), all_numeric_predictors(), num_terms = 1,
2525
neighbors = 1))
26-
Message
2726
Condition
2827
Error in `step_isomap()`:
2928
Caused by error in `prep()`:
3029
! Failed to compute:
3130
Caused by error:
3231
! TridiagEigen: eigen decomposition failed
3332

33+
# ISOmap suppresses only messages, not errors
34+
35+
Code
36+
prep(step_isomap(recipe(mpg ~ ., data = mtcars), all_numeric_predictors(),
37+
neighbors = 31, options = list(.mute = character(0))))
38+
Message
39+
40+
-- Recipe ----------------------------------------------------------------------
41+
42+
-- Inputs
43+
Number of variables by role
44+
outcome: 1
45+
predictor: 10
46+
47+
-- Training information
48+
Training data contained 32 data points and no incomplete rows.
49+
50+
-- Operations
51+
* Isomap approximation with: cyl, disp, hp, drat, wt, qsec, ... | Trained
52+
53+
---
54+
55+
Code
56+
prep(step_isomap(recipe(mpg ~ ., data = mtcars), all_numeric_predictors(),
57+
neighbors = 32, options = list(.mute = c("message"))))
58+
Condition
59+
Error in `step_isomap()`:
60+
Caused by error in `prep()`:
61+
! Failed to compute:
62+
Caused by error in `RANN::nn2()`:
63+
! Cannot find more nearest neighbours than there are points
64+
3465
# check_name() is used
3566

3667
Code
3768
prep(rec, training = dat)
38-
Message
3969
Condition
4070
Error in `step_isomap()`:
4171
Caused by error in `bake()`:
@@ -98,7 +128,6 @@
98128

99129
Code
100130
rec <- prep(rec)
101-
Message
102131
Condition
103132
Warning:
104133
`keep_original_cols` was added to `step_isomap()` after this recipe was created.

tests/testthat/test-isomap.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,48 @@ test_that("ISOmap fails gracefully", {
116116
)
117117
})
118118

119+
test_that("ISOmap suppresses only messages, not errors", {
120+
skip_if_not_installed("RSpectra")
121+
skip_if_not_installed("igraph")
122+
skip_if_not_installed("RANN")
123+
skip_if_not_installed("dimRed")
124+
skip_if(getRversion() <= "3.4.4")
125+
126+
expect_snapshot(
127+
transform = scrub_timestamp,
128+
recipe(mpg ~ ., data = mtcars) |>
129+
step_isomap(
130+
all_numeric_predictors(),
131+
neighbors = 31,
132+
options = list(.mute = character(0))
133+
) |>
134+
prep()
135+
)
136+
137+
expect_no_message(
138+
recipe(mpg ~ ., data = mtcars) |>
139+
step_isomap(
140+
all_numeric_predictors(),
141+
neighbors = 31,
142+
options = list(.mute = c("message"))
143+
) |>
144+
prep()
145+
)
146+
147+
expect_snapshot(
148+
error = TRUE,
149+
transform = scrub_timestamp,
150+
recipe(mpg ~ ., data = mtcars) |>
151+
step_isomap(
152+
all_numeric_predictors(),
153+
# number of neighbors has to be < nrow
154+
neighbors = 32,
155+
options = list(.mute = c("message"))
156+
) |>
157+
prep()
158+
)
159+
})
160+
119161
test_that("check_name() is used", {
120162
skip_on_cran()
121163
skip_if_not_installed("RSpectra")

0 commit comments

Comments
 (0)