Debian Codegraph @

tests/testthat/test-attr_access.R

raw
1 # library(testthat)
2
3 context("attr access (get/set)")
4
5
6 test_that("Get a dendrogram leaves attributes", {
7 hc <- hclust(dist(USArrests[1:3, ]), "ave")
8 dend <- as.dendrogram(hc)
9
10 expect_identical(get_leaves_attr(dend, "label"), labels(dend, "label"))
11 expect_identical(get_leaves_attr(dend, "height"), rep(0, 3))
12 expect_identical(get_leaves_attr(dend, "leaf"), rep(TRUE, 3))
13 expect_identical(get_leaves_attr(dend, "members"), rep(1L, 3))
14
15 expect_false(
16 identical(get_leaves_attr(hang.dendrogram(dend), "height"), rep(0, 3))
17 )
18
19 # when using simplify=FALSE, we get back a list...
20 expect_true(is.list(get_leaves_attr(dend, "height", simplify = FALSE)))
21 expect_identical(unlist(get_leaves_attr(dend, "height", simplify = FALSE)), rep(0, 3))
22
23 # if non-dendrogram object is passed in and 'attribute' parameter is missing
24 expect_warning(expect_error(
25 get_leaves_attr(hc)
26 ))
27
28 })
29
30
31 test_that("Get a dendrogram nodes attributes", {
32 hc <- USArrests[1:3, ] %>%
33 dist() %>%
34 hclust("ave")
35 dend <- as.dendrogram(hc)
36
37 expect_error(get_nodes_attr(dend)) # we need attribute!
38
39
40 # NA's are from the nodes which are not leaves:
41 expect_identical(
42 get_nodes_attr(dend, "label"),
43 c(NA, "Arizona", NA, "Alabama", "Alaska")
44 )
45
46 # removing NA's
47 expect_identical(get_nodes_attr(dend, "label", na.rm = TRUE), labels(dend, "label"))
48
49 # leaves have 0 height
50 expect_equal(get_nodes_attr(dend, "height"), c(54.8004107236398, 0, 37.1770090243957, 0, 0))
51
52 # when excluding leaves - it replaces them with NA:
53 expect_equal(get_nodes_attr(dend, "height", include_leaves = FALSE), c(54.8004107236398, NA, 37.1770090243957, NA, NA))
54
55 # this gives ONLY the attribute of the branches nodes (not that of the leaves)
56 expect_equal(get_nodes_attr(dend, "height", include_leaves = FALSE, na.rm = TRUE), c(54.8004107236398, 37.1770090243957))
57
58 expect_identical(get_nodes_attr(dend, "leaf", na.rm = TRUE), rep(TRUE, 3))
59
60 # how to make get_nodes_attr act like get_leaves_attr
61 expect_identical(
62 get_leaves_attr(dend, "members"), # should be 1's
63 get_nodes_attr(dend, "members", include_branches = FALSE, na.rm = TRUE)
64 )
65
66 expect_identical(
67 get_leaves_attr(dend, "height"), # should be 1's
68 get_nodes_attr(dend, "height", include_branches = FALSE, na.rm = TRUE)
69 )
70
71 expect_identical(get_nodes_attr(dend, "members"), c(3L, 1L, 2L, 1L, 1L))
72
73
74 expect_identical(
75 get_nodes_attr(dend, "members", simplify = FALSE),
76 list(3L, 1L, 2L, 1L, 1L)
77 )
78
79 # dealing with a missing/junk attribute:
80 expect_identical(
81 get_nodes_attr(dend, "blablabla"),
82 c(NA, NA, NA, NA, NA)
83 )
84
85 # check the id paramter:
86 expect_identical(
87 get_nodes_attr(dend, "member", id = c(1, 3)),
88 c(3L, 2L)
89 )
90
91
92 dend <- 1:3 %>%
93 dist() %>%
94 hclust() %>%
95 as.dendrogram() %>%
96 set("branches_k_color", k = 2) %>%
97 set("branches_lwd", c(1.5, 1, 1.5)) %>%
98 set("branches_lty", c(1, 1, 3, 1, 1, 2)) %>%
99 set("labels_colors") %>%
100 set("labels_cex", c(.9, 1.2))
101 # plot(dend)
102 # dput(get_nodes_attr(dend, "nodePar"))
103 should_be <- list(
104 NA, structure(list(lab.col = "#CC476B", pch = NA, lab.cex = 0.9), .Names = c(
105 "lab.col",
106 "pch", "lab.cex"
107 )), NA, structure(list(
108 lab.col = "#228B00", pch = NA,
109 lab.cex = 1.2
110 ), .Names = c("lab.col", "pch", "lab.cex")),
111 structure(list(lab.col = "#0082CE", pch = NA, lab.cex = 0.9), .Names = c(
112 "lab.col",
113 "pch", "lab.cex"
114 ))
115 )
116 expect_identical(get_nodes_attr(dend, "nodePar"), should_be)
117 })
118
119
120 # \dontrun{
121 # library(microbenchmark)
122 # # get_leaves_attr is twice faster than get_nodes_attr
123 # microbenchmark( get_leaves_attr(dend, "members"), # should be 1's
124 # get_nodes_attr(dend, "members", include_branches = FALSE, na.rm = TRUE)
125 # )
126 # }
127
128
129
130 test_that("Get a dendrogram's branches heights", {
131 hc <- hclust(dist(USArrests[1:3, ]), "ave")
132 dend <- as.dendrogram(hc)
133
134 # expect_equal(dendextend_get_branches_heights(dend),c(37.1770090243957, 54.8004107236398))
135 # expect_equal(dendextend_options("get_branches_heights")(dend),c(37.1770090243957, 54.8004107236398))
136 expect_equal(get_branches_heights(dend), c(37.1770090243957, 54.8004107236398))
137
138 expect_identical(
139 get_branches_heights(dend),
140 sort(get_nodes_attr(dend, "height", include_leaves = FALSE, na.rm = TRUE))
141 )
142 })
143
144
145
146
147 test_that("get_leaves_nodePar works", {
148 hc <- hclust(dist(USArrests[1:3, ]), "ave")
149 dend <- as.dendrogram(hc)
150
151 # get_leaves_attr(dend) # error :)
152 expect_identical(get_leaves_nodePar(dend, simplify = TRUE), rep(NA, 3))
153
154 labels_colors(dend) <- 1:3
155 should_be <- structure(c(1L, NA, 2L, NA, 3L, NA), .Names = c(
156 "lab.col", "pch",
157 "lab.col", "pch", "lab.col", "pch"
158 ))
159 expect_identical(get_leaves_nodePar(dend, simplify = TRUE), should_be)
160
161 dend <- assign_values_to_leaves_nodePar(dend, 2, "lab.cex")
162 # dput(get_leaves_nodePar(dend))
163 should_be <- list(structure(list(lab.col = 1L, pch = NA, lab.cex = 2), .Names = c(
164 "lab.col",
165 "pch", "lab.cex"
166 )), structure(list(lab.col = 2L, pch = NA, lab.cex = 2), .Names = c(
167 "lab.col",
168 "pch", "lab.cex"
169 )), structure(list(lab.col = 3L, pch = NA, lab.cex = 2), .Names = c(
170 "lab.col",
171 "pch", "lab.cex"
172 )))
173 expect_identical(get_leaves_nodePar(dend), should_be)
174
175 # if non-dendrogram object passed in
176 expect_error(expect_warning(
177 get_leaves_nodePar(hc)
178 ))
179 })
180
181
182
183 test_that("Hanging dendrogram works just like hclust", {
184 hc <- hclust(dist(USArrests[1:5, ]), "ave")
185 dend <- as.dendrogram(hc)
186
187 expect_true(identical(
188 as.dendrogram(hc, hang = 0.1),
189 hang.dendrogram(dend, hang = 0.1)
190 ))
191 })
192
193
194
195 test_that("Hanging dendrogram works for unbranched trees", {
196 hc <- hclust(dist(USArrests[1:5, ]), "ave")
197 dend <- as.dendrogram(hc)
198
199 unbranched_dend <- unbranch(dend, 2)
200
201 # we can't hclust an unbranched tree...
202 expect_error(as.hclust(unbranched_dend))
203
204 # plot(hang.dendrogram(unbranched_dend, hang = 0.1))
205
206 # showing that we can hang an unbranched tree
207 expect_false(identical(
208 unbranched_dend,
209 hang.dendrogram(unbranched_dend, hang = 0.1)
210 ))
211 })
212
213
214
215
216
217
218 test_that("Assigning several values to a tree's leaves nodePar", {
219 hc <- hclust(dist(USArrests[1:3, ]), "ave")
220 dend <- as.dendrogram(hc)
221
222 dend <- suppressWarnings(assign_values_to_leaves_nodePar(dend, 2, "lab.cex"))
223 dend <- suppressWarnings(assign_values_to_leaves_nodePar(dend, value = c(3, 2), nodePar = "lab.col"))
224
225 dend_leaf_nodePar <- get_leaves_attr(dend, "nodePar", simplify = FALSE)[[1]]
226
227 # notice how pch is added automatically!
228 expect_identical(length(dend_leaf_nodePar), 3L)
229 expect_identical(names(dend_leaf_nodePar), c("lab.cex", "pch", "lab.col"))
230 should_be <- structure(list(lab.cex = 2, pch = NA, lab.col = 3), .Names = c(
231 "lab.cex",
232 "pch", "lab.col"
233 ))
234 expect_identical(dend_leaf_nodePar, should_be)
235 })
236
237
238
239
240 test_that("We can remove leaves nodePar", {
241 dend <- as.dendrogram(hclust(dist(USArrests[1:5, ])))
242 dend <- color_labels(dend, 3)
243
244 expect_false(is.null(get_leaves_attr(dend, "nodePar")))
245 expect_true(is.null(get_leaves_attr(remove_leaves_nodePar(dend), "nodePar")))
246 })
247
248
249
250 test_that("rank_branches work", {
251 dend <- as.dendrogram(hclust(dist(USArrests[1:5, ])))
252
253 expect_equal(round(get_branches_heights(dend)), c(23, 37, 63, 109))
254
255 expect_equal(get_branches_heights(rank_branches(dend)), c(1, 1, 2, 3))
256 })
257
258
259
260
261 test_that("fix_members_attr.dendrogram work", {
262 dend <- as.dendrogram(hclust(dist(USArrests[1:5, ])))
263
264 # ruin members
265 attr(dend, "members") <- 1
266 fixed_dend <- fix_members_attr.dendrogram(dend)
267
268 expect_equal(attr(fixed_dend, "members"), 5)
269 })
270
271
272
273
274 test_that("get_branches_heights on a simple dendrogram", {
275 dend <- as.dendrogram(hclust(dist(1:5)))
276 result <- get_branches_heights(dend)
277 expect_true(is.numeric(result))
278 expect_true(length(result) > 0)
279 })
280
281 test_that("hang.dendrogram modifies leaf heights", {
282 dend <- as.dendrogram(hclust(dist(1:5)))
283 hanged_dend <- hang.dendrogram(dend, hang = 0.1)
284 expect_true(is.dendrogram(hanged_dend))
285 expect_true(all(get_leaves_attr(dend, "height") == 0))
286 expect_false(any(get_leaves_attr(hanged_dend, "height") == 0))
287 })
288
289 test_that("get_childrens_heights on a simple dendrogram", {
290 dend <- as.dendrogram(hclust(dist(1:5)))
291 result <- get_childrens_heights(dend)
292 expect_true(is.numeric(result))
293 })
294
295 test_that("rank_branches adjusts branch heights", {
296 dend <- as.dendrogram(hclust(dist(1:5)))
297 ranked_dend <- rank_branches(dend)
298 expect_true(is.dendrogram(ranked_dend))
299 # Check if heights are adjusted
300 heights <- sapply(ranked_dend, function(x) attr(x, "height"))
301 expect_true(all(diff(heights) == 1))
302 })
303
304 test_that("assign_values_to_leaves_nodePar updates nodePar", {
305 dend <- as.dendrogram(hclust(dist(1:5)))
306 updated_dend <- assign_values_to_leaves_nodePar(dend, value = 1:5, nodePar = "col")
307 expect_false(identical(get_leaves_nodePar(dend),
308 get_leaves_nodePar(updated_dend)))
309 })
310
311
312 test_that("remove_branches_edgePar removes edgePar", {
313 dend <- as.dendrogram(hclust(dist(1:5)))
314 dend <- assign_values_to_branches_edgePar(dend, value = 1, edgePar = "col")
315 cleaned_dend <- remove_branches_edgePar(dend)
316 expect_true(all(is.na(unlist(get_leaves_nodePar(cleaned_dend)))))
317 # if non dendrogram object passed in
318 expect_error(
319 remove_branches_edgePar(1:4)
320 )
321 })
322
323
324 test_that("remove_nodes_nodePar removes nodePar", {
325 dend <- as.dendrogram(hclust(dist(1:5)))
326 dend <- assign_values_to_nodes_nodePar(dend, value = 1:5, nodePar = "col")
327 cleaned_dend <- remove_nodes_nodePar(dend)
328 expect_true(all(is.na(unlist(get_leaves_nodePar(cleaned_dend)))))
329 # if non dendrogram object passed in
330 expect_error(
331 remove_nodes_nodePar(1:4)
332 )
333 })
334
335 test_that("remove_leaves_nodePar removes nodePar from leaves", {
336 dend <- as.dendrogram(hclust(dist(1:5)))
337 dend <- assign_values_to_leaves_nodePar(dend, value = 1:5, nodePar = "col")
338 cleaned_dend <- remove_leaves_nodePar(dend)
339 leaf_cols <- sapply(cleaned_dend, function(x) if(is.leaf(x)) attr(x, "nodePar")$col else NA)
340 expect_true(all(is.na(leaf_cols)))
341 # if non dendrogram object passed in
342 expect_error(
343 remove_leaves_nodePar(1:4)
344 )
345 })
346
347 test_that("fix_members_attr.dendrogram updates members attribute", {
348 dend <- as.dendrogram(hclust(dist(1:5)))
349 fixed_dend <- fix_members_attr.dendrogram(dend)
350 expect_true(is.dendrogram(fixed_dend))
351 # Check if members attribute is correctly updated
352 members_attr <- sapply(fixed_dend, function(x) attr(x, "members"))
353 expect_true(all(!is.na(members_attr)))
354 # if non dendrogram object passed in
355 expect_error(
356 fix_members_attr.dendrogram(1:4)
357 )
358 })
359
360 test_that("rank_order.dendrogram updates leaf order", {
361 dend <- as.dendrogram(hclust(dist(1:5)))
362 ranked_dend <- rank_order.dendrogram(dend)
363 expect_true(is.dendrogram(ranked_dend))
364 # Check if order is correctly updated
365 expect_equal(order.dendrogram(ranked_dend), rank(order.dendrogram(dend), ties.method = "first"))
366 # if non dendrogram object passed in
367 expect_error(
368 rank_order.dendrogram(1:4)
369 )
370 })
371
372 test_that("get_leaves_edgePar works", {
373 hc <- hclust(dist(1:5))
374 dend <- as.dendrogram(hc)
375 result <- get_leaves_edgePar(dend, simplify = T)
376 expect_true(
377 all(is.na(result))
378 )
379 # if non-dendrogram object passed in
380 expect_error(expect_warning(
381 get_leaves_edgePar(hc)
382 ))
383 })
384
385 test_that("get_leaves_branches_attr works", {
386 hc <- hclust(dist(1:5))
387 # if non-dendrogram object passed in
388 expect_error(expect_warning(
389 get_leaves_branches_attr(hc)
390 ))
391 })
392
393 test_that("get_nodes_attr works", {
394 hc <- hclust(dist(1:5))
395 dend <- as.dendrogram(hc)
396 # if non-dendrogram object passed in
397 expect_error(expect_warning(
398 get_nodes_attr(hc)
399 ))
400 dendextend_options("warn", T)
401 expect_warning(
402 result <- get_nodes_attr(dend, "fake attribute")
403 )
404 dendextend_options("warn", F)
405 expect_true(
406 all(is.na(result))
407 )
408 })
409
410 test_that("rllply works", {
411 x <- list(1, 2, list(31))
412 result <- rllply(x, function(x) {x}, add_notation = TRUE)
413 expect_identical(
414 result[[1]][[1]],
415 1
416 )
417 x <- 1:4
418 result <- rllply(x, function(x) {x}, add_notation = TRUE)
419 expect_identical(
420 attributes(result)$position_type,
421 "Leaf"
422 )
423 })
424
425 test_that("hang.dendrogram works", {
426 hc <- hclust(dist(1:5))
427 dend <- as.dendrogram(hc)
428 expect_no_error(
429 hang.dendrogram(dend, hang = -1)
430 )
431 # if non-dendrogram object passed in
432 expect_error(
433 hang.dendrogram(hc)
434 )
435 })
436
437 test_that("rank_branches works", {
438 hc <- hclust(dist(1:5))
439 # if non-dendrogram object passed in
440 expect_error(
441 rank_branches(hc)
442 )
443 })
444
445 test_that("assign_values_to_leaves_nodePar works", {
446 hc <- hclust(dist(1:5))
447 dend <- as.dendrogram(hc)
448 # if non-dendrogram object passed in
449 expect_error(
450 assign_values_to_leaves_nodePar(hc)
451 )
452 dendextend_options("warn", T)
453 # if 'value' parameter missing
454 expect_warning(
455 assign_values_to_leaves_nodePar(dend)
456 )
457 # if 'value' recycled
458 expect_warning(
459 assign_values_to_leaves_nodePar(dend, c(1), warn = T)
460 )
461 dendextend_options("warn", F)
462 })
463
464 test_that("assign_values_to_nodes_nodePar works", {
465 hc <- hclust(dist(1:5))
466 dend <- as.dendrogram(hc)
467 # if non-dendrogram object passed in
468 expect_error(
469 assign_values_to_nodes_nodePar(hc)
470 )
471 dendextend_options("warn", T)
472 # if 'value' parameter missing
473 expect_warning(
474 assign_values_to_nodes_nodePar(dend)
475 )
476 # if 'value' recycled
477 expect_warning(
478 assign_values_to_nodes_nodePar(dend, c(1), warn = T)
479 )
480 dendextend_options("warn", F)
481
482 # testing remove nodePar if it is empty
483 attr(dend[[1]][[1]], "nodePar") <- list()
484 expect_no_error(
485 result <- assign_values_to_nodes_nodePar(dend, rep(Inf, 10))
486 )
487
488 })
489
490 test_that("assign_values_to_branches_edgePar works", {
491 hc <- hclust(dist(1:5))
492 dend <- as.dendrogram(hc)
493 # if non-dendrogram object passed in
494 expect_error(
495 assign_values_to_branches_edgePar(hc)
496 )
497 dendextend_options("warn", T)
498 # if 'value' parameter missing
499 expect_warning(
500 assign_values_to_branches_edgePar(dend)
501 )
502 # if 'value' recycled
503 expect_warning(
504 assign_values_to_branches_edgePar(dend, c(1), warn = T)
505 )
506 dendextend_options("warn", F)
507
508 # if leaf passed in
509 result <- assign_values_to_branches_edgePar(dend[[1]][[1]], 1:10, skip_leaves = T)
510 expect_identical(
511 result,
512 dend[[1]][[1]]
513 )
514
515 })
516
517 test_that("assign_values_to_leaves_edgePar works", {
518 hc <- hclust(dist(1:5))
519 dend <- as.dendrogram(hc)
520 # if non-dendrogram object passed in
521 expect_error(
522 assign_values_to_leaves_edgePar(hc)
523 )
524 dendextend_options("warn", T)
525 # if 'value' parameter missing
526 expect_warning(
527 assign_values_to_leaves_edgePar(dend)
528 )
529 # if 'value' recycled
530 expect_warning(
531 assign_values_to_leaves_edgePar(dend, c(1), warn = T)
532 )
533 dendextend_options("warn", F)
534
535 # if leaf passed in
536 result <- assign_values_to_leaves_edgePar(dend[[1]][[1]], 1:10, skip_leaves = T)
537 expect_identical(
538 result,
539 dend[[1]][[1]]
540 )
541
542 # if edgePar already present for a leaf
543 attr(dend[[1]][[1]], "edgePar") <- list(test = 1)
544 expect_no_error(
545 assign_values_to_leaves_edgePar(dend, 1:10)
546 )
547 })