File: test_indexing.R

package info (click to toggle)
r-cran-igraph 1.0.1-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 18,232 kB
  • sloc: ansic: 173,538; cpp: 19,365; fortran: 4,550; yacc: 1,164; tcl: 931; lex: 484; makefile: 149; sh: 9
file content (263 lines) | stat: -rw-r--r-- 9,432 bytes parent folder | download | duplicates (4)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263

context("Indexing")

mm <- function(...) {
  v <- as.numeric(as.vector(list(...)))
  matrix(v, nrow=sqrt(length(v)))
}
am <- function(x) {
  x <- as.matrix(x)
  dimnames(x) <- NULL
  x
}

library(igraph)
library(Matrix, quietly=TRUE, warn.conflicts=FALSE)

g <- make_tree(20)

test_that("[ indexing works", {
  
  ## Are these vertices connected?
  expect_that(g[1,2], equals(1))
  expect_that(am(g[c(1,1,7), c(2,3,14)]), equals(mm(1,1,0, 1,1,0, 0,0,1)))
  expect_that(am(g[c(1,1,7), c(5,3,12)]), equals(mm(0,0,0, 1,1,0 ,0,0,0)))
  expect_that(am(g[c(1,1,1,1), c(2,3,2,2)]), equals(matrix(1, 4, 4)))
  expect_that(am(g[c(8,17), c(17,8)]), equals(mm(1,0, 0,0)))

})

V(g)$name <- letters[1:vcount(g)]

test_that("[ indexing works with symbolic names", {

  ## The same with symbolic names
  expect_that(g['a','b'], equals(1))
  expect_that(am(g[c('a','a','g'), c('b','c','n')]),
              equals(mm(1,1,0, 1,1,0, 0,0,1)))
  expect_that(am(g[c('a','a','g'), c('e','c','l')]),
              equals(mm(0,0,0, 1,1,0, 0,0,0)))
  expect_that(am(g[c('a','a','a','a'), c('b','c','b','b')]),
              equals(matrix(1, 4, 4)))
  expect_that(am(g[c('h','q'), c('q','h')]), equals(mm(1,0, 0,0)))
})

