Skip to content

Bug in mapper2D$adjacency #6

@pcamara

Description

@pcamara

Hi,

There seems to be a bug in mapper2D$adjacency (and I did not check mapper1D$adjacency; potentially it is also there). For instance, consider the following example:

m2 <- mapper2D(
	distance_matrix = dist(data.frame( x=2*cos(1:100), y=sin(1:100) )),
	filter_values = list( 2*cos(1:100), sin(1:100) ),
	num_intervals = c(15,15),
	percent_overlap = 70,
	num_bins_when_clustering = 10)

and let us look at the first 7 vertices:

m2$points_in_vertex[1:7]
[[1]]
[1]  4 48 92

[[2]]
[1] 29 73

[[3]]
[1]  4 48 92

[[4]]
[1] 23 67

[[5]]
[1] 29 73

[[6]]
[1]  4 48 92

[[7]]
[1] 23 67

There should be edges at (1,3), (1,6), (3,6), (2,5), and (4,7). However, m2$adjacency misses the (1,6) edge:

m2$adjacency[1:7,1:7]
      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    0    0    1    0    0    0    0
[2,]    0    0    0    0    1    0    0
[3,]    1    0    0    0    0    1    0
[4,]    0    0    0    0    0    0    1
[5,]    0    1    0    0    0    0    0
[6,]    0    0    1    0    0    0    0
[7,]    0    0    0    1    0    0    0

The following code I think would correctly compute the adjacency matrix and can be potentially useful to fix the bug (although I am sure there are more efficient ways to do it):

library(Matrix)
adjacency <- function(m2) {
  l <- length(m2$points_in_vertex)
  simps <- Matrix(0, l, l, sparse=TRUE)
  simps[lower.tri(simps, diag=FALSE)] <- as.numeric(
    lapply(apply(as.matrix(combn(l,2)), 2, 
                 function(x) {c(m2$points_in_vertex[x[1]], m2$points_in_vertex[x[2]])}),
           function(x) {length(intersect(x[[1]], x[[2]])) > 0}))
  return(as.matrix(simps+t(simps)))
}
adjacency(m2)[1:7,1:7]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    0    0    1    0    0    1    0
[2,]    0    0    0    0    1    0    0
[3,]    1    0    0    0    0    1    0
[4,]    0    0    0    0    0    0    1
[5,]    0    1    0    0    0    0    0
[6,]    1    0    1    0    0    0    0
[7,]    0    0    0    1    0    0    0

By the way, thanks for this repository. It is really useful.

Best,

Pablo

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions