corybrunson / plt

R interface & extension to an adapted Persistence Landscapes Toolbox
https://corybrunson.github.io/plt
GNU General Public License v3.0
0 stars 2 forks source link

Error in pl_mean() when using a single persistence landscape #7

Open chackoemily opened 1 day ago

chackoemily commented 1 day ago

When calculating the mean persistence landscape with pl_mean(), an error is thrown if there is only one persistence landscape in the 'pl_list' argument. The error states, "Error in as.environment(new("refMethodDef", .Data = function (...) : invalid object for 'as.environment' "

Code to reproduce error:

library(plt)

# single feature in dimension 0 with birth = 1 and death = 2
pd <- matrix(c(1, 2), ncol = 2, byrow = TRUE)

# persistence landscape
pl <- landscape(pd, degree = 0)

# computing mean of one landscape throws an error 
pl_mean_result <- pl_mean(pl)

I expected that pl_mean() would either handle a single persistence landscape without error and return the landscape itself as the mean or provide a more informative message that explains why computing the mean is not possible with a single landscape.

corybrunson commented 1 day ago

Thank you @chackoemily for pointing this out!

This behavior is intentional, but i agree that it's suboptimal. These functions require lists (of PLs) as input, so they just don't know how to handle non-list objects.

I've pushed a hopeful fix to the list branch, which adds a check-step to each such function as follows:

pl_function <- function(pl_list, ...) {
  # if not a list, wrap in a list and recurse
  if (! is.list(pl_list)) return(pl_function(list(pl_list)))
  # handle a list
  <original code>
}

Please try installing from that branch and let me know if the problem persists. Note that this branch is also updated with pl_new() having replaced landscape(). This should do the installation:

remotes::install_github("corybrunson/plt", ref = "list")