test_that("[ indexing works with logical vectors", {

  ## Logical vectors
  lres <- structure(c(0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(2L, 20L),
                    .Dimnames = list(c("b", "c"), c("a", "b", "c",
                      "d", "e", "f", "g", "h", "i", "j", "k", "l",
                      "m", "n", "o", "p", "q", "r", "s", "t")))
  expect_that(g[degree(g,mode="in")==0,2], equals(1))
  expect_that(as.matrix(g[2:3,TRUE]), equals(lres))
})

test_that("[ indexing works with negative indices", {
  
  ## Negative indices
  nres <- structure(c(0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0), .Dim = c(2L, 19L),
                    .Dimnames=list(c("b", "c"),
                      c("b", "c", "d", "e", "f", "g", "h", "i", "j",
                        "k", "l", "m", "n", "o", "p", "q", "r", "s",
                        "t")))
  expect_that(as.matrix(g[2:3,-1]), equals(nres))
})

el <- as_edgelist(g, names=FALSE)
E(g)$weight <- el[,1] * el[,2]

test_that("[ indexing works with weighted graphs", {
  
  ## Weighted graphs
  expect_that(g[1,2], equals(2))
  expect_that(am(g[c(1,1,7), c(2,3,14)]), equals(mm(2,2,0, 3,3,0, 0,0,98)))
  expect_that(am(g[c(1,1,7), c(5,3,12)]), equals(mm(0,0,0, 3,3,0, 0,0,0)))
  expect_that(am(g[c(1,1,1,1), c(2,3,2,2)]),
              equals(mm(2,2,2,2, 3,3,3,3, 2,2,2,2, 2,2,2,2)))
  expect_that(am(g[c(8,17), c(17,8)]), equals(mm(136,0, 0,0)))
})

test_that("[ indexing works with weighted graphs and symbolic names",
          {
            
  ## Weighted graph, with symbolic names
  expect_that(g['a','b'], equals(2))
  expect_that(am(g[c('a','a','g'), c('b','c','n')]),
              equals(mm(2,2,0, 3,3,0, 0,0,98)))
  expect_that(am(g[c('a','a','g'), c('e','c','l')]),
              equals(mm(0,0,0, 3,3,0, 0,0,0)))
  expect_that(am(g[c('a','a','a','a'), c('b','c','b','b')]),
              equals(mm(2,2,2,2, 3,3,3,3, 2,2,2,2, 2,2,2,2)))  
  expect_that(am(g[c('h','q'), c('q','h')]), equals(mm(136,0, 0,0)))
})

################################################################

test_that("[[ indexing works", {

  ## Adjacent vertices
  expect_that(g[[1, ]], is_equivalent_to(list(a=V(g)[2:3])))
  expect_that(g[[, 2]], is_equivalent_to(list(b=V(g)[1])))
  expect_that(g[[, 2, directed=FALSE]],
              is_equivalent_to(list(b=V(g)[c(1,4,5)])))
  expect_that(g[[2, directed=FALSE]],
              is_equivalent_to(list(b=V(g)[c(1,4,5)])))

  expect_that(g[[1:3, ]], is_equivalent_to(list(a=V(g)[2:3], b=V(g)[4:5],
                                                c=V(g)[6:7])))
  expect_that(g[[, 1:3]], is_equivalent_to(list(a=V(g)[numeric()],
                                                b=V(g)[1], c=V(g)[1])))
})

test_that("[[ indexing works with symbolic names", {
  
  ## Same with vertex names
  expect_that(g[['a', ]], is_equivalent_to(list(a=V(g)[2:3])))
  expect_that(g[[, 'b']], is_equivalent_to(list(b=V(g)[1])))
  expect_that(g[[, 'b', directed=FALSE]],
              is_equivalent_to(list(b=V(g)[c(1,4,5)])))
  expect_that(g[['b', directed=FALSE]],
              is_equivalent_to(list(b=V(g)[c(1,4,5)])))

  expect_that(g[[letters[1:3],]],
    is_equivalent_to(list(a=V(g)[2:3], b=V(g)[4:5], c=V(g)[6:7])))
  expect_that(g[[, letters[1:3]]],
    is_equivalent_to(list(a=V(g)[numeric()], b=V(g)[1], c=V(g)[1])))
})

test_that("[[ indexing works with logical vectors", {

  ## Logical vectors
  expect_that(g[[degree(g,mode="in")==0,]],
              is_equivalent_to(list(a=V(g)[2:3])))
})

test_that("[[ indexing works with filtering on both ends", {

  ## Filtering on both ends
  expect_that(g[[1:10, 1:10]],
    is_equivalent_to(list(a=V(g)[2:3], b=V(g)[4:5], c=V(g)[6:7], d=V(g)[8:9],
      e=V(g)[10], f=V(g)[numeric()], g=V(g)[numeric()], h=V(g)[numeric()],
                          i=V(g)[numeric()], j=V(g)[numeric()])))
})

################################################################

test_that("[ can query edge ids", {
  
  ## Query edge ids
  expect_that(g[1,2, edges=TRUE], equals(1))
  expect_that(am(g[c(1,1,7), c(2,3,14), edges=TRUE]),
              equals(mm(1,1,0, 2,2,0, 0,0,13)))
  expect_that(am(g[c(1,1,7), c(5,3,12), edges=TRUE]),
              equals(mm(0,0,0, 2,2,0, 0,0,0)))
  expect_that(am(g[c(1,1,1,1), c(2,3,2,2), edges=TRUE]),
              equals(mm(1,1,1,1, 2,2,2,2, 1,1,1,1, 1,1,1,1)))
  expect_that(am(g[c(8,17), c(17,8), edges=TRUE]),
              equals(mm(16,0, 0,0)))
})

test_that("[ can query edge ids with symbolic names", {
  
  ## The same with symbolic names
  expect_that(g['a','b', edges=TRUE], equals(1))
  expect_that(am(g[c('a','a','g'), c('b','c','n'), edges=TRUE]),
              equals(mm(1,1,0, 2,2,0, 0,0,13)))
  expect_that(am(g[c('a','a','g'), c('e','c','l'), edges=TRUE]),
              equals(mm(0,0,0, 2,2,0, 0,0,0)))
  expect_that(am(g[c('a','a','a','a'), c('b','c','b','b'), edges=TRUE]),
              equals(mm(1,1,1,1, 2,2,2,2, 1,1,1,1, 1,1,1,1)))
  expect_that(am(g[c('h','q'), c('q','h'), edges=TRUE]),
              equals(mm(16,0 ,0,0)))
})

################################################################

test_that("[[ can query incident edges", {
  
  ## Incident edges of vertices
  expect_that(g[[1, , edges=TRUE]], is_equivalent_to(list(a=E(g)[1:2])))
  expect_that(g[[, 2, edges=TRUE]], is_equivalent_to(list(b=E(g)[1])))
  expect_that(g[[, 2, directed=FALSE, edges=TRUE]],
              is_equivalent_to(list(b=E(g)[c(3,4,1)])))
  expect_that(g[[2, directed=FALSE, edges=TRUE]],
              is_equivalent_to(list(b=E(g)[c(3,4,1)])))

  expect_that(g[[1:3, , edges=TRUE]],
              is_equivalent_to(list(a=E(g)[1:2], b=E(g)[3:4], c=E(g)[5:6])))
  expect_that(g[[, 1:3, edges=TRUE]],
              is_equivalent_to(list(a=E(g)[numeric()], b=E(g)[1], c=E(g)[2])))
})

test_that("[[ queries edges with vertex names", {
  
  ## Same with vertex names
  expect_that(g[['a', , edges=TRUE]],
              is_equivalent_to(list(a=E(g)[1:2])))
  expect_that(g[[, 'b', edges=TRUE]],
              is_equivalent_to(list(b=E(g)[1])))
  expect_that(g[[, 'b', directed=FALSE, edges=TRUE]],
              is_equivalent_to(list(b=E(g)[c(3,4,1)])))
  expect_that(g[['b', directed=FALSE, edges=TRUE]],
              is_equivalent_to(list(b=E(g)[c(3,4,1)])))

  expect_that(g[[letters[1:3],, edges=TRUE]],
              is_equivalent_to(list(a=E(g)[1:2], b=E(g)[3:4], c=E(g)[5:6])))
  expect_that(g[[, letters[1:3], edges=TRUE]],
              is_equivalent_to(list(a=E(g)[numeric()], b=E(g)[1], c=E(g)[2])))

  ## Filtering on both ends
  expect_that(g[[1:10, 1:10, edges=TRUE]],
    is_equivalent_to(list(E(g)[1:2], E(g)[3:4], E(g)[5:6], E(g)[7:8],
                          E(g)[9], E(g)[numeric()], E(g)[numeric()],
                          E(g)[numeric()], E(g)[numeric()], E(g)[numeric()])))
})

#################################################################

test_that("[ handles from and to properly", {
  
  ## from & to
  g <- make_tree(20)
  expect_that(g[from=c(1,2,2,3), to=c(3,4,8,7)], equals(c(1,1,0,1)))

  V(g)$name <- letters[1:20]
  expect_that(g[from=c("a","b","b","c"), to=c("c","d","h","g")],
              equals(c(1,1,0,1)))
  
  E(g)$weight <- (1:ecount(g)) ^ 2 
  expect_that(g[from=c("a","b","b","c"), to=c("c","d","h","g")],
              equals(c(4,9,0,36)))

  expect_that(g[from=c("a","b","b","c"), to=c("c","d","h","g"),
                edges=TRUE], equals(c(2,3,0,6)))
              

})

test_that("[[ works with from and to", {

  g <- make_tree(20)

  expect_equivalent(g[[1, ]], g[[from = 1]])
  expect_equivalent(g[[, 1]], g[[to = 1]])
  expect_equivalent(g[[1:5, 4:10]], g[[from = 1:5, to = 4:10]])

  expect_error(g[[1, from = 1]], "Cannot give both")
  expect_error(g[[, 2, to = 10]], "Cannot give both")
})

test_that("[[ returns vertex and edges sequences", {

  g <- make_tree(20)
  expect_true(is_igraph_vs(g[[1]][[1]]))
  expect_true(is_igraph_es(g[[1, edges = TRUE]][[1]]))
  expect_true(is_igraph_vs(g[[1:3, 2:6]][[1]]))
  expect_true(is_igraph_es(g[[1:3, 2:6, edges = TRUE]][[1]]))

})