Skip to content

make as_persistence metadata handling less fragile #64

Open

Description

@corybrunson

PR #58 sidesteps a problem with how as_persistence.diagram() recovers package, function, and argument information from a diagram object: Each TDA::*Diag() obtains a list with a diagram element that in turn contains a call attribute. This attribute behaves unexpectedly when the function is not called directly; see below.

Since {phutil} needs to be flexible enough to work with input from packages outside the TDAverse, i don't think the solution is either to attempt to harmonize other packages through contributions or to meticulously solve these edge cases (unless they're solved by a more general package that phutil can depend on). Rather i think a few small changes should be made:

  • Remove package, engine, and parameter details from the print() method.
  • Instead, put these metadata in a summary() method with more careful and transparent handling of missingness and different structuring.
  • Coordinate the metadata captured by {phreng} with that used by {phutil}, so that a fully TDAverse workflow is consistent across all engines.

I'm open to alternative long-term plans! This is my opening salvo. (I'd like to be able to point to a proof of concept in another package, but i'm not aware of one.)

library(phutil)

x <- cbind(x = runif(6), y = runif(6))

ph_direct <- TDA::ripsDiag(x, maxdimension = 2, maxscale = 10)
attributes(ph_direct$diagram)$call
#> TDA::ripsDiag(X = x, maxdimension = 2, maxscale = 10)

ph_lapply <- lapply(list(x), TDA::ripsDiag, maxdimension = 2, maxscale = 10)
attributes(ph_lapply[[1]]$diagram)$call
#> FUN(X = X[[i]], maxdimension = 2, maxscale = 10)

ph_docall <- do.call(
  TDA::ripsDiag,
  c(list(X = x, maxdimension = 2, maxscale = 10))
)
attributes(ph_docall$diagram)$call
#> (function (X, maxdimension, maxscale, dist = "euclidean", library = "GUDHI", 
#>     location = FALSE, printProgress = FALSE) 
#> {
#>     if (!is.numeric(X) && !is.data.frame(X)) {
#>         stop("X should be a matrix of coordinates")
#>     }
#>     if (!is.numeric(maxdimension) || length(maxdimension) != 
#>         1 || maxdimension < 0) {
#>         stop("maxdimension should be a nonnegative integer")
#>     }
#>     if (!is.numeric(maxscale) || length(maxscale) != 1) {
#>         stop("maxscale should be a number")
#>     }
#>     if (dist != "euclidean" && dist != "arbitrary") {
#>         stop("dist should be either 'euclidean' or 'arbitrary'")
#>     }
#>     if (length(library) == 1) {
#>         library <- rep(library, 2)
#>     }
#>     if (library[1] == "gudhi" || library[1] == "Gudhi") {
#>         library[1] <- "GUDHI"
#>     }
#>     if (library[1] == "dionysus" || library[1] == "DIONYSUS") {
#>         library[1] <- "Dionysus"
#>     }
#>     if (library[1] != "GUDHI" && library[1] != "Dionysus") {
#>         stop("library for building a filtration should be a string: either 'GUDHI' or 'Dionysus'")
#>     }
#>     if (library[2] == "gudhi" || library[2] == "Gudhi") {
#>         library[2] <- "GUDHI"
#>     }
#>     if (library[2] == "dionysus" || library[2] == "DIONYSUS") {
#>         library[2] <- "Dionysus"
#>     }
#>     if (library[2] == "phat" || library[2] == "Phat") {
#>         library[2] <- "PHAT"
#>     }
#>     if (library[2] != "GUDHI" && library[2] != "Dionysus" && 
#>         library[2] != "PHAT") {
#>         stop("library for computing persistence diagram should be a string: either 'GUDHI', 'Dionysus', or 'PHAT'")
#>     }
#>     if (!is.logical(location)) {
#>         stop("location should be logical")
#>     }
#>     if (!is.logical(printProgress)) {
#>         stop("printProgress should be logical")
#>     }
#>     if (dist == "arbitrary" && library[2] == "GUDHI" && maxdimension > 
#>         1) {
#>         stop("there is a bug for computing homological features of dimension higher than 1 when the distance is arbitrary and library 'GUDHI' is used")
#>     }
#>     if (dist == "arbitrary" && library[1] == "GUDHI") {
#>         library[1] <- "Dionysus"
#>     }
#>     X <- as.matrix(X)
#>     max_num_pairs <- 5000
#>     if (dist == "euclidean" && library[1] == "Dionysus" && .Machine[["sizeof.pointer"]] != 
#>         8) {
#>         ripsOut <- RipsDiag(X = as.matrix(dist(X)), maxdimension = maxdimension, 
#>             maxscale = maxscale, dist = "arbitrary", libraryFiltration = library[1], 
#>             libraryDiag = library[2], location = location, printProgress = printProgress)
#>     }
#>     else {
#>         ripsOut <- RipsDiag(X = X, maxdimension = maxdimension, 
#>             maxscale = maxscale, dist = dist, libraryFiltration = library[1], 
#>             libraryDiag = library[2], location = location, printProgress = printProgress)
#>     }
#>     if (location == TRUE) {
#>         if (dist == "euclidean") {
#>             BirthLocation <- X[ripsOut[[2]][, 1], ]
#>             DeathLocation <- X[ripsOut[[2]][, 2], ]
#>             if (library[2] == "Dionysus") {
#>                 CycleLocation <- lapply(ripsOut[[3]], function(bdy) {
#>                   array(X[bdy, ], dim = c(dim(bdy), NCOL(X)))
#>                 })
#>             }
#>         }
#>         else {
#>             BirthLocation <- ripsOut[[2]][, 1]
#>             DeathLocation <- ripsOut[[2]][, 2]
#>             if (library[2] == "Dionysus") {
#>                 CycleLocation <- ripsOut[[3]]
#>             }
#>         }
#>     }
#>     Diag <- ripsOut[[1]]
#>     if (NROW(Diag) > 0) {
#>         Diag[which(Diag[, 3] == Inf), 3] <- maxscale
#>     }
#>     colnames(Diag) <- c("dimension", "Birth", "Death")
#>     class(Diag) <- "diagram"
#>     attributes(Diag)[["maxdimension"]] <- max(Diag[, 1])
#>     attributes(Diag)[["scale"]] <- c(0, maxscale)
#>     attributes(Diag)[["call"]] <- match.call()
#>     if (location == FALSE || library[2] == "GUDHI") {
#>         out <- list(diagram = Diag)
#>     }
#>     else if (library[2] == "PHAT") {
#>         out <- list(diagram = Diag, birthLocation = BirthLocation, 
#>             deathLocation = DeathLocation)
#>     }
#>     else {
#>         out <- list(diagram = Diag, birthLocation = BirthLocation, 
#>             deathLocation = DeathLocation, cycleLocation = CycleLocation)
#>     }
#>     return(out)
#> })(X = c(0.280194469494745, 0.555343792075291, 0.751159406732768, 
#> 0.427674155915156, 0.0581452425103635, 0.207330663921311, 0.645935284672305, 
#> 0.835648278240114, 0.429336545523256, 0.510585440322757, 0.462143029319122, 
#> 0.357985361479223), maxdimension = 2, maxscale = 10)

Created on 2026-10-06 with reprex v2.1.1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions