tests/testthat/test_lr_modularity.R

context("testing LinkRank modularity function")

if (!requireNamespace("igraph"))
    testthat::skip()

if (!requireNamespace("Matrix"))
    testhat::skip()

# create random matrix, partition, and add weights
A = matrix(sample.int(2, 100, replace = T), nrow = 10) - 1L
diag(A) = 0L
g = igraph::graph_from_adjacency_matrix(A, mode = "directed")
p = sample.int(3, 10, replace = T)
w = runif(igraph::ecount(g))
igraph::edge_attr(g, "weight") = w

test_that("weight function works properly", {
    
    # weights should be automatically used
    m1 = lr_modularity(g, p, weight = w)
    m2 = lr_modularity(g, p)
    expect_equal(m1, m2)
    
    # weight = NA -> no weights used
    g_nw = igraph::delete_edge_attr(g, "weight")
    m1 = lr_modularity(g_nw, p)
    m2 = lr_modularity(g, p, weight = NA)
    expect_equal(m1, m2)
    
    # weight = NULL -> weights are used
    m1 = lr_modularity(g_nw, p, weight = w)
    m2 = lr_modularity(g, p, weight = NULL)
    expect_equal(m1, m2)
    
    # undirected & damping = 1 -> lr_modularity = modularity
    pr = pr_w = NaN
    while (any(is.na(c(pr, pr_w)))) {
        
        tmp = matrix(sample.int(2, 100, replace = T), nrow = 10) - 1L
        diag(tmp) = 0L
        A_sym = symmetrize(tmp, upper_to_lower = T)
        g_sym = igraph::graph_from_adjacency_matrix(A_sym, mode = "undirected")
        w_sym = runif(igraph::ecount(g_sym))
        
        pr = igraph::page_rank(g_sym, damping = 1)$vector
        pr_w = igraph::page_rank(g_sym, damping = 1, weights = w_sym)$vector
        
    } 

    expect_equal(lr_modularity(g_sym, p, damping = 1), 
                 igraph::modularity(g_sym, p))
    
    
    igraph::edge_attr(g_sym, "weight") = w_sym
    expect_equal(
        lr_modularity(g_sym, p, damping = 1), 
        igraph::modularity(g_sym, p, weights = igraph::E(g_sym)$weight)
    )
    
})

test_that("function throws appropriate errors", {
    
    # different results from modularity
    expect_true(
        !isTRUE(
            all.equal(
                lr_modularity(g, p),
                igraph::modularity(g, p),
                check.attributes = FALSE
            )
        )
    )
    
    # negative weights
    expect_error(lr_modularity(g, p, weight = rnorm(igraph::ecount(g))))
    
    # wrong length of weights
    expect_error(lr_modularity(g, p, weight = w[-1]))
    
    # damping coefficient out of range
    expect_error(lr_modularity(g, p, damping = -.1))
    expect_error(lr_modularity(g, p, damping = 1.1))
    
    # communities have to be integers
    expect_error(lr_modularity(g, as.numeric(p)))
    
    # wrong length of communities vector
    expect_error(lr_modularity(g, p[-1]))
    
})

test_that("function works with communities object", {
    
    # get communities and test
    com = list(membership = p); class(com) = "communities"
    
    expect_equal(lr_modularity(g, com), lr_modularity(g, p))

})
baruuum/btoolbox documentation built on Aug. 17, 2020, 1:29 a.m.