| 1 |
# library(dendextend) |
| 2 |
# library(testthat) |
| 3 |
|
| 4 |
|
| 5 |
context("Trimming a tree") |
| 6 |
|
| 7 |
|
| 8 |
test_that("Trimming removes leaves and updates the object", { |
| 9 |
assign_dendextend_options() |
| 10 |
|
| 11 |
hc <- hclust(dist(USArrests[1:5, ]), "ave") |
| 12 |
dend <- as.dendrogram(hc) |
| 13 |
pruned_dend <- prune(dend, c("Alaska", "California")) |
| 14 |
|
| 15 |
|
| 16 |
expect_equal(nleaves(dend), 5L) |
| 17 |
expect_equal(nleaves(pruned_dend), 3L) # we pruned two leaves |
| 18 |
expect_equal(attr(pruned_dend, "members"), 3L) # checking the attributes are correct. |
| 19 |
expect_equal(attr(dend, "height"), attr(pruned_dend, "height")) # root height should not change due to this specific pruning |
| 20 |
expect_true(all(labels(pruned_dend) %in% labels(dend))) |
| 21 |
expect_warning(prune(dend, c("non-existing leaf"))) |
| 22 |
}) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
test_that("Trimming works for unbranched trees", { |
| 27 |
assign_dendextend_options() |
| 28 |
|
| 29 |
hc <- hclust(dist(USArrests[1:5, ]), "ave") |
| 30 |
dend <- as.dendrogram(hc) |
| 31 |
|
| 32 |
unbranched_dend <- unbranch(dend, 2) |
| 33 |
pruned_unbranched_dend <- prune(unbranched_dend, c("Alaska", "California")) |
| 34 |
|
| 35 |
expect_equal(nleaves(pruned_unbranched_dend), 3L) |
| 36 |
expect_equal(attr(pruned_unbranched_dend, "members"), 3L) # checking the attributes are correct. |
| 37 |
expect_true(all(labels(pruned_unbranched_dend) %in% labels(dend))) |
| 38 |
expect_warning(prune(unbranched_dend, c("non-existing leaf"))) |
| 39 |
}) |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
test_that("Intersecting-trees works", { |
| 44 |
assign_dendextend_options() |
| 45 |
|
| 46 |
hc_1 <- hclust(dist(USArrests[1:5, ]), "single") |
| 47 |
hc_2 <- hclust(dist(USArrests[1:5, ]), "complete") |
| 48 |
dend_1 <- as.dendrogram(hc_1) |
| 49 |
dend_2 <- as.dendrogram(hc_2) |
| 50 |
|
| 51 |
pruned_dend_1 <- prune(dend_1, c("Alaska")) |
| 52 |
pruned_dend_2 <- prune(dend_2, c("California")) |
| 53 |
|
| 54 |
dends_12 <- intersect_trees(pruned_dend_1, pruned_dend_2) |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
expect_equal(length(dends_12), 2L) |
| 59 |
expect_equal(nleaves(dends_12[[1]]), 3L) # 3 leaves - tree 1 |
| 60 |
expect_equal(nleaves(dends_12[[2]]), 3L) # 3 leaves - tree 2 |
| 61 |
expect_equal( |
| 62 |
length( |
| 63 |
intersect(labels(dends_12[[1]]), labels(dends_12[[2]])) |
| 64 |
), |
| 65 |
3L |
| 66 |
) # 3 labels are the same for both trees. |
| 67 |
|
| 68 |
|
| 69 |
# that it returns the same object |
| 70 |
dends_11 <- intersect_trees(dend_1, dend_1) |
| 71 |
expect_identical(dends_11[[1]], dend_1) |
| 72 |
}) |
| 73 |
|
| 74 |
test_that("Trimming works for non-binary trees", { |
| 75 |
assign_dendextend_options() |
| 76 |
|
| 77 |
## Simplest case: A flat, non-binary dendrogram |
| 78 |
hc <- hclust(dist(iris[1:5, -5])) |
| 79 |
dend_a <- as.dendrogram(hc) |
| 80 |
dend_a <- collapse_branch(dend_a, tol = 0.5) |
| 81 |
|
| 82 |
# Remove 1 non-binary leaf |
| 83 |
targets_1 <- c("2") |
| 84 |
pruned_dend_1 <- prune(dend_a, targets_1) |
| 85 |
|
| 86 |
# Since this is flat, expect a length of 5 - 1 = 4 |
| 87 |
expect_equal(length(pruned_dend_1), 4L) |
| 88 |
# Labels should transfer as expected |
| 89 |
expect_equal(labels(pruned_dend_1), setdiff(labels(dend_a), targets_1)) |
| 90 |
|
| 91 |
# Remove multiple non-binary leaves |
| 92 |
targets_2 <- c("2", "5") |
| 93 |
pruned_dend_2 <- prune(dend_a, targets_2) |
| 94 |
|
| 95 |
# Since this is flat, expect a length of 5 - 2 = 3 |
| 96 |
expect_equal(length(pruned_dend_2), 3L) |
| 97 |
# Labels should transfer as expected |
| 98 |
expect_equal(labels(pruned_dend_2), setdiff(labels(dend_a), targets_2)) |
| 99 |
|
| 100 |
|
| 101 |
## A more complex example |
| 102 |
hc <- hclust(dist(iris[1:15, -5])) |
| 103 |
dend_b <- as.dendrogram(hc) |
| 104 |
dend_b <- collapse_branch(dend_b, tol = 0.3) |
| 105 |
|
| 106 |
# Remove 1 non-binary leaf in a terminal node |
| 107 |
targets_3 <- c("2") |
| 108 |
pruned_dend_3 <- prune(dend_b, targets_3) |
| 109 |
|
| 110 |
# The top of the dend shouldn't change, so length is 2 |
| 111 |
expect_equal(length(pruned_dend_3), 2L) |
| 112 |
# N Leaves should now be reduced. 15 - 1 = 14 |
| 113 |
expect_equal(nleaves(pruned_dend_3), 14L) |
| 114 |
# Labels should transfer as expected |
| 115 |
expect_equal(labels(pruned_dend_3), setdiff(labels(dend_b), targets_3)) |
| 116 |
|
| 117 |
# Remove multiple non-binary leaves in a terminal node |
| 118 |
targets_4 <- c("2", "10", "13") |
| 119 |
pruned_dend_4 <- prune(dend_b, targets_4) |
| 120 |
|
| 121 |
# The top of the dend shouldn't change, so length is 2 |
| 122 |
expect_equal(length(pruned_dend_4), 2L) |
| 123 |
# N Leaves should now be reduced. 15 - 3 = 12 |
| 124 |
expect_equal(nleaves(pruned_dend_4), 12L) |
| 125 |
# Labels should transfer as expected |
| 126 |
expect_equal(labels(pruned_dend_4), setdiff(labels(dend_b), targets_4)) |
| 127 |
|
| 128 |
# Remove all but one leaf from a non-binary node |
| 129 |
# Should merge up to the next level. |
| 130 |
targets_5 <- c("2", "3", "4", "10") |
| 131 |
pruned_dend_5 <- prune(dend_b, targets_5) |
| 132 |
|
| 133 |
# The top of the dend shouldn't change, so length is 2 |
| 134 |
expect_equal(length(pruned_dend_5), 2L) |
| 135 |
# N Leaves should now be reduced. 15 - 4 = 11 |
| 136 |
expect_equal(nleaves(pruned_dend_5), 11L) |
| 137 |
# Labels should transfer as expected |
| 138 |
expect_equal(labels(pruned_dend_5), setdiff(labels(dend_b), targets_5)) |
| 139 |
# The remaining leaf, 13, was originally at: |
| 140 |
# dend[[2]][[2]][[1]][[2]][[3]] |
| 141 |
expect_true("13" == labels(dend_b[[2]][[2]][[1]][[2]][[3]])) |
| 142 |
expect_false(is.leaf(dend_b[[2]][[2]][[1]][[2]])) # This is the node with the non-binary split |
| 143 |
# The remaining leaf, 13, should now move up to: |
| 144 |
# dend[[2]][[2]][[1]][[2]] |
| 145 |
expect_true("13" == labels(pruned_dend_5[[2]][[2]][[1]][[2]])) |
| 146 |
expect_true(is.leaf(pruned_dend_5[[2]][[2]][[1]][[2]])) # This should now be true. |
| 147 |
|
| 148 |
# Remove a non-binary leaf from an upper branch |
| 149 |
targets_6 <- "1" |
| 150 |
pruned_dend_6 <- prune(dend_b, targets_6) |
| 151 |
|
| 152 |
# The top of the dend shouldn't change, so length is 2 |
| 153 |
expect_equal(length(pruned_dend_6), 2L) |
| 154 |
# N Leaves should now be reduced. 15 - 1 = 14 |
| 155 |
expect_equal(nleaves(pruned_dend_6), 14L) |
| 156 |
# Labels should transfer as expected |
| 157 |
expect_equal(labels(pruned_dend_6), setdiff(labels(dend_b), targets_6)) |
| 158 |
|
| 159 |
# Remove multiple non-binary leaves from an upper branch |
| 160 |
targets_7 <- c("1", "5") |
| 161 |
pruned_dend_7 <- prune(dend_b, targets_7) |
| 162 |
|
| 163 |
# The top of the dend shouldn't change, so length is 2 |
| 164 |
expect_equal(length(pruned_dend_7), 2L) |
| 165 |
# N Leaves should now be reduced. 15 - 2 = 13 |
| 166 |
expect_equal(nleaves(pruned_dend_7), 13L) |
| 167 |
# Labels should transfer as expected |
| 168 |
expect_equal(labels(pruned_dend_7), setdiff(labels(dend_b), targets_7)) |
| 169 |
# dend_b[[2]][[2]] should now be binary |
| 170 |
expect_equal(length(dend_b[[2]][[2]]), 4L) |
| 171 |
expect_equal(length(pruned_dend_7[[2]][[2]]), 2L) |
| 172 |
|
| 173 |
# Remove all non-binary leaves from an upper branch |
| 174 |
targets_8 <- c("1", "5", "8") |
| 175 |
pruned_dend_8 <- prune(dend_b, targets_8) |
| 176 |
|
| 177 |
# The top of the dend shouldn't change, so length is 2 |
| 178 |
expect_equal(length(pruned_dend_8), 2L) |
| 179 |
# N Leaves should now be reduced. 15 - 3 = 12 |
| 180 |
expect_equal(nleaves(pruned_dend_8), 12L) |
| 181 |
# Labels should transfer as expected |
| 182 |
expect_equal(labels(pruned_dend_8), setdiff(labels(dend_b), targets_8)) |
| 183 |
# The branch at dend_b[[2]][[2]] should merge up and should have length 2, with 7 leaves |
| 184 |
expect_equal(length(pruned_dend_8[[2]][[2]]), 2L) |
| 185 |
expect_equal(nleaves(pruned_dend_8[[2]][[2]]), 7L) |
| 186 |
# Max depth for this branching should be reduced by 1 |
| 187 |
expect_true(is.leaf(dend_b[[2]][[2]][[1]][[1]][[1]])) |
| 188 |
expect_true(is.leaf(pruned_dend_8[[2]][[2]][[1]][[1]])) |
| 189 |
|
| 190 |
|
| 191 |
# Remove non-binary and binary branches from multiple levels in one go |
| 192 |
targets_9 <- c("6", "14", "13", "8") |
| 193 |
pruned_dend_9 <- prune(dend_b, targets_9) |
| 194 |
|
| 195 |
# The top of the dend shouldn't change, so length is 2 |
| 196 |
expect_equal(length(pruned_dend_9), 2L) |
| 197 |
# N Leaves should now be reduced. 15 - 4 = 11 |
| 198 |
expect_equal(nleaves(pruned_dend_9), 11L) |
| 199 |
# Labels should transfer as expected |
| 200 |
expect_equal(labels(pruned_dend_9), setdiff(labels(dend_b), targets_9)) |
| 201 |
}) |
| 202 |
|
| 203 |
|
| 204 |
# unbranched_dend <- unbranch(dend,2) |
| 205 |
# pruned_dend <- prune(unbranched_dend , c("Alaska", "California")) |
| 206 |
# str(unclass(pruned_dend)) |
| 207 |
# str(unclass(unbranched_dend)) |
| 208 |
# plot(unbranched_dend) |
| 209 |
# plot(pruned_dend) |
| 210 |
# |
| 211 |
# |