Debian Codegraph @

tests/testthat/test-get_nodes_xy.R

raw
1 # library(testthat)
2 # library(dendextend)
3
4 context("Tests for get_nodes_xy function")
5 # Tests for get_nodes_xy function
6
7 test_that("get_nodes_xy returns correct dimensions", {
8 dend <- as.dendrogram(hclust(dist(1:5)))
9 xy <- get_nodes_xy(dend)
10 expect_equal(ncol(xy), 2)
11 expect_equal(nrow(xy), nnodes(dend))
12 })
13
14
15 # TODO: it seems that the type argument doesn't make a difference.
16 # it probably needs to be removed.
17 test_that("get_nodes_xy handles different types correctly", {
18 dend <- as.dendrogram(hclust(dist(1:5)))
19 xy_rectangle <- get_nodes_xy(dend, type = "rectangle")
20 xy_triangle <- get_nodes_xy(dend, type = "triangle")
21 expect_true(identical(xy_rectangle, xy_triangle))
22 # par(mfrow = c(1,2))
23 # plot(dend, type = "rectangle")
24 # plot(dend, type = "triangle")
25 })
26
27
28 test_that("get_nodes_xy handles center parameter", {
29 dend <- as.dendrogram(hclust(dist(1:5)))
30 xy_center_false <- get_nodes_xy(dend, center = FALSE)
31 xy_center_true <- get_nodes_xy(dend, center = TRUE)
32 expect_false(identical(xy_center_false, xy_center_true))
33 })
34
35 test_that("get_nodes_xy handles single node dendrogram", {
36 dend <- as.dendrogram(hclust(dist(1:2)))
37 xy <- get_nodes_xy(dend)
38 expect_equal(dim(xy), c(3, 2))
39 })
40
41
42 options(verbose = TRUE)
43 test_that("plotNode2 works", {
44
45 hc <- USArrests[1:10, ] %>%
46 dist() %>%
47 hclust()
48 dend <- as.dendrogram(hc)
49 reset_nodes_xy <- function(dend) {
50 local({
51 xy_matrix <- matrix(0, nrow = nnodes(dend), ncol = 2)
52 i_node <- 0
53
54 function(xy) {
55 if (missing(xy)) {
56 return(with(environment(nodes_xy), xy_matrix))
57 }
58 # Increment node and assign xy values
59 i_node <<- i_node + 1
60 xy_matrix[i_node, ] <<- xy
61 }
62 })
63 }
64 nodes_xy <- reset_nodes_xy(dend)
65 attr(dend[[1]], "edgetext") <- "t"
66 # general case which covers most lines
67 plot.new()
68 capture.output(expect_no_error(
69 plotNode2(1, 1, dend, center = T, leaflab = "perpendicular", nodes_xy = nodes_xy)
70 ))
71 nodes_xy <- reset_nodes_xy(dend)
72 capture.output(expect_no_error(
73 plotNode2(1, 0.9, dend, center = F, type = "rectangle", leaflab = "perpendicular", nodes_xy = nodes_xy)
74 ))
75 nodes_xy <- reset_nodes_xy(dend)
76 capture.output(expect_no_error(
77 plotNode2(1, 0.9, dend, center = F, type = "rectangle", leaflab = "perpendicular", nodes_xy = nodes_xy)
78 ))
79
80 # specific cases to cover all lines
81 nodes_xy <- reset_nodes_xy(dend)
82 capture.output(expect_no_error(
83 plotNode2(0.5, 1, dend, center = T, horiz = F, leaflab = "perpendicular", type = "rectangle", nodes_xy = nodes_xy)
84 ))
85 nodes_xy <- reset_nodes_xy(dend)
86 capture.output(expect_no_error(
87 plotNode2(0.5, 1, dend, center = T, leaflab = "textlike", type = "rectangle", nodes_xy = nodes_xy)
88 ))
89
90 # with type triangle
91 nodes_xy <- reset_nodes_xy(dend)
92 capture.output(expect_no_error(
93 plotNode2(0.5, 1, dend, center = T, leaflab = "textlike", horiz = T, type = "triangle", nodes_xy = nodes_xy)
94 ))
95 nodes_xy <- reset_nodes_xy(dend)
96 capture.output(expect_no_error(
97 plotNode2(0.5, 1, dend, center = T, horiz = F, leaflab = "perpendicular", type = "triangle", nodes_xy = nodes_xy)
98 ))
99
100 # if a leaf is passed in
101 leaf <- dend[[1]]
102 attr(leaf, "label") <- 1
103 nodes_xy <- reset_nodes_xy(dend)
104 capture.output(expect_no_error(
105 plotNode2(0.5, 1, leaf, center = T, leaflab = "perpendicular", horiz = T, type = "triangle", nodes_xy = nodes_xy)
106 ))
107 # if ybot is null
108 attr(dend[[1]], "height") <- NULL
109 nodes_xy <- reset_nodes_xy(dend)
110 capture.output(expect_no_error(
111 plotNode2(0.5, 1, dend, center = T, leaflab = "textlike", horiz = T, type = "triangle", nodes_xy = nodes_xy)
112 ))
113
114 })
115 options(verbose = FALSE)