Debian Codegraph @

tests/testthat/test-branches_attr_by.R

raw
1 # library(testthat)
2
3 context("branches_attr_by_")
4
5
6 test_that("is.infinite works", {
7 expect_false(is.infinite(NA))
8 expect_false(is.infinite(as.numeric(NA)))
9 expect_true(is.infinite(Inf))
10 expect_false(is.infinite("Inf"))
11 expect_true(is.infinite(as.numeric("Inf")))
12 })
13
14
15 test_that("assign_values_to_branches_edgePar - when trying to keep a value un-touched", {
16 a <- c(1:2) %>%
17 dist() %>%
18 hclust() %>%
19 as.dendrogram()
20
21 # this is how we want it to look:
22 # Original remains:
23 # str(assign_values_to_branches_edgePar(a, value = c(1,"Inf"), edgePar = "col"))
24 # str(assign_values_to_branches_edgePar(a, value = c(1,Inf), edgePar = "col"))
25 # A change is made:
26 # str(assign_values_to_branches_edgePar(a, value = c(1,NULL), edgePar = "col")) # since it recycles 1
27 # str(assign_values_to_branches_edgePar(a, value = c(1,NA), edgePar = "col")) # One is 1, and the other NA.
28
29
30
31 # this is false because one has
32 # attr(*, "edgePar")= Named num 1
33 # and the other:
34 # attr(*, "edgePar")= Named chr "1"
35 expect_false(
36 identical(
37 assign_values_to_branches_edgePar(a, value = c(1, "Inf"), edgePar = "col"),
38 assign_values_to_branches_edgePar(a, value = c(1, Inf), edgePar = "col")
39 )
40 )
41 # str(unclass(assign_values_to_branches_edgePar(a, value = c(1,"Inf"), edgePar = "col")))
42 # str(unclass(assign_values_to_branches_edgePar(a, value = c(1,Inf), edgePar = "col")))
43
44
45 # answer %>% unclass %>% str
46 # plot(answer)
47 # dput(answer)
48 answer <- assign_values_to_branches_edgePar(a, value = c(1, "Inf"), edgePar = "col")
49 true_answer <- structure(list(
50 structure(1L, label = 1L, members = 1L, height = 0, leaf = TRUE),
51 structure(2L, label = 2L, members = 1L, height = 0, leaf = TRUE, edgePar = structure(list(
52 col = "1"
53 ), .Names = "col"))
54 ), members = 2L, midpoint = 0.5, height = 1, edgePar = structure(list(
55 col = "1"
56 ), .Names = "col"), class = "dendrogram")
57 expect_true(
58 identical(answer, true_answer)
59 )
60
61 answer <- assign_values_to_branches_edgePar(a, value = c(1, Inf), edgePar = "col")
62 true_answer <- structure(list(
63 structure(1L, label = 1L, members = 1L, height = 0, leaf = TRUE),
64 structure(2L, label = 2L, members = 1L, height = 0, leaf = TRUE, edgePar = structure(list(
65 col = 1
66 ), .Names = "col"))
67 ), members = 2L, midpoint = 0.5, height = 1, edgePar = structure(list(
68 col = 1
69 ), .Names = "col"), class = "dendrogram")
70
71 expect_true(
72 identical(answer, true_answer)
73 )
74
75
76
77 answer <- assign_values_to_branches_edgePar(a, value = c(1, NULL), edgePar = "col")
78 true_answer <- structure(list(structure(1L, label = 1L, members = 1L, height = 0, leaf = TRUE, edgePar = structure(list(
79 col = 1
80 ), .Names = "col")), structure(2L, label = 2L, members = 1L, height = 0, leaf = TRUE, edgePar = structure(list(
81 col = 1
82 ), .Names = "col"))), members = 2L, midpoint = 0.5, height = 1, edgePar = structure(list(
83 col = 1
84 ), .Names = "col"), class = "dendrogram")
85 expect_true(
86 identical(answer, true_answer)
87 )
88
89
90 answer <- assign_values_to_branches_edgePar(a, value = c(1, NA), edgePar = "col")
91 true_answer <- structure(list(structure(1L, label = 1L, members = 1L, height = 0, leaf = TRUE, edgePar = structure(list(
92 col = NA_real_
93 ), .Names = "col")), structure(2L, label = 2L, members = 1L, height = 0, leaf = TRUE, edgePar = structure(list(
94 col = 1
95 ), .Names = "col"))), members = 2L, midpoint = 0.5, height = 1, edgePar = structure(list(
96 col = 1
97 ), .Names = "col"), class = "dendrogram")
98 expect_true(
99 identical(answer, true_answer)
100 )
101 })
102
103
104
105 # dend_node <- 1
106 # dend_node
107 # attr(dend_node, "edgePar")[["a"]] <- list(1) # doesn't work
108 # attr(dend_node, "edgePar")["a"] <- list(1) # works!
109 # attr(dend_node, "edgePar")["b"] <- list(1) # works!
110 # attr(dend_node, "edgePar")[["b"]] <- list(1) # doesn't work
111
112
113
114 test_that("branches_attr_by_lists works", {
115 dend <- c(1:4) %>%
116 dist() %>%
117 hclust() %>%
118 as.dendrogram()
119 L <- list(1:2, 3)
120 tmp <- dend %>% branches_attr_by_lists(L)
121 expect_equal(attr(tmp[[1]], "edgePar")$col, 2)
122 expect_equal(attr(tmp[[2]], "edgePar")$col, 2)
123 expect_equal(attr(tmp[[1]][[1]], "edgePar")$col, 1)
124 expect_equal(attr(tmp[[1]][[2]], "edgePar")$col, 1)
125 expect_equal(attr(tmp[[2]][[1]], "edgePar")$col, 2)
126 expect_equal(attr(tmp[[2]][[2]], "edgePar")$col, 1)
127
128 # with changed col/lwd/lty
129 tmp <- dend %>%
130 branches_attr_by_lists(L, TF_value = "blue") %>%
131 branches_attr_by_lists(L, attr = "lwd", TF_value = 4) %>%
132 branches_attr_by_lists(L, attr = "lty", TF_value = 3)
133 expect_equal(attr(tmp[[1]], "edgePar")$col, "blue")
134 expect_equal(attr(tmp[[1]], "edgePar")$lwd, 4)
135 expect_equal(attr(tmp[[1]], "edgePar")$lty, 3)
136
137 # when individual features are selected
138 tmp <- dend %>% branches_attr_by_lists(c(1, 4))
139 expect_equal(attr(tmp[[1]], "edgePar")$col, 2)
140 expect_equal(attr(tmp[[2]], "edgePar")$col, 2)
141 expect_equal(attr(tmp[[1]][[1]], "edgePar")$col, 2)
142 expect_equal(attr(tmp[[1]][[2]], "edgePar")$col, 1)
143 expect_equal(attr(tmp[[2]][[1]], "edgePar")$col, 1)
144 expect_equal(attr(tmp[[2]][[2]], "edgePar")$col, 2)
145
146 # test when list argument not provided and non-dendrogram object passed in
147 expect_error(branches_attr_by_lists(dend))
148 hc <- c(1:4) %>%
149 dist() %>%
150 hclust()
151 expect_error(branches_attr_by_lists(hc))
152 })
153
154
155 test_that("branches_attr_by_clusters works", {
156
157 dend <- c(1:4) %>%
158 dist() %>%
159 hclust() %>%
160 as.dendrogram()
161
162 # fourth cluster should be purple color if we assign branches colors
163 clusters <- c(1,2,3,4)
164 new_dend <- branches_attr_by_clusters(dend, clusters, attr = "col")
165 expect_identical(
166 dendrapply(new_dend, function(dend_node) attributes(dend_node))$class$class$edgePar$col,
167 "#7866D8"
168 )
169
170 # if non-dendrogram object is passed in
171 hc <- c(1:4) %>%
172 dist() %>%
173 hclust()
174 expect_error(expect_warning(
175 branches_attr_by_clusters(hc, clusters, attr = "col")
176 ))
177 # if clusters is missing
178 expect_error(
179 branches_attr_by_clusters(dend)
180 )
181 # if clusters isn't numeric
182 expect_warning(
183 branches_attr_by_clusters(dend, as.character(clusters))
184 )
185 # if length of clusters isn't the same as the number of leaves
186 expect_error(
187 branches_attr_by_clusters(dend, clusters[-1])
188 )
189 # if number of colors provided does not equal number of clusters
190 values = c("red", "blue", "green")
191 expect_warning(
192 branches_attr_by_clusters(dend, clusters, values)
193 )
194 # if any colors are NA
195 values = c("red", "blue", "green", NA)
196 expect_warning(
197 branches_attr_by_clusters(dend, clusters, values)
198 )
199 })
200
201
202 test_that("branches_attr_by_labels works", {
203
204 dend <- c(1:4) %>%
205 dist() %>%
206 hclust() %>%
207 as.dendrogram()
208
209 # assign labels 1 and 2 a color
210 labels <- as.character(1:2)
211 new_dend <- branches_attr_by_labels(dend, labels, 2, attr = "col")
212 suppressWarnings(expect_identical(
213 dendrapply(new_dend, function(dend_node) attributes(dend_node))$members$members$edgePar$col,
214 2
215 ))
216 # assign labels 1 and 2 a lwd
217 new_dend <- branches_attr_by_labels(dend, labels, 2, attr = "lwd")
218 suppressWarnings(expect_identical(
219 dendrapply(new_dend, function(dend_node) attributes(dend_node))$height$class$edgePar$lwd,
220 2
221 ))
222 # assign labels 1 and 2 a lty
223 new_dend <- branches_attr_by_labels(dend, labels, 2, attr = "lty")
224 suppressWarnings(expect_identical(
225 dendrapply(new_dend, function(dend_node) attributes(dend_node))$members$members$edgePar$lty,
226 2
227 ))
228
229 # if non-dendrogram object is passed in
230 x <- matrix(1:4, nrow = 2)
231 expect_error(
232 branches_attr_by_labels(x, labels, attr = "col")
233 )
234 # if labels is missing
235 expect_error(
236 branches_attr_by_labels(dend)
237 )
238 # if labels is not of type character
239 dendextend_options("warn", T)
240 expect_warning(
241 branches_attr_by_labels(dend, as.numeric(labels), attr = "col")
242 )
243 dendextend_options("warn", F)
244 # if any labels aren't in the dendrogram
245 labels = as.character(c(1,5))
246 expect_warning(
247 branches_attr_by_labels(dend, labels, attr = "col")
248 )
249
250 })
251