Debian Codegraph @

tests/testthat/test-entanglement.R

raw
1 # library(testthat)
2
3
4 # fixing how the tests are printed
5 # cat("\n")
6
7 context("Measuring entanglement of two trees")
8
9
10 test_that("Match order of one dend based on another (using their labels)", {
11 dend <- USArrests[1:4, ] %>%
12 dist() %>%
13 hclust() %>%
14 as.dendrogram()
15 expect_identical(order.dendrogram(dend), c(4L, 3L, 1L, 2L))
16
17 dend_changed <- dend
18 order.dendrogram(dend_changed) <- 1:4
19 expect_identical(order.dendrogram(dend_changed), c(1:4))
20
21 # now let's fix the order of the new object to be as it was:
22 dend_changed <- match_order_by_labels(dend_changed, dend)
23 expect_identical(order.dendrogram(dend_changed), order.dendrogram(dend))
24
25 # producing an error due to different labels in the two trees:
26 # Fails when tree sizes are different:
27 expect_error(match_order_by_labels(dend_changed[[-1]], dend))
28 # It would also fail when one of the labels is different:
29 labels(dend_changed)[1] <- "CAT"
30 expect_error(match_order_by_labels(dend_changed, dend))
31 })
32
33
34
35
36 test_that("Match order of one dend based on another (using their order valuess)", {
37 dend <- as.dendrogram(hclust(dist(USArrests[1:4, ])))
38 expect_identical(order.dendrogram(dend), c(4L, 3L, 1L, 2L))
39
40 dend_changed <- dend
41 order.dendrogram(dend_changed) <- 1:4
42 expect_identical(order.dendrogram(dend_changed), c(1:4))
43
44 # now let's fix the order of the new object to be as it was:
45 dend_changed <- match_order_dendrogram_by_old_order(
46 dend_changed, dend,
47 order.dendrogram(dend_changed)
48 )
49 expect_identical(order.dendrogram(dend_changed), order.dendrogram(dend))
50
51 # if check_that_labels_match and check_that_leaves_order_match parameters used
52 dend_changed <- dend
53 dend_changed <- match_order_dendrogram_by_old_order(
54 dend_changed, dend,
55 order.dendrogram(dend_changed),
56 check_that_labels_match = T, check_that_leaves_order_match = T
57 )
58 expect_identical(order.dendrogram(dend_changed), order.dendrogram(dend))
59
60 # Watch this!
61 # take a dend and change it:
62 dend_changed <- dend
63 dend_changed <- rev(dend_changed)
64 expect_false(identical(order.dendrogram(dend_changed), order.dendrogram(dend)))
65 # we keep the order of dend_change, so that the leaves order are synced
66 # with their labels JUST LIKE dend:
67 old_dend_changed_order <- order.dendrogram(dend_changed)
68 # now we change dend_changed leaves order values:
69 order.dendrogram(dend_changed) <- 1:4
70 # and we can fix them again, based on their old kept leaves order:
71
72 # # alabama and alaska are different
73 # data.frame(order.dendrogram(dend),
74 # labels(dend),
75 # order.dendrogram(dend_changed),
76 # labels(dend_changed))
77
78 # a = match_order_dendrogram_by_old_order(dend_changed, dend,
79 # order.dendrogram(dend))
80 # order.dendrogram(a)
81 # labels(a)
82
83 # I am not sure this works right...
84 # dend_changed <- match_order_dendrogram_by_old_order(dend_changed, dend,
85 # old_dend_changed_order)
86 # expect_identical(order.dendrogram(dend_changed), order.dendrogram(dend))
87
88
89 # expect_identical(order.dendrogram(dend_changed), order.dendrogram(dend))
90 # identical(match_order_by_labels(dend_changed, dend), dend_changed)
91
92 # if labels do not match
93 dend1 <- as.dendrogram(hclust(dist(iris[1:10, -5])))
94 dend2 <- as.dendrogram(hclust(dist(iris[2:11, -5])))
95 dend3 <- as.dendrogram(hclust(dist(iris[1:9, -5])))
96 expect_error(
97 match_order_dendrogram_by_old_order(dend1, dend2, check_that_labels_match = T)
98 )
99 # if label order does not match
100 expect_error(expect_warning(
101 match_order_dendrogram_by_old_order(dend1, dend3, check_that_leaves_order_match = T)
102 ))
103 })
104
105
106
107
108
109 test_that("Entanglement works", {
110 hc1 <- hclust(dist(datasets::iris[, -5]), "com")
111 hc2 <- hclust(dist(datasets::iris[, -5]), "single")
112 dend1 <- as.dendrogram(hc1)
113 dend2 <- as.dendrogram(hc2)
114
115 expect_identical(round(entanglement(dend1, dend2, 0, "labels"), 2), 1)
116 expect_identical(round(entanglement(dend1, dend2, 1, "labels"), 2), 0.93)
117 expect_identical(round(entanglement(dend1, dend2, 1.5, "labels"), 2), 0.91)
118 expect_identical(round(entanglement(dend1, dend2, 2, "labels"), 2), 0.89)
119
120 expect_identical(
121 round(entanglement(dend1, dend2, 2, "labels"), 2),
122 round(entanglement(dend1, dend2, 2, "order"), 2)
123 )
124
125 # library(microbenchmark)
126 # microbenchmark(entanglement(dend1 , dend2, 2, "labels"),
127 # entanglement(dend1 , dend2, 2, "order"), times = 10L ) # order is about twice as fast...
128
129 # if entanglement calculated for non dend objects
130 expect_error(
131 entanglement.default(dend1, dend2)
132 )
133 expect_identical(
134 round(entanglement.hclust(hc1, hc2, 1.5, "labels"), 2),
135 0.91
136 )
137 ph1 <- ape::as.phylo(hc1)
138 ph2 <- ape::as.phylo(hc2)
139 expect_identical(
140 round(entanglement.phylo(ph1, ph2, 1.5, "labels"), 2),
141 0.91
142 )
143 dend12 <- dendlist(dend1, dend2)
144 expect_error(
145 entanglement.dendlist(dend12, 1.5, "labels")
146 )
147 expect_error(
148 entanglement.dendlist(dend12[1], 1.5, "labels")
149 )
150
151 })
152
153
154
155
156
157 test_that("Entanglement with labels vs order", {
158 hc1 <- hclust(dist(iris[, -5]), "com")
159 hc2 <- hclust(dist(iris[, -5]), "single")
160 dend1 <- as.dendrogram(hc1)
161 dend2 <- as.dendrogram(hc2)
162
163 # massing up the order of leaves is dangerous:
164 expect_identical(round(entanglement(dend1, dend2, 1.5, "order"), 2), 0.91)
165 order.dendrogram(dend2) <- seq_len(nleaves(dend2))
166 # this 0.95 number is NO LONGER correct!!
167 expect_identical(round(entanglement(dend1, dend2, 1.5, "order"), 2), 0.95)
168 # but if we use the "labels" method - we still get the correct number:
169 expect_identical(round(entanglement(dend1, dend2, 1.5, "labels"), 2), 0.91)
170
171 # however, we can fix our dend2, as follows:
172 dend2 <- match_order_by_labels(dend2, dend1)
173 # Now that labels and order are matched - entanglement is back at working fine:
174 expect_identical(round(entanglement(dend1, dend2, 1.5, "order"), 2), 0.91)
175 })
176
177
178 test_that("Entanglement works for dendlist objects ", {
179 dend1 <- iris[, -5] %>%
180 dist() %>%
181 hclust("com") %>%
182 as.dendrogram()
183 dend2 <- iris[, -5] %>%
184 dist() %>%
185 hclust("sin") %>%
186 as.dendrogram()
187 dend12 <- dendlist(dend1, dend2)
188 expect_identical(
189 entanglement(dend12, L = .25),
190 entanglement(dend1, dend2, L = .25)
191 )
192 })