diff --git a/R/persistence-class.R b/R/persistence-class.R index 3fed9f4..c96a184 100644 --- a/R/persistence-class.R +++ b/R/persistence-class.R @@ -257,6 +257,13 @@ as_persistence.matrix <- function(x, warn = TRUE, ...) { #' @export as_persistence.diagram <- function(x, warn = TRUE, ...) { info <- attributes(x) + # check column names + stopifnot( + info$dimnames[[2L]][1L] == "dimension", + all(c("Birth", "Death") %in% info$dimnames[[2L]][c(2L, 3L)]) + ) + bd_cols <- match(c("Birth", "Death"), info$dimnames[[2L]]) + filt_nm <- gsub("*Diag", "", rlang::call_name(info$call)) if (filt_nm == "rips") { filt_nm <- "Vietoris-Rips" @@ -281,7 +288,7 @@ as_persistence.diagram <- function(x, warn = TRUE, ...) { } dims <- dim(x) - x <- as.matrix(x)[1:dims[1], 1:dims[2]] + x <- as.matrix(x)[1:dim(x)[1], c(1L, bd_cols)] colnames(x) <- base::tolower(colnames(x)) as_persistence.matrix(x, warn = warn, rlang::splice(params)) } diff --git a/inst/tinytest/test-persistence-class.R b/inst/tinytest/test-persistence-class.R index 5c15f2c..e1cc26e 100644 --- a/inst/tinytest/test-persistence-class.R +++ b/inst/tinytest/test-persistence-class.R @@ -121,6 +121,14 @@ expect_message( pattern = "Negative, infinite, and missing dimensions will be omitted." ) +# Test that as_persistence() locates births and deaths in columns 2 and 3 +x <- TDA::gridDiag(FUNvalues = volcano, sublevel = FALSE) +expect_message(p <- as_persistence(x), pattern = "[Bb]irth") +expect_equal(p$pairs[[1]][, 1], x$diagram[x$diagram[, 1] == 0, 3]) +expect_equal(p$pairs[[1]][, 2], x$diagram[x$diagram[, 1] == 0, 2]) +expect_equal(p$pairs[[2]][, 1], unname(x$diagram[x$diagram[, 1] == 1, 3])) +expect_equal(p$pairs[[2]][, 2], unname(x$diagram[x$diagram[, 1] == 1, 2])) + # Test that as_persistence() errors out if provided with a matrix with # less than 2 columns x <- matrix(1:6, ncol = 1)