Debian Codegraph @

tests/testthat/test-color_branches.R

raw
1 # library(testthat)
2 # library(dendextend)
3
4
5 test_that("color_branches works", {
6
7 dend <- c(1:5) %>%
8 dist() %>%
9 hclust() %>%
10 as.dendrogram()
11
12 # test case where k and colors are misaligned
13 expect_warning(dend %>% color_branches(k = 3, col = c("red", "green")))
14 expect_warning(dend %>% color_branches(k = 2, col = c("red", "green", "blue")))
15
16 # test case where k and cluster are both provided
17 expect_warning(dend %>% color_branches(k = 2, clusters = c(1, 1, 2, 2, 3)))
18
19 # test case where number of cluster does not equal number of leaves
20 expect_warning(dend %>% color_branches(clusters = c(1, 1, 2, 3)))
21
22 # test case where labels are not unique, also tests when both k and h aren't specified
23 labels(dend)[2] = 1
24 expect_warning(dend %>% color_branches(warn = T))
25
26 # test when non-dendrogram object is passed in
27 x <- matrix(1:4, nrow = 2)
28 suppressWarnings(expect_error(x %>% color_branches(k = 2)))
29 hc <- c(1:5) %>%
30 dist() %>%
31 hclust()
32 dend <- as.dendrogram(hc)
33 expect_equal(
34 labels(color_branches(hc)),
35 labels(color_branches(dend))
36 ) # hc gets converted to dendrogram so should be the same
37
38 # temporarily redefine cutree to return 0 so we can activate warning for 'dend has only one level'
39 expect_warning(with_mocked_bindings(
40 color_branches(dend, k = 2, warn = T),
41 cutree = function(...) 0
42 ))
43 # trigger 'else' clause in addcol
44 expect_no_error(color_branches(dend) %>% color_branches())
45
46 # test when groupLabels argument used
47 expect_error(
48 color_branches(dend, k = 2, warn = T, groupLabels = function(...) TRUE)
49 )
50 # structure doesnt change
51 result1 <- color_branches(dend, k = 2, warn = T, groupLabels = TRUE)
52 result2 <- color_branches(dend, k = 2, warn = T, groupLabels = FALSE)
53 expect_equal(
54 labels(result1),
55 labels(result2)
56 )
57 # should be labeled now
58 suppressWarnings(expect_identical(
59 dendrapply(result1, function(dend_node) attributes(dend_node))$members$edgetext$label,
60 "1"
61 ))
62 # should remain unlabeled
63 suppressWarnings(expect_identical(
64 dendrapply(result2, function(dend_node) attributes(dend_node))$members$edgetext$label,
65 NULL
66 ))
67 })
68
69
70 test_that("color_labels works", {
71
72 dend <- c(1:5) %>%
73 dist() %>%
74 hclust() %>%
75 as.dendrogram()
76
77 # should recolor first and second label to red
78 colorful_dend <- color_labels(dend, labels = c(1,2), col = "red")
79 expect_identical(
80 dendrapply(colorful_dend, function(dend_node) attributes(dend_node))$members$members$nodePar$lab.col,
81 "red"
82 )
83
84 # color and labels vector have a length mismatch, creates warning because it has to recycle colors
85 expect_warning(
86 color_labels(dend, labels = c(3,4,5), col = c("red", "blue"), warn = T)
87 )
88
89 # if something other than a dend / hclust is passed in
90 x <- matrix(1:4, nrow = 2)
91 expect_error(color_labels(x))
92
93 # if hclust is passed in
94 hc <- c(1:5) %>%
95 dist() %>%
96 hclust()
97 colorful_dend <- color_labels(hc, k = 2)
98 expect_identical(
99 dendrapply(colorful_dend, function(dend_node) attributes(dend_node))$members$members$nodePar$lab.col,
100 "#CC476B"
101 )
102
103 # if misalignment in k and number of colors
104 expect_warning(
105 color_labels(dend, k = 3, col = c("red", "blue"), warn = T)
106 )
107 expect_warning(
108 color_labels(dend, k = 2, col = c("red", "blue", "green"), warn = T)
109 )
110
111 })
112
113
114 test_that("lty_branches works", {
115
116 dend <- c(1:5) %>%
117 dist() %>%
118 hclust() %>%
119 as.dendrogram()
120
121 # dend branches for second cluster should be dashed (aka lty = 2)
122 lty_dend = lty_branches(dend, k = 2)
123 suppressWarnings(expect_identical(
124 dendrapply(lty_dend, function(dend_node) attributes(dend_node))$class$class$edgePar$lty,
125 2
126 ))
127 })
128
129
130 test_that("leaf_Colors works", {
131
132 dend <- c(1:5) %>%
133 dist() %>%
134 hclust() %>%
135 as.dendrogram()
136 colorful_dend <- color_labels(dend, k = 2)
137
138 # extract leaf labels (none for uncolored dendrogram vs. #CC476B and #009681 for colorful dendrogram)
139 expect_true(
140 all(is.na(leaf_Colors(dend, "label")))
141 )
142 expect_true(
143 all(leaf_Colors(colorful_dend, "label") == c("#CC476B", "#CC476B", "#009681", "#009681", "#009681"))
144 )
145
146 # if something other than a dend
147 x <- matrix(1:4, nrow = 2)
148 expect_error(leaf_Colors(x))
149 })
150
151
152 test_that("color_branches_by_clusters works", {
153
154 dend <- c(1:5) %>%
155 dist() %>%
156 hclust() %>%
157 as.dendrogram()
158
159 # cluster color for the first edge should be based on cluster # 2
160 colorful_dend <- color_branches_by_clusters(dend, clusters = c(2,2,1,1,1), order_value = T)
161 suppressWarnings(expect_identical(
162 dendrapply(colorful_dend, function(dend_node) attributes(dend_node))$members$members$edgePar$col,
163 "2"
164 ))
165
166 # if wrong number of clusters is passed in
167 expect_error(
168 color_branches_by_clusters(dend, clusters = c(2,2,1,1), order_value = T)
169 )
170
171 })