| 1 |
# library(testthat) |
| 2 |
|
| 3 |
context("Tree size") |
| 4 |
|
| 5 |
|
| 6 |
test_that("Get a hclust number of leaves", { |
| 7 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 8 |
|
| 9 |
expect_true(nleaves(hc) == 3L) |
| 10 |
}) |
| 11 |
|
| 12 |
|
| 13 |
test_that("Get a dendrogram number of leaves", { |
| 14 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 15 |
dend <- as.dendrogram(hc) |
| 16 |
|
| 17 |
expect_true(nleaves(dend) == 3L) |
| 18 |
}) |
| 19 |
|
| 20 |
|
| 21 |
test_that("nleaves works the same for members and order", { |
| 22 |
dend <- as.dendrogram(hclust(dist(USArrests))) |
| 23 |
expect_identical(nleaves(dend, "order"), nleaves(dend, "members")) |
| 24 |
}) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
test_that("Get a dendrogram number of nodes", { |
| 29 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 30 |
dend <- as.dendrogram(hc) |
| 31 |
|
| 32 |
expect_true(nnodes(dend) == 5L) |
| 33 |
expect_true(nnodes(hc) == 5L) |
| 34 |
|
| 35 |
# no default for nnodes |
| 36 |
expect_error( |
| 37 |
nnodes.default() |
| 38 |
) |
| 39 |
# if non-dend object passed in |
| 40 |
expect_warning(expect_error( |
| 41 |
nnodes.dendrogram(hc) |
| 42 |
)) |
| 43 |
# for phylo |
| 44 |
expect_identical( |
| 45 |
nnodes.phylo(as.phylo(hc)), |
| 46 |
5 |
| 47 |
) |
| 48 |
|
| 49 |
}) |
| 50 |
|
| 51 |
|
| 52 |
test_that("Can't get a tree size for an object which is not hclust, dendrogram or phylo", { |
| 53 |
x <- 1:4 |
| 54 |
expect_error(nleaves(x)) |
| 55 |
x <- letters[1:4] |
| 56 |
expect_error(nleaves(x)) |
| 57 |
x <- list(1:4) |
| 58 |
expect_error(nleaves(x)) |
| 59 |
}) |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
test_that("Get a dendrogram tree size with leaves having their branch of height 0", { |
| 64 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 65 |
dend <- as.dendrogram(hc) |
| 66 |
|
| 67 |
### |
| 68 |
# Trivial case |
| 69 |
# count_terminal_nodes(dend) # 3 terminal nodes |
| 70 |
# nleaves(dend) # 3 - the same number |
| 71 |
expect_true(count_terminal_nodes(dend) == nleaves(dend)) |
| 72 |
|
| 73 |
### |
| 74 |
# NON-Trivial case |
| 75 |
# str(dend) |
| 76 |
attr(dend[[2]], "height") <- 0 |
| 77 |
# count_terminal_nodes(dend) # 2 terminal nodes, why? see this plot: |
| 78 |
# plot(dend, main = "This is considered a tree \n with TWO terminal nodes only") # while we have 3 leaves, in practice we have only 2 terminal nodes (this is a feature, not a bug.) |
| 79 |
expect_false(count_terminal_nodes(dend) == nleaves(dend)) |
| 80 |
}) |
| 81 |
|
| 82 |
|
| 83 |
test_that("nleaves.dendlist works", { |
| 84 |
dend1 <- as.dendrogram(hclust(dist(USArrests[1:3, ]))) |
| 85 |
dend2 <- as.dendrogram(hclust(dist(USArrests[4:5, ]))) |
| 86 |
dend12 <- dendlist(dend1, dend2) |
| 87 |
|
| 88 |
expect_equal( |
| 89 |
nleaves.dendlist(dend12), |
| 90 |
c(3,2) |
| 91 |
) |
| 92 |
|
| 93 |
}) |
| 94 |
|
| 95 |
|
| 96 |
test_that("unclass_dend works", { |
| 97 |
dend <- as.dendrogram(hclust(dist(USArrests[1:3, ]))) |
| 98 |
|
| 99 |
result <- unclass_dend(dend) |
| 100 |
expect_identical( |
| 101 |
typeof(result), |
| 102 |
"list" |
| 103 |
) |
| 104 |
|
| 105 |
}) |
| 106 |
|
| 107 |
|
| 108 |
test_that("head.dendrogram works", { |
| 109 |
hc <- hclust(dist(USArrests), "ave") |
| 110 |
dend <- as.dendrogram(hc) |
| 111 |
|
| 112 |
capture.output(expect_no_error( |
| 113 |
head(dend) |
| 114 |
)) |
| 115 |
}) |