Skip to content

Commit

Permalink
test: add test for hits_scores() directly (#1444)
Browse files Browse the repository at this point in the history
  • Loading branch information
aviator-app[bot] authored Aug 13, 2024
2 parents 393e05e + 53e6359 commit a9b2be6
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 16 deletions.
3 changes: 2 additions & 1 deletion R/centrality.R
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,8 @@ diversity <- diversity_impl
#' [arpack()] for details.
#' @inheritParams rlang::args_dots_empty
#' @return A named list with members:
#' \item{vector}{The hub or authority scores of the vertices.}
#' \item{hub}{The hub score of the vertices.}
#' \item{authority}{The authority score of the vertices.}
#' \item{value}{The corresponding eigenvalue of the calculated
#' principal eigenvector.}
#' \item{options}{Some information about the ARPACK computation, it has
Expand Down
3 changes: 2 additions & 1 deletion man/hits_scores.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions tests/testthat/_snaps/authority.score.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# `authority_score()` works

Code
s3 <- authority_score(g2, options = arpack_defaults)$vector
Condition
Warning:
`authority_score()` was deprecated in igraph 2.0.4.
i Please use `hits_scores()` instead.
Warning:
arpack_defaults was deprecated in igraph 1.6.0.
i Please use `arpack_defaults()` instead.
i So the function arpack_defaults(), not an object called arpack_defaults.

# `hub_score()` works

Code
s3 <- hub_score(g2, options = arpack_defaults)$vector
Condition
Warning:
`hub_score()` was deprecated in igraph 2.0.3.
i Please use `hits_scores()` instead.
Warning:
arpack_defaults was deprecated in igraph 1.6.0.
i Please use `arpack_defaults()` instead.
i So the function arpack_defaults(), not an object called arpack_defaults.

84 changes: 70 additions & 14 deletions tests/testthat/test-authority.score.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("`authority_score()` works", {
rlang::local_options(lifecycle_verbosity = "quiet")
mscale <- function(x) {
if (sd(x) != 0) {
x <- scale(x)
Expand All @@ -21,14 +22,16 @@ test_that("`authority_score()` works", {
s2 <- authority_score(g2)$vector
expect_equal(mscale(s1), mscale(s2), ignore_attr = TRUE)

rlang::local_options(lifecycle_verbosity = "warning")
expect_warning(
s3 <- authority_score(g2, options = arpack_defaults)$vector
)
rlang::with_options(lifecycle_verbosity = "warning", {
expect_snapshot(
s3 <- authority_score(g2, options = arpack_defaults)$vector
)
})
expect_equal(s2, s3)
})

test_that("`hub_score()` works", {
rlang::local_options(lifecycle_verbosity = "quiet")
mscale <- function(x) {
if (sd(x) != 0) {
x <- scale(x)
Expand All @@ -51,10 +54,11 @@ test_that("`hub_score()` works", {
s2 <- hub_score(g2)$vector
expect_equal(mscale(s1), mscale(s2), ignore_attr = TRUE)

rlang::local_options(lifecycle_verbosity = "warning")
expect_warning(
s3 <- hub_score(g2, options = arpack_defaults)$vector
)
rlang::with_options(lifecycle_verbosity = "warning", {
expect_snapshot(
s3 <- hub_score(g2, options = arpack_defaults)$vector
)
})
expect_equal(s2, s3)
})

Expand All @@ -64,9 +68,10 @@ test_that("`hub_score()` works", {
# and any vector with alternating values (a, b, a, b, ...) is a valid
# solution, not just all-ones.
test_that("authority scores of a ring are all one", {
rlang::local_options(lifecycle_verbosity = "quiet")
g3 <- make_ring(99)
expect_equal(authority_score(g3)$vector, rep(1, vcount(g3)))
expect_equal(hub_score(g3)$vector, rep(1, vcount(g3)))
expect_equal(hits_scores(g3)$authority, rep(1, vcount(g3)))
expect_equal(hits_scores(g3)$hub, rep(1, vcount(g3)))
})

test_that("authority_score survives stress test", {
Expand All @@ -89,15 +94,66 @@ test_that("authority_score survives stress test", {

for (i in 1:100) {
G <- sample_gnm(10, sample(1:20, 1))
as <- authority_score(G)
as <- hits_scores(G)
M <- as_adj(G, sparse = FALSE)
is.good(t(M) %*% M, as$vector, as$value)
is.good(t(M) %*% M, as$authority, as$value)
}

for (i in 1:100) {
G <- sample_gnm(10, sample(1:20, 1))
hs <- hub_score(G)
hs <- hits_scores(G)
M <- as_adj(G, sparse = FALSE)
is.good(M %*% t(M), hs$vector, hs$value)
is.good(M %*% t(M), hs$hub, hs$value)
}
})

test_that("`hits_score()` works -- authority", {
mscale <- function(x) {
if (sd(x) != 0) {
x <- scale(x)
}
if (x[1] < 0) {
x <- -x
}
x
}

g1 <- sample_pa(100, m = 10)
A <- as_adj(g1, sparse = FALSE)
s1 <- eigen(t(A) %*% A)$vectors[, 1]
s2 <- hits_scores(g1)$authority
expect_equal(mscale(s1), mscale(s2), ignore_attr = TRUE)

g2 <- sample_gnp(100, 2 / 100)
A <- as_adj(g2, sparse = FALSE)
s1 <- eigen(t(A) %*% A)$vectors[, 1]
s2 <- hits_scores(g2)$authority
expect_equal(mscale(s1), mscale(s2), ignore_attr = TRUE)

})

test_that("`hits_scores()` works -- hub", {
mscale <- function(x) {
if (sd(x) != 0) {
x <- scale(x)
}
if (x[1] < 0) {
x <- -x
}
x
}

g1 <- sample_pa(100, m = 10)
A <- as_adj(g1, sparse = FALSE)
s1 <- eigen(A %*% t(A))$vectors[, 1]
s2 <- hits_scores(g1)$hub
expect_equal(mscale(s1), mscale(s2), ignore_attr = TRUE)

g2 <- sample_gnp(100, 2 / 100)
A <- as_adj(g2, sparse = FALSE)
s1 <- eigen(A %*% t(A))$vectors[, 1]
s2 <- hits_scores(g2)$hub
expect_equal(mscale(s1), mscale(s2), ignore_attr = TRUE)

})

0 comments on commit a9b2be6

Please sign in to comment.