| 1 |
context("unbranching a tree") |
| 2 |
|
| 3 |
# set this for testing |
| 4 |
dendextend_options("warn", TRUE) |
| 5 |
|
| 6 |
test_that("Get attribute of node's branches (height)", { |
| 7 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 8 |
dend <- as.dendrogram(hc) |
| 9 |
|
| 10 |
expect_equal(get_root_branches_attr(dend, "height"), c(0, 37.17700902)) |
| 11 |
expect_true(unlist(get_root_branches_attr(dend, "leaf"))) |
| 12 |
expect_warning(get_root_branches_attr(unclass(dend), "height")) # warn when used on an object which is NOT of class "dendrogram" |
| 13 |
}) |
| 14 |
|
| 15 |
|
| 16 |
test_that("Raising a dendrogram's root height", { |
| 17 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 18 |
dend <- as.dendrogram(hc) |
| 19 |
|
| 20 |
taller_dend <- raise.dendrogram(dend, 10) |
| 21 |
shorter_dend <- raise.dendrogram(dend, -10) |
| 22 |
|
| 23 |
expect_equal(attr(dend, "height"), 54.800410723) |
| 24 |
expect_equal(attr(taller_dend, "height"), 64.800410723) |
| 25 |
expect_equal(attr(shorter_dend, "height"), 44.800410723) |
| 26 |
}) |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
test_that("unbranching a dendrogram", { |
| 31 |
hc <- hclust(dist(USArrests[10:13, ]), "ave") |
| 32 |
dend <- as.dendrogram(hc) |
| 33 |
# plot(dend) |
| 34 |
|
| 35 |
unbranched_dend <- unbranch(dend, 1) |
| 36 |
unbranched_dend_2 <- unbranch(unbranched_dend, 3) |
| 37 |
# plot(unbranched_dend) |
| 38 |
# plot(unbranched_dend_2) |
| 39 |
|
| 40 |
expect_equal(length(unbranched_dend), 3L) # our new tree has 3 branches |
| 41 |
expect_equal(length(unbranched_dend_2), 4L) # our new tree has 3 branches |
| 42 |
expect_equal(labels(dend), labels(unbranched_dend_2)) # order of leaves is preserved |
| 43 |
expect_warning(unbranch(dend[[1]])) # since the new root can not be a leaf. |
| 44 |
expect_error(unbranch(hc)) # While there is an hclust method, |
| 45 |
# this object can not handle non-binary trees. |
| 46 |
|
| 47 |
# if 'new_root_height' parameter used |
| 48 |
result <- unbranch.dendrogram(dend, new_root_height = 4.5) |
| 49 |
expect_equal(length(result), 3) |
| 50 |
|
| 51 |
# no default method |
| 52 |
expect_error( |
| 53 |
unbranch.default(dend) |
| 54 |
) |
| 55 |
}) |
| 56 |
|
| 57 |
|
| 58 |
test_that("flatten.dendrogram works", { |
| 59 |
hc <- hclust(dist(USArrests[10:13, ]), "ave") |
| 60 |
dend <- as.dendrogram(hc) |
| 61 |
|
| 62 |
result <- flatten.dendrogram(dend) |
| 63 |
expect_equal( |
| 64 |
attr(result[[1]], "height"), |
| 65 |
attr(result[[2]], "height") |
| 66 |
) |
| 67 |
|
| 68 |
}) |
| 69 |
|
| 70 |
|
| 71 |
test_that("collapse_branch works", { |
| 72 |
hc <- hclust(dist(USArrests[10:13, ]), "ave") |
| 73 |
dend <- as.dendrogram(hc) |
| 74 |
|
| 75 |
result <- collapse_branch(dend, lower = F) |
| 76 |
expect_equal( |
| 77 |
attr(result[[1]], "height"), |
| 78 |
attr(result[[2]], "height") |
| 79 |
) |
| 80 |
|
| 81 |
# if leaf object passed in |
| 82 |
expect_warning( |
| 83 |
collapse_branch(dend[[1]][[1]]) |
| 84 |
) |
| 85 |
|
| 86 |
# if non-dendrogram object passed in |
| 87 |
expect_error( |
| 88 |
collapse_branch(hc) |
| 89 |
) |
| 90 |
}) |