[73f552]: / tests / testthat / test-RWR.R

Download this file

128 lines (90 with data), 5.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
context("RWR")
graph1 <- igraph::graph_from_data_frame(list(from = c("A", "B", "A", "D", "C", "A", "C"),
to = c("B", "C", "D", "E", "D", "F", "G")), directed = FALSE)
graph1 <- set_vertex_attr(graph = graph1, name = 'type', index = c("A","B","C"),value = "1")
graph1 <- set_vertex_attr(graph = graph1, name = 'type', index = c("D","E"),value = "2")
graph1 <- set_vertex_attr(graph = graph1, name = 'type', index = c("F", "G"),value = "3")
rwr_res <- random_walk_restart(X = graph1, seed = c("A", "B", "C", "D", "E"))
rwr_res_type <- rwr_find_seeds_between_attributes(X = rwr_res, attribute = "type", k = 3)
graph1.list <- list("X" = graph1, "Y"= graph1)
rwr_res.list <- random_walk_restart(X = graph1.list, seed = c("A", "B", "C", "D", "E"))
rwr_res_type.list <- rwr_find_seeds_between_attributes(X = rwr_res.list, attribute = "type", k = 3)
test_that("random_walk_with_restart fails with invalid input", {
# X
expect_error(random_walk_restart(X=NULL))
expect_error(random_walk_restart(X=data.frame()))
expect_error(random_walk_restart(X=list(3)))
# seed
expect_error(random_walk_restart(X=graph1, seed = 3))
# r
expect_error(random_walk_restart(X=graph1, r = -2), "'r' must be a numeric value between 0 and 1", fixed = TRUE)
expect_error(random_walk_restart(X=graph1, r = matrix(2)), "'r' must be a numeric value", fixed = TRUE)
})
test_that("random_walk_with_restart works", {
# X
expect_is(random_walk_restart(graph1.list), "list.rwr")
expect_is(random_walk_restart(graph1), "rwr")
# seed
expect_is(random_walk_restart(graph1, seed = 'A'), "rwr")
expect_is(random_walk_restart(graph1.list, seed = "A"), "list.rwr")
expect_is(random_walk_restart(graph1, seed = NULL), "rwr")
# r
expect_is(random_walk_restart(graph1, seed = NULL, r = 0.4), "rwr")
})
test_that("rwr_find_seeds_between_attributes fails with invalid input", {
# X
expect_error(rwr_find_seeds_between_attributes(X=NULL))
expect_error(rwr_find_seeds_between_attributes(X=matrix()))
# k
expect_error(rwr_find_seeds_between_attributes(X=rwr_res, k = -5), "'k' must be a numeric value between 0 and 200", fixed = TRUE)
expect_error(rwr_find_seeds_between_attributes(X=rwr_res, k = list(3)),"'k' must be a numeric value", fixed = TRUE)
# seed / attribute
expect_error(rwr_find_seeds_between_attributes(X=rwr_res, seed = matrix(3)), "'seed' must be a charactor vector", fixed = TRUE)
expect_error(rwr_find_seeds_between_attributes(X=rwr_res, attribute = matrix(3)), "'attribute' must be a charactor vector", fixed = TRUE)
expect_error(rwr_find_seeds_between_attributes(X=rwr_res, attribute = c("4", "5")), "invalid length", fixed = TRUE)
})
test_that("rwr_find_seeds_between_attributes works", {
# X
expect_is(rwr_find_seeds_between_attributes(X=rwr_res), "rwr.attributes")
expect_is(rwr_find_seeds_between_attributes(X=rwr_res.list), "list.rwr.attributes")
# seed
expect_is(rwr_find_seeds_between_attributes(X=rwr_res, seed = logical(0)), "rwr.attributes")
expect_is(rwr_find_seeds_between_attributes(X=rwr_res, attribute = logical(0)), "rwr.attributes")
expect_is(rwr_find_seeds_between_attributes(X=rwr_res, seed = "Z"), "rwr.attributes")
expect_is(rwr_find_seeds_between_attributes(X=rwr_res.list, seed = "Z"), "list.rwr.attributes")
# k
expect_is(rwr_find_seeds_between_attributes(X=rwr_res, k = 1) , "rwr.attributes")
expect_is(rwr_find_seeds_between_attributes(X=rwr_res, k = NULL), "rwr.attributes")
})
test_that("rwr_find_closest_type fails with invalid input", {
# X
expect_error(rwr_find_closest_type(X=NULL))
expect_error(rwr_find_closest_type(X=matrix()))
# seed / attribute
expect_error(rwr_find_closest_type(X=rwr_res, seed = matrix(3)), "'seed' must be a charactor vector", fixed = TRUE)
expect_error(rwr_find_closest_type(X=rwr_res, attribute = matrix(3)), "'attribute' must be a charactor vector", fixed = TRUE)
expect_error(rwr_find_closest_type(X=rwr_res, attribute = c("4", "5")), "invalid length", fixed = TRUE)
# value
expect_error(rwr_find_closest_type(X=rwr_res, attribute = NULL, seed = NULL, value = c("a", "b")), "invalid length", fixed = TRUE)
expect_error(rwr_find_closest_type(X=rwr_res, attribute = NULL, seed = NULL, value = matrix(1,2)), "'value' must be a charactor vector", fixed = TRUE)
})
test_that("rwr_find_closest_type works", {
# X
expect_is(rwr_find_closest_type(X=rwr_res), "rwr.closest")
expect_is(rwr_find_closest_type(X=rwr_res.list), "list.rwr.closest")
# seeds / attributes
expect_is(rwr_find_closest_type(X=rwr_res, seed = logical(0)), "rwr.closest")
expect_is(rwr_find_closest_type(X=rwr_res, attribute = logical(0)), "rwr.closest")
expect_is(rwr_find_closest_type(X=rwr_res, attribute = NULL, seed = NULL), "rwr.closest")
expect_is(rwr_find_closest_type(X=rwr_res, attribute = "NULL", seed = NULL), "rwr.closest")
#value
expect_is(rwr_find_closest_type(X=rwr_res, attribute = "NULL", seed = NULL, value = NULL), "rwr.closest")
expect_is(rwr_find_closest_type(X=rwr_res, attribute = NULL, seed = NULL, value = "test"), "rwr.closest")
expect_is(rwr_find_closest_type(X=rwr_res, value = "test"), "rwr.closest")
expect_is(rwr_find_closest_type(X=rwr_res, seed = "Z"), "rwr.closest")
expect_is(rwr_find_closest_type(X=rwr_res.list, seed = "Z"), "list.rwr.closest")
})
# test_that("top_k_graph returns NULL", {
# rwr_top_k_graph
# rwr_top_k_graph(X = rwr_res$graph, RWRM_Result_Object = rwr_res$rwr, Seed = "A", k = 1)
# })