Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion R/persistence-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
}
Expand Down
8 changes: 8 additions & 0 deletions inst/tinytest/test-persistence-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